Search code examples
rubycucumberwatirpage-object-gem

Page-object gem - how to implement wait_until_present


I am taking my first steps with the page-object gem working with Cucumber and Watir. My present step definition file has commands like the following:

@browser.text_field(:name => 'Username').wait_until_present.set username

Using page-object I am testing the watir (sorry) with the following code:

class HomePage
  include PageObject
  text_field(:user_name, name: 'username').wait_until_present
}
end

home_page = HomePage.new
home_page.user_name = 'Mickey'

This does not work as I get a 'no method error'

A bit of googling reveals I can do this. but it is using when_visible instead of wait_until_present

class HomePage
  include PageObject
  text_field(:user_name){
      text_field_element(:name => 'username').when_visible
}
end

home_page = HomePage.new
home_page.user_name = 'Mickey'

What is the best of using wait_until or when_visible with page-object? I don't want to put lots of sleeps in my step files if possible.


Solution

  • Assuming you are using the latest version of Watir, you can simply do:

    class HomePage
      include PageObject
      text_field(:user_name, name: 'username')
    end
    
    home_page = HomePage.new
    home_page.user_name = 'Mickey'
    

    In other words, you do not need to explicitly wait for the text field to be present before setting it.

    Watir has had a number of improvements including automatically waiting for elements to be present before setting them. As a result, you should rarely (never?) need to call wait_until_present.