uction page public class MyPage extends WebPage { public MyPage() { add(new Label("myMessage", "Hello!")); add(new Link("toYourPage") { public void onClick() { setResponsePage(new YourPage("Hi!")); } }); } }
// test code private WicketTester tester; public void setUp() { tester = new WicketTester(); } public void testRenderMyPage() { // start and render the test page tester.startPage(MyPage.class); // assert rendered page class tester.assertRenderedPage(MyPage.class); // assert rendered label component tester.assertLabel("myMessage", "Hello!"); }
The above example is straight forward: start
MyPage.class
and assert
Label
it rendered. Next, we try to navigate through a
Link
:
// production page public class YourPage extends WebPage { public YourPage(String message) { add(new Label("yourMessage", message)); info("Wicket Rocks ;-)"); } } // test code public void testLinkToYourPage() { tester.startPage(MyPage.class); // click link and render tester.clickLink("toYourPage"); tester.assertRenderedPage(YourPage.class); tester.assertLabel("yourMessage", "Hi!"); }
tester.clickLink(path);
will simulate user click on the component (in this case, it's a
Link
) and render the response page
YourPage
. Ok, unit test of
MyPage
is completed. Now we test
YourPage
standalone:
// test code public void testRenderYourPage() { // provide page instance source for WicketTester tester.startPage(new TestPageSource() { public Page getTestPage() { return new YourPage("mock message"); } }); tester.assertRenderedPage(YourPage.class); tester.assertLabel("yourMessage", "mock message"); // assert feedback messages in INFO Level tester.assertInfoMessages(new String[] { "Wicket Rocks ;-)" }); }
Many methods require a 'path' parameter. E.g. the page relative path can be obtained via {@link Component#getPageRelativePath()}. Since each Component has an ID/name, any Component can also be referenced by its ID {@link MarkupContainer#get(String)}. And since MarkupContainer's and its subclasses are containers which allow to add Components (in sync with the markup hierarchy), you may not only access direct childs but also subchilds like get("myPanel:myForm:myNameField") separating each ID with a ':'. TODO General: Example usage of FormTester
@author Ingram Chen
@author Juergen Donnerstag
@author Frank Bille
@since 1.2.6