12 Replies Latest reply on Feb 16, 2009 2:28 AM by Manish Jain

    How to test applications where there are no static jsf pages

    Manish Jain Newbie

      Hello Stan,

      I have a project called XYZ. There are no jsp's in my project. All views like login.faces, logout.faces, etc are generated at runtime using JSF framework classes(JSF version 2). I am using tomcat 6.0.

      When a web request is made for the login.html page, the request goes to my servelt (doGet function), where I start my internal processing and then render the requested page to the client.

      Now, how do I get the objects for "org.jboss.jsfunit.facade.JSFClientSession" and "org.jboss.jsfunit.facade.JSFServerSession" classes, so that I can test my application. The constructor for these classes only takes the jsp page String("login.faces") and then process it. I tried other API's to get my job done but in vain.

      Next, let us assume I am through the first question, then my next question is this. Let say I have logged in and I arrive at a page called desktop.faces, from there I go to "batch.faces". Now my question is - when I have to write test cases for "batch.faces", do I need to write the logic to first come to this page(batch.faces) from "login.faces". Is there a way so that I can bypass these pages(login and desktop) and directly arrive at the "batch.faces" for its testcases to execute.

      I would higly appreciate your help.

      Thanks & Regards,
      Manish

        • 1. Re: How to test applications where there are no static jsf p
          Stan Silvert Master

           

          "dareurdream" wrote:

          I have a project called XYZ. There are no jsp's in my project. All views like login.faces, logout.faces, etc are generated at runtime using JSF framework classes(JSF version 2). I am using tomcat 6.0.

          When a web request is made for the login.html page, the request goes to my servelt (doGet function), where I start my internal processing and then render the requested page to the client.

          So you are saying that your login page DOES NOT use JSF tags, right? You handle login using your own servlet and then redirect to a JSF page on successful login. For this scenario, you should provide an AuthenticationStrategy as described on this wiki page: http://www.jboss.org/community/docs/DOC-10974

          If you are using standard JEE FORM or BASIC security then JSFUnit has AuthenticationStrategy classes already written for you. Otherwise, you will need to provide your own implementation, which gets you past the login page using HtmlUnit.

          "dareurdream" wrote:

          Now, how do I get the objects for "org.jboss.jsfunit.facade.JSFClientSession" and "org.jboss.jsfunit.facade.JSFServerSession" classes, so that I can test my application. The constructor for these classes only takes the jsp page String("login.faces") and then process it. I tried other API's to get my job done but in vain.

          Once you provide the AuthenticationStrategy to WebClientSpec/JSFSession then you can proceed as usual.

          "dareurdream" wrote:

          Next, let us assume I am through the first question, then my next question is this. Let say I have logged in and I arrive at a page called desktop.faces, from there I go to "batch.faces". Now my question is - when I have to write test cases for "batch.faces", do I need to write the logic to first come to this page(batch.faces) from "login.faces". Is there a way so that I can bypass these pages(login and desktop) and directly arrive at the "batch.faces" for its testcases to execute.

          This depends a lot on your application. If your application is written in such a way that you can go directly to any page after login, then you can use JSFSession to go directly to the page:
          WebClientSpec wcSpec = new WebClientSpec("/batch.faces");
          FormAuthenticationStrategy formAuth = new FormAuthenticationStrategy("username", "password", "login_button");
          wcSpec.setInitialRequestStrategy(formAuth);
          JSFSession jsfSession = new JSFSession(wcSpec);
          JSFClientSession client = jsfSession.getJSFClientSession();
          JSFServerSession server = jsfSession.getJSFServerSession();

          In this example, we tell JSFUnit to go directly to batch.faces at the beginning of the test. It will use HtmlUnit to request the batch.faces page. When challenged with a login screen, JSFUnit's FormAuthenticationStrategy will log in and you will end up on /batch.faces.

          But like I said, your application has to have the ability to do this. If your application requires that you always land on /desktop.faces after login then you will need to change your application or write a more clever AuthenticationStrategy that navigates to the page you are requesting.

          Stan

          • 2. Re: How to test applications where there are no static jsf p
            Manish Jain Newbie

             

            So you are saying that your login page DOES NOT use JSF tags, right? You handle login using your own servlet and then redirect to a JSF page on successful login. For this scenario, you should provide an AuthenticationStrategy as described on this wiki page:


            No, what I am saying is one have to call the first page(http://localhost:8080/XYZ/login or http://localhost:8080/XYZ/login.html) i.e. the "login.html" ( Just contains a jpg image and a redirect tag), an ordinary html page to start the application. Now this page redirects me to my servlet(Servlet ABC) from where I do the processing and renderer the view called "login.faces"(a JSF page created from JSF framework classes at runtime) for the user to login in. From there on, every page is a JSF page.

            • 3. Re: How to test applications where there are no static jsf p
              Stan Silvert Master

               

              "dareurdream" wrote:
              So you are saying that your login page DOES NOT use JSF tags, right? You handle login using your own servlet and then redirect to a JSF page on successful login. For this scenario, you should provide an AuthenticationStrategy as described on this wiki page:


              No, what I am saying is one have to call the first page(http://localhost:8080/XYZ/login or http://localhost:8080/XYZ/login.html) i.e. the "login.html" ( Just contains a jpg image and a redirect tag), an ordinary html page to start the application. Now this page redirects me to my servlet(Servlet ABC) from where I do the processing and renderer the view called "login.faces"(a JSF page created from JSF framework classes at runtime) for the user to login in. From there on, every page is a JSF page.


              Is "Servlet ABC" an instance of javax.faces.webapp.FacesServlet? If not, why not?

              If so, then all you need to do is start your test like this:
              JSFSession jsfSession = new JSFSession("/login.faces");

              Stan

              • 4. Re: How to test applications where there are no static jsf p
                Manish Jain Newbie

                 

                Is "Servlet ABC" an instance of javax.faces.webapp.FacesServlet? If not, why not?

                If so, then all you need to do is start your test like this:
                JSFSession jsfSession = new JSFSession("/login.faces");


                No, "Servlet ABC" is not an instance of FacesServlet. FacesServlet is a final class and so it is not possible to extend or implement it.

                "Servlet ABC" is an ordinary HttpServlet.

                Manish


                • 5. Re: How to test applications where there are no static jsf p
                  Stan Silvert Master

                  Then you will probably need to follow my earlier advice. Just implement InitialRequestStrategy (or extend FormAuthenticationStrategy). Use HtmlUnit to make whatever request is needed before JSFUnit takes over.

                  I'm still interested as to why you have your own servlet that renders JSF? Why do you need this?

                  Stan

                  • 6. Re: How to test applications where there are no static jsf p
                    Manish Jain Newbie

                     

                    I'm still interested as to why you have your own servlet that renders JSF? Why do you need this?


                    Hi Stan,

                    As I told you my web application has no jsp pages deployed on the server. It has only one html page(other than the configuration files like faces-config.xml, web.xml, etc.) and that's it. For JSF to work I have to work via a non-faces request generating a faces response. And that is why I need my own servlet. When the call is made to my servlet, I get the faces servlet and my overridden jsf viewhandler takes the job from there on.

                    Stan, I have found a way to use the org.jboss.jsfunit.jsfsession.JSFServerSession and JSFClientSession classes to get my job done. By passing the html page name as the constructor argument to the org.jboss.jsfunit.jsfsession.JSFSession class I am able to get the jsfsession and through which I am able to access the other objects. But yes those are not as user friendly as the objects in the org.jboss.jsfunit.facade.* pakage, but still I can do with that. Can you please tell me is that the right thing to do?

                    • 7. Re: How to test applications where there are no static jsf p
                      Stan Silvert Master

                      I'm still not sure I understand your architecture. Your JSF markup (JSP or Facelets files) sits on another server? I guess I misunderstood what you meant by "no static jsf pages". I would think that you could create a ViewHandler that would let you continue to use the normal FacesServlet. But I probably just don't understand your requirements.

                      Anyhow, you should not be using anything in the org.jboss.jsfunit.facade package. That package is being phased out in favor of the org.jboss.jsfunit.jsfsession package. So it sounds like you are doing it right.

                      Is there something you like better about the facade package? I could see about getting it into the jsfsession package.

                      org.jboss.jsfunit.facade will not be included in JSFUnit 1.0, which should be out in the next two weeks.

                      Stan

                      • 8. Re: How to test applications where there are no static jsf p
                        Manish Jain Newbie

                         

                        Is there something you like better about the facade package? I could see about getting it into the jsfsession package.


                        Hi Stan,

                        Thanks for asking me to suggest something. As per my usage, what I felt is most of the functions in the facade.JSFClientSession are pretty user friendly, where as there are few very functions defined in the jsfsession.JSFClientSession. I would appreciate if it is possible to move all functions from facade.JSFClientSession to jsfsession.JSFClientSession class. They are direct methods and easy to use. I haven't gone through the other classes in much detail, so I will not be able to comment on them.

                        Moreover, I would appreciate if we can have a function or two which can take care of the method expressions that are attached to a jsf control when an a4j:support is added to it. As of now, what I have to do is - First get the value expression and then from that I have to get the method expression. Since, most jsf developers attach a4j:support tag to there components we can have a function which can directly get the value of the method expression if a component has any tag(like a4j:support) attached to it which gives a method expression.

                        If I am not clear on my explanation let me know. I will attach the code and object pattern in the memory for you to get the requirements. I will not be able to do it right now as am on a trip this weekend.

                        Regards,
                        Manish

                        • 9. Re: How to test applications where there are no static jsf p
                          Stan Silvert Master

                          Hi Manish,

                          Please post code examples. Show me what you are doing now and how you would prefer it to work. I'm very interested in feedback on the API.

                          I'm not sure exactly what you are trying to do, but ajax support is one of the reasons we switched to the new API. If you just call JSFClientSession.click() on the component, or JSFClientSession.type() or whatever, the javascript will execute and the right things will happen.

                          Stan

                          • 10. Re: How to test applications where there are no static jsf p
                            Manish Jain Newbie

                            Hi Stan,

                            I will be posting code samples on Monday. In the meantime, can you please tell me how do I handle multi-user support in JSFUnit.

                            Manish

                            • 11. Re: How to test applications where there are no static jsf p
                              Stan Silvert Master

                              Please make a new post for each new topic. Also, please give a little more detail about the use case your are trying to test.

                              Regards,

                              Stan

                              • 12. Re: How to test applications where there are no static jsf p
                                Manish Jain Newbie

                                Hi Stan,

                                You were correct, the JSFClientSession.click() does my job. I shouldn't be coding for rest of the things. So, I think you need not have to look into the stuff I was doing.

                                Thanks for the help.