-
Notifications
You must be signed in to change notification settings - Fork 244
AJAX and waiting for elements
Titus edited this page May 1, 2017
·
5 revisions
Note: This wiki is outdated, please visit our website for the latest documentation.
watir-webdriver aims to provide a blocking API when possible. The asynchronous nature of browsers makes it sometimes impossible for the tool to know when you (as the author) consider the page to be “loaded”. To avoid brittle tests in these cases, you should explicitly wait for elements to appear before trying to interact with them. Polling the DOM for the state you want is highly preferable to adding sleep() calls that need constant adjustment.
Encapsulating waiting for elements is a lot less painful if you use Page Objects.
browser.button(:id => "send").when_present.click # default timeout of 30 seconds
browser.button(:id => "send").when_present(2).click # time out after 2 seconds
browser.div(:id => "container").when_present { |div| div.button.click }
browser.div(:id => "foo").present? # returns true if element exists and is visible
browser.div(:id => "loading" ).wait_while_present
browser.div(:id => "container").wait_until_present
Watir::Wait.until { ...arbitrary predicate... }
Watir::Wait.while { ...arbitrary predicate... }Relevant docs:
These are not (at the moment) available in Watir 1.6, so be aware if you use both libraries side by side.