6 Replies Latest reply on Jun 13, 2008 12:59 PM by ssilvert

    how to test javascript alert messages

    bngupta

      Hi Stan,
      I have test the alert messages and confirmation messages ....
      what are the api methods i need to use for that....


      please help in this

      thanks & Regards,
      Nara

        • 1. Re: how to test javascript alert messages
          ssilvert

          Hi Nara,

          For this you will need to use the HtmlUnit API. The current beta release uses HttpUnit, but the next one will introduce HtmlUnit and we will almost certainly use HtmlUnit going forward.

          If you checkout and build JSFUnit from SVN, you will see a new package called org.jboss.jsfunit.jsfsession.
          See http://www.jboss.org/jsfunit/source-repository.html

          Many of the classes in that package look the same as the ones in the old facade package. The difference is that you will start out your test code like this:

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


          The JSFServerSession API is exactly the same, but the JSFClientSession API is now based on HtmlUnit instead of HttpUnit. So for testing javascript and things like alert messages, you will use HtmlUnit, which has much better javascript and AJAX support. See http://htmlunit.sourceforge.net/.

          Right now we are having a problem with HtmlUnit and RichFaces. As soon as that is resolved and HtmlUnit puts out a new release with the fix, I'm going to do another release of JSFUnit.

          I'd love to get feedback on the new JSFClientSession. Please post comments to new threads on this forum.

          Thanks,

          Stan

          • 2. Re: how to test javascript alert messages
            bngupta

            Hi stan,

            I tried with HtmlUnit API. Below is the code for that


            import org.jboss.jsfunit.jsfsession.JSFClientSession;
            import org.jboss.jsfunit.jsfsession.JSFServerSession;
            import org.jboss.jsfunit.jsfsession.JSFSession;

            public class TestHome extends ServletTestCase
            {

            private JSFClientSession client;
            private JSFServerSession server;
            private HtmlPage htmlPage;
            public void setUp() throws IOException, SAXException
            {
            JSFSession jsfSession = new JSFSession("/home/home.faces?src=test");
            this.client = jsfSession.getJSFClientSession();
            this.server = jsfSession.getJSFServerSession();
            }
            }

            ----------------------------------------------------------------------------------

            I am getting the following error


            TypeError: Cannot set property "value" of null to "" (script in http://localhost:8080/ibclover/clover/home/home.faces?sid=O023384&appId=1&appName=CLOVER&src=test from (1725, 10) to (0, 0)#1873)

            ======= EXCEPTION START ========
            EcmaError: lineNumber=[1873] column=[0] lineSource=[<no source>] name=[TypeError] sourceName=[script in http://localhost:8080/ibclover/clover/home/home.faces?sid=O023384&appId=1&appName=CLOVER&src=test from (1725, 10) to (0, 0)] message=[TypeError: Cannot set property "value" of null to "" (script in http://localhost:8080/ibclover/clover/home/home.faces?sid=O023384&appId=1&appName=CLOVER&src=test from (1725, 10) to (0, 0)#1873)]
            com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot set property "value" of null to "" (script in http://localhost:8080/ibclover/clover/home/home.faces?sid=O023384&appId=1&appName=CLOVER&src=test from (1725, 10) to (0, 0)#1873)
            at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:553)
            at org.mozilla.javascript.Context.call(Context.java:499)
            at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:511)
            at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:436)
            at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:411)
            at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptIfPossible(HtmlPage.java:935)
            at com.gargoylesoftware.htmlunit.html.HtmlScript.executeInlineScriptIfNeeded(HtmlScript.java:285)
            at com.gargoylesoftware.htmlunit.html.HtmlScript.appendChild(HtmlScript.java:193)
            at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.handleCharacters(HTMLParser.java:516)
            at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:478)
            at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
            at org.cyberneko.html.filters.DefaultFilter.endElement(DefaultFilter.java:210)
            at org.cyberneko.html.filters.NamespaceBinder.endElement(NamespaceBinder.java:329)
            at org.cyberneko.html.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:971)
            at org.cyberneko.html.HTMLTagBalancer.endElement(HTMLTagBalancer.java:874)
            at org.cyberneko.html.HTMLScanner$SpecialScanner.scan(HTMLScanner.java:2906)
            at org.cyberneko.html.HTMLScanner.scanDocument(HTMLScanner.java:877)
            at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:495)
            at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:448)
            at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
            at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:261)
            at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:155)
            at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:128)
            at org.jboss.jsfunit.jsfsession.JSFUnitPageCreator.createPage(JSFUnitPageCreator.java:54)
            at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:445)
            at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:352)
            at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:402)
            at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:389)
            at org.jboss.jsfunit.jsfsession.JSFSession.(JSFSession.java:88)
            at org.jboss.jsfunit.jsfsession.JSFSession.(JSFSession.java:59)
            at com.jpmorgan.test.TestHome.setUp(TestHome.java:59)
            at org.apache.cactus.internal.AbstractCactusTestCase.runBareServer(AbstractCactusTestCase.java:153)
            at org.apache.cactus.internal.server.AbstractWebTestCaller.doTest(AbstractWebTestCaller.java:119)
            at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody0(AbstractWebTestController.java:93)
            at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody1$advice(AbstractWebTestController.java:224)
            at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest(AbstractWebTestController.java)
            at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody2(ServletTestRedirector.java:101)
            at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody3$advice(ServletTestRedirector.java:224)
            at org.apache.cactus.server.ServletTestRedirector.doPost(ServletTestRedirector.java)
            at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody0(ServletTestRedirector.java:72)
            at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody1$advice(ServletTestRedirector.java:224)
            at org.apache.cactus.server.ServletTestRedirector.doGet(ServletTestRedirector.java)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
            at org.jboss.jsfunit.framework.JSFUnitFilter.doFilter(JSFUnitFilter.java:122)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
            at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
            at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
            at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
            at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
            at java.lang.Thread.run(Thread.java:595)
            Caused by: org.mozilla.javascript.EcmaError: TypeError: Cannot set property "value" of null to "" (script in http://localhost:8080/ibclover/clover/home/home.faces?sid=O023384&appId=1&appName=CLOVER&src=test from (1725, 10) to (0, 0)#1873)
            at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3557)
            at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3535)
            at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3563)
            at org.mozilla.javascript.ScriptRuntime.typeError3(ScriptRuntime.java:3589)
            at org.mozilla.javascript.ScriptRuntime.undefWriteError(ScriptRuntime.java:3611)
            at org.mozilla.javascript.ScriptRuntime.setObjectProp(ScriptRuntime.java:1505)
            at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:3063)
            at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2484)
            at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:162)
            at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:401)
            at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:192)
            at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3003)
            at org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:173)
            at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$5.doRun(JavaScriptEngine.java:427)
            at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:549)
            ... 61 more
            Enclosed exception:
            org.mozilla.javascript.EcmaError: TypeError: Cannot set property "value" of null to "" (script in http://localhost:8080/ibclover/clover/home/home.faces?sid=O023384&appId=1&appName=CLOVER&src=test from (1725, 10) to (0, 0)#1873)
            at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3557)
            at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3535)
            at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3563)
            at org.mozilla.javascript.ScriptRuntime.typeError3(ScriptRuntime.java:3589)
            at org.mozilla.javascript.ScriptRuntime.undefWriteError(ScriptRuntime.java:3611)
            at org.mozilla.javascript.ScriptRuntime.setObjectProp(ScriptRuntime.java:1505)
            at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:3063)
            at script(script in http://localhost:8080/ibclover/clover/home/home.faces?sid=O023384&appId=1&appName=CLOVER&src=test from (1725, 10) to (0, 0):1873)
            at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2484)
            at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:162)
            at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:401)
            at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:192)
            at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3003)
            at org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:173)
            at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$5.doRun(JavaScriptEngine.java:427)
            at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:549)
            at org.mozilla.javascript.Context.call(Context.java:499)
            at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:511)
            at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:436)
            at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:411)
            at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptIfPossible(HtmlPage.java:935)
            at com.gargoylesoftware.htmlunit.html.HtmlScript.executeInlineScriptIfNeeded(HtmlScript.java:285)
            at com.gargoylesoftware.htmlunit.html.HtmlScript.appendChild(HtmlScript.java:193)
            at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.handleCharacters(HTMLParser.java:516)
            at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:478)
            at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
            at org.cyberneko.html.filters.DefaultFilter.endElement(DefaultFilter.java:210)
            at org.cyberneko.html.filters.NamespaceBinder.endElement(NamespaceBinder.java:329)
            at org.cyberneko.html.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:971)
            at org.cyberneko.html.HTMLTagBalancer.endElement(HTMLTagBalancer.java:874)
            at org.cyberneko.html.HTMLScanner$SpecialScanner.scan(HTMLScanner.java:2906)
            at org.cyberneko.html.HTMLScanner.scanDocument(HTMLScanner.java:877)
            at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:495)
            at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:448)
            at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
            at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:261)
            at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:155)
            at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:128)
            at org.jboss.jsfunit.jsfsession.JSFUnitPageCreator.createPage(JSFUnitPageCreator.java:54)
            at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:445)
            at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:352)
            at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:402)
            at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:389)
            at org.jboss.jsfunit.jsfsession.JSFSession.(JSFSession.java:88)
            at org.jboss.jsfunit.jsfsession.JSFSession.(JSFSession.java:59)
            at com.jpmorgan.test.TestHome.setUp(TestHome.java:59)
            at org.apache.cactus.internal.AbstractCactusTestCase.runBareServer(AbstractCactusTestCase.java:153)
            at org.apache.cactus.internal.server.AbstractWebTestCaller.doTest(AbstractWebTestCaller.java:119)
            at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody0(AbstractWebTestController.java:93)
            at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody1$advice(AbstractWebTestController.java:224)
            at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest(AbstractWebTestController.java)
            at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody2(ServletTestRedirector.java:101)
            at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody3$advice(ServletTestRedirector.java:224)
            at org.apache.cactus.server.ServletTestRedirector.doPost(ServletTestRedirector.java)
            at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody0(ServletTestRedirector.java:72)
            at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody1$advice(ServletTestRedirector.java:224)
            at org.apache.cactus.server.ServletTestRedirector.doGet(ServletTestRedirector.java)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
            at org.jboss.jsfunit.framework.JSFUnitFilter.doFilter(JSFUnitFilter.java:122)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
            at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
            at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
            at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
            at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
            at java.lang.Thread.run(Thread.java:595)
            ======= EXCEPTION END ========



            Please advise me how to proceed further..Thanks for your help..

            Regards
            burula

            • 3. Re: how to test javascript alert messages
              ssilvert

              Thanks for trying out the new HtmlUnit stuff.

              So what I see happening is that HtmlUnit is unable to process the initial page because it can't execute some javascript.

              It is throwing an HtmlUnit ScriptException. You can catch that exception and get a lot of information about what it is choking on, including the script source code that failed. See the methods for ScriptExcption here:
              http://htmlunit.sourceforge.net/apidocs/com/gargoylesoftware/htmlunit/ScriptException.html

              Also, I Googled and found a thread where someone else was getting the exact same exception with the same values. BTW, Marc Guillemot is the one who answered the thread. He is the lead for HtmlUnit.
              http://lists.canoo.com/pipermail/webtest/2007q4/009291.html
              http://lists.canoo.com/pipermail/webtest/2007q4/009327.html
              You may want to file a bug with HtmlUnit if it turns out to be a problem on their end.

              Hope that helps. Please write back and let us know what you find out.

              Stan

              • 4. Re: how to test javascript alert messages
                sreedhar.pamidiparthi

                Hi Stan,

                I think this is related to the BrowserVersion we are using while creating the WebClient.

                I asked for the same in this topic,

                http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4154085#4154085

                Please make us clear about this. I'm also trying find out what is happening.

                • 5. Re: how to test javascript alert messages
                  ssilvert

                  For anyone else reading this thread, the BrowserVersion problem is now fixed. There is a new constructor for JSFSession that allows you to pass in the BrowserVersion.

                  Stan

                  • 6. Re: how to test javascript alert messages
                    ssilvert

                     

                    "stan.silvert@jboss.com" wrote:
                    For anyone else reading this thread, the BrowserVersion problem is now fixed. There is a new constructor for JSFSession that allows you to pass in the BrowserVersion.

                    Stan


                    We changed the way this is done in JSFUNIT-99. See http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4158029#4158029

                    Stan