Menu

#203 Better support to synchronize JUnit tests with XMLHttpReques

open
nobody
None
5
2014-03-21
2012-01-13
No

Currently, it's hard to synchronize JUnit tests with AJAX requests.

To improve the situation, I suggest this change:

In com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest.doSend(Context), increment a variable in the web driver that counts the current active requests. When the request completes, decrement the counter.

Maybe add a second counter to count the total number of requests.

Most of the requests are posted when I create an event on the page and not in a timeout handler. So the change above would allow to write this code in JUnit tests:

new Wait() {
    return driver.getActiveAjaxRequests() == 0;
}

If you also offer a total number of requests, more complex scenarios can be solved as well:

int start = driver.getAjaxTotalCount();
new Wait() {
    return start != driver.getAjaxTotalCount();
}

// Wait until all requests complete or until the total reaches start + 3, etc.

Discussion

  • Marc Guillemot

    Marc Guillemot - 2012-01-16

    While it would be quite easy (and interesting) to add such a feature to HtmlUnit, the "vocabulary" you use let me think that you're using HtmlUnit through WebDriver. If this is the case, such a feature would only make sense if you can get the information for all drivers. I fear that this will never be the case.

    Nevetheless, it makes sense to me to add such a feature to HtmlUnit.

     
  • Aaron Optimizer Digulla

    I agree to both :-) Actually, I don't see much value on the WebDriver API since it doesn't even give me access to the DOM. But the Wait API is nice and compact.

    For the AJAX tests, I'm casting "drive" to HtmlUnitWebDriver, so I have access to the internal API of HtmlUnit.

    As for the implementation, I suggest an "AjaxInfo" class which gives me a list of requests in the current "session" (plus a way to reset that between tests), which requests are in which state and maybe a "wait for a request to ${url}"

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.