Ajax

gmile edited this page Aug 14, 2010 · 13 revisions

There are several ways to deal with Ajax functionality when using Celerity:

Resynchronize

By using HtmlUnit’s NicelyResynchronizingAjaxController, HtmlUnit will synchronize Ajax calls made from the main thread (i.e. in response to user actions). From Celerity you can enable this functionality in one of two ways:

1. When creating the Browser instance.

browser = Celerity::Browser.new(:resynchronize => true)

2. For a code block

browser.resynchronized do
   browser.link(:id, 'foo').click
end

This will likely solve your problems if you see warnings like this in Celerity’s output:

Exception in thread "HtmlUnit Managed Thread #38 for WebWindow : XMLHttpRequest.send" java.lang.IllegalThreadStateException: Interrupted while waiting in MultiThreadedHttpConnectionManager

Browser#wait

(needs version >= 0.0.5.5)

Wait for Ajax calls to finish by joining all threads. This should be called right after the action that triggers the Ajax request, and only applies to the current page.

browser.button(:id, 'foo').click
browser.wait

This variant is mentioned in HtmlUnit’s FAQ and is equivalent to calling

browser.page.getEnclosingWindow.getJobManager.waitForAllJobsToFinish(10000)

Wait for a specific condition

Celerity’s Browser class provides some helpers to wait for a specific condition before continuing:

browser.wait_until { browser.div(:id, 'foo').visible? } 
browser.wait_while { browser.span(:id, 'bar').visible? }

You can set your own timeout in seconds by using the optional argument. The default timeout is 30 seconds.

browser.wait_until(80) { browser.div(:id, 'foo').visible? }

Give me more info!

If none of the above helps, turning on full logging and JavaScript exceptions can possibly provide some useful information:

browser = Celerity::Browser.new(:javascript_exceptions => true, :log_level => :all)