9 Replies Latest reply on Mar 31, 2009 11:28 AM by Eric Taieb

    Problems with Richfaces onBlur testing

    Eric Taieb Newbie

      Hi,

      We have input fields that replace/convert Dates when onBlur event in inputFields.

      In our JSFUnit tests we enter the text (via client.setValue) in the inputField then we enter other text in other fields.

      If we then check the value on the client, the field has still the original value, not the converted one.

      How can we test this kind of ajax behavior with JSFUnit ?

      We are not sure how to do it as it seems the javascript event is not fired...

      I guess it is an htmlunit issue but we wanted to know if anyone tried this successfully with JSFUnit and what is the best way to test this kind of ajax behavior...

      Thanks in advance for your help,

      --Eric

        • 1. Re: Problems with Richfaces onBlur testing
          Brian Gregory Newbie

          If you use client.getElement("name of input") you should get something back that you can cast as a HtmlInput (HtmlUnit class).
          http://htmlunit.sourceforge.net/apidocs/com/gargoylesoftware/htmlunit/html/HtmlInput.html

          The base class HtmlElement has methods for blur() and focus() which should work.

          -Brian

          • 2. Re: Problems with Richfaces onBlur testing
            Eric Taieb Newbie

            Thanks for your fast reply!

            As per your recommendation, we have done the following:

            client.setValue("mainFormNewRequest:quote_values:0:expiry", "Dec17");
            //check the expiry auto-date 15-Dec-17
            //On client side
            HtmlInput expiryInputField=(HtmlInput) client.getElement("mainFormNewRequest:quote_values:0:expiry");
            expiryInputField.blur();
            assertEquals(expiryInputField.getAttribute("value"),"15-Dec-17");
            


            Nevertheless expiryInputField.getAttribute("value") always returns 'Dec17'

            This works perfectly on FireFox,Safari and IE...

            After setting breakpointsr we can tell the ActionListener method for the a4j:support is never fired...

            We'll continue testing/debugging see if we find anything...

            Thanks again,

            Cheers,


            --Eric






            • 3. Re: Problems with Richfaces onBlur testing
              Eric Taieb Newbie

              In fact to be more precise our InputText works as follows:

              It has a Seam/JSF converter (specified with the converter attribute).
              The a4j:support persists the data (calls a method onExpiryChanged which does a FacesContext processupdates), that does the conversion and the reRender attribute resfreshes then the Field.

              It works very well with browsers (we have tried several ways to do this but the InpuText being in a DataTable, this is the only way we managed to get it work flawlessly)

              It seems that with the code in my earlier post, neither the onExpiryChanged method, nor the Converter are called when I fire .blur()


              I thought I'd just give more infos on the code behavior to let you understand...

              A big thanks again to all of you for your help,

              --Eric

              • 4. Re: Problems with Richfaces onBlur testing
                Stan Silvert Master

                As you know, onblur is triggered when the object loses focus. So you need to gain AND lose focus for HtmlUnit to trigger the event. First, look at the HTML source to make sure that the onblur attribute is present on the element you are getting with JSFClientSession.

                Then try something like this:

                client.setValue("mainFormNewRequest:quote_values:0:expiry", "Dec17");
                //check the expiry auto-date 15-Dec-17
                //On client side
                HtmlInput expiryInputField=(HtmlInput) client.getElement("mainFormNewRequest:quote_values:0:expiry");
                expiryInputField.focus();
                expiryInputField.blur();
                assertEquals(expiryInputField.getAttribute("value"),"15-Dec-17");


                Please let me know if that works.

                When you call JSFClientSession(), it is calling HtmlElement.setAttributeValue() under the covers. I think that this is not triggering focus()/blur(). Perhaps I need to be calling that inside JSFClientSession(). What do you guys think?

                Stan

                • 5. Re: Problems with Richfaces onBlur testing
                  Eric Taieb Newbie

                  Thanks for your help Stan !

                  I have checked the source (produced by RichFaces' Ajax) and it has the onblur event:

                  <input id="mainFormNewRequest:quote_values:0:expiry" type="text" name="mainFormNewRequest:quote_values:0:expiry" onblur="A4J.AJAX.Submit('_viewRoot','mainFormNewRequest',event,{'parameters':{'ajaxSingle':'mainFormNewRequest:quote_values:0:expiry','mainFormNewRequest:quote_values:0:j_id33':'mainFormNewRequest:quote_values:0:j_id33'} ,'actionUrl':'/newInterest.seam','similarityGroupingId':'mainFormNewRequest:quote_values:0:j_id33','control':this,'implicitEventsQueue':'mainFormNewRequest:quote_values:0:j_id33','affected':['mainFormNewRequest:errorMessages','mainFormNewRequest:quote_values:0:expiry'] ,'requestDelay':300} )" />
                  


                  I have tried your suggestion but it still doesn't fire the event...

                  I have also tried this (which should be the same as your method but using directly HtmlUnit), should mimic typical user interaction in a browser:

                  HtmlInput expiryInputField=(HtmlInput) client.getElement("mainFormNewRequest:quote_values:0:expiry");
                  expiryInputField.focus();
                  expiryInputField.setAttribute("value", "Dec17");
                  expiryInputField.blur();
                  assertEquals(expiryInputField.getAttribute("value"),"15-Dec-17");
                  


                  Doesn't work either...onExpiryChanged never gets called...

                  Thanks again for your help...I'll keep you posted on our progress identifyin the issue...

                  We have already tried tweaking the a4j:support line (removing the request delay, the limitToList, etc.) whith no success...

                  • 6. Re: Problems with Richfaces onBlur testing
                    Stan Silvert Master

                    I'll see if one of the HtmlUnit guys can comment on this.

                    Stan

                    • 7. Re: Problems with Richfaces onBlur testing [SOLVED]
                      Eric Taieb Newbie

                      Hi,

                      I found the problem.

                      Let me share it with you guys:

                      It seems that the ajax update takes a little bit longer than the execution of the test, java code is so fast!! ;-)

                      I found it after adding a wait for 3 seconds after the .blur() and I noticed this in my server logs (just after barely a second):

                      2009-03-31 16:48:25,407 INFO [com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController] - Re-synchronized call to http://localhost:8080/newInterest.seam


                      I figured out it had something to do with the test not waiting enough for the new Date to be filled by the ajax request.

                      Therefore I modified my code as follows:
                      Basically I removed the requestDelay attribute from the a4j:support (was 300ms) and I added a wait for 3 seconds before checking for the new value

                      client.setValue("mainFormNewRequest:quote_values:0:expiry", "Dec17");
                      
                      HtmlInput expiryInputField=(HtmlInput) client.getElement("mainFormNewRequest:quote_values:0:expiry");
                      expiryInputField.focus();
                      expiryInputField.blur();
                      System.out.println(sleepForXSeconds(3));
                      expiryInputField=(HtmlInput) client.getElement("mainFormNewRequest:quote_values:0:expiry");
                      assertEquals(expiryInputField.getAttribute("value"),"15-Dec-17");
                      


                      And it works like a charm!

                      In real life, with a user and a browser the ajax update on the field is instantly applied (no 2 seconds lag)...seems htmlunit is slower than a real browser (Safari4beta on my side)...

                      I wish to thank averyone here that help me in sorting this out!

                      Keep up the good work on JSFUnit! I already love its approach to JSF/Seam/richfaces integration testing!!


                      • 8. Re: Problems with Richfaces onBlur testing
                        Stan Silvert Master

                        Glad you figured it out. Can you try it without focus() and blur()? Does it still work?

                        Stan

                        • 9. Re: Problems with Richfaces onBlur testing
                          Eric Taieb Newbie

                          Hi Stan,

                          I can confirm it DOESN'T WORK if i remove the focus()/.blur() sequence.

                          Again, Thanks for your QUICK responses mate !

                          --Eric