Version 40

    The JSFClientSession class

    JSFUnit's JSFClientSession class is used to impersonate a browser and its user input during a test.

     

    To see the full javadoc for this class, click here.

     

    Getting a JSFClientSession

    You get a JSFClientSession from the JSFSession object.  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.  After that, you can examine the HTML returned by the server, fill in form values, and do another submit.  The JSFClientSession automatically keeps track of your HttpSession just as if it were a user working in a browser.

     

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

     

    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".

     

    Input Methods

    The input methods are used to fill in a form.  For most parameters on a form, you simply use the setValue() method:

     

    JSF page:

    <h:inputText value="#{foo.text}" id="myInputFieldId"></h:inputText>
    

     

    JSFUnit code:

    client.setValue("myInputFieldId", "myValue");
    

     

     

     

    Alternatively, you can use the type() method.  This is especially useful for AJAX components where you have javascript that reacts to keystrokes.

     

    JSF page:

    <h:inputText id="name" value="#{userBean.name}">
      <a4j:support id="rerenderOuttext" event="onkeyup" reRender="outtext" ></a4j:support>
    </h:inputText>
    

     

    JSFUnit code:

    client.type("name", 'S');
    client.type("name", 't');
    client.type("name", 'a');
    client.type("name", 'n');
    

     

     

    For buttons, checkboxes, or anything else you might click, you use the click() method:

     

    JSF page:

    <h:selectBooleanCheckbox id="mycheckboxId" value="false"></h:selectBooleanCheckbox>
    

     

    JSFUnit code:

    client.click("myCheckboxId");
    

     

     

    Usually these three methods are all you need for input.

     

     

    Note above that "myinputfield", "name", and "myCheckboxId" refer to the JSF component ID.  For more information about that, see UsingComponentIDs.

     

    HtmlUnit Methods

    The JSFClientSession class is essentially a facade for HtmlUnit.  If you need to break out of that facade and Using the HtmlUnit API with JSFUnit while still maintaining your JSFUnit session, you can do that as long as you started with a JSFSession and JSFClientSession.

    com.gargoylesoftware.html.Page client.getContentPage();
    

     

    Now you can examine the HTML and perform assertions and other operations on it using the DOM API and the HtmlUnit API.  Note that you may need to call getContentPage() again if you trigger a request to the server that changes the Page.

     

     

    Often, the Using the HtmlUnit API with JSFUnit requires that you do an operation on an Element object.  JSFClientSession allows you to look up that org.w3c.dom.Element using the JSF component ID.

    Element domElement = client.getElement("myinputfield");
    

     

    Examining the server's response as text

    Besides examining the page as a DOM, you can also get the current page as a String of text.  A common test is to get the entire body text of the response and check its contents:

    String pageText = client.getPageAsText();
    assertTrue(pageText.contains("Hello World");