blob: 488301c413e96ea2c1a6488bec2ca9b430020df6 [file] [log] [blame]
If we need to test component markup at a more fine-grained level, we can use class @TagTester@ from package @org.apache.wicket.util.tester@.
This test class allows to check if the generated markup contains one or more tags having a given attribute with a given value. TagTester can not be directly instantiated but it comes with three factory methods that return one or more TagTester matching the searching criteria. In the following test case (from project @TagTesterExample@) we retrieve the first tag of the home page (a <span> tag) having attribute class equal to myClass:
*HomePage markup:*
{code:html}
<html xmlns:wicket="http://wicket.apache.org">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<span class="myClass"></span>
<div class="myClass"></div>
</body>
</html>
{code}
*Test method:*
{code}
@Test
public void homePageMarkupTest()
{
//start and render the test page
tester.startPage(HomePage.class);
//retrieve response's markup
String responseTxt = tester.getLastResponse().getDocument();
TagTester tagTester = TagTester.createTagByAttribute(responseTxt, "class", "myClass");
Assert.assertNotNull(tagTester);
Assert.assertEquals("span", tagTester.getName());
List<TagTester> tagTesterList = TagTester.createTagsByAttribute(responseTxt,
"class", "myClass", false);
Assert.assertEquals(2, tagTesterList.size());
}
{code}
The name of the tag found by TagTester can be retrieved with its method getName. Method @createTagsByAttribute@ returns all the tags that have the given value on the class attribute. In the code above we have used this method to test that our markup contains two tags having attribute class equal to myClass.
Another utility class that comes in handy when we want to test components markup is @ComponentRenderer@ in package @org.apache.wicket.core.util.string@. The purpose of this class is to render a page or a component in isolation with its static methods @renderComponent@ and @renderPage@. Both methods return the generated markup as @CharSequence@:
{code}
@Test
public void customComponentMarkupTest()
{
//instantiate MyComponent
MyComponent myComponent = //...
//render and save component markup
String componentMarkup = ComponentRenderer.renderComponent(myComponent);
//perform test operations
//...
}
{code}