Shwheelz Shwheelz - 7 months ago 32
Ruby Question

How do I access Watir properties of PageObject elements?

I'm new to PageObject, but I've used Watir before. I have a Login class

class Login
include PageObject

page_url 'http://localhost:9090/#/login'

button(:login, :id => 'login-btn')
end


I want to check whether the button is enabled. Normally, I'd just call it in Watir with something like

@browser.button(:id => 'login-btn').enabled?


When I try to use the login PageObject alias, I get an error.

on(Login).login.enabled?
> NoMethodError: undefined method 'enabled?' for nil:NilClass


Is there a PageObject-flavored way to access Watir properties such as enabled, or do I need to manually find the element through the @browser variable and check it that way? That seems to defeat the purpose of using PageObject to begin with.

Answer

The page object accessor methods, eg button, will create a <name>_element method that returns a PageObject::Elements::Element. With this object, you can inspect various aspects of the element.

For this case, the button state can be checked with:

on(Login).login_element.enabled?

Note that the PageObject::Elements::Element is a wrapper around the Watir::Element. This means that methods you used in Watir may not be available (ie not implemented). If you run into this situation, you can directly access the underlying Watir::Element using the element method.

on(Login).login_element.element.enabled?
Comments