Marc Guillemot wrote:
Hi,

if your asynchronous calls are just made using XMLHttpRequest, you can
try this:

<groovy description="setup AJAX calls resynchronizer (this will become
the default in WebTest)">
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController
as NRAC
step.context.webClient.ajaxController = new NRAC();
</groovy>

Nevertheless many AJAX frameworks use setTimeout (with a small timeout
value) and start processing from there. Ideally WebTest should detect
that and automatically wait until this kind of processing is finished
but it's not yet the case. You can try something like

<groovy description="wait that setTimeout terminates">
step.context.currentResponse.enclosingWindow.threadManager.joinAll(10000);
</groovy>

this is better than <sleep seconds="10"/> because it will wait at most
~10 seconds but will complete faster if processing is finished.

You can also write your own WebListener, something like:

        <groovy description="wait until page loads">
        import com.gargoylesoftware.htmlunit.WebWindowListener
        import com.gargoylesoftware.htmlunit.WebWindowEvent
        import org.apache.log4j.Logger

        class WindowListener implements WebWindowListener {
                def changed = false
                def currentWindow

                public WindowListener(currentWindow) {
                        this.currentWindow = currentWindow
                }

                void webWindowContentChanged(WebWindowEvent event) {
                        if (currentWindow == event.webWindow) {
                                changed = true
                        }
                }

                void webWindowClosed(WebWindowEvent event) {}
                void webWindowOpened(WebWindowEvent event) {}
        }

        def listener = new 
WindowListener(step.context.currentResponse.page.enclosingWindow)
        step.context.webClient.addWebWindowListener(listener)
        def waited = 0
        def log = Logger.getLogger(step.class)
        log.info("Waiting for current page to change...")
        while (!listener.changed) {
                Thread.sleep(200)
                waited += 200
                if (waited >= 10000) {
                        log.error("Gave up waiting after " + waited + "ms")
                        break
                }
        }
        log.info("Current page changed.")
        step.context.webClient.removeWebWindowListener(listener)
        </groovy>

As written, it provides no advantage to Marc's excellent snippet (apart from 
some extra logging) but you could tweak it to look for particular page content, 
so that if you have several things going on, you don't need to wait for all of 
them to finish.

Paul.


_______________________________________________
WebTest mailing list
[email protected]
http://lists.canoo.com/mailman/listinfo/webtest

Reply via email to