org.apache.wicket.util.tester
Class TagTester

java.lang.Object
  extended by org.apache.wicket.util.tester.TagTester

public class TagTester
extends java.lang.Object

Tag tester is used to test that a generated markup tag contains the correct attributes, values etc. This can be done instead of comparing generated markup with some expected markup. The advantage of this is that a lot of tests don't fail when the generated markup changes just a little bit.

It also gives a more programmatic way of testing the generated output, by not having to worry about precisely how the markup looks instead of which attributes exists on the given tags, and what values they have.

Example:

  ...
  TagTester tagTester = application.getTagByWicketId("form");
  assertTrue(tag.hasAttribute("action"));
  ...
 

Since:
1.2.6

Method Summary
static TagTester createTagByAttribute(java.lang.String markup, java.lang.String attribute, java.lang.String value)
          Static factory method for creating a TagTester based on a tag found by an attribute with a specific value.
 java.lang.String getAttribute(java.lang.String attribute)
          Gets the value for a given attribute.
 boolean getAttributeContains(java.lang.String attribute, java.lang.String partialValue)
          Checks if an attribute contains the specified partial value.
 boolean getAttributeEndsWith(java.lang.String attribute, java.lang.String expected)
          Checks if an attribute's value ends with the given parameter.
 boolean getAttributeIs(java.lang.String attribute, java.lang.String expected)
          Checks if an attribute's value is the exact same as the given value.
 TagTester getChild(java.lang.String attribute, java.lang.String value)
          Gets a child tag for testing.
 java.lang.String getMarkup()
          Gets the markup for this tag.
 java.lang.String getName()
          Gets the tag's name.
 java.lang.String getValue()
          Returns the value for this tag.
 boolean hasAttribute(java.lang.String attribute)
          Tests if the tag contains the given attribute.
 boolean hasChildTag(java.lang.String tagName)
          Checks if the tag has a child with the given tagName.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getName

public java.lang.String getName()
Gets the tag's name.

Returns:
the tag name

hasAttribute

public boolean hasAttribute(java.lang.String attribute)
Tests if the tag contains the given attribute. Please note that this is non case-sensitive, because attributes in HTML may be non case-sensitive.

Parameters:
attribute - an attribute to look for in the tag
Returns:
true if the tag has the attribute, false if not.

getAttribute

public java.lang.String getAttribute(java.lang.String attribute)
Gets the value for a given attribute. Please note that this is non case-sensitive, because attributes in HTML may be non case-sensitive.

Parameters:
attribute - an attribute to look for in the tag
Returns:
the value of the attribute or null if it isn't found.

getAttributeContains

public boolean getAttributeContains(java.lang.String attribute,
                                    java.lang.String partialValue)
Checks if an attribute contains the specified partial value.

For example:

Markup:

  <span wicket:id="helloComp" class="style1 style2">Hello</span>
 

Test:

 TagTester tester = application.getTagByWicketId("helloComp");
 assertTrue(tester.getAttributeContains("class", "style2"));
 

Parameters:
attribute - the attribute to test on
partialValue - the partial value to test if the attribute value contains it
Returns:
true if the attribute value contains the partial value

getAttributeIs

public boolean getAttributeIs(java.lang.String attribute,
                              java.lang.String expected)
Checks if an attribute's value is the exact same as the given value.

Parameters:
attribute - an attribute to test
expected - the value which should be the same at the attribute's value
Returns:
true if the attribute's value is the same as the given value

getAttributeEndsWith

public boolean getAttributeEndsWith(java.lang.String attribute,
                                    java.lang.String expected)
Checks if an attribute's value ends with the given parameter.

Parameters:
attribute - an attribute to test
expected - the expected value
Returns:
true if the attribute's value ends with the expected value

hasChildTag

public boolean hasChildTag(java.lang.String tagName)
Checks if the tag has a child with the given tagName.

Parameters:
tagName - the tag name to search for
Returns:
true if this tag has a child with the given tagName.

getChild

public TagTester getChild(java.lang.String attribute,
                          java.lang.String value)
Gets a child tag for testing. If this tag contains child tags, you can get one of them as a TagTester instance.

Parameters:
attribute - an attribute on the child tag to search for
value - a value that the attribute must have
Returns:
the TagTester for the child tag

getMarkup

public java.lang.String getMarkup()
Gets the markup for this tag. This includes all markup between the open tag and the close tag.

Returns:
all the markup between the open tag and the close tag

getValue

public java.lang.String getValue()
Returns the value for this tag. This includes all data between the open tag and the close tag.

Returns:
all the data between the open tag and the close tag
Since:
1.3

createTagByAttribute

public static TagTester createTagByAttribute(java.lang.String markup,
                                             java.lang.String attribute,
                                             java.lang.String value)
Static factory method for creating a TagTester based on a tag found by an attribute with a specific value. Please note that it will return the first tag which matches the criteria. It's therefore good for attributes suck as "id" or "wicket:id", but only if "wicket:id" is unique in the specified markup.

Parameters:
markup - the markup to look for the tag to create the TagTester from
attribute - the attribute which should be on the tag in the markup
value - the value which the attribute must have
Returns:
the TagTester which matches the tag in the markup, that has the given value on the given attribute


Copyright © 2004-2010 Apache Software Foundation. All Rights Reserved.