HtmlUnit and Ajax Calls

By default HtmlUnit 2.13  does not wait for asynchronous  java scripts call to finish when loading a page. This can cause difficulties if you need to use the updated DOM of the new page after it has been manipulated by the javascript.

The FAQ gives some clues how it can be done. I found setting the ajax controller during initialising of the WebClient
         setAjaxController(new NicelyResynchronizingAjaxController());
 didn’t work for me. Elsewhere on the web there are suggestions that changing the Browser type to FIREFOX_3.6 might do the trick – in my case it didn’t.

However I was reading the FAQ incorrectly.  The call
         webClient.waitForBackgroundJavaScript(10000)
should be done inline not during initialization.  So we get something like

submit.click();
waitForBackgroundJavaScript(20000);
currentPage = (HtmlPage) getCurrentWindow().getEnclosedPage();

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s