4 Replies Latest reply on Jun 18, 2009 12:04 PM by ssilvert

    JSFUnit Navigation Problem

      New to and JSFUnit and somewhat new to JSF. Having a problem in the first 'real world' test case I'm trying to write. My goal in this test case is to Navigate from our webapp's intial screen to its login screen, then put some bad values in the username and password fields, and to verify that it fails to login. I'm stuck at getting it to navigate to the login screen. It just stays at the banner page after clicking the button.

      Here's my test case so far, trying to just get it to navigate to login page. client.getPageAsText(), viewId, currentView, they all never change after click.

       /**
       * Test to make sure a bad userName fails login
       */
       public void testBadUser()
       {
       try {
       JSFSession jsfSession = new JSFSession("/ui/Banner.jsf");
       JSFClientSession client = jsfSession.getJSFClientSession();
       JSFServerSession server = jsfSession.getJSFServerSession();
      
      log.fine( "\nserver currentViewID=" + server.getCurrentViewID() );
      log.fine( "\nserver facesContext.viewRoot.viewId=" + server.getFacesContext().getViewRoot().getViewId() );
      log.fine( "\n============================ client page =======================\n" + client.getPageAsText() );
      
      log.fine( "\n========== clicking ================" );
      // client.click( "formBanner:btnAgree" );
       client.click( "btnAgree" );
      log.fine( "\n========== after click ================" );
      
      log.fine( "\nserver currentViewID=" + server.getCurrentViewID() );
      log.fine( "\nserver facesContext.viewRoot.viewId=" + server.getFacesContext().getViewRoot().getViewId() );
      log.fine( "\n============================ client page =======================\n" + client.getPageAsText() );
      
       } catch ( Exception e ) {
       e.printStackTrace();
       fail( "Unexpected Exception in testBadUser(): " + e.toString() );
       }
       }



      Here's the button's HTML returned from getPageAsText() that I need to click to get to the login page:

       <input id="formBanner:btnAgree" name="formBanner:btnAgree" onclick="A4J.AJAX.Submit('j_id_id2','formBanner',event,{'parameters':{'formBanner:btnAgree':'formFederalBanner:btnAgree'} ,'actionUrl':'/foo/ui/Banner.jsf'} );return false;" value="I Agree" style="cursor:pointer" type="submit"/>
      


      See anything obvious I am doing wrong? Or maybe I just don't understand how it works--should clicking the button change what is returned by getCurrrentViewID(), getViewId() and getPageAsText()?


        • 1. Re: JSFUnit Navigation Problem
          ssilvert

          Are you sure your application is working correctly from the browser? It looks like A4J is just submitting the form back to /foo/ui/Banner.jsf.

          Can you post the xhtml or jsp?
          Are there any errors in the server log?

          Stan

          • 2. Re: JSFUnit Navigation Problem

            Thanks for the response. Yeah, I think some kind of strange redirection is going on with that page. The backing bean is expecting some attribute to be set in the session or it just goes back to the intial screen. I'd like to get to the bottom of the problem, but I found a workaround. May not be worth the effort, unless I run into navigation problems in the future.

            I was looking thru earlier posts on this forum, and found one that let me re-think this test case. You wrote a post about how JSFUnit tests should test the state, not the behavior, and then gave an example of how to get an instance of a backing bean in a testcase. I thought this was a terrific post, answered a few questions I had. So I rewrote my testcase to just set the username/password in the login backing bean, then get a reference to it, and invoke the method to login, and test the FacesMessage to verify the right error happened.

            Suggestion: stick that post in the official JSFUnit docs if its not already there. It would have saved me some time.

            • 3. Re: JSFUnit Navigation Problem

              Heres the post I was talking about:

              http://www.jboss.org/index.html?module=bb&op=viewtopic&t=150929
              Unit Testing Backing Beans with Mock dependencies

              • 4. Re: JSFUnit Navigation Problem
                ssilvert

                Thanks for the suggestion. I'll add that info to the wiki docs when I get a chance next week.

                Stan