如何在 WicketTester 中测试 AjaxFormChoiceComponentUpdatingBehavior

发布于 2024-11-10 11:29:05 字数 158 浏览 1 评论 0原文

在我的 Wicket 应用程序中,我使用了一个带有“是”和“否”选项的单选按钮。如果我选择“否”,我应该显示一个下拉选项。我使用 AjaxFormChoiceComponentUpdatingBehavior 编写了代码。如何使用 WicketTester 对其进行单元测试?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

触ぅ动初心 2024-11-17 11:29:05

Wicket 1.5.x 的解决方案:

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

Solution for Wicket 1.5.x:

 AbstractAjaxBehavior behavior = (AbstractAjaxBehavior)WicketTesterHelper.
          findBehavior(getTester().getComponentFromLastRenderedPage("path:to:component"),
                        AjaxFormChoiceComponentUpdatingBehavior.class);
 getTester().executeBehavior(behavior);
冷弦 2024-11-17 11:29:05

首先选择您想要的单选按钮。

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

然后执行ajax行为:

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

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));
極樂鬼 2024-11-17 11:29:05

这是我的一段代码,它非常适合我使用选择框,但如果您更改行为类,也应该适用于单选按钮。所需的步骤是:

  • 将新值插入表单(使用 FormTester)
  • 查找行为
  • 在更改时执行行为

这是一个代码示例:

//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);

我错过了之前答案中如何更新表单值的部分。

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.

寄离 2024-11-17 11:29:05

如果单选按钮位于表单上,我认为您应该使用 FormTester 类:

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

对于 Ajax 表单提交测试的示例,您可以采取看一下:

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

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

樱花坊 2024-11-17 11:29:05

尝试这样的事情:
tester.executeAjaxEvent("form:myRadioButtonId", "onchange");

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

二智少女 2024-11-17 11:29:05

事实证明这有点痛苦,至少在 Wicket 1.4 中是这样(我还没有尝试过 1.5)。

通过网络搜索,我在 Mischa Dasberg 的博客中找到了提示。基本上,您不能使用 BaseWicketTester.executeAjaxEvent((String componentPath, String event) 方法,因为您使用的行为不是 AjaxEventBehavior 并且您可以'不要使用 BaseWicketTester.executeBehavior(final AbstractAjaxBehavior 行为),因为它会清除请求参数。Mischa

的解决方案是在父测试用例中实现他自己的executeBehavior 方法,该方法有效。适合他的情况,但不适合我的需要,因为它假设请求参数 id 与完整组件路径相同,

我通过在 扩展中实现我自己的 executeAjaxBehavior 来完成类似的操作 。 >WicketTester,但假设(在我的例子中也是如此)请求参数是组件路径的最后一个“:”分隔部分:

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);
}

他的解决方案和我的(基于他的)也假设行为是组件上的第一个(或唯一一个)

这有点笨拙,但类似的东西可能对您有用。

如果单独获取 id 和行为并将其作为参数传递可能会更好,当然,您可能会更好地找到实际上是 AjaxFormChoiceComponentUpdatingBehavior 的第一个行为,而不是轻松地假设它是第一个行为,但这只是一个开始。

这也与其他行为测试方法的 BaseWicketTester 类中的代码类似,可能值得仔细研究。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文