Module org.htmlunit
Package org.htmlunit

Class WebAssert

java.lang.Object
org.htmlunit.WebAssert

public final class WebAssert extends Object
Utility class which contains standard assertions for HTML pages.

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 Details

    • assertTitleEquals

      public static void assertTitleEquals(HtmlPage page, String title)
      Verifies that the specified page's title equals the specified expected title.
      Parameters:
      page - the page to check
      title - the expected title
      Throws:
      AssertionError - if the page title does not match the expected title
      NullPointerException - if page or title is null
    • assertTitleContains

      public static void assertTitleContains(HtmlPage page, String titlePortion)
      Verifies that the specified page's title contains the specified substring.
      Parameters:
      page - the page to check
      titlePortion - the substring which the page title is expected to contain
      Throws:
      AssertionError - if the page title does not contain the substring
      NullPointerException - if page or titlePortion is null
    • assertTitleMatches

      public static void assertTitleMatches(HtmlPage page, String regex)
      Verifies that the specified page's title matches the specified regular expression.
      Parameters:
      page - the page to check
      regex - the regular expression that the page title is expected to match
      Throws:
      AssertionError - if the page title does not match the regular expression
      NullPointerException - if page or regex is null
    • assertElementPresent

      public static void assertElementPresent(HtmlPage page, String id)
      Verifies that the specified page contains an element with the specified ID.
      Parameters:
      page - the page to check
      id - the ID of an element expected in the page
      Throws:
      AssertionError - if no element with the specified ID is found
      NullPointerException - if page or id is null
    • assertElementPresentByXPath

      public static void assertElementPresentByXPath(HtmlPage page, String xpath)
      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 check
      xpath - the XPath expression which is expected to match an element in the page
      Throws:
      AssertionError - if no elements match the XPath expression
      NullPointerException - if page or xpath is null
    • assertElementNotPresent

      public static void assertElementNotPresent(HtmlPage page, String id)
      Verifies that the specified page does not contain an element with the specified ID.
      Parameters:
      page - the page to check
      id - the ID of an element which is expected to not exist on the page
      Throws:
      AssertionError - if an element with the specified ID is found
      NullPointerException - if page or id is null
    • assertElementNotPresentByXPath

      public static void assertElementNotPresentByXPath(HtmlPage page, String xpath)
      Verifies that the specified page does not contain an element matching the specified XPath expression.
      Parameters:
      page - the page to check
      xpath - the XPath expression which is expected to not match any element in the page
      Throws:
      AssertionError - if any elements match the XPath expression
    • assertTextPresent

      public static void assertTextPresent(HtmlPage page, String text)
      Verifies that the specified page contains the specified text.
      Parameters:
      page - the page to check
      text - the text to check for
      Throws:
      AssertionError - if the page does not contain the specified text
      NullPointerException - if page or text is null
    • assertTextPresentInElement

      public static void assertTextPresentInElement(HtmlPage page, String text, String id)
      Verifies that the element on the specified page which matches the specified ID contains the specified text.
      Parameters:
      page - the page to check
      text - the text to check for
      id - the ID of the element which is expected to contain the specified text
      Throws:
      AssertionError - if the element does not contain the specified text
      ElementNotFoundException - if no element with the specified ID exists
      NullPointerException - if any parameter is null
    • assertTextNotPresent

      public static void assertTextNotPresent(HtmlPage page, String text)
      Verifies that the specified page does not contain the specified text.
      Parameters:
      page - the page to check
      text - the text to check for
      Throws:
      AssertionError - if the page contains the specified text
      NullPointerException - if page or text is null
    • assertTextNotPresentInElement

      public static void assertTextNotPresentInElement(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.
      Parameters:
      page - the page to check
      text - the text to check for
      id - the ID of the element which is expected to not contain the specified text
    • assertLinkPresent

      public static void assertLinkPresent(HtmlPage page, String id)
      Verifies that the specified page contains a link with the specified ID.
      Parameters:
      page - the page to check
      id - 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

      public static void assertLinkNotPresent(HtmlPage page, String id)
      Verifies that the specified page does not contain a link with the specified ID.
      Parameters:
      page - the page to check
      id - 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

      public static void assertLinkPresentWithText(HtmlPage page, String text)
      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 check
      text - the text which a link in the specified page is expected to contain
    • assertLinkNotPresentWithText

      public static void assertLinkNotPresentWithText(HtmlPage page, String text)
      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 check
      text - the text which a link in the specified page is not expected to contain
    • assertFormPresent

      public static void assertFormPresent(HtmlPage page, String name)
      Verifies that the specified page contains a form with the specified name.
      Parameters:
      page - the page to check
      name - the expected name of a form on the page
      Throws:
      AssertionError - if no form with the specified name is found
      See Also:
    • assertFormNotPresent

      public static void assertFormNotPresent(HtmlPage page, String name)
      Verifies that the specified page does not contain a form with the specified name.
      Parameters:
      page - the page to check
      name - 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

      public static void assertInputPresent(HtmlPage page, String name)
      Verifies that the specified page contains an input element with the specified name.
      Parameters:
      page - the page to check
      name - the name of the input element to look for
      Throws:
      AssertionError - if no input element with the specified name is found
      See Also:
    • assertInputNotPresent

      public static void assertInputNotPresent(HtmlPage page, String name)
      Verifies that the specified page does not contain an input element with the specified name.
      Parameters:
      page - the page to check
      name - the name of the input element to look for
      Throws:
      AssertionError - if an input element with the specified name is found
      NullPointerException - if page or name is null
    • assertInputContainsValue

      public static void assertInputContainsValue(HtmlPage page, String name, String value)
      Verifies that the input element with the specified name on the specified page contains the specified value.
      Parameters:
      page - the page to check
      name - the name of the input element to check
      value - the value to check for
    • assertInputDoesNotContainValue

      public static void assertInputDoesNotContainValue(HtmlPage page, String name, String value)
      Verifies that the input element with the specified name on the specified page does not contain the specified value.
      Parameters:
      page - the page to check
      name - the name of the input element to check
      value - the value to check for
    • assertAllTabIndexAttributesSet

      public static void assertAllTabIndexAttributesSet(HtmlPage page)

      Many HTML elements are "tabbable" and can have a tabindex attribute 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 the tabindex attribute set.

      This method verifies that all tabbable elements have a valid value set for the tabindex attribute. 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 has an invalid or missing tabindex attribute
    • assertAllAccessKeyAttributesUnique

      public static void assertAllAccessKeyAttributesUnique(HtmlPage page)
      Many HTML components can have an accesskey attribute which defines a hot key for keyboard navigation. This method verifies that all the accesskey attributes 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

      public static void assertAllIdAttributesUnique(HtmlPage page)
      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 page
      NullPointerException - if page is null
    • notNull

      public static void notNull(String description, Object object)
      Assert that the specified parameter is not null. Throw a NullPointerException if a null is found.
      Parameters:
      description - the description to pass into the NullPointerException
      object - the object to check for null
      Throws:
      NullPointerException - if the object is null