Unit testing - updating a DropDownChoice with Ajax

classic Classic list List threaded Threaded
12 messages Options
glr
Reply | Threaded
Open this post in threaded view
|

Unit testing - updating a DropDownChoice with Ajax

glr
Hi

I have 2 DropDownChoice's on a form. When making a choice in the first, the choices in the second are updated using AjaxFormComponentUpdatingBehavior. (Just like in the Drop Down Choice Example of the "live action" Wicket Examples (http://www.wicket-library.com/wicket-examples/ajax?wicket:bookmarkablePage=:wicket.examples.ajax.builtin.ChoicePage)).

Could anyone tell me what the recommended way is to unit-test the above case?

I tried using WicketTester with FormTester but I cannot have the model of the first DropDownChoice updated as a response to making a selection in it. As a result, when the OnEvent handler of the AjaxFormComponentUpdatingBehavior fires, it looks as if there was no selection in the first DropDownChoice.

Thanks for your answer in advance,
Richard.
Reply | Threaded
Open this post in threaded view
|

Re: Unit testing - updating a DropDownChoice with Ajax

Timo Rantalaiho
On Tue, 19 Jun 2007, glr wrote:

> I have 2 DropDownChoice's on a form. When making a choice in the first, the
> choices in the second are updated using AjaxFormComponentUpdatingBehavior.
> (Just like in the Drop Down Choice Example of the "live action" Wicket
> Examples
> (http://www.wicket-library.com/wicket-examples/ajax?wicket:bookmarkablePage=:wicket.examples.ajax.builtin.ChoicePage)).
>
> Could anyone tell me what the recommended way is to unit-test the above
> case?
>
> I tried using WicketTester with FormTester but I cannot have the model of
> the first DropDownChoice updated as a response to making a selection in it.
> As a result, when the OnEvent handler of the
> AjaxFormComponentUpdatingBehavior fires, it looks as if there was no
> selection in the first DropDownChoice.

These model updates and/or ajax functionality cause a lot of
problems for WicketTester in our experience. I think that
you just need to make your tests more static, e.g. one test
to see that the onEvent handler is called and then another
one where you set the model of the first DropDownChoice
beforehand. Often you have more success in the tests if you
can init the models in the state to test already before
firing up WicketTester.

With SeleniumTestCase of Wicket Bench you can test
individual components with Selenium on Firefox to complement
WicketTester, which is an approach we used with success on
our previous project that was on Java 5 (required by Wicket
Bench). There's practically nothing that you couldn't test
in Wicket like that, our ui code line coverage was well over
90 % in that project.

(It's a pity that WicketTester has so many bugs or
shortcomings; if I ever find the spare moment I'll try to
understand how it works to be able to do better bug reports
on it or even patches. Any help for getting into it is
welcome!)

Best wishes,
Timo

--
Timo Rantalaiho          
Reaktor Innovations Oy    <URL: http://www.ri.fi/ >

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/wicket-user
Reply | Threaded
Open this post in threaded view
|

Re: Unit testing - updating a DropDownChoice with Ajax

Eelco Hillenius
> (It's a pity that WicketTester has so many bugs or
> shortcomings; if I ever find the spare moment I'll try to
> understand how it works to be able to do better bug reports
> on it or even patches. Any help for getting into it is
> welcome!)

If you can, do it before the book comes out please :) We still have to
write about unit testing.

Eelco

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/wicket-user
Reply | Threaded
Open this post in threaded view
|

Re: Unit testing - updating a DropDownChoice with Ajax

Jean-Baptiste Quenot-3
In reply to this post by glr
* glr:

> I tried using WicketTester with FormTester but I cannot have the
> model  of the  first  DropDownChoice updated  as  a response  to
> making a selection in it.  As a result, when the OnEvent handler
> of the  AjaxFormComponentUpdatingBehavior fires, it looks  as if
> there was no selection in the first DropDownChoice.

Can you please provide a sample  code for your test?  Did you make
use of FormTester.select(String, int)?
--
     Jean-Baptiste Quenot
aka  John Banana   Qwerty
http://caraldi.com/jbq/

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/wicket-user
glr
Reply | Threaded
Open this post in threaded view
|

Re: Unit testing - updating a DropDownChoice with Ajax

glr
Hi

Here is the code for the test-case. I tried to use FormTester.select(...):

    public void testSelectResources() {
        // Set expectations to the mock content store
        final String title1 = "MyArticle-1";
        List<String> editorials = new ArrayList<String>();
        editorials.add(title1);
        // Expectations for the editor display
        expect(this.mockArticleStore.listEditorials()).andReturn(editorials);
       
        // Expectations for listing the resources (uploaded files).
        List<String> fileNames = new ArrayList();
        fileNames.add("rsc-1.bin");
        fileNames.add("rsc-2.bin");
        expect(this.mockResourceStore.getFilenames((String)anyObject())).andReturn(fileNames);
       
        // Replay recorded expectations
        replay(this.mockArticleStore);
        replay(this.mockResourceStore);
       
        this.tester.startPage(ArticleEditor.class);

        // Check if the editor page is shown.
        this.tester.assertRenderedPage(ArticleEditor.class);        
       
        // Test resource selection
        DropDownChoice rscValuesChoice =
                (DropDownChoice)this.tester.getLastRenderedPage().get(
                    "articleEditorForm:rscValuesList");
        List rscValues = rscValuesChoice.getChoices();
        assertTrue(rscValues.size() == 0);
       
        FormTester formTester =
                this.tester.newFormTester("articleEditorForm", false);
        formTester.select("rscTypesList", 0);  
       
        this.tester.executeAjaxEvent("articleEditorForm:rscTypesList", "onchange");
   
        rscValuesChoice =
                (DropDownChoice)this.tester.getLastRenderedPage().get(
                    "articleEditorForm:rscValuesList");
        rscValues = rscValuesChoice.getChoices();
        assertTrue(rscValues.size() == 2);
       
        verify(this.mockResourceStore);
        verify(this.mockArticleStore);
    }

mockResourceStore and mockArticleStore are two mock objects used to interact with at unit-testing time.
The AJAX event fires correctly, but the component that it would update does not have its model in the expected state. (The production code does work correctly as integration testing can verify it.)

Richard.

Jean-Baptiste Quenot-3 wrote
* glr:

> I tried using WicketTester with FormTester but I cannot have the
> model  of the  first  DropDownChoice updated  as  a response  to
> making a selection in it.  As a result, when the OnEvent handler
> of the  AjaxFormComponentUpdatingBehavior fires, it looks  as if
> there was no selection in the first DropDownChoice.

Can you please provide a sample  code for your test?  Did you make
use of FormTester.select(String, int)?
--
     Jean-Baptiste Quenot
aka  John Banana   Qwerty
http://caraldi.com/jbq/

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
Wicket-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-user
Reply | Threaded
Open this post in threaded view
|

Re: Unit testing - updating a DropDownChoice with Ajax

Jean-Baptiste Quenot-3
* glr:
>
>         FormTester formTester = this.tester.newFormTester("articleEditorForm", false);
>         formTester.select("rscTypesList", 0);
>         this.tester.executeAjaxEvent("articleEditorForm:rscTypesList", "onchange");

Can you please try with:

        formTester.submit();

instead of:

        this.tester.executeAjaxEvent("articleEditorForm:rscTypesList", "onchange");

To see if it's not the ajax behavior that causes problems?

You may want to have a look at an existing working unit test for
selecting a new value with the DropDownChoice:

http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/tester/apps_3/FormTesterTest.java?revision=530304&view=markup
--
     Jean-Baptiste Quenot
aka  John Banana   Qwerty
http://caraldi.com/jbq/

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/wicket-user
Reply | Threaded
Open this post in threaded view
|

Re: Unit testing - updating a DropDownChoice with Ajax

Jean-Baptiste Quenot-3
* Jean-Baptiste Quenot:

> * glr:
> >
> >         FormTester formTester = this.tester.newFormTester("articleEditorForm", false);
> >         formTester.select("rscTypesList", 0);
> >         this.tester.executeAjaxEvent("articleEditorForm:rscTypesList", "onchange");
>
> Can you please try with:
>
>         formTester.submit();
>
> instead of:
>
>         this.tester.executeAjaxEvent("articleEditorForm:rscTypesList", "onchange");
>
> To see if it's not the ajax behavior that causes problems?

Indeed  WicketTester was  overwriting field  values in  two places
when submitting via  Ajax.  I just fixed this, you  can safely use
executeAjaxEvent() now.

The related JIRA issue is:

Allow to set field values before submitting a form with Ajax in WicketTester
https://issues.apache.org/jira/browse/WICKET-254
--
     Jean-Baptiste Quenot
aka  John Banana   Qwerty
http://caraldi.com/jbq/

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/wicket-user
Reply | Threaded
Open this post in threaded view
|

Re: Unit testing - updating a DropDownChoice with Ajax

Jean-Baptiste Quenot-3
In reply to this post by Timo Rantalaiho
* Timo Rantalaiho:

> (It's a pity that WicketTester has so many bugs or shortcomings;
> if I  ever find the spare  moment I'll try to  understand how it
> works  to  be able  to  do  better bug  reports  on  it or  even
> patches. Any help for getting into it is welcome!)

Hi Timo,

It's a pity  that you didn't mention the JIRA  issue in this email
thread, as it would have helped to address the problem in a timely
manner, I was not sure what bug you were exactly talking about and
didn't ask.  Anyway now it's fixed, I discovered it is WICKET-254.

If you  are aware  of other  bugs in  WicketTester, please  let us
know, there  is no reason  why we  would want to  let WicketTester
unmaintained, I'm sorry if you feel so.  This is a great tool that
we  use  a  lot,  we  just  have  different  usecases  than  yours
obviously, or it also happens that when something doesn't work, we
don't always have the  time to fix it and work  around it like any
developer would do.

Be sure  that your bug reports  are very valuable, it's  true that
you opened the issue  a few months ago, but one  day or another we
always end up resolving it.  Don't hesitate to remind us from time
to time of  the little cracks that  went in, and keep  up the good
bug reports ;-)
--
     Jean-Baptiste Quenot
aka  John Banana   Qwerty
http://caraldi.com/jbq/

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/wicket-user
Reply | Threaded
Open this post in threaded view
|

Re: Unit testing - updating a DropDownChoice with Ajax

Timo Rantalaiho
On Mon, 25 Jun 2007, Jean-Baptiste Quenot wrote:
> It's a pity  that you didn't mention the JIRA  issue in this email
> thread, as it would have helped to address the problem in a timely
> manner, I was not sure what bug you were exactly talking about and
> didn't ask.  Anyway now it's fixed, I discovered it is WICKET-254.

Yep I noticed, thankyou very much!

In fact we just discovered a workaround to a DropDownChoice
ajax issue with WicketTester in our app yesterday; I was
going to file an issue or talk about it here, but didn't
have time yet. It was something similar to the one just
discussed here, so I'll try later today whether your fix
also fixes that and report.

> If you  are aware  of other  bugs in  WicketTester, please  let us
> know, there  is no reason  why we  would want to  let WicketTester
> unmaintained, I'm sorry if you feel so.  This is a great tool that

No I don't, my workmate Kare seems more worried ;), and also
I think that the GUIs we're doing are relatively complex
with everything ajaxified and lots of dependencies between
screen components. That combined with serious TDD makes the
limitations of Wicket, WicketTester and our skills to
surface easily.

I for one will put more effort into bug reporting and making the
issues reproduceable after this encouraging experience of
having WICKET-254 fixed :) We have a couple of other test
methods disabled because we at least think that it was due
to WicketTester shortcomings, so let's see if something new
comes up.

> Be sure  that your bug reports  are very valuable, it's  true that

Good to hear. And to make it clear I also want to say that
Wicket and its community are excellent, and so is even
WicketTester with its possiblle shortcomings, as it allows
us to do 80-90 % of UI coding with relatively sane TDD.
That's much for any kind of UI framework.

Merci beaucoup,
Timo

--
Timo Rantalaiho          
Reaktor Innovations Oy    <URL: http://www.ri.fi/ >

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/wicket-user
Reply | Threaded
Open this post in threaded view
|

Re: Unit testing - updating a DropDownChoice with Ajax

Timo Rantalaiho
On Tue, 26 Jun 2007, Timo Rantalaiho wrote:
> In fact we just discovered a workaround to a DropDownChoice
> ajax issue with WicketTester in our app yesterday; I was
> going to file an issue or talk about it here, but didn't
> have time yet. It was something similar to the one just
> discussed here, so I'll try later today whether your fix
> also fixes that and report.

Yes, it's also fixed now!

Before we had to do

  wicket.setParameterForNextRequest(dropDownChoice.getPageRelativePath(), new Integer(index));

to get wicket.executeAjaxEvent(dropDownChoice, "onchange");
to use the model value of choice "index" within the choices.  

Now we can do the more normal (albeit in this case more
verbose)

  wicket.newFormTester(getForm().getPageRelativePath()).select("dropDownChoice", index);

Before, wicket.executeAjaxEvent() and FormTester seemed to
use separate requests (and thus the values set by FormTester
never ended up in the ajax request), but it seems that your
fix makes FormTester to work better with ajax.

Rock!
- Timo

--
Timo Rantalaiho          
Reaktor Innovations Oy    <URL: http://www.ri.fi/ >

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/wicket-user
glr
Reply | Threaded
Open this post in threaded view
|

Re: Unit testing - updating a DropDownChoice with Ajax

glr
In reply to this post by Jean-Baptiste Quenot-3
I tried with formTester.submit() and the test went OK with that.

Anyway, thanks for informing about the fix (WICKET-254), I will take it into use.

Regards,
Richard.

Jean-Baptiste Quenot-3 wrote
* Jean-Baptiste Quenot:
> * glr:
> >
> >         FormTester formTester = this.tester.newFormTester("articleEditorForm", false);
> >         formTester.select("rscTypesList", 0);
> >         this.tester.executeAjaxEvent("articleEditorForm:rscTypesList", "onchange");
>
> Can you please try with:
>
>         formTester.submit();
>
> instead of:
>
>         this.tester.executeAjaxEvent("articleEditorForm:rscTypesList", "onchange");
>
> To see if it's not the ajax behavior that causes problems?

Indeed  WicketTester was  overwriting field  values in  two places
when submitting via  Ajax.  I just fixed this, you  can safely use
executeAjaxEvent() now.

The related JIRA issue is:

Allow to set field values before submitting a form with Ajax in WicketTester
https://issues.apache.org/jira/browse/WICKET-254
--
     Jean-Baptiste Quenot
aka  John Banana   Qwerty
http://caraldi.com/jbq/

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
Wicket-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-user
Reply | Threaded
Open this post in threaded view
|

Re: Unit testing - updating a DropDownChoice with Ajax

SimonCC
Hi,

Ok, so I've got the first part of updating a DropDownChoice Ajax unit test working as described previously but when I try to select the second DropDownChoice and submit the form I'm getting the form returning that the value is missing. It is a required field so its supposed to object if it isn't set but I'm not sure why it's not picking up that I've set it.

Here is a bit of the code:

                FormTester ftester = tester.newFormTester("wizard:form");
               
                // select the phone maker
                ftester.select("view:phone.make", 0);
                tester.executeAjaxEvent(PHONEMAKE, "onchange");
               
                // select the phone model
                DropDownChoice phoneModels =  (DropDownChoice)tester.getLastRenderedPage().get(PHONEMODEL);

                // This passes
                assertEquals(20, phoneModels.getChoices().size());
               
                 // Here I'm selecting the second dropdown, as usual right?
                ftester.select("view:phone.model", 0);
               
                // submit the wizard to the next
                ftester.submit("buttons:next");

                tester.assertNoInfoMessage();

                // Here is where I'm expecting nothing but get the missing required field
                tester.assertNoErrorMessage();

I've tried various combinations of the above but to no avail.
Thanks Simon


glr wrote
I tried with formTester.submit() and the test went OK with that.

Anyway, thanks for informing about the fix (WICKET-254), I will take it into use.

Regards,
Richard.

Jean-Baptiste Quenot-3 wrote
* Jean-Baptiste Quenot:
> * glr:
> >
> >         FormTester formTester = this.tester.newFormTester("articleEditorForm", false);
> >         formTester.select("rscTypesList", 0);
> >         this.tester.executeAjaxEvent("articleEditorForm:rscTypesList", "onchange");
>
> Can you please try with:
>
>         formTester.submit();
>
> instead of:
>
>         this.tester.executeAjaxEvent("articleEditorForm:rscTypesList", "onchange");
>
> To see if it's not the ajax behavior that causes problems?

Indeed  WicketTester was  overwriting field  values in  two places
when submitting via  Ajax.  I just fixed this, you  can safely use
executeAjaxEvent() now.

The related JIRA issue is:

Allow to set field values before submitting a form with Ajax in WicketTester
https://issues.apache.org/jira/browse/WICKET-254
--
     Jean-Baptiste Quenot
aka  John Banana   Qwerty
http://caraldi.com/jbq/

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
Wicket-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-user