Image of an drop down menu and loading statusThe Watir problem with waiting for Ajax elements…

When testing a webpage that contain elements like Javascript or ajax which, after an event fire, reload page elements without reloading the whole page can become difficult to script for using plain Watir or Selenium. For example, Watir’s wait_until method will wait for an element to show up then continue on with the task. This is fine for pages not using dynamic ajax elements because the after the DOM completely loads all the elements of the page should be available.

But when faced with an ajax element that is not reloading the page and there for reloading the DOM, those new elements cannot be accessed by watir and we wrote about this issue previously in a post entitled “When is a linked select list really ready? Watir::Wait.”

In that post we decided the best solution at the time was to use a combination of Watir wait_until and list validations. Ultimately, that means you would click an Ajax element, wait for some values to exist, and then proceed. But then came a problem with ready state and enabled elements. In some cases, even though the elements exist, due to some funky JS mumbo jumbo they have not yet become enabled for manipulation by Watir. In this case, the wait_until method works properly, grabbing the element after it exists but the test fails because though the element exists, it is not yet ready or enabled.

Our newest solution is a Watir wait_until_ready method, which will identify the Ajax element and wait until it, not the browser, is in a ready state.

watir ajax