This class provides a collection of static assertion methods for testing
HTML page content, structure, and behavior. All assertion methods throw
AssertionError when the expected condition is not met.
Common use cases include:
- Verifying page titles and content
- Checking for presence/absence of elements
- Validating form inputs and links
- Ensuring accessibility attributes are properly set
- Author:
- Daniel Gredler, Mike Bowler, Ahmed Ashour, Ronald Brill
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidMany HTML components can have anaccesskeyattribute which defines a hot key for keyboard navigation.static voidVerifies that all element IDs in the specified page are unique.static voidMany HTML elements are "tabbable" and can have atabindexattribute that determines the order in which the components are navigated when pressing the tab key.static voidassertElementNotPresent(HtmlPage page, String id) Verifies that the specified page does not contain an element with the specified ID.static voidassertElementNotPresentByXPath(HtmlPage page, String xpath) Verifies that the specified page does not contain an element matching the specified XPath expression.static voidassertElementPresent(HtmlPage page, String id) Verifies that the specified page contains an element with the specified ID.static voidassertElementPresentByXPath(HtmlPage page, String xpath) Verifies that the specified page contains an element matching the specified XPath expression.static voidassertFormNotPresent(HtmlPage page, String name) Verifies that the specified page does not contain a form with the specified name.static voidassertFormPresent(HtmlPage page, String name) Verifies that the specified page contains a form with the specified name.static voidassertInputContainsValue(HtmlPage page, String name, String value) Verifies that the first input element with the specified name on the specified page contains the specified value.static voidassertInputDoesNotContainValue(HtmlPage page, String name, String value) Verifies that the first input element with the specified name on the specified page does not contain the specified value.static voidassertInputNotPresent(HtmlPage page, String name) Verifies that the specified page does not contain an input element with the specified name.static voidassertInputPresent(HtmlPage page, String name) Verifies that the specified page contains an input element with the specified name.static voidassertLinkNotPresent(HtmlPage page, String id) Verifies that the specified page does not contain a link with the specified ID.static voidassertLinkNotPresentWithText(HtmlPage page, String text) Verifies that the specified page does not contain a link with the specified text.static voidassertLinkPresent(HtmlPage page, String id) Verifies that the specified page contains a link with the specified ID.static voidassertLinkPresentWithText(HtmlPage page, String text) Verifies that the specified page contains a link with the specified text.static voidassertTextNotPresent(HtmlPage page, String text) Verifies that the specified page does not contain the specified text.static voidassertTextNotPresentInElement(HtmlPage page, String text, String id) Verifies that the element on the specified page which matches the specified ID does not contain the specified text.static voidassertTextPresent(HtmlPage page, String text) Verifies that the specified page contains the specified text.static voidassertTextPresentInElement(HtmlPage page, String text, String id) Verifies that the element on the specified page which matches the specified ID contains the specified text.static voidassertTitleContains(HtmlPage page, String titlePortion) Verifies that the specified page's title contains the specified substring.static voidassertTitleEquals(HtmlPage page, String title) Verifies that the specified page's title equals the specified expected title.static voidassertTitleMatches(HtmlPage page, String regex) Verifies that the specified page's title matches the specified regular expression.static voidAsserts that the specified object is notnull, throwing aNullPointerExceptionwith the given description if it is.
-
Method Details
-
assertTitleEquals
Verifies that the specified page's title equals the specified expected title.- Parameters:
page- the page to checktitle- the expected title- Throws:
AssertionError- if the page title does not match the expected titleNullPointerException- if page or title is null
-
assertTitleContains
Verifies that the specified page's title contains the specified substring.- Parameters:
page- the page to checktitlePortion- the substring which the page title is expected to contain- Throws:
AssertionError- if the page title does not contain the substringNullPointerException- if page or titlePortion is null
-
assertTitleMatches
Verifies that the specified page's title matches the specified regular expression.- Parameters:
page- the page to checkregex- the regular expression that the page title is expected to match- Throws:
AssertionError- if the page title does not match the regular expressionNullPointerException- if page or regex is null
-
assertElementPresent
Verifies that the specified page contains an element with the specified ID.- Parameters:
page- the page to checkid- the ID of an element expected in the page- Throws:
AssertionError- if no element with the specified ID is foundNullPointerException- if page or id is null
-
assertElementPresentByXPath
Verifies that the specified page contains an element matching the specified XPath expression.Example usage:
WebAssert.assertElementPresentByXPath(page, "//div[@class='error']"); WebAssert.assertElementPresentByXPath(page, "//input[@type='submit' and @value='Login']");- Parameters:
page- the page to checkxpath- the XPath expression which is expected to match an element in the page- Throws:
AssertionError- if no elements match the XPath expressionNullPointerException- if page or xpath is null
-
assertElementNotPresent
Verifies that the specified page does not contain an element with the specified ID.- Parameters:
page- the page to checkid- the ID of an element which is expected to not exist on the page- Throws:
AssertionError- if an element with the specified ID is foundNullPointerException- if page or id is null
-
assertElementNotPresentByXPath
Verifies that the specified page does not contain an element matching the specified XPath expression.- Parameters:
page- the page to checkxpath- the XPath expression which is expected to not match any element in the page- Throws:
AssertionError- if any elements match the XPath expression
-
assertTextPresent
Verifies that the specified page contains the specified text.- Parameters:
page- the page to checktext- the text to check for- Throws:
AssertionError- if the page does not contain the specified textNullPointerException- if page or text is null
-
assertTextPresentInElement
Verifies that the element on the specified page which matches the specified ID contains the specified text.- Parameters:
page- the page to checktext- the text to check forid- the ID of the element which is expected to contain the specified text- Throws:
AssertionError- if the element does not contain the specified text, or if no element with the specified ID existsNullPointerException- if any parameter is null
-
assertTextNotPresent
Verifies that the specified page does not contain the specified text.- Parameters:
page- the page to checktext- the text to check for- Throws:
AssertionError- if the page contains the specified textNullPointerException- if page or text is null
-
assertTextNotPresentInElement
Verifies that the element on the specified page which matches the specified ID does not contain the specified text.If no element with the specified ID exists, an
AssertionErroris thrown — the element must be present for its text content to be verified.- Parameters:
page- the page to checktext- the text to check forid- the ID of the element which is expected to not contain the specified text- Throws:
AssertionError- if the element contains the text, or if no element with the specified ID existsNullPointerException- if any parameter is null
-
assertLinkPresent
Verifies that the specified page contains a link with the specified ID.- Parameters:
page- the page to checkid- the ID of the link which the page is expected to contain- Throws:
AssertionError- if no link with the specified ID is found- See Also:
-
assertLinkNotPresent
Verifies that the specified page does not contain a link with the specified ID.- Parameters:
page- the page to checkid- the ID of the link which the page is expected to not contain- Throws:
AssertionError- if a link with the specified ID is found- See Also:
-
assertLinkPresentWithText
Verifies that the specified page contains a link with the specified text. The specified text may be a substring of the entire text contained by the link.- Parameters:
page- the page to checktext- the text which a link in the specified page is expected to contain- Throws:
AssertionError- if no link containing the specified text is found
-
assertLinkNotPresentWithText
Verifies that the specified page does not contain a link with the specified text. The specified text may be a substring of the entire text contained by the link.- Parameters:
page- the page to checktext- the text which a link in the specified page is not expected to contain- Throws:
AssertionError- if a link containing the specified text is found
-
assertFormPresent
Verifies that the specified page contains a form with the specified name.- Parameters:
page- the page to checkname- the expected name of a form on the page- Throws:
AssertionError- if no form with the specified name is found- See Also:
-
assertFormNotPresent
Verifies that the specified page does not contain a form with the specified name.- Parameters:
page- the page to checkname- the name of a form which should not exist on the page- Throws:
AssertionError- if a form with the specified name is found- See Also:
-
assertInputPresent
Verifies that the specified page contains an input element with the specified name.- Parameters:
page- the page to checkname- the name of the input element to look for- Throws:
AssertionError- if no input element with the specified name is foundNullPointerException- if page or name is null- See Also:
-
assertInputNotPresent
Verifies that the specified page does not contain an input element with the specified name.- Parameters:
page- the page to checkname- the name of the input element to look for- Throws:
AssertionError- if an input element with the specified name is foundNullPointerException- if page or name is null
-
assertInputContainsValue
Verifies that the first input element with the specified name on the specified page contains the specified value.If multiple
<input>elements share the same name, only the first is checked.- Parameters:
page- the page to checkname- the name of the input element to checkvalue- the value to check for- Throws:
AssertionError- if no input element with the specified name is found, or if the first matching element does not contain the expected valueNullPointerException- if any parameter is null
-
assertInputDoesNotContainValue
Verifies that the first input element with the specified name on the specified page does not contain the specified value.If multiple
<input>elements share the same name, only the first is checked.- Parameters:
page- the page to checkname- the name of the input element to checkvalue- the value to check for- Throws:
AssertionError- if no input element with the specified name is found, or if the first matching element contains the unexpected valueNullPointerException- if any parameter is null
-
assertAllTabIndexAttributesSet
Many HTML elements are "tabbable" and can have a
tabindexattribute that determines the order in which the components are navigated when pressing the tab key. To ensure good usability for keyboard navigation, all tabbable elements should have thetabindexattribute set.This method verifies that all tabbable elements have a valid value set for the
tabindexattribute. Valid values are positive integers, 0 (for default tab order), or -1 (to exclude from tab order).The following elements are checked: a, area, button, input, object, select, textarea
- Parameters:
page- the page to check- Throws:
AssertionError- if any tabbable element is missing thetabindexattribute or has an out-of-bounds value
-
assertAllAccessKeyAttributesUnique
Many HTML components can have anaccesskeyattribute which defines a hot key for keyboard navigation. This method verifies that all theaccesskeyattributes on the specified page are unique.Duplicate access keys can confuse users and make keyboard navigation unpredictable.
- Parameters:
page- the page to check- Throws:
AssertionError- if any access key is used more than once on the page
-
assertAllIdAttributesUnique
Verifies that all element IDs in the specified page are unique.- Parameters:
page- the page to check- Throws:
AssertionError- if any element ID is used more than once on the pageNullPointerException- if page is null
-
notNull
Asserts that the specified object is notnull, throwing aNullPointerExceptionwith the given description if it is.- Parameters:
description- the message for theNullPointerExceptionobject- the object to check- Throws:
NullPointerException- ifobjectisnull
-