|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectwicket.Application
wicket.protocol.http.WebApplication
wicket.protocol.http.MockWebApplication
wicket.util.tester.WicketTester
public class WicketTester
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
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
| Field Summary |
|---|
| Fields inherited from class wicket.Application |
|---|
CONFIGURATION, CONTEXTPATH, DEPLOYMENT, DEVELOPMENT |
| Constructor Summary | |
|---|---|
WicketTester()
create WicketTester with null path |
|
WicketTester(java.lang.String path)
create a WicketTester to help unit testing. |
|
| Method Summary | |
|---|---|
void |
assertComponent(java.lang.String path,
java.lang.Class expectedComponentClass)
assert component class |
void |
assertComponentOnAjaxResponse(Component component)
Test that a component has been added to a AjaxRequestTarget, using AjaxRequestTarget.addComponent(Component). |
void |
assertContains(java.lang.String pattern)
assert the content of last rendered page contains(matches) regex pattern. |
void |
assertErrorMessages(java.lang.String[] expectedErrorMessages)
assert error feedback messages |
void |
assertInfoMessages(java.lang.String[] expectedInfoMessages)
assert info feedback message |
void |
assertInvisible(java.lang.String path)
assert component invisible. |
void |
assertLabel(java.lang.String path,
java.lang.String expectedLabelText)
assert the text of Label component. |
void |
assertListView(java.lang.String path,
java.util.List expectedList)
assert the model of ListView use expectedList |
void |
assertNoErrorMessage()
assert no error feedback messages |
void |
assertNoInfoMessage()
assert no info feedback messages |
void |
assertPageLink(java.lang.String path,
java.lang.Class expectedPageClass)
assert PageLink link to page class. |
void |
assertRenderedPage(java.lang.Class expectedReneredPageClass)
assert last rendered Page class |
void |
assertVisible(java.lang.String path)
assert component visible. |
void |
clickLink(java.lang.String path)
Click the Link in the last rendered Page. |
void |
clickLink(java.lang.String path,
boolean isAjax)
Click the Link in the last rendered Page. |
void |
debugComponentTrees()
dump component tree |
void |
debugComponentTrees(java.lang.String filter)
Dump the component trees to log. |
void |
dumpPage()
dump the source of last rendered page |
void |
executeAjaxEvent(Component component,
java.lang.String event)
Simulate that an AJAX event has been fired. |
void |
executeAjaxEvent(java.lang.String componentPath,
java.lang.String event)
Simulate that an AJAX event has been fired. |
Component |
getComponentFromLastRenderedPage(java.lang.String path)
Gets the component with the given path from last rendered page. |
java.util.List |
getMessages(int level)
get feedback messages |
TagTester |
getTagById(java.lang.String id)
Get a TagTester based on an dom id. |
TagTester |
getTagByWicketId(java.lang.String wicketId)
Get a TagTester based on a wicket:id. |
FormTester |
newFormTester(java.lang.String path)
create a FormTester for the form at path, and fill all child
FormComponents with blank String
initially. |
FormTester |
newFormTester(java.lang.String path,
boolean fillBlankString)
create a FormTester for the form at path. |
void |
setParameterForNextRequest(java.lang.String componentPath,
java.lang.Object value)
Sets a parameter for the component with the given path to be used with the next request. |
Page |
startPage(java.lang.Class pageClass)
Render a page from its default constructor. |
Page |
startPage(ITestPageSource testPageSource)
Render a page defined in TestPageSource. |
Page |
startPage(Page page)
Render the page |
Panel |
startPanel(java.lang.Class panelClass)
Render a panel from Panel(String id) constructor. |
Panel |
startPanel(TestPanelSource testPanelSource)
Render a panel defined in TestPanelSource. |
void |
submitForm(java.lang.String path)
submit the Form in the last rendered Page. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public WicketTester()
WicketTester(String)public WicketTester(java.lang.String path)
path - The absolute path on disk to the web application contents
(e.g. war root) - may be nullMockWebApplication.MockWebApplication(String)| Method Detail |
|---|
public final Page startPage(ITestPageSource testPageSource)
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);
}
});
testPageSource - a page factory that creating test page instance
public final Page startPage(Page page)
page -
public final Page startPage(java.lang.Class pageClass)
pageClass - a test page class with default constructor
public final Panel startPanel(TestPanelSource testPanelSource)
TestPanelSource. The usage is
similar with 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
- Returns:
- Panel rendered panel
public final Panel startPanel(java.lang.Class panelClass)
Panel(String id) constructor.
panelClass - a test panel class with Panel(String id)
constructor
public Component getComponentFromLastRenderedPage(java.lang.String path)
path - Path to component
MarkupContainer.get(String)
public void assertLabel(java.lang.String path,
java.lang.String expectedLabelText)
Label component.
path - path to Label componentexpectedLabelText - expected label text
public void assertPageLink(java.lang.String path,
java.lang.Class expectedPageClass)
PageLink link to page class.
path - path to PageLink componentexpectedPageClass - expected page class to link
public void assertComponent(java.lang.String path,
java.lang.Class expectedComponentClass)
path - path to componentexpectedComponentClass - expected component classpublic void assertVisible(java.lang.String path)
path - path to componentpublic void assertInvisible(java.lang.String path)
path - path to componentpublic void assertContains(java.lang.String pattern)
pattern - reqex pattern to match
public void assertListView(java.lang.String path,
java.util.List expectedList)
ListView use expectedList
path - path to ListView componentexpectedList - expected list in the model of ListViewpublic FormTester newFormTester(java.lang.String path)
FormTester for the form at path, and fill all child
FormComponents with blank String
initially.
path - path to Form component
newFormTester(String, boolean)
public FormTester newFormTester(java.lang.String path,
boolean fillBlankString)
FormTester for the form at path.
path - path to Form componentfillBlankString - specify whether fill all child
FormComponents with
blankString initially.
FormTesterpublic void clickLink(java.lang.String path)
Link in the last rendered Page.
Simulate that AJAX is enabled.
path - Click the Link in the last rendered Page.clickLink(String, boolean)
public void clickLink(java.lang.String path,
boolean isAjax)
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.
path - path to Link componentisAjax - 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 be
invoked with null as the AjaxRequestTarget parameter.public void submitForm(java.lang.String path)
Form in the last rendered Page.
path - path to Form component
public void setParameterForNextRequest(java.lang.String componentPath,
java.lang.Object value)
componentPath - path of the componentvalue - the parameter value to setpublic void assertRenderedPage(java.lang.Class expectedReneredPageClass)
expectedReneredPageClass - expected class of last renered pagepublic void assertNoErrorMessage()
public void assertNoInfoMessage()
public void assertErrorMessages(java.lang.String[] expectedErrorMessages)
expectedErrorMessages - expected error messagespublic void assertInfoMessages(java.lang.String[] expectedInfoMessages)
expectedInfoMessages - expected info messagespublic java.util.List getMessages(int level)
level - level of feedback message, ex.
FeedbackMessage.DEBUG or FeedbackMessage.INFO.. etc
FeedbackMessagepublic void dumpPage()
public void debugComponentTrees()
public void debugComponentTrees(java.lang.String filter)
filter - Show only the components, which path contains the
filterstring.public void assertComponentOnAjaxResponse(Component component)
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.
component - The component to test whether it's on the response.
public void executeAjaxEvent(java.lang.String componentPath,
java.lang.String event)
componentPath - The component path.event - The event which we simulate is fired. If the event is null,
the test will fail.executeAjaxEvent(Component, String)
public void executeAjaxEvent(Component component,
java.lang.String event)
...
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.
component - The component which has the AjaxEventBehavior we wan't to
test. If the component is null, the test will fail.event - The event which we simulate is fired. If the event is null,
the test will fail.public TagTester getTagByWicketId(java.lang.String wicketId)
wicketId - The wicket:id to search for.
public TagTester getTagById(java.lang.String id)
id - The dom id to search for.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||