How to test AjaxFormChoiceComponentUpdatingBehavior in WicketTester

In my Wicket app, I used one radio button with the yes and no options. If I select No, I must display one drop-down list. I wrote code using AjaxFormChoiceComponentUpdatingBehavior . How to do it with WicketTester?

+7
source share
6 answers

Solution for Wicket 1.5.x:

  AbstractAjaxBehavior behavior = (AbstractAjaxBehavior)WicketTesterHelper. findBehavior(getTester().getComponentFromLastRenderedPage("path:to:component"), AjaxFormChoiceComponentUpdatingBehavior.class); getTester().executeBehavior(behavior); 
+4
source

First select the switch you want.

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

Then do the ajax behavior:

 tester.executeBehavior((AbstractAjaxBehavior)tester.getComponentFromLastRenderedPage("path to radio buttons").getBehaviors().get(0)); 
+2
source

Here is my code snippet that works great for me using the select box, but should also use for the switch if you change the Behavior class. Necessary steps:

  • Paste the new value into the form (use FormTester)
  • Find behavior
  • Change behavior

Here is a sample 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 steam how to update the form value in previous answers.

+1
source

If the switch is in the 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 Ajax form submission 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

0
source

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

0
source

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

Through a web search, I found tips on Mischa Dasberg's blog . Basically, you cannot use the BaseWicketTester.executeAjaxEvent((String componentPath, String event) method because the behavior you are using is not AjaxEventBehavior and you cannot use BaseWicketTester.executeBehavior(final AbstractAjaxBehavior behavior) because it destroys request parameters.

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

I did something similar by implementing my own executeAjaxBehavior in the WicketTester extension, but assuming (as it was in my case) that the request parameter is the last ":" section of the separated component

 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 decision and mine (based on it) also suggest that behavior is the first (or only) component.

It's a bit awkward, but something like this might work for you.

Perhaps it would be better if the identifiers and behavior were obtained separately and passed as parameters, and, of course, you could find the first behavior, which was actually AjaxFormChoiceComponentUpdatingBehavior instead of blithely assuming that this was the first behavior, but this is the beginning.

This is also similar code to what is inside the BaseWicketTester class for other behavior testing methods that might be worth a look.

0
source

All Articles