Package org.apache.wicket.util.tester
Class WicketTester
- java.lang.Object
-
- org.apache.wicket.util.tester.BaseWicketTester
-
- org.apache.wicket.util.tester.WicketTester
-
public class WicketTester extends BaseWicketTester
A helper class to ease unit testing of Wicket applications without the need for a servlet container. To start a test, either usestartPageorstartPanel:// 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: startMyPage.classand assertLabelit rendered. Next, we try to navigate through aLink:// 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 aLink) and render the response pageYourPage. Ok, unit test ofMyPageis completed. Now we testYourPagestandalone:// 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 viaComponent.getPageRelativePath(). Since each Component has an ID/name, any Component can also be referenced by its IDMarkupContainer.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 FormTester- Since:
- 1.2.6
- Author:
- Ingram Chen, Juergen Donnerstag, Frank Bille
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.wicket.util.tester.BaseWicketTester
BaseWicketTester.StartComponentInPage, BaseWicketTester.TestFilterConfig
-
-
Constructor Summary
Constructors Constructor Description WicketTester()Creates aWicketTesterand automatically creates aWebApplication, but the tester will have no home page.WicketTester(java.lang.Class<? extends Page> homePage)Creates aWicketTesterand automatically creates aWebApplication.WicketTester(WebApplication application)Creates aWicketTester.WicketTester(WebApplication application, boolean init)Creates aWicketTesterto help unit testing.WicketTester(WebApplication application, java.lang.String path)Creates aWicketTesterto help unit testing.WicketTester(WebApplication application, javax.servlet.ServletContext servletCtx)Creates aWicketTesterto help unit testing.WicketTester(WebApplication application, javax.servlet.ServletContext servletCtx, boolean init)Creates aWicketTesterto help unit testing.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidassertAjaxLocation()Asserts that the Ajax location header is present.voidassertBehavior(java.lang.String path, java.lang.Class<? extends Behavior> expectedBehaviorClass)Asserts that theComponenta the given path has a behavior of the given type.voidassertBookmarkablePageLink(java.lang.String id, java.lang.Class<? extends WebPage> pageClass, org.apache.wicket.request.mapper.parameter.PageParameters parameters)Asserts that that the BookmarkablePageLink> identified by "id" points to the page as expected - including parameters.voidassertComponent(java.lang.String path, java.lang.Class<? extends Component> expectedComponentClass)Asserts aComponentclass.voidassertComponentFeedbackMessage(Component component, java.lang.String key, IModel<?> model, IFeedbackMessageFilter filter)Asserts that there is a feedback message provided by a given componentvoidassertComponentOnAjaxResponse(java.lang.String componentPath)Tests that aComponenthas been added to aAjaxRequestTarget, usingIPartialPageRequestHandler.add(Component...).voidassertComponentOnAjaxResponse(Component component)Tests that aComponenthas been added to aAjaxRequestTarget, usingIPartialPageRequestHandler.add(Component...).voidassertContains(java.lang.String pattern)Asserts the content of last rendered page contains (matches) a given regex pattern.voidassertContainsNot(java.lang.String pattern)The opposite ofassertContains(String).voidassertDisabled(java.lang.String path)assert component is enabled.voidassertEnabled(java.lang.String path)assert component is enabled.voidassertErrorMessages(java.io.Serializable... expectedErrorMessages)Asserts error-level feedback messages.voidassertFeedback(java.lang.String path, java.io.Serializable... messages)Assert that a particular feedback panel is rendering certain messages.voidassertFeedbackMessages(IFeedbackMessageFilter filter, java.io.Serializable... expectedMessages)Assert there are feedback messages accepted by the provided filter.voidassertInfoMessages(java.io.Serializable... expectedInfoMessages)Assert info-level feedback messages.voidassertInvisible(java.lang.String path)Asserts that aComponentis invisible.voidassertLabel(java.lang.String path, java.lang.String expectedLabelText)Asserts the text of aLabelComponent.voidassertMarkupLocale(Component component, java.util.Locale expectedLocale)Asserts that a component's markup has loaded with the given localevoidassertMarkupStyle(Component component, java.lang.String expectedStyle)Asserts that a component's markup has loaded with the given style.voidassertMarkupVariation(Component component, java.lang.String expectedVariation)Asserts that a component's markup has loaded with the given variationvoidassertModelValue(java.lang.String path, java.lang.Object expectedValue)Asserts the model value of a component.voidassertNoErrorMessage()Asserts no error-level feedback messages.voidassertNoFeedbackMessage(int level)Asserts there are no feedback messages with a certain level.voidassertNoInfoMessage()Asserts no info-level feedback messages.voidassertNotRequired(java.lang.String path)assert form component is required.voidassertRedirectUrl(java.lang.String expectedRedirectUrl)Assert that the last request redirected to the given Url.voidassertRenderedPage(java.lang.Class<? extends Page> expectedRenderedPageClass)Asserts a last-renderedPageclass.voidassertRequired(java.lang.String path)assert form component is required.voidassertResultPage(java.lang.Class<?> clazz, java.lang.String filename)Asserts last-renderedPageagainst an expected HTML document.voidassertResultPage(java.lang.String expectedDocument)Asserts last-renderedPageagainst an expected HTML document as aStringvoidassertUsability(Component component)Checks whether a component is visible and/or enabled before usagevoidassertVisible(java.lang.String path)Asserts that aComponentis visible.voidclickLink(Component link)voidexecuteBehavior(java.lang.Class<?> testClass, AbstractAjaxBehavior behavior, java.lang.String filename)voidexecuteListener(java.lang.Class<?> testClass, Component component, java.lang.String filename)<T extends Page>
voidexecuteTest(java.lang.Class<?> testClass, java.lang.Class<T> pageClass, java.lang.String filename)Use-Dwicket.replace.expected.results=trueto automatically replace the expected output file.<T extends Page>
voidexecuteTest(java.lang.Class<?> testClass, java.lang.Class<T> pageClass, org.apache.wicket.request.mapper.parameter.PageParameters parameters, java.lang.String filename)Use-Dwicket.replace.expected.results=trueto automatically replace the expected output file.voidexecuteTest(java.lang.Class<?> testClass, Component component, java.lang.String filename)Use-Dwicket.replace.expected.results=trueto automatically replace the expected output file.voidexecuteTest(java.lang.Class<?> testClass, Page page, java.lang.String filename)Use-Dwicket.replace.expected.results=trueto automatically replace the expected output file.static java.lang.StringgetBasedir()Returns the current Maven build directory taken from the basedir system property, or null if not set-
Methods inherited from class org.apache.wicket.util.tester.BaseWicketTester
addRequestHeader, applyRequest, assertExists, assertNotExists, checkUsability, cleanupFeedbackMessages, cleanupFeedbackMessages, clearFeedbackMessages, clickLink, clickLink, createOriginHeader, createPage, createPageMarkup, debugComponentTrees, debugComponentTrees, destroy, dumpPage, executeAjaxEvent, executeAjaxEvent, executeAjaxUrl, executeAllTimerBehaviors, executeBehavior, executeListener, executeUrl, getApplication, getComponentFromLastRenderedPage, 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, 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, servletRequestLocale, setExposeExceptions, setFollowRedirects, setLastResponse, setRequest, setUseRequestUrlAsBase, startComponentInPage, startComponentInPage, startComponentInPage, startComponentInPage, startPage, startPage, startPage, startPage, startResource, startResourceReference, startResourceReference, submitForm, submitForm, urlFor, urlFor, urlFor
-
-
-
-
Constructor Detail
-
WicketTester
public WicketTester()
Creates aWicketTesterand automatically creates aWebApplication, but the tester will have no home page.
-
WicketTester
public WicketTester(java.lang.Class<? extends Page> homePage)
Creates aWicketTesterand automatically creates aWebApplication.- Parameters:
homePage- a home pageClass
-
WicketTester
public WicketTester(WebApplication application)
Creates aWicketTester.- Parameters:
application- aWicketTesterWebApplicationobject
-
WicketTester
public WicketTester(WebApplication application, java.lang.String path)
Creates aWicketTesterto help unit testing.- Parameters:
application- aWicketTesterWebApplicationobjectpath- the absolute path on disk to the web application's contents (e.g. war root) - may benull- See Also:
MockApplication()
-
WicketTester
public WicketTester(WebApplication application, javax.servlet.ServletContext servletCtx)
Creates aWicketTesterto help unit testing.- Parameters:
application- aWicketTesterWebApplicationobjectservletCtx- the servlet context used as backend
-
WicketTester
public WicketTester(WebApplication application, boolean init)
Creates aWicketTesterto help unit testing.- Parameters:
application- aWicketTesterWebApplicationobjectinit- force the application to be initialized (default = true)
-
WicketTester
public WicketTester(WebApplication application, javax.servlet.ServletContext servletCtx, boolean init)
Creates aWicketTesterto help unit testing.- Parameters:
application- aWicketTesterWebApplicationobjectservletCtx- the servlet context used as backendinit- force the application to be initialized (default = true)
-
-
Method Detail
-
getBasedir
public static java.lang.String getBasedir()
Returns the current Maven build directory taken from the basedir system property, or null if not set- Returns:
- path with a trailing slash
-
assertAjaxLocation
public void assertAjaxLocation()
Asserts that the Ajax location header is present.
-
assertComponent
public void assertComponent(java.lang.String path, java.lang.Class<? extends Component> expectedComponentClass)Asserts aComponentclass.- Parameters:
path- path toComponentexpectedComponentClass- expectedComponentclass
-
assertBehavior
public void assertBehavior(java.lang.String path, java.lang.Class<? extends Behavior> expectedBehaviorClass)Asserts that theComponenta the given path has a behavior of the given type.- Parameters:
path- path toComponentexpectedBehaviorClass- expectedBehaviorclass
-
assertComponentOnAjaxResponse
public void assertComponentOnAjaxResponse(Component component)
Tests that aComponenthas been added to aAjaxRequestTarget, usingIPartialPageRequestHandler.add(Component...). This method actually tests that aComponentis on the Ajax response sent back to the client.PLEASE NOTE! This method doesn't actually insert the
Componentin the client DOM tree, using JavaScript. But it shouldn't be needed because you just have to trust that Wicket Ajax JavaScript works.- Parameters:
component- aComponentto be tested
-
assertComponentOnAjaxResponse
public void assertComponentOnAjaxResponse(java.lang.String componentPath)
Tests that aComponenthas been added to aAjaxRequestTarget, usingIPartialPageRequestHandler.add(Component...). This method actually tests that aComponentis on the Ajax response sent back to the client.PLEASE NOTE! This method doesn't actually insert the
Componentin the client DOM tree, using JavaScript. But it shouldn't be needed because you just have to trust that Wicket Ajax JavaScript works.- Parameters:
componentPath- aComponentpath to test
-
assertContains
public void assertContains(java.lang.String pattern)
Asserts the content of last rendered page contains (matches) a given regex pattern.- Parameters:
pattern- a regex pattern to match
-
assertContainsNot
public void assertContainsNot(java.lang.String pattern)
The opposite ofassertContains(String).- Parameters:
pattern- pattern
-
assertMarkupVariation
public void assertMarkupVariation(Component component, java.lang.String expectedVariation)
Asserts that a component's markup has loaded with the given variation- Parameters:
component- The component which markup to checkexpectedVariation- The expected variation of the component's markup
-
assertMarkupStyle
public void assertMarkupStyle(Component component, java.lang.String expectedStyle)
Asserts that a component's markup has loaded with the given style.- Parameters:
component- The component which markup to checkexpectedStyle- The expected style of the component's markup. For example: green inMyPanel_green.html
-
assertMarkupLocale
public void assertMarkupLocale(Component component, java.util.Locale expectedLocale)
Asserts that a component's markup has loaded with the given locale- Parameters:
component- The component which markup to checkexpectedLocale- The expected locale of the component's markup
-
assertErrorMessages
public void assertErrorMessages(java.io.Serializable... expectedErrorMessages)
Asserts error-level feedback messages.- Parameters:
expectedErrorMessages- expected error messages
-
assertInfoMessages
public void assertInfoMessages(java.io.Serializable... expectedInfoMessages)
Assert info-level feedback messages.- Parameters:
expectedInfoMessages- expected info messages
-
assertFeedbackMessages
public void assertFeedbackMessages(IFeedbackMessageFilter filter, java.io.Serializable... expectedMessages)
Assert there are feedback messages accepted by the provided filter.- Parameters:
filter- the filter that will decide which messages to checkexpectedMessages- expected feedback messages
-
assertComponentFeedbackMessage
public void assertComponentFeedbackMessage(Component component, java.lang.String key, IModel<?> model, IFeedbackMessageFilter filter)
Asserts that there is a feedback message provided by a given component- Parameters:
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.
-
assertFeedback
public void assertFeedback(java.lang.String path, java.io.Serializable... messages)Assert that a particular feedback panel is rendering certain messages. NOTE: this casts the component at the specified path to aFeedbackPanel, so it will not work with customIFeedbackimplementations unless you are subclassingFeedbackPanel- Parameters:
path- path to the feedback panelmessages- messages expected to be rendered
-
assertInvisible
public void assertInvisible(java.lang.String path)
Asserts that aComponentis invisible.- Parameters:
path- path toComponent
-
assertLabel
public void assertLabel(java.lang.String path, java.lang.String expectedLabelText)Asserts the text of aLabelComponent.- Parameters:
path- path toLabelComponentexpectedLabelText- expected text of theLabel
-
assertModelValue
public void assertModelValue(java.lang.String path, java.lang.Object expectedValue)Asserts the model value of a component.- Parameters:
path- path to the component on the pageexpectedValue- expected value of the component's model
-
assertNoErrorMessage
public void assertNoErrorMessage()
Asserts no error-level feedback messages.
-
assertNoInfoMessage
public void assertNoInfoMessage()
Asserts no info-level feedback messages.
-
assertNoFeedbackMessage
public void assertNoFeedbackMessage(int level)
Asserts there are no feedback messages with a certain level.- Parameters:
level- the level to check for
-
assertRenderedPage
public void assertRenderedPage(java.lang.Class<? extends Page> expectedRenderedPageClass)
Asserts a last-renderedPageclass.- Parameters:
expectedRenderedPageClass- expected class of last renderedPage
-
assertResultPage
public void assertResultPage(java.lang.Class<?> clazz, java.lang.String filename) throws java.lang.ExceptionAsserts last-renderedPageagainst an expected HTML document.Use
-Dwicket.replace.expected.results=trueto automatically replace the expected output file.- Overrides:
assertResultPagein classBaseWicketTester- Parameters:
clazz-Classused to load the file (relative toclazzpackage)filename- expected output filenameString- Throws:
java.lang.Exception
-
assertResultPage
public void assertResultPage(java.lang.String expectedDocument)
Asserts last-renderedPageagainst an expected HTML document as aString- Parameters:
expectedDocument- expected outputString
-
assertVisible
public void assertVisible(java.lang.String path)
Asserts that aComponentis visible.- Parameters:
path- path to aComponent
-
assertEnabled
public void assertEnabled(java.lang.String path)
assert component is enabled.- Parameters:
path- path to component
-
assertDisabled
public void assertDisabled(java.lang.String path)
assert component is enabled.- Parameters:
path- path to component
-
assertRequired
public void assertRequired(java.lang.String path)
assert form component is required.- Parameters:
path- path to form component
-
assertNotRequired
public void assertNotRequired(java.lang.String path)
assert form component is required.- Parameters:
path- path to form component
-
assertUsability
public void assertUsability(Component component)
Checks whether a component is visible and/or enabled before usage- Parameters:
component-
-
clickLink
public void clickLink(Component link)
- Parameters:
link-
-
assertBookmarkablePageLink
public void assertBookmarkablePageLink(java.lang.String id, java.lang.Class<? extends WebPage> pageClass, org.apache.wicket.request.mapper.parameter.PageParameters parameters)Asserts that that the BookmarkablePageLink> identified by "id" points to the page as expected - including parameters.- Parameters:
id-pageClass-parameters-
-
executeTest
public <T extends Page> void executeTest(java.lang.Class<?> testClass, java.lang.Class<T> pageClass, java.lang.String filename) throws java.lang.Exception
Use-Dwicket.replace.expected.results=trueto automatically replace the expected output file.- Type Parameters:
T-- Parameters:
testClass-pageClass-filename-- Throws:
java.lang.Exception
-
executeTest
public void executeTest(java.lang.Class<?> testClass, Page page, java.lang.String filename) throws java.lang.ExceptionUse-Dwicket.replace.expected.results=trueto automatically replace the expected output file.- Parameters:
testClass-page-filename-- Throws:
java.lang.Exception
-
executeTest
public void executeTest(java.lang.Class<?> testClass, Component component, java.lang.String filename) throws java.lang.ExceptionUse-Dwicket.replace.expected.results=trueto automatically replace the expected output file.- Parameters:
testClass-component-filename-- Throws:
java.lang.Exception
-
executeTest
public <T extends Page> void executeTest(java.lang.Class<?> testClass, java.lang.Class<T> pageClass, org.apache.wicket.request.mapper.parameter.PageParameters parameters, java.lang.String filename) throws java.lang.Exception
Use-Dwicket.replace.expected.results=trueto automatically replace the expected output file.- Type Parameters:
T-- Parameters:
testClass-pageClass-parameters-filename-- Throws:
java.lang.Exception
-
executeListener
public void executeListener(java.lang.Class<?> testClass, Component component, java.lang.String filename) throws java.lang.Exception- Parameters:
testClass-component-filename-- Throws:
java.lang.Exception
-
executeBehavior
public void executeBehavior(java.lang.Class<?> testClass, AbstractAjaxBehavior behavior, java.lang.String filename) throws java.lang.Exception- Parameters:
testClass-behavior-filename-- Throws:
java.lang.Exception
-
assertRedirectUrl
public void assertRedirectUrl(java.lang.String expectedRedirectUrl)
Assert that the last request redirected to the given Url.- Parameters:
expectedRedirectUrl- expected
-
-