public class WicketTester extends BaseWicketTester
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!");
}
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 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
Component.getPageRelativePath(). Since each Component has an ID/name, any Component can
also be referenced by its ID 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 ':'.
Cookie handling:
There are some expectations about wicket tester cookie handling which should match as best as
it can be with a real client server request response cycle:
- all valid cookies set before a request is made (tester.getRequest().addCookie()) should
appear in the page request
- all cookies set in the response should appear in the last response (tester.getLastResponse())
after the request is made (expired cookies and others)
- all cookies set in the response should appear even after a redirect response is made
until the final response (tester.getLastResponse()) is written to the client (wicket tester)
- all valid cookies (maxAge!=0) from the last response should be added to
the next request cookies (tester.getRequest().getCookies())
TODO General: Example usage of FormTesterBaseWicketTester.StartComponentInPage, BaseWicketTester.TestFilterConfig| Constructor and Description |
|---|
WicketTester()
Creates a
WicketTester and automatically creates a WebApplication,
but the tester will have no home page. |
WicketTester(Class<? extends Page> homePage)
Creates a
WicketTester and automatically creates a WebApplication. |
WicketTester(WebApplication application)
Creates a
WicketTester. |
WicketTester(WebApplication application,
boolean init)
Creates a
WicketTester to help unit testing. |
WicketTester(WebApplication application,
javax.servlet.ServletContext servletCtx)
Creates a
WicketTester to help unit testing. |
WicketTester(WebApplication application,
javax.servlet.ServletContext servletCtx,
boolean init)
Creates a
WicketTester to help unit testing. |
WicketTester(WebApplication application,
String path)
Creates a
WicketTester to help unit testing. |
| Modifier and Type | Method and Description |
|---|---|
void |
assertAjaxLocation()
Asserts that the Ajax location header is present.
|
void |
assertBookmarkablePageLink(String id,
Class<? extends WebPage> pageClass,
PageParameters parameters)
Asserts that that the BookmarkablePageLink> identified by "id" points to the page as
expected - including parameters.
|
void |
assertComponent(String path,
Class<? extends Component> expectedComponentClass)
Asserts a
Component class. |
void |
assertComponentFeedbackMessage(Component component,
String key,
IModel<?> model,
IFeedbackMessageFilter filter)
Asserts that there is a feedback message provided by a given component
|
void |
assertComponentOnAjaxResponse(Component component)
Tests that a
Component has been added to a AjaxRequestTarget, using
IPartialPageRequestHandler.add(Component...). |
void |
assertComponentOnAjaxResponse(String componentPath)
Tests that a
Component has been added to a AjaxRequestTarget, using
IPartialPageRequestHandler.add(Component...). |
void |
assertContains(String pattern)
Asserts the content of last rendered page contains (matches) a given regex pattern.
|
void |
assertContainsNot(String pattern)
The opposite of
assertContains(String). |
void |
assertDisabled(String path)
assert component is enabled.
|
void |
assertEnabled(String path)
assert component is enabled.
|
void |
assertErrorMessages(Serializable... expectedErrorMessages)
Asserts error-level feedback messages.
|
void |
assertFeedback(String path,
Serializable... messages)
Assert that a particular feedback panel is rendering certain messages.
|
void |
assertFeedbackMessages(IFeedbackMessageFilter filter,
Serializable... expectedMessages)
Assert there are feedback messages accepted by the provided filter.
|
void |
assertInfoMessages(Serializable... expectedInfoMessages)
Assert info-level feedback messages.
|
void |
assertInvisible(String path)
Asserts that a
Component is invisible. |
void |
assertLabel(String path,
String expectedLabelText)
Asserts the text of a
Label Component. |
void |
assertListView(String path,
List<?> expectedList)
Asserts the model of a
ListView. |
void |
assertMarkupLocale(Component component,
Locale expectedLocale)
Asserts that a component's markup has loaded with the given locale
|
void |
assertMarkupStyle(Component component,
String expectedStyle)
Asserts that a component's markup has loaded with the given style.
|
void |
assertMarkupVariation(Component component,
String expectedVariation)
Asserts that a component's markup has loaded with the given variation
|
void |
assertModelValue(String path,
Object expectedValue)
Asserts the model value of a component.
|
void |
assertNoErrorMessage()
Asserts no error-level feedback messages.
|
void |
assertNoFeedbackMessage(int level)
Asserts there are no feedback messages with a certain level.
|
void |
assertNoInfoMessage()
Asserts no info-level feedback messages.
|
void |
assertNotRequired(String path)
assert form component is required.
|
void |
assertRedirectUrl(String expectedRedirectUrl)
Assert that the last request redirected to the given Url.
|
void |
assertRenderedPage(Class<? extends Page> expectedRenderedPageClass)
Asserts a last-rendered
Page class. |
void |
assertRequired(String path)
assert form component is required.
|
void |
assertResultPage(Class<?> clazz,
String filename)
Asserts last-rendered
Page against an expected HTML document. |
void |
assertResultPage(String expectedDocument)
Asserts last-rendered
Page against an expected HTML document as a
String |
void |
assertUsability(Component component)
Checks whether a component is visible and/or enabled before usage
|
void |
assertVisible(String path)
Asserts that a
Component is visible. |
void |
clickLink(Component link) |
void |
executeBehavior(Class<?> testClass,
AbstractAjaxBehavior behavior,
String filename) |
void |
executeListener(Class<?> testClass,
Component component,
String filename) |
<T extends Page> |
executeTest(Class<?> testClass,
Class<T> pageClass,
PageParameters parameters,
String filename)
Use
-Dwicket.replace.expected.results=true to automatically replace the expected
output file. |
<T extends Page> |
executeTest(Class<?> testClass,
Class<T> pageClass,
String filename)
Use
-Dwicket.replace.expected.results=true to automatically replace the expected
output file. |
void |
executeTest(Class<?> testClass,
Component component,
String filename)
Use
-Dwicket.replace.expected.results=true to automatically replace the expected
output file. |
void |
executeTest(Class<?> testClass,
Page page,
String filename)
Use
-Dwicket.replace.expected.results=true to automatically replace the expected
output file. |
static String |
getBasedir()
Returns the current Maven build directory taken from the basedir system property, or
null if not set
|
addRequestHeader, applyRequest, checkUsability, cleanupFeedbackMessages, cleanupFeedbackMessages, clearFeedbackMessages, clickLink, clickLink, createOriginHeader, createPage, createPageMarkup, debugComponentTrees, debugComponentTrees, destroy, dumpPage, executeAjaxEvent, executeAjaxEvent, executeAjaxUrl, executeAllTimerBehaviors, executeBehavior, executeListener, executeListener, executeUrl, getApplication, getComponentFromLastRenderedPage, getComponentFromLastRenderedPage, getContentDispositionFromResponseHeader, getContentLengthFromResponseHeader, getContentTypeFromResponseHeader, getFeedbackMessages, getHttpSession, getLastModifiedFromResponseHeader, getLastRenderedPage, getLastRequest, getLastResponse, getLastResponseAsString, getMessages, getPreviousRequests, getPreviousResponses, getRequest, getRequestCycle, getResourcePollFrequency, getResponse, getServletContext, getSession, getTagById, getTagByWicketId, getTagsByWicketId, getWicketAjaxBaseUrlEncodedInLastResponse, hasLabel, hasNoErrorMessage, hasNoFeedbackMessage, hasNoInfoMessage, ifContains, ifContainsNot, invokeListener, isComponent, isComponentOnAjaxResponse, isDisabled, isEnabled, isEqual, isExposeExceptions, isFollowRedirects, isInvisible, isNotRequired, isNotRequired, isRenderedPage, isRequired, isRequired, isResultPage, isUseRequestUrlAsBase, isVisible, newFormTester, newFormTester, newServletWebResponse, newTestPageManagerProvider, processRequest, processRequest, processRequest, processRequest, processRequest, setExposeExceptions, setFollowRedirects, setLastResponse, setRequest, setUseRequestUrlAsBase, startComponentInPage, startComponentInPage, startComponentInPage, startComponentInPage, startPage, startPage, startPage, startPage, startResource, startResourceReference, startResourceReference, submitForm, submitForm, urlFor, urlFor, urlForpublic WicketTester()
WicketTester and automatically creates a WebApplication,
but the tester will have no home page.public WicketTester(Class<? extends Page> homePage)
WicketTester and automatically creates a WebApplication.homePage - a home page Classpublic WicketTester(WebApplication application)
WicketTester.application - a WicketTester WebApplication objectpublic WicketTester(WebApplication application, String path)
WicketTester to help unit testing.application - a WicketTester WebApplication objectpath - the absolute path on disk to the web application's contents (e.g. war root) - may
be nullMockApplication.MockApplication()public WicketTester(WebApplication application, javax.servlet.ServletContext servletCtx)
WicketTester to help unit testing.application - a WicketTester WebApplication objectservletCtx - the servlet context used as backendpublic WicketTester(WebApplication application, boolean init)
WicketTester to help unit testing.application - a WicketTester WebApplication objectinit - force the application to be initialized (default = true)public WicketTester(WebApplication application, javax.servlet.ServletContext servletCtx, boolean init)
WicketTester to help unit testing.application - a WicketTester WebApplication objectservletCtx - the servlet context used as backendinit - force the application to be initialized (default = true)public void assertAjaxLocation()
public void assertComponent(String path, Class<? extends Component> expectedComponentClass)
Component class.path - path to ComponentexpectedComponentClass - expected Component classpublic void assertComponentOnAjaxResponse(Component component)
Component has been added to a AjaxRequestTarget, using
IPartialPageRequestHandler.add(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 just have to trust that Wicket
Ajax JavaScript works.
component - a Component to be testedpublic void assertComponentOnAjaxResponse(String componentPath)
Component has been added to a AjaxRequestTarget, using
IPartialPageRequestHandler.add(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 just have to trust that Wicket
Ajax JavaScript works.
componentPath - a Component path to testpublic void assertContains(String pattern)
pattern - a regex pattern to matchpublic void assertContainsNot(String pattern)
assertContains(String).pattern - patternpublic void assertMarkupVariation(Component component, String expectedVariation)
component - The component which markup to checkexpectedVariation - The expected variation of the component's markuppublic void assertMarkupStyle(Component component, String expectedStyle)
component - The component which markup to checkexpectedStyle - The expected style of the component's markup.
For example: green in MyPanel_green.htmlpublic void assertMarkupLocale(Component component, Locale expectedLocale)
component - The component which markup to checkexpectedLocale - The expected locale of the component's markuppublic void assertErrorMessages(Serializable... expectedErrorMessages)
expectedErrorMessages - expected error messagespublic void assertInfoMessages(Serializable... expectedInfoMessages)
expectedInfoMessages - expected info messagespublic void assertFeedbackMessages(IFeedbackMessageFilter filter, Serializable... expectedMessages)
filter - the filter that will decide which messages to checkexpectedMessages - expected feedback messagespublic void assertComponentFeedbackMessage(Component component, String key, IModel<?> model, IFeedbackMessageFilter filter)
component - the component that provided the expected feedback message. Optional.key - the resource key for the feedback message. Mandatory.model - the model used for interpolating the feedback message. Optional.filter - the filter that decides in which messages to look in. E.g. with a specific
level, rendered or not, etc.public void assertFeedback(String path, Serializable... messages)
FeedbackPanel, so it will
not work with custom IFeedback implementations unless you are subclassing
FeedbackPanelpath - path to the feedback panelmessages - messages expected to be renderedpublic void assertInvisible(String path)
Component is invisible.path - path to Componentpublic void assertLabel(String path, String expectedLabelText)
Label Component.path - path to Label ComponentexpectedLabelText - expected text of the Labelpublic void assertModelValue(String path, Object expectedValue)
path - path to the component on the pageexpectedValue - expected value of the component's modelpublic void assertListView(String path, List<?> expectedList)
ListView.assertListView in class BaseWicketTesterpath - path to a ListView ComponentexpectedList - expected List in the model of the given ListViewpublic void assertNoErrorMessage()
public void assertNoInfoMessage()
public void assertNoFeedbackMessage(int level)
level - the level to check forpublic void assertRenderedPage(Class<? extends Page> expectedRenderedPageClass)
Page class.expectedRenderedPageClass - expected class of last rendered Pagepublic void assertResultPage(Class<?> clazz, String filename) throws Exception
Page against an expected HTML document.
Use -Dwicket.replace.expected.results=true to automatically replace the expected
output file.
assertResultPage in class BaseWicketTesterclazz - Class used to load the file (relative to clazz package)filename - expected output filename StringExceptionpublic void assertResultPage(String expectedDocument) throws Exception
Page against an expected HTML document as a
StringexpectedDocument - expected output StringExceptionpublic void assertVisible(String path)
Component is visible.path - path to a Componentpublic void assertEnabled(String path)
path - path to componentpublic void assertDisabled(String path)
path - path to componentpublic void assertRequired(String path)
path - path to form componentpublic void assertNotRequired(String path)
path - path to form componentpublic void assertUsability(Component component)
component - public void clickLink(Component link)
link - public void assertBookmarkablePageLink(String id, Class<? extends WebPage> pageClass, PageParameters parameters)
id - pageClass - parameters - public <T extends Page> void executeTest(Class<?> testClass, Class<T> pageClass, String filename) throws Exception
-Dwicket.replace.expected.results=true to automatically replace the expected
output file.T - testClass - pageClass - filename - Exceptionpublic void executeTest(Class<?> testClass, Page page, String filename) throws Exception
-Dwicket.replace.expected.results=true to automatically replace the expected
output file.testClass - page - filename - Exceptionpublic void executeTest(Class<?> testClass, Component component, String filename) throws Exception
-Dwicket.replace.expected.results=true to automatically replace the expected
output file.testClass - component - filename - Exceptionpublic <T extends Page> void executeTest(Class<?> testClass, Class<T> pageClass, PageParameters parameters, String filename) throws Exception
-Dwicket.replace.expected.results=true to automatically replace the expected
output file.T - testClass - pageClass - parameters - filename - Exceptionpublic void executeListener(Class<?> testClass, Component component, String filename) throws Exception
testClass - component - filename - Exceptionpublic void executeBehavior(Class<?> testClass, AbstractAjaxBehavior behavior, String filename) throws Exception
testClass - behavior - filename - Exceptionpublic void assertRedirectUrl(String expectedRedirectUrl)
expectedRedirectUrl - expectedpublic static String getBasedir()
Copyright © 2006–2021 Apache Software Foundation. All rights reserved.