Forum: Ruby Find element test with webdriver

Posted by Mattias A. (mattias_a)
on 2012-12-05 21:15
Hi, if my test not find the element i want it to continue and output
that it was not found. But below code fails, i want my test to output '
Did not find element with id: gbgfba'. Thanks for your help in advance!
(element id should be gbqfba but have entered gbqfbax so it should fail)

require 'selenium-webdriver'
class Find_element

  def initialize
    @driver = Selenium::WebDriver.for :firefox
    @driver.get "http://google.com"
  end

  def by_id
    #By ID - driver.find_element(:id,<elementID>)
    if @driver.find_element(:id, "gbqfbax")
      puts 'Did find element with Id: gbqfba'
    else
      puts 'Did not find element with Id: gbqfba'
    end
  end
end

test = Find_element.new
test.by_id
Posted by Joel Pearson (virtuoso)
on 2012-12-05 23:13
The best workaround I've seen for this was to use findelements to return 
an array and check whether the size is 0.
Of course, you could always use Watir-Webdriver which is more versatile 
and uses the same underlying driver with a better API.

http://watirmelon.com/2011/05/05/selenium-webdrive...
Posted by Mattias A. (mattias_a)
on 2012-12-10 19:18
Thanks for your reply. According to array you mean something like below? 
But it output that the method not find the element but it should. Any 
ide?

  def by_id_array
    by_id = []
    by_id << @driver.find_element(:id, "gbqfba")
    if by_id.empty?
      puts 'Did find element with Id: gbqfba'
    else
      puts 'Did not find element with id gbqfba'
    end
  end

test = Find_element.new
test.by_id_array

--output--
Did not find element with id gbqfba
Posted by Joel Pearson (virtuoso)
on 2012-12-10 20:28
I meant something like this:

array = @driver.find_elements(:id, "gbqfba")
puts 'Did not find element' if array.size = 0

As for whether the element exists to be found you'll need to ensure that 
it's actually the ID you need to identify it. I recommend using Firebug 
as it will give you the names, IDs, even the full XPath of any element. 
Use the Interactive Ruby console (IRB) to test out what works in 
real-time.

For example, this is what I'd do in watir-webdriver:

irb(main):001:0> require 'watir-webdriver'
=> true
irb(main):002:0> b = Watir::Browser.new
=> #<Watir::Browser:0x10e5b480 url="about:blank" title="">
irb(main):003:0> b.goto 'www.google.com'
=> "http://www.google.co.uk/"
irb(main):004:0> b.element(:id,'gbqfba').exists?
=> true
Posted by Mattias A. (mattias_a)
on 2012-12-12 20:51
Thanks for your answer Joel. I really want to solve this with Selenium 
Webdriver even if watir is better. (cannot give up now :))

Your array tips dosent work when the element not exists or i maybe I do 
something wrong? Expected output below would be "No", but the test fails 
and will not complete the run. It complains about the row array = 
@driver.find_element(:id, "gbqfqX")

  def by_id_array
     array = []
     array = @driver.find_element(:id, "gbqfqX")
    puts 'No' if array.size == 0
  end
Posted by Hassan Schroeder (Guest)
on 2012-12-12 21:24
(Received via mailing list)
On Wed, Dec 12, 2012 at 11:51 AM, Mattias A. <lists@ruby-forum.com> 
wrote:

> Your array tips dosent work when the element not exists or i maybe I do
> something wrong? Expected output below would be "No", but the test fails
> and will not complete the run. It complains about the row array =
> @driver.find_element(:id, "gbqfqX")

Specifying exactly what that "complaint" is would help people help
you, but ...

>   def by_id_array
>      array = []
>      array = @driver.find_element(:id, "gbqfqX")
>     puts 'No' if array.size == 0
>   end

It helps to pay attention to what's actually returned by the method
you're calling. Try using this:

  def by_id
     begin
       @driver.find_element(:id, 'gbqfbax')
    rescue => err
       puts 'Did not find element with Id: gbqfba'
       puts "because of #{err} (#{err.class})"
    end
 end

HTH,
Posted by unknown (Guest)
on 2012-12-12 21:25
(Received via mailing list)
Am 12.12.2012 20:51, schrieb Mattias A.:
> Thanks for your answer Joel. I really want to solve this with Selenium
> Webdriver even if watir is better. (cannot give up now :))
>
> Your array tips dosent work when the element not exists or i maybe I do
> something wrong? Expected output below would be "No", but the test fails
> and will not complete the run. It complains about the row array =
> @driver.find_element(:id, "gbqfqX")

Be more specific (provide the error message).

>    def by_id_array
>       array = []

The previous line is useless, because `array' is redefined
in the following line:

>       array = @driver.find_element(:id, "gbqfqX")
>      puts 'No' if array.size == 0
>    end
>

You need to read Joel's answers more carefully, he suggested

   array = @driver.find_elements(:id, "gbqfba")

(mind the plural).
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.