How to test AjaxFormChoiceComponentUpdatingBehavior in WicketTester
Asked Answered
M

6

8

In my Wicket application I used one radio button with "yes" and "no" options. If I select "No", I should display one dropdown choice. I wrote code using AjaxFormChoiceComponentUpdatingBehavior. How do I unittest this using WicketTester?

Macle answered 30/5, 2011 at 13:1 Comment(0)
S
5

Solution for Wicket 1.5.x:

 AbstractAjaxBehavior behavior = (AbstractAjaxBehavior)WicketTesterHelper.
          findBehavior(getTester().getComponentFromLastRenderedPage("path:to:component"),
                        AjaxFormChoiceComponentUpdatingBehavior.class);
 getTester().executeBehavior(behavior);
Sauncho answered 2/8, 2012 at 10:13 Comment(1)
Tried this with success in Wicket 6Quixotic
D
3

First select the radio button that you want.

form.select("path to radio button", 0/1)

Then execute ajax behaviour:

tester.executeBehavior((AbstractAjaxBehavior)tester.getComponentFromLastRenderedPage("path to radio buttons").getBehaviors().get(0));
Dropline answered 31/7, 2013 at 8:5 Comment(0)
S
1

Here is my piece of code which works perfectly for me with select box but should fiat as well for radio button if you change Behaviour class. Needed steps are:

  • Insert new value into form (use FormTester)
  • Find behaviour
  • Execute behaviour on change

Here is an example of code:

//simulate insert new value
FormTester formTester = tester.newFormTester(PANEL_ID + FORM); 
formTester.setValue("selectBox", "newValue");
//Find onchange behaviour
AjaxFormComponentUpdatingBehavior behavior = 
       (AjaxFormComponentUpdatingBehavior) WicketTesterHelper.findBehavior(
       tester.getComponentFromLastRenderedPage(PANEL_ID + FORM + ":" + "selectBox"), 
       ajaxFormComponentUpdatingBehavior.class);
//execute onchange
tester.executeBehavior(behavior);

I missed the par how to update form value in previous answers.

Syllogize answered 23/6, 2016 at 14:13 Comment(0)
S
0

If the radio button is on a form I think you should use the FormTester class:

http://wicket.apache.org/apidocs/1.4/org/apache/wicket/util/tester/FormTester.html

For an example of an Ajax form submit test you can take a look at:

http://www.java2s.com/Open-Source/Java-Document/J2EE/wicket-1.4/org/apache/wicket/ajax/form/AjaxFormSubmitTest.java.htm

Sunder answered 29/10, 2011 at 10:47 Comment(0)
T
0

Try something like this: tester.executeAjaxEvent("form:myRadioButtonId", "onchange");

Trexler answered 29/1, 2012 at 16:56 Comment(1)
Good instinct, but this unfortunately won't work as AjaxFormChoiceComponentUpdatingBehavior is not an AjaxEventBehavior.Incorruptible
I
0

This turns out to be somewhat painful, at least in Wicket 1.4 (I haven't tried with 1.5).

Via a web search, I found hints in Mischa Dasberg's blog. Basically, you can't use the BaseWicketTester.executeAjaxEvent((String componentPath, String event) method because the behavior you're using isn't an AjaxEventBehavior and you can't use the BaseWicketTester.executeBehavior(final AbstractAjaxBehavior behavior) because it wipes out the request parameters.

Mischa's solution was to implement his own executeBehavior method in a parent test case, which worked for his situation, but not for my need, as it assumed the request parameter id was the same as the full component path.

I've done something similar by implementing my own executeAjaxBehavior in an extension of WicketTester, but assuming (as is true in my case) that the request parameter is the last ":" separated section of the component path:

public void executeAjaxBehavior(String path, String value) {
    AbstractAjaxBehavior behavior = (AbstractAjaxBehavior) getComponentFromLastRenderedPage(path).getBehaviors().get(0);
    CharSequence url = behavior.getCallbackUrl(false);
    WebRequestCycle cycle = setupRequestAndResponse(true);
    getServletRequest().setRequestToRedirectString(url.toString());
    String[] ids = path.split(":");
    String id = ids[ids.length-1];
    getServletRequest().setParameter(id, value);
    processRequestCycle(cycle);
}

Both his solution and mine (based on his) also assume that the behavior is the first (or only) one on the component.

This is a bit clunky, but something like this may work for you.

It might be better if the ids and behavior were gotten separately and passed as parameters, and of course you might do well to find the first behavior that actually was an AjaxFormChoiceComponentUpdatingBehavior instead of blithely assuming it was the first behavior, but this is a start.

This is also similar code to what's inside the BaseWicketTester class for the other behavior testing methods, which might be worth looking through.

Incorruptible answered 6/3, 2012 at 20:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.