diesel
diesel

Reputation: 107

How to find element on page using ruby

I use Ruby's PageObject for testing and have an element

button(:showLocs,:title=>"Show All Items")

in my page-class.

What method I should use to check that page includes this object? I think that it should look as

if self.showLocs_element.find then #doing smth

Upvotes: 1

Views: 1229

Answers (2)

Justin Ko
Justin Ko

Reputation: 46836

The button accessor creates three methods - one to click a button, another to return the button element, and another to check the button's existence. The example in the documentation being:

button(:purchase, :id => 'purchase')
# will generate 'purchase', 'purchase_element', and 'purchase?' methods

In your specific case, this means that you can check the existence of the button by using the method:

showLocs?

So your if statement could be:

if showLocs?
  #doing smth
end

Upvotes: 1

Chris
Chris

Reputation: 788

If you are using PageObject with Watir, you would check an objects existence by using the .exists? method on the object in question. In addition, there are also .visible? (returns true if the object is both .present? and .displayed? on the page) as well as the .present? and .displayed? methods themselves if you would like additional granularity in your testing. I will summarize below to make sure this all makes sense.

watir_element.exists?  #=> returns true if the element exists
watir_element.visible? #=> returns true if the element is visible on the page
watir_element.present? #=> returns true if the element exists & is visible on the page

I would definitely suggest bookmarking the Watir Cheat Sheet on GitHub while you are learning as it helps break down some of the more common problems you will run into as well as the Watir Wiki in general. Lastly, Watir Webdriver.com has a lot of solid resources as well.

Upvotes: 1

Related Questions