1 Reply Latest reply on Feb 21, 2012 7:07 PM by kragoth

    Re login & popup window

    thomas.c

      I'm writing JSFUnit test for testing my web application. Often during the tests I need switch the users, so I have done login and logout methods:

       

       

      public abstract class MyTestJSF extends ServletTestCase {

       

           private JSFSession jsfSession;    

       

           @Override

           public void setUp() throws IOException { }

       

       

       

           protected void login(String userName, String password) throws IOException {

                WebClientSpec webClientSpec = new WebClientSpec("/form/login.jsf");

                FormAuthenticationStrategy formAuth = new FormAuthenticationStrategy(userName, password, "login_button");

                webClientSpec.setInitialRequestStrategy(formAuth);

                jsfSession = new JSFSession(webClientSpec);

           }

       

       

       

           protected void logout() throws IOException {

                HttpSession httpSession = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);

                if(httpSession != null) httpSession.invalidate();

           }

      }

       

      public class MyTestUnit extends MyTestJSF {

           public void testWebPage() throws IOException {

                login("user1", "pass")

                // do something

                logout();

                login("user2", "pass")

                // do something

                logout();   

           }

      }

       

       

       

      At the beginning first call the method login() works fine, application opens new popup window and user is logged successful (popup window contain login form).

      After the second call the login() JSFUnit is trying login into the opener window and I got exception "Component with name=j_username was not found on the login page."

      Somehow I need to do the same action like during first call of the login() method.

      I will be thankful for some hints how to resolve this issue.

       

      Message was edited by: Thomas Collins

        • 1. Re: Re login & popup window
          kragoth

          When writing JsfUnit tests I always recommend avoiding things like this:

          Thomas Collins wrote:

               protected void logout() throws IOException {

                    HttpSession httpSession = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);

                    if(httpSession != null) httpSession.invalidate();

               }

           

          Instead, use the power of JSFUnit and write your test as if you are the user.

           

          protected void logout() throws IOException {

                    client.click("logout");

          }

           

          The reason for this is that often times logout does a little more then just call httpSession.invalidate();. By writing what you have it may actually be breaking the normal flow of your application.

           

          This may not fix your issue but, I think it will help you in the future to try write your tests like this.

           

          If the problem still exists let me know. I have a few other ideas as to where your problem might be.