如何测试Apache Wicket单选按钮的AjaxEventBehavior("onClick")?

sak*_*han 4 testing ajax wicket

我正在使用apache wicket,我遇到了关于为单选按钮测试AjaxEventBehavior的麻烦.实际上我想测试"onClick"事件就像在我的情况下选择/单击RadioGroup中的单选按钮呈现特定页面一样.

代码段:

RadioGroup<Boolean> selectPageRadioGroup =
        new RadioGroup<Boolean>("selectPageRadioGroup", new Model<Boolean>(Boolean.TRUE));
selectPageRadioGroup.setDefaultModel(new Model<Boolean>(Boolean.TRUE));
final Radio<Boolean> radioButton1 =
        new Radio<Boolean>("radioButton1", new Model<Boolean>(Boolean.FALSE));
radioButton1.add(new AjaxEventBehavior("onclick") {
    @Override
    protected void onEvent(AjaxRequestTarget target) {
        setResponsePage(MyWebPage.class);
    }
});
selectPageRadioGroup.add(radioButton1);
Run Code Online (Sandbox Code Playgroud)

Don*_*oby 8

假设你已经完成了

WicketTester tester = new WicketTester();
tester.startPage(PageContainingRadioButton.class);
Run Code Online (Sandbox Code Playgroud)

或者类似的startPanel(Wicket 1.4)或startComponent(Wicket 1.5),这样你的测试就会在一个已知路径上呈现一个包含按钮的页面,你应该能够让WicketTester通过类似的方式来模拟ajax行为.

tester.executeAjaxEvent("blabla:form:selectPageRadioGroup:radioButton1", "onclick");
Run Code Online (Sandbox Code Playgroud)

(当然,你需要调整这条路径.)

然后检查它是否做对了

tester.assertRenderedPage(MyWebPage.class);  
Run Code Online (Sandbox Code Playgroud)