| wicket.protocol.http.MockWebApplication wicket.util.tester.WicketTester
All known Subclasses: wicket.util.tester.apps_1.MyMockApplication, wicket.resource.DummyApplication, wicket.properties.MyApplication, wicket.properties.MyTesterApplication,
WicketTester | public class WicketTester extends MockWebApplication (Code) | | A helper to ease unit testing of Wicket applications without the need for a
servlet container. To start a test, we can use either startPage() or
startPanel():
// production 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!");
}
Above example is straight forward: start MyPage.class and assert Label it
rendered. Next, we try to navigate through 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 ;-)" });
}
Instead of tester.startPage(pageClass) , we define a
wicket.util.tester.ITestPageSource to provide testing page instance
for WicketTester. This is necessary because YourPage uses a
custom constructor, which is very common for transfering model data, can not
be instansiated by reflection. Finally, we use
assertInfoMessages to assert there is a feedback message
"Wicket Rocks ;-)" in INFO level.
TODO General: Example usage of FormTester
author: Ingram Chen author: Juergen Donnerstag author: Frank Bille |
Constructor Summary | |
public | WicketTester() | public | WicketTester(String path) create a WicketTester to help unit testing.
Parameters: path - The absolute path on disk to the web application contents(e.g. |
Method Summary | |
public void | assertComponent(String path, Class expectedComponentClass) | public void | assertComponentOnAjaxResponse(Component component) Test that a component has been added to a AjaxRequestTarget, using
AjaxRequestTarget.addComponent(Component) . | public void | assertContains(String pattern) assert the content of last rendered page contains(matches) regex pattern. | public void | assertErrorMessages(String[] expectedErrorMessages) | public void | assertInfoMessages(String[] expectedInfoMessages) | public void | assertInvisible(String path) assert component invisible. | public void | assertLabel(String path, String expectedLabelText) assert the text of Label component. | public void | assertListView(String path, List expectedList) | public void | assertNoErrorMessage() | public void | assertNoInfoMessage() | public void | assertPageLink(String path, Class expectedPageClass) assert PageLink link to page class. | public void | assertRenderedPage(Class expectedReneredPageClass) | public void | assertVisible(String path) assert component visible. | public void | clickLink(String path) Click the
Link in the last rendered Page. | public void | clickLink(String path, boolean isAjax) Click the
Link in the last rendered Page.
This method also works for
AjaxLink ,
AjaxFallbackLink and
AjaxSubmitLink .
On AjaxLinks and AjaxFallbackLinks the onClick method is invoked with a
valid AjaxRequestTarget. | public void | debugComponentTrees() | public void | debugComponentTrees(String filter) Dump the component trees to log. | public void | dumpPage() | public void | executeAjaxEvent(String componentPath, String event) Simulate that an AJAX event has been fired.
See Also: WicketTester.executeAjaxEvent(Component,String) since: 1.2.3 Parameters: componentPath - The component path. Parameters: event - The event which we simulate is fired. | public void | executeAjaxEvent(Component component, String event) Simulate that an AJAX event has been fired. | public Component | getComponentFromLastRenderedPage(String path) Gets the component with the given path from last rendered page. | public List | getMessages(int level) get feedback messages
Parameters: level - level of feedback message, ex.FeedbackMessage.DEBUG or FeedbackMessage.INFO.. | public TagTester | getTagById(String id) Get a TagTester based on an dom id. | public TagTester | getTagByWicketId(String wicketId) Get a TagTester based on a wicket:id. | public FormTester | newFormTester(String path) create a
FormTester for the form at path, and fill all child
wicket.markup.html.form.FormComponent s with blank String
initially. | public FormTester | newFormTester(String path, boolean fillBlankString) create a
FormTester for the form at path.
Parameters: path - path to Form component Parameters: fillBlankString - specify whether fill all childwicket.markup.html.form.FormComponents withblankString initially. | public void | setParameterForNextRequest(String componentPath, Object value) Sets a parameter for the component with the given path to be used with
the next request. | final public Page | startPage(ITestPageSource testPageSource) Render a page defined in TestPageSource . | final public Page | startPage(Page page) | final public Page | startPage(Class pageClass) Render a page from its default constructor. | final public Panel | startPanel(TestPanelSource testPanelSource) Render a panel defined in TestPanelSource . | final public Panel | startPanel(Class panelClass) Render a panel from Panel(String id) constructor. | public void | submitForm(String path) submit the Form in the last rendered Page. |
assertComponent | public void assertComponent(String path, Class expectedComponentClass)(Code) | | assert component class
Parameters: path - path to component Parameters: expectedComponentClass - expected component class |
assertComponentOnAjaxResponse | public void assertComponentOnAjaxResponse(Component component)(Code) | | Test that a component has been added to a AjaxRequestTarget, using
AjaxRequestTarget.addComponent(Component) . This method actually
tests that a component is on the AJAX response sent back to the client.
PLEASE NOTE! This method doesn't actually insert the component in the
client DOM tree, using javascript. But it shouldn't be needed because you
have to trust that the Wicket Ajax Javascript just works.
Parameters: component - The component to test whether it's on the response. |
assertContains | public void assertContains(String pattern)(Code) | | assert the content of last rendered page contains(matches) regex pattern.
Parameters: pattern - reqex pattern to match |
assertErrorMessages | public void assertErrorMessages(String[] expectedErrorMessages)(Code) | | assert error feedback messages
Parameters: expectedErrorMessages - expected error messages |
assertInfoMessages | public void assertInfoMessages(String[] expectedInfoMessages)(Code) | | assert info feedback message
Parameters: expectedInfoMessages - expected info messages |
assertInvisible | public void assertInvisible(String path)(Code) | | assert component invisible.
Parameters: path - path to component |
assertLabel | public void assertLabel(String path, String expectedLabelText)(Code) | | assert the text of Label component.
Parameters: path - path to Label component Parameters: expectedLabelText - expected label text |
assertListView | public void assertListView(String path, List expectedList)(Code) | | assert the model of
ListView use expectedList
Parameters: path - path to ListView component Parameters: expectedList - expected list in the model of ListView |
assertNoErrorMessage | public void assertNoErrorMessage()(Code) | | assert no error feedback messages
|
assertNoInfoMessage | public void assertNoInfoMessage()(Code) | | assert no info feedback messages
|
assertPageLink | public void assertPageLink(String path, Class expectedPageClass)(Code) | | assert PageLink link to page class.
Parameters: path - path to PageLink component Parameters: expectedPageClass - expected page class to link |
assertRenderedPage | public void assertRenderedPage(Class expectedReneredPageClass)(Code) | | assert last rendered Page class
Parameters: expectedReneredPageClass - expected class of last renered page |
assertVisible | public void assertVisible(String path)(Code) | | assert component visible.
Parameters: path - path to component |
clickLink | public void clickLink(String path, boolean isAjax)(Code) | | Click the
Link in the last rendered Page.
This method also works for
AjaxLink ,
AjaxFallbackLink and
AjaxSubmitLink .
On AjaxLinks and AjaxFallbackLinks the onClick method is invoked with a
valid AjaxRequestTarget. In that way you can test the flow of your
application when using AJAX.
When clicking an AjaxSubmitLink the form, which the AjaxSubmitLink is
attached to is first submitted, and then the onSubmit method on
AjaxSubmitLink is invoked. If you have changed some values in the form
during your test, these will also be submitted. This should not be used
as a replacement for the
FormTester to test your forms. It should
be used to test that the code in your onSubmit method in AjaxSubmitLink
actually works.
This method is also able to simulate that AJAX (javascript) is disabled
on the client. This is done by setting the isAjax parameter to false. If
you have an AjaxFallbackLink you can then check that it doesn't fail when
invoked as a normal link.
Parameters: path - path to Link component Parameters: isAjax - Whether to simulate that AJAX (javascript) is enabled or not.If it's false then AjaxLink and AjaxSubmitLink will fail,since it wouldn't work in real life. AjaxFallbackLink will beinvoked with null as the AjaxRequestTarget parameter. |
debugComponentTrees | public void debugComponentTrees()(Code) | | dump component tree
|
debugComponentTrees | public void debugComponentTrees(String filter)(Code) | | Dump the component trees to log.
Parameters: filter - Show only the components, which path contains thefilterstring. |
dumpPage | public void dumpPage()(Code) | | dump the source of last rendered page
|
executeAjaxEvent | public void executeAjaxEvent(String componentPath, String event)(Code) | | Simulate that an AJAX event has been fired.
See Also: WicketTester.executeAjaxEvent(Component,String) since: 1.2.3 Parameters: componentPath - The component path. Parameters: event - The event which we simulate is fired. If the event is null,the test will fail. |
executeAjaxEvent | public void executeAjaxEvent(Component component, String event)(Code) | | Simulate that an AJAX event has been fired. You add an AJAX event to a
component by using:
...
component.add(new AjaxEventBehavior("ondblclick") {
public void onEvent(AjaxRequestTarget) {
// Do something.
}
});
...
You can then test that the code inside onEvent actually does what it's
supposed to, using the WicketTester:
...
tester.executeAjaxEvent(component, "ondblclick");
// Test that the code inside onEvent is correct.
...
This also works with AjaxFormSubmitBehavior, where it will "submit" the
form before executing the command.
PLEASE NOTE! This method doesn't actually insert the component in the
client DOM tree, using javascript.
Parameters: component - The component which has the AjaxEventBehavior we wan't totest. If the component is null, the test will fail. Parameters: event - The event which we simulate is fired. If the event is null,the test will fail. |
getComponentFromLastRenderedPage | public Component getComponentFromLastRenderedPage(String path)(Code) | | Gets the component with the given path from last rendered page. This
method fails in case the component couldn't be found, and it will return
null if the component was found, but is not visible.
Parameters: path - Path to component The component at the path See Also: wicket.MarkupContainer.get(String) |
getMessages | public List getMessages(int level)(Code) | | get feedback messages
Parameters: level - level of feedback message, ex.FeedbackMessage.DEBUG or FeedbackMessage.INFO.. etc List list of messages (in String) See Also: FeedbackMessage |
getTagById | public TagTester getTagById(String id)(Code) | | Get a TagTester based on an dom id. If more components exists with the
same id in the markup only the first one is returned.
Parameters: id - The dom id to search for. The TagTester for the tag which has the given dom id. |
getTagByWicketId | public TagTester getTagByWicketId(String wicketId)(Code) | | Get a TagTester based on a wicket:id. If more components exists with the
same wicket:id in the markup only the first one is returned.
Parameters: wicketId - The wicket:id to search for. The TagTester for the tag which has the given wicket:id. |
setParameterForNextRequest | public void setParameterForNextRequest(String componentPath, Object value)(Code) | | Sets a parameter for the component with the given path to be used with
the next request. NOTE: this method only works when a page was rendered
first.
Parameters: componentPath - path of the component Parameters: value - the parameter value to set |
startPage | final public Page startPage(ITestPageSource testPageSource)(Code) | | Render a page defined in TestPageSource . This usually
used when a page does not have default consturctor. For example, a
ViewBook page requires a Book instance:
tester.startPage(new TestPageSource()
{
public Page getTestPage()
{
Book mockBook = new Book("myBookName");
return new ViewBook(mockBook);
}
});
Parameters: testPageSource - a page factory that creating test page instance Page rendered page |
startPage | final public Page startPage(Page page)(Code) | | Render the page
Parameters: page - The page rendered |
startPage | final public Page startPage(Class pageClass)(Code) | | Render a page from its default constructor.
Parameters: pageClass - a test page class with default constructor Page Rendered Page |
startPanel | final public Panel startPanel(TestPanelSource testPanelSource)(Code) | | Render a panel defined in TestPanelSource . The usage is
similar with
WicketTester.startPage(ITestPageSource) . Please note that
testing panel must use supplied panelId as component id.
tester.startPanel(new TestPanelSource()
{
public Panel getTestPanel(String panelId)
{
MyData mockMyData = new MyData();
return new MyPanel(panelId, mockMyData);
}
});
Parameters: testPanelSource - a panel factory that creating test panel instance Panel rendered panel |
startPanel | final public Panel startPanel(Class panelClass)(Code) | | Render a panel from Panel(String id) constructor.
Parameters: panelClass - a test panel class with Panel(String id) constructor Panel rendered panel |
submitForm | public void submitForm(String path)(Code) | | submit the Form in the last rendered Page.
Parameters: path - path to Form component |
|
|