|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.wicket.protocol.http.MockWebApplication org.apache.wicket.util.tester.BaseWicketTester
public class BaseWicketTester
A helper class to ease unit testing of Wicket applications without the need for a servlet
container. See javadoc of WicketTester
for example usage. This class can be used
as is, but JUnit users should use derived class WicketTester
.
WicketTester
Nested Class Summary | |
---|---|
static class |
BaseWicketTester.DummyWebApplication
|
Constructor Summary | |
---|---|
BaseWicketTester()
Creates WicketTester and automatically create a WebApplication ,
but the tester will have no home page. |
|
BaseWicketTester(java.lang.Class homePage)
Creates WicketTester and automatically creates a WebApplication . |
|
BaseWicketTester(WebApplication application)
Creates a WicketTester . |
|
BaseWicketTester(WebApplication application,
java.lang.String path)
Creates a WicketTester for unit testing. |
Method Summary | |
---|---|
void |
assertListView(java.lang.String path,
java.util.List expectedList)
assert the model of ListView use expectedList |
void |
assertResultPage(java.lang.Class pageClass,
java.lang.String filename)
Asserts last rendered Page against an expected HTML document. |
static void |
callOnBeginRequest(RequestCycle rc)
|
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()
Dumps the Component trees. |
void |
debugComponentTrees(java.lang.String filter)
Dumps the Component trees to log. |
void |
dumpPage()
Dumps the source of last rendered Page . |
void |
executeAjaxEvent(Component component,
java.lang.String event)
Simulates the firing of an Ajax event. |
void |
executeAjaxEvent(java.lang.String componentPath,
java.lang.String event)
Simulates the firing of an Ajax event. |
void |
executeBehavior(AbstractAjaxBehavior behavior)
Builds and processes a request suitable for executing an AbstractAjaxBehavior . |
void |
executeListener(Component component)
Builds and processes a request suitable for invoking a listener. |
Component |
getComponentFromLastRenderedPage(java.lang.String path)
Gets the component with the given path from last rendered page. |
java.lang.String |
getContentDispositionFromResponseHeader()
Retrieves the content disposition from the response header. |
int |
getContentLengthFromResponseHeader()
Retrieves the content length from the response header. |
java.lang.String |
getContentTypeFromResponseHeader()
Retrieves the content type from the response header. |
java.lang.String |
getLastModifiedFromResponseHeader()
Retrieves the last-modified value from the response header. |
java.util.List |
getMessages(int level)
Retrieves FeedbackMessages . |
TagTester |
getTagById(java.lang.String id)
Retrieves a TagTester based on an DOM id. |
TagTester |
getTagByWicketId(java.lang.String wicketId)
Retrieves a TagTester based on a wicket:id . |
Result |
hasLabel(java.lang.String path,
java.lang.String expectedLabelText)
assert the text of Label component. |
Result |
hasNoErrorMessage()
Asserts no error-level feedback messages. |
Result |
hasNoInfoMessage()
Asserts no info-level feedback messages. |
Result |
ifContains(java.lang.String pattern)
assert the content of last rendered page contains(matches) regex pattern. |
Result |
isComponent(java.lang.String path,
java.lang.Class expectedComponentClass)
assert component class |
Result |
isComponentOnAjaxResponse(Component component)
Tests that a Component has been added to a AjaxRequestTarget ,
using AjaxRequestTarget.addComponent(Component) . |
Result |
isInvisible(java.lang.String path)
assert component invisible. |
Result |
isPageLink(java.lang.String path,
java.lang.Class expectedPageClass)
assert PageLink link to page class. |
Result |
isRenderedPage(java.lang.Class expectedRenderedPageClass)
Asserts the last rendered Page class. |
Result |
isResultPage(java.lang.String expectedDocument)
Asserts last rendered Page against an expected HTML document as a
String . |
Result |
isVisible(java.lang.String path)
assert component visible. |
FormTester |
newFormTester(java.lang.String path)
Creates a FormTester for the Form at a given path, and fills all child
FormComponent s with blank String s. |
FormTester |
newFormTester(java.lang.String path,
boolean fillBlankString)
Creates a FormTester for the Form at a given path. |
protected WebRequestCycle |
resolveRequestCycle()
|
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. |
void |
startComponent(Component component)
A helper method for starting a component for a test without attaching it to a Page. |
Page |
startPage(java.lang.Class pageClass)
Renders a Page from its default constructor. |
Page |
startPage(java.lang.Class pageClass,
PageParameters parameters)
Renders a Page from its default constructor. |
Page |
startPage(ITestPageSource testPageSource)
Renders a Page defined in TestPageSource . |
Page |
startPage(Page page)
Renders the Page . |
Panel |
startPanel(java.lang.Class panelClass)
Renders a Panel from a Panel(String id) constructor. |
Panel |
startPanel(TestPanelSource testPanelSource)
Renders a Panel defined in TestPanelSource . |
void |
submitForm(java.lang.String path)
Submits 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 BaseWicketTester()
WicketTester
and automatically create a WebApplication
,
but the tester will have no home page.
public BaseWicketTester(java.lang.Class homePage)
WicketTester
and automatically creates a WebApplication
.
homePage
- a home page Class
public BaseWicketTester(WebApplication application)
WicketTester
.
application
- a WicketTester
WebApplication
objectpublic BaseWicketTester(WebApplication application, java.lang.String path)
WicketTester
for unit testing.
application
- a WicketTester
WebApplication
objectpath
- the absolute path on disk to the WebApplication
's contents (e.g.
war root) - may be null
MockWebApplication.MockWebApplication(
org.apache.wicket.protocol.http.WebApplication, String)
Method Detail |
---|
public final Page startPage(ITestPageSource testPageSource)
Page
defined in TestPageSource
. This is usually used
when a page does not have default constructor. 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 creates a test page instance
public void executeListener(Component component)
Component
must implement any of the known IListener
interfaces.
component
- the listener to invokepublic void executeBehavior(AbstractAjaxBehavior behavior)
AbstractAjaxBehavior
.
behavior
- an AbstractAjaxBehavior
to executepublic final Page startPage(Page page)
Page
.
page
- a Page
to render
Page
public final Page startPage(java.lang.Class pageClass)
Page
from its default constructor.
pageClass
- a test Page
class with default constructor
Page
public final Page startPage(java.lang.Class pageClass, PageParameters parameters)
Page
from its default constructor.
pageClass
- a test Page
class with default constructorparameters
- the parameters to use for the class.
Page
public FormTester newFormTester(java.lang.String path)
FormTester
for the Form
at a given path, and fills all child
FormComponent
s with blank String
s.
path
- path to FormComponent
FormTester
instance for testing the Form
newFormTester(String, boolean)
public FormTester newFormTester(java.lang.String path, boolean fillBlankString)
FormTester
for the Form
at a given path.
path
- path to FormComponent
fillBlankString
- specifies whether to fill all child FormComponent
s with blank
String
s
FormTester
instance for testing the Form
FormTester
public final Panel startPanel(TestPanelSource testPanelSource)
Panel
defined in TestPanelSource
. The usage is
similar to startPage(ITestPageSource)
. Please note that testing Panel
must use the supplied panelId as a 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 creates test Panel
instances
- Returns:
- a rendered
Panel
public final Panel startPanel(java.lang.Class panelClass)
Panel
from a Panel(String id)
constructor.
panelClass
- a test Panel
class with Panel(String id)
constructor
Panel
public void startComponent(Component component)
component
- public Component getComponentFromLastRenderedPage(java.lang.String path)
path
- Path to component
MarkupContainer.get(String)
public Result hasLabel(java.lang.String path, java.lang.String expectedLabelText)
Label
component.
path
- path to Label
componentexpectedLabelText
- expected label text
Result
public Result isPageLink(java.lang.String path, java.lang.Class expectedPageClass)
PageLink
link to page class.
path
- path to PageLink
componentexpectedPageClass
- expected page class to link
Result
public Result isComponent(java.lang.String path, java.lang.Class expectedComponentClass)
path
- path to componentexpectedComponentClass
- expected component class
Result
public Result isVisible(java.lang.String path)
path
- path to component
Result
public Result isInvisible(java.lang.String path)
path
- path to component
Result
public Result ifContains(java.lang.String pattern)
pattern
- reqex pattern to match
Result
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 ListView
public 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
componentpublic void setParameterForNextRequest(java.lang.String componentPath, java.lang.Object value)
Component
with the given path to be used with the
next request.
NOTE: this method only works when a Page
was rendered first.
componentPath
- path to the Component
value
- the parameter value to setpublic Result isRenderedPage(java.lang.Class expectedRenderedPageClass)
Page
class.
FIXME explain why the code is so complicated to compare two classes, or simplify
expectedRenderedPageClass
- expected class of last rendered page
Result
public void assertResultPage(java.lang.Class pageClass, java.lang.String filename) throws java.lang.Exception
Page
against an expected HTML document.
Use -Dwicket.replace.expected.results=true
to automatically replace the
expected output file.
pageClass
- used to load the File
(relative to clazz
package)filename
- expected output File
name
java.lang.Exception
public Result isResultPage(java.lang.String expectedDocument) throws java.lang.Exception
Page
against an expected HTML document as a
String
.
expectedDocument
- expected output
Result
java.lang.Exception
public Result hasNoErrorMessage()
Result
public Result hasNoInfoMessage()
Result
public java.util.List getMessages(int level)
FeedbackMessages
.
level
- level of feedback message, for example:
FeedbackMessage.DEBUG or FeedbackMessage.INFO.. etc
List
of messages (as String
s)FeedbackMessage
public void dumpPage()
Page
.
public void debugComponentTrees()
Component
trees.
public void debugComponentTrees(java.lang.String filter)
Component
trees to log. Show only the Component
s
whose paths contain the filter String
.
filter
- a filter String
public Result isComponentOnAjaxResponse(Component component)
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.
component
- the Component
to test
Result
public void executeAjaxEvent(java.lang.String componentPath, java.lang.String event)
componentPath
- the Component
pathevent
- the event which we simulate being fired. If event
is
null
, the test will fail.executeAjaxEvent(Component, String)
public void executeAjaxEvent(Component component, java.lang.String event)
Component
by
using:
... component.add(new AjaxEventBehavior("ondblclick") { public void onEvent(AjaxRequestTarget) {} }); ...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
that has the AjaxEventBehavior
we want
to test. If the Component
is null
, the test will
fail.event
- the event to simulate being fired. If event
is null
,
the test will fail.protected WebRequestCycle resolveRequestCycle()
public TagTester getTagByWicketId(java.lang.String wicketId)
TagTester
based on a wicket:id
. If more
Component
s exist with the same wicket:id
in the markup, only
the first one is returned.
wicketId
- the wicket:id
to search for
TagTester
for the tag which has the given wicket:id
public TagTester getTagById(java.lang.String id)
TagTester
based on an DOM id. If more Component
s
exist with the same id in the markup, only the first one is returned.
id
- the DOM id to search for.
TagTester
for the tag which has the given DOM idpublic java.lang.String getContentTypeFromResponseHeader()
public int getContentLengthFromResponseHeader()
public java.lang.String getLastModifiedFromResponseHeader()
public java.lang.String getContentDispositionFromResponseHeader()
public static void callOnBeginRequest(RequestCycle rc)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |