You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Neil Green <ne...@gmail.com> on 2007/12/01 10:14:25 UTC

WicketTester, FormTester and submit after validation failure

I'm using WicketTester with wicket 1.3.0-rc1 and I've run into some 
problems when I test form validation. I submit an invalid form and as 
expected I am returned to the same page with feedback errors. In the 
same test, I correct the error in the form and resubmit. Instead of the 
form being submitted and the result page rendered the same form page is 
rendered with the same validation error. The form is correctly submitted 
if there is no validation error on the first try.

    @Test
    public void testOne() {

        WicketTester tester = new WicketTester();
        tester.startPage(DemoPage.class);
        tester.assertRenderedPage(DemoPage.class);
        tester.assertComponent("demoForm", Form.class);
        tester.assertComponent("demoForm:name", RequiredTextField.class);

        // submit an invalid form
        FormTester form = tester.newFormTester("demoForm");
        form.submit();

        // validation should keep us on the same page
        tester.assertRenderedPage(DemoPage.class);

        // submit a valid form and we should be sent to the result page
        form = tester.newFormTester("demoForm");
        form.setValue("name", "ASDF");
        form.submit();
        // this is where the failure occurs
        // we remain on DemoPage
        // the error message is "Field 'name' is required."
        tester.assertRenderedPage(ResultPage.class);
    }

    @Test
    public void testTwo() {

        WicketTester tester = new WicketTester();
        tester.startPage(DemoPage.class);
        tester.assertRenderedPage(DemoPage.class);
        tester.assertComponent("demoForm", Form.class);
        tester.assertComponent("demoForm:name", RequiredTextField.class);

        // submit a valid form and we should be sent to the result page
        FormTester form = tester.newFormTester("demoForm");
        form.setValue("name", "ASDF");
        form.submit();
        // this works fine
        tester.assertRenderedPage(ResultPage.class);
    }

In the code above, testTwo passes successfully but testOne fails with 
"expected:<ResultPage> but was:<DemoPage>"

Am I doing something wrong in my use of WicketTester?

Many thanks
Neil

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: WicketTester, FormTester and submit after validation failure

Posted by Per Newgro <pe...@gmx.ch>.
PlinkPlink. The coin has reached the bottom. Then it makes sense. I have to 
check my testcases for this behavior.

Thanks
Per

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: WicketTester, FormTester and submit after validation failure

Posted by Timo Rantalaiho <Ti...@ri.fi>.
On Sat, 01 Dec 2007, Per Newgro wrote:
> but isn't this cheating? Hmm. I never saw the FeedbackMessageClearing.
> I always assumed that my test has to simulate the browser behavior. So i used 

Well, I find WicketTester tests to be so white-box that they
are pretty far from the actual browser anyway, and find them
better for single-component unit tests. Thus I would have 
fixed the original problem by putting the valid and invalid
cases to separate tests methods.

WicketTester tests are not purist unit tests as they use
the file system (to load the markup files) and parse markup
xml which is slowish, but this is not a practical problem 
before you have thousands of tests. Then it might get slow
(over 10 seconds) to run them.

Off-topic: if you use feedback messages, I heartily 
recommend reading _About Face_ by Alan Cooper and checking
out Wicket built-in ajax :)

Best wishes,
Timo

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

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: WicketTester, FormTester and submit after validation failure

Posted by Neil Green <ne...@gmail.com>.
Per Newgro wrote:
> but isn't this cheating? Hmm. I never saw the FeedbackMessageClearing.
> I always assumed that my test has to simulate the browser behavior. So i used 
> the request cycle. If you get an error prone page (with messages) the browser 
> will not reset the messages simply, or? If i'm not completely wrong you will 
> stay on the page if you get errors. So you have to assign valid values to 
> form components and send the page again. Then the appropriate attributes 
> (error messages and so on) have to be reseted by the framework. If you do it 
> manually it isn't testing as expected or?
>   
I don't think I'm cheating. I think it's some cheating causing the 
problem: it seems to be caused by the method

    public void setAutomaticallyClearFeedbackMessages(boolean 
automaticallyClearFeedbackMessages)
    {
        // FIXME This method is a quick fix for a unit testing problem that
        // should not exist
        this.automaticallyClearFeedbackMessages = 
automaticallyClearFeedbackMessages;
    }


in RequestCycle. I haven't examined the source in detail (yet), but I 
assume that is there so that the error messages are available in the 
tester for assertions, i.e. in a normal runtime the messages are cleared 
after the page is rendered but they are not cleared in testing.

Kind regards
Neil

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: WicketTester, FormTester and submit after validation failure

Posted by Per Newgro <pe...@gmx.ch>.
Hi Neil,

but isn't this cheating? Hmm. I never saw the FeedbackMessageClearing.
I always assumed that my test has to simulate the browser behavior. So i used 
the request cycle. If you get an error prone page (with messages) the browser 
will not reset the messages simply, or? If i'm not completely wrong you will 
stay on the page if you get errors. So you have to assign valid values to 
form components and send the page again. Then the appropriate attributes 
(error messages and so on) have to be reseted by the framework. If you do it 
manually it isn't testing as expected or?

Don't hit me if i'm wrong. Maybe i'm on the wooden way.

Cheers
Per

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: WicketTester, FormTester and submit after validation failure

Posted by Neil Green <ne...@gmail.com>.
Per Newgro wrote:
> you always get the same page, unless you send it again.
> I think you have to use a WebRequestCycle to achieve your goal.
> See this thread
> http://www.nabble.com/WicketTester---reload-page-after-model-changed-tf4691262.html#a13422228
>   

Thanks for your response. I think I've found the problem: 
session.cleanupFeedbackMessages() is not being called in the test 
context as RequestCycle#automaticallyClearFeedbackMessages is set to 
false. Perhaps the call to cleanupFeedbackMessages should be in 
FormTester#submit().

To fix the problem I added 
tester.getWicketSession().cleanupFeedbackMessages() to my test method 
after asserting on the messages.

Kind regards
Neil



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: WicketTester, FormTester and submit after validation failure

Posted by Per Newgro <pe...@gmx.ch>.
Hi Neil,

you always get the same page, unless you send it again.
I think you have to use a WebRequestCycle to achieve your goal.
See this thread
http://www.nabble.com/WicketTester---reload-page-after-model-changed-tf4691262.html#a13422228

Cheers
Per

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org