1 2 Previous Next 22 Replies Latest reply on Oct 5, 2010 8:49 PM by Tim Evers

    Testing a4j with JSFUnit

    marti marti Newbie

       

      I have 2 selectOneMenu. the second one becomes acrtive and gets <option>s after user select any option from first one.

      the fulfillment of options is being done by a4j.

      here is the code:

       

      <h:selectOneMenu  required="true" rendered="#{selectType!='' and selectType != null and  selectType =='menu' }" id="orderForm"
      value="#{controller.printProductSpecificationsFacesProxy[rs]}" disabled="#{! controller.canSelectSpecification(rs)}">
      <f:selectItem itemLabel="#{messages.message('selectOption')}" itemValue="" />
      <f:selectItems id="#{rsOption}" value="#{selectItems}" />
      <a4j:support  event="onchange" limitToList="false"  reRender="outputPanel"  ajaxSingle="true" eventsQueue="queue" ignoreDupResponses="true" />
      </h:selectOneMenu>

       

      When  i select any option from first one, i output the page to a file to see  if ajax fills the options or not. i can see that second selectOneMenu  still has one option and not filled.

       

      i use following method to work with SelectOneMenu:

      select = (HtmlSelect) client.getElement("orderForm");
      select.getOptionByValue("1").setSelected(true);

       

      what should i do to get ajax working.

      thanks.

        • 1. Re: Testing a4j with JSFUnit
          Stan Silvert Master

          I assume all this works in the browser?

           

          Are you saying that the selectOneMenu, "orderForm" only has one item in it?

           

          I'm not an a4j expert, but I'd play around with some of the attributes.  Do you really need to use the eventsQueue?

           

          How are you outputting to a file?

           

          Stan

          • 2. Re: Testing a4j with JSFUnit
            marti marti Newbie

            selectOneMenu has one option initally but after selecting one of the options from first one, second one generates its options using this line:

            <a4j:support  event="onchange" limitToList="false"   reRender="outputPanel"  ajaxSingle="true" eventsQueue="queue"  ignoreDupResponses="true" />

            I only need to get these updates.

            it seems that select.getOptionByValue("1").setSelected(true) doent produce ajax which is inside <h:selectOneMenu> tag.

            • 3. Re: Testing a4j with JSFUnit
              Tim Evers Master

              Stan, I've been helping Marti get JSFUnit running on his app for a few days now (hampered by time difference a little) and we've made pretty good progress. But, I'm not quite sure why his ajax isn't working on this particular case. I want to go write a small demo page to test it out myself and see if I can track it down. Hopefully I can get around to it today. (But, I really should be doing my own work )

              My initial thought is that the onchange event isn't firing. That being said we could test that I guess.

               

              For the record though, the scenario (as best I can tell as I don't actually have the source just parts of it). Is that the page contains a selectOneMenu that when a value is selected causes a second selectOneMenu to now contain dependent options which then populates a 3rd and the 3rd populates a 4th. So initially the 1st selectOneMenu is the only one with selectable options and once a value is selected the panel is rerendered by ajax and the 2nd selectOneMenu now contains selectable values and so on and so on. Marti, please correct me if I'm wrong

               

              Marti,

              How bout we put some logging in your method that generates the options and see if it is being executed. Let's work out how far we are getting in this process. We can then start narrowing down where we should start looking.

               

              Message was edited by: Tim Evers Clarified the scenario.

              • 4. Re: Testing a4j with JSFUnit
                marti marti Newbie

                Hey Tim

                thanks for joining...

                this is the error i get

                 

                elementName=[option] attributeName=[value] attributeValue=[1]

                com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[option] attributeName=[value] attributeValue=[1]

                 

                now I'm sure

                select.getOptionByValue("101").setSelected(true);

                does not call

                <a4j:support event="onclick"

                 

                yes it's a good idea to put log.

                • 5. Re: Testing a4j with JSFUnit
                  marti marti Newbie

                  O yes the senario is totally correct.

                  • 6. Re: Testing a4j with JSFUnit
                    Tim Evers Master

                    It doesn't need to fire the onclick event, it needs to fire the onchange event. At least that's what I see in your a4j tag

                     

                    "<a4j:support  event="onchange .../>"

                     

                    and the setSelected(true) *should* fire that event.

                    • 7. Re: Testing a4j with JSFUnit
                      marti marti Newbie

                      yes i copied wrong <a4j

                      yes it should work with <a4j:support event="onchange" and fire it.

                      • 8. Re: Testing a4j with JSFUnit
                        Tim Evers Master

                        OK, so I went and wrote a simple test. This works for me. Marti, can you copy this test to your source and run it and see if it runs for you. If it does then it narrows down where the problem lies. Can you also post your entire source for your test and xhtml including the xhtml that is generating these dynamic selectOneMenus.

                         

                        The code below is very primitive and poorly written but it was just to prove a point So, please don't think this is how I normally write code LOL.

                         

                         

                        Seam bean for test

                        @Name(AjaxTest.SEAM_ID)
                        @Scope(ScopeType.PAGE)
                        public class AjaxTest extends AbstractSeamComponent {
                            public static final String SEAM_ID = "AjaxTest";
                            
                            String[] options = {"1", "2", "3", "4"};
                            
                            String[][] dependentLists = 
                                {{"1-op1", "1-op2", "1-op3", "1-op4"},
                                 {"2-op1", "2-op2", "2-op3", "2-op4"},
                                 {"3-op1", "3-op2", "3-op3", "3-op4"},
                                 {"4-op1", "4-op2", "4-op3", "4-op4"}};
                            
                            String selectedOption = "";
                            String selectedDependentOption = "";
                            
                        
                            public String getSelectedOption() {
                                return selectedOption;
                            }
                        
                            public void setSelectedOption(String selectedOption) {
                                this.selectedOption = selectedOption;
                                this.selectedDependentOption = "";
                            }
                            
                            public String getSelectedDependentOption() {
                                return selectedDependentOption;
                            }
                        
                            public void setSelectedDependentOption(String selectedDependentOption) {
                                this.selectedDependentOption = selectedDependentOption;
                            }
                        
                            public List<SelectItem> getOptions() {
                                List<String> optionsList = Arrays.asList(options);
                                List<SelectItem> selections = new ArrayList<SelectItem>();
                                for (String option : optionsList) {
                                    selections.add(new SelectItem(option, "option " + option));
                                }
                                return selections;
                            }
                            
                            public List<SelectItem> getSecondDependantList() {
                                if (StringUtils.isBlank(selectedOption)) {
                                    return new ArrayList<SelectItem>();
                                }
                                else {
                                    List<String> optionList = new ArrayList<String>(Arrays.asList(options));
                                    List<String> dependentOptionsList = Arrays.asList(dependentLists[optionList.indexOf(selectedOption)]);
                                    List<SelectItem> selections = new ArrayList<SelectItem>();
                                    for (String option : dependentOptionsList) {
                                        selections.add(new SelectItem(option, "option " + option));
                                    }
                                    return selections;
                                }
                            }
                        
                            public String[][] getDependantLists() {
                                return dependentLists;
                            }
                        
                            public void setDependantLists(String[][] dependantLists) {
                                this.dependentLists = dependantLists;
                            }   
                        }
                        

                         

                        XHTML file for test

                        <?xml version="1.0" encoding="UTF-8"?>
                        <ui:composition
                            xmlns="http://www.w3.org/1999/xhtml"
                            xmlns:s="http://jboss.com/products/seam/taglib"
                            xmlns:ui="http://java.sun.com/jsf/facelets"
                            xmlns:f="http://java.sun.com/jsf/core"
                            xmlns:h="http://java.sun.com/jsf/html"
                            xmlns:rich="http://richfaces.org/rich"
                            xmlns:a4j="https://ajax4jsf.dev.java.net/ajax">
                            
                            <h:form id="testForm">
                                <ui:debug hotkey="Q" />
                                <a4j:log />
                                <a4j:queue name="queue"/>
                                <h:panelGrid id="outputPanel" columns="1">
                                    <h:selectOneMenu
                                        id="menu1" 
                                        value="#{AjaxTest.selectedOption}">
                                        <f:selectItem itemLabel="Please Select" itemValue="" />
                                        <f:selectItems value="#{AjaxTest.options}"/>
                                        <a4j:support
                                            event="onchange" 
                                            limitToList="false"  
                                            reRender="outputPanel"  
                                            ajaxSingle="true" 
                                            eventsQueue="queue" 
                                            ignoreDupResponses="true" /> 
                                    </h:selectOneMenu>
                                    
                                    <h:selectOneMenu
                                        id="menu2" 
                                        value="#{AjaxTest.selectedDependentOption}"
                                        rendered="#{not empty AjaxTest.selectedOption}">
                                        <f:selectItem itemLabel="Please Select" itemValue="" />
                                        <f:selectItems value="#{AjaxTest.secondDependantList}"/>
                                        <a4j:support
                                            event="onchange" 
                                            limitToList="false"  
                                            reRender="outputPanel"  
                                            ajaxSingle="true" 
                                            eventsQueue="queue" 
                                            ignoreDupResponses="true" />
                                    </h:selectOneMenu>
                                </h:panelGrid>
                            </h:form>
                        </ui:composition>    
                           
                        
                        

                         

                        Test class

                        public class AjaxTestPage extends ServletTestCase {
                            
                            JSFClientSession client;
                            JSFServerSession server;
                            
                            
                            @Override
                            public void setUp() throws IOException {
                                final JSFSession jsfSession = new JSFSession("/scratch/tje/ajaxTest.jsf");
                                client = jsfSession.getJSFClientSession();
                                server = jsfSession.getJSFServerSession();
                            }
                            
                            public void testIt() {
                                HtmlSelect select = (HtmlSelect) client.getElement("menu1");
                                select.getOptionByValue("2").setSelected(true);
                                
                                HtmlSelect select2 = (HtmlSelect) client.getElement("menu2");
                                select2.getOptionByValue("2-op2").setSelected(true);
                                
                            }
                            
                        }
                        
                        • 9. Re: Testing a4j with JSFUnit
                          marti marti Newbie

                          Hey Tim

                          can you send me required jars for class AjaxTest. what are the imports for that class?

                          thanks.

                          • 10. Re: Testing a4j with JSFUnit
                            Tim Evers Master

                            Marti,

                             

                            The jars you are using for your current testing should be fine. I purposfully made the test use as little as possible. The only thing you might want to do is remove the line

                            xmlns:rich="http://richfaces.org/rich"
                            

                            from the xhtml.

                             

                            If you are getting ClassNotFound exceptions then please post them here so we can work it out. But, I'm pretty sure I have no references to any special libs in that example.

                             

                            So yeah, if you can't get this test running then post the errors maybe it will help us find your underlying problem.

                            • 11. Re: Testing a4j with JSFUnit
                              marti marti Newbie

                              I'm getting red lines under @Name, @Scope lines and also under AbstractSeamComponent.

                              When I import jboss.Seam.* lib in order to make annotations understandable, in that case i'm getting server 500 error when I run the test in browser.

                              I deleted that jar and now test is working.

                              what makes that conflict?

                              • 12. Re: Testing a4j with JSFUnit
                                Tim Evers Master

                                Bleh, I forgot to remove the

                                extends AbstractSeamComponent

                                 

                                that's just my internal superclass for all our Seam beans. Nothing fancy in there.

                                 

                                The @Name and @Scope are plain Seam annotations if you are getting a 500 error then check your server log, or make sure you are typing in the right url.

                                • 13. Re: Testing a4j with JSFUnit
                                  Tim Evers Master

                                  With regards to the comment about the Jar.

                                   

                                  What Jar did you delete and from where did you delete it?

                                  So, the example test is running now? Does it succeed or fail?

                                  • 14. Re: Testing a4j with JSFUnit
                                    marti marti Newbie

                                    OK, I deleted jboss-seam.jar from WEB-INF/lib

                                    I commented 2 annotations, because there is no jboss-seam.jar anymore. i run the test and get this error.

                                     

                                     

                                    org.apache.jasper.JasperException: /pages/ajaxTest.jsp(18,25) #{...} is not allowed in template text
                                         org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
                                         org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
                                         org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:102)
                                         org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:706)
                                         org.apache.jasper.compiler.Node$ELExpression.accept(Node.java:958)
                                         org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
                                         org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2411)
                                         org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2417)
                                         org.apache.jasper.compiler.Node$Root.accept(Node.java:495)
                                         org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
                                         org.apache.jasper.compiler.Validator.validateExDirectives(Validator.java:1763)
                                         org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:198)
                                         org.apache.jasper.compiler.Compiler.compile(Compiler.java:347)
                                         org.apache.jasper.compiler.Compiler.compile(Compiler.java:327)
                                         org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
                                         org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
                                         org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
                                         org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
                                         org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
                                         javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
                                         org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:706)
                                         org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:677)
                                         org.apache.jsp.index_jsp._jspService(index_jsp.java:57)
                                         org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
                                         javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
                                         org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
                                         org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
                                         org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
                                         javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
                                    
                                    1 2 Previous Next