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();