Version 9

    Using the JSFSession API

    The JSFSession is the starting point for most JSFUnit tests.  The javadoc for JSFSession is here

     

     

    You create the JSFSession object by passing a partial JSF URL into the constructor.  This causes a real HTTP request to be sent to your JSF application.

     

     

    JSFSession jsfSession = new JSFSession("/mypage.jsf");
    

     

    Note that the above URL needs to be a JSF URL that corresponds to how your FacesServlet is mapped.  It could also be something like "/mypage.faces".  If your FacesServlet is path-mapped then the URL would be something like "/faces/mypage.jsp".

     

    Afterward, you will probably want to get a JSFClientSession object and a JSFServerSession object.

     

     

    JSFSession jsfSession = new JSFSession("/mypage.jsf");
    JSFClientSession client = jsfSession.getJSFClientSession();
    JSFServerSession server = jsfSession.getJSFServerSession();
    

     

     

     

     

    If you need more customization to your session, you can pass a WebClientSpec to the constructor.  The javadoc for WebClientSpec is here

     

     

    WebClientSpec wcSpec = new WebClientSpec("/mypage.jsf", BrowserVersion.FIREFOX_2);
    wcSpec.addCookie("mycookie", "mycookievalue");
    wcSpec.setInitialRequestStrategy(new BasicAuthenticationStrategy("username", "password"));
    wcSpec.getWebClient().setRedirectEnabled(false);
    JSFSession jsfSession = new JSFSession(wcSpec);
    JSFClientSession client = jsfSession.getJSFClientSession();
    JSFServerSession server = jsfSession.getJSFServerSession();
    

     

     

    The above example uses the WebClientSpec to emulate the Firefox browser instead of IE, add a cookie to be sent with every request, login using BASIC authentication, and disable following redirects.  See JSFUnitTestingSecurePages for details on testing applications that require authentication/login.

     

    Incidentally, WebClientSpec.getWebClient() is very powerful because it lets you drop into the Using the HtmlUnit API with JSFUnit and control the way HTMLUnit connects sends requests to the server and processes the response.  You also have access to the WebClient object after the first request using JSFSession.getWebClient().