Version 6

    Spying on HtmlUnit

    The JSFUnit JSFClientSession is a wrapper for HtmlUnit.  The nice thing about HtmlUnit is that it is a true headless browser and it takes care of a lot of things automatically such as handling AJAX requests and redirects.  The downside is that because a lot is going on behind the scenes it is sometimes hard to understand what is happening.  This is especially true when a single click or other event causes multiple requests to the server.  Sometimes you would like to know exactly what is happening during each individual request and response.

     

    Enabling the Snooper

    JSFUnit includes a "Snooper" class that dumps the contents of HtmlUnit's request and response to standard out.  To enable the snooper, just set the "jsfunit.htmlunitsnooper" system property on the server.  This property does not need to have any particular value.

     

    The RequestListener interface

    JSFUnit provides a way to peek at each individual request using the org.jboss.jsfunit.framework.RequestListener interface.  The interface looks like this:

    public interface RequestListener
    {
       /**
        * This is called before HtmlUnit makes a request to the server.
        *
        * @param webRequestSettings The settings for the request.
        */
       public void beforeRequest(WebRequestSettings webRequestSettings);
       
       /**
        * This is called after the request is over.  In the case that the request
        * throws an IOException, the webResponse will be null.
        *
        * @param webResponse The response, or <code>null</code> if the request
        *                    threw an IOException.
        */
       public void afterRequest(WebResponse webResponse);
    }

     

    Adding your own RequestListener

    JSFUnit uses a RequestListener to keep itself up to date.  But you can also add your own implementation of RequestListener in your JSFUnit tests.  You would do something like this:

    WebClientSpec wcSpec = new WebClientSpec("/index.jsf");
    WebClient webClient = wcSpec.getWebClient();
    JSFUnitWebConnection webConnection = (JSFUnitWebConnection)webClient.getWebConnection();
    webConnection.addListener(new MyRequestListener());
    JSFSession jsfSession = new JSFSession(wcSpec);