[英]Test Ajax-submit a form in Apache Wicket
Suppose I have a form as shown below, which is submitted via Ajax: 假设我有一个如下所示的表单,该表单是通过Ajax提交的:
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
import org.apache.wicket.markup.html.WebPage;
public class FormPage extends WebPage {
public FormPage(final PageParameters parameters) {
Form form = new Form("myForm");
// Ajax submit
form.add(new AjaxFormSubmitBehavior("onsubmit") {
@Override
protected void onSubmit(AjaxRequestTarget target) {
super.onSubmit(target); // Breakpoint on this line
}
});
add(form);
}
}
Now I need to test the form, and set a breakpoint in the onSubmit method. 现在,我需要测试表单,并在onSubmit方法中设置一个断点。 Here is the test code: 这是测试代码:
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.junit.Before;
import org.junit.Test;
public class TestFormPage {
private WicketTester tester;
@Before
public void setUp() {
tester = new WicketTester(new WicketApplication());
}
@Test
public void homepageRendersSuccessfully() {
tester.startPage(FormPage.class);
tester.assertRenderedPage(FormPage.class);
FormTester formTester = tester.newFormTester("myForm");
formTester.submit();
}
}
But the problem is, when I run the unit test, the line with breakpoint set is never reached. 但是问题是,当我运行单元测试时,从未达到设置断点的行。 Wonder what is going wrong? 想知道出了什么问题?
In the test, the following line would submit a normal form, but would not fire the Ajax submit event. 在测试中,以下行将提交常规表单,但不会触发Ajax提交事件。
formTester.submit();
In order to fire the Ajax submit event, WicketTester#executeAjaxEvent should be used instead, as shown below: 为了触发Ajax提交事件,应该改用WicketTester#executeAjaxEvent,如下所示:
import org.apache.wicket.util.tester.WicketTester;
import org.junit.Before;
import org.junit.Test;
public class TestFormPage {
private WicketTester tester;
@Before
public void setUp() {
tester = new WicketTester(new WicketApplication());
}
@Test
public void homepageRendersSuccessfully() {
tester.startPage(FormPage.class);
tester.assertRenderedPage(FormPage.class);
tester.executeAjaxEvent("myForm", "submit");
}
}
Ref: 参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.