Class HtmlPageUtils

    • Constructor Summary

      Constructors 
      Constructor Description
      HtmlPageUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void checkRadioButton​(HtmlForm form, java.lang.String radioButtonName, int index)
      Checks the HTML radio button input element with the given name and index in the specified form.
      static void checkRadioButton​(HtmlForm form, java.lang.String radioButtonName, java.lang.String value)
      Checks the HTML radio button input element with the given name and value in the specified form.
      static void checkRadioButtonRandomly​(HtmlForm form, java.lang.String radioButtonName)
      Checks one of the radio buttons with the given name in the specified form.
      static void checkRadioButtonRandomly​(HtmlForm form, java.lang.String radioButtonName, boolean excludeFirst, boolean excludeLast)
      Checks one of the radio buttons with the given name in the specified form.
      static int countElementsByXPath​(HtmlPage page, java.lang.String xpath)
      Returns the number of elements that match the given XPath expression.
      static <T extends HtmlElement>
      T
      createHtmlElement​(java.lang.String tagName, HtmlElement parent)
      Creates a new HTML element with the specified tag name and adds it as a child to the given parent element.
      static HtmlInput createInput​(HtmlForm form, java.lang.String type, java.lang.String name, java.lang.String value)
      Creates a new input HTML element, initializes it with the specified type, name, and value, and inserts it to the given form.
      static <T extends HtmlElement>
      java.util.List<T>
      findHtmlElements​(HtmlElement element, java.lang.String xpath)
      Finds HTML elements using the given XPath expression within the specified HTML element.
      static <T extends HtmlElement>
      java.util.List<T>
      findHtmlElements​(HtmlPage page, java.lang.String xpath)
      Finds HTML elements using the given XPath expression on the specified page.
      static <T extends HtmlElement>
      T
      findHtmlElementsAndPickOne​(HtmlElement element, java.lang.String xpath)
      Finds HTML elements using the given XPath expression within the specified HTML element, selects one of them randomly and returns it.
      static <T extends HtmlElement>
      T
      findHtmlElementsAndPickOne​(HtmlElement element, java.lang.String xpath, boolean excludeFirst)
      Finds HTML elements using the given XPath expression within the specified HTML element, selects one of them randomly and returns it.
      static <T extends HtmlElement>
      T
      findHtmlElementsAndPickOne​(HtmlElement element, java.lang.String xpath, boolean excludeFirst, boolean excludeLast)
      Finds HTML elements using the given XPath expression within the specified HTML element, selects one of them randomly and returns it.
      static <T> T findHtmlElementsAndPickOne​(HtmlPage page, java.lang.String xpath)
      Finds HTML elements using the given XPath expression on the specified page, selects one of them randomly and returns it.
      static <T> T findHtmlElementsAndPickOne​(HtmlPage page, java.lang.String xpath, boolean excludeFirst)
      Finds HTML elements using the given XPath expression on the specified page, selects one of them randomly and returns it.
      static <T extends HtmlElement>
      T
      findHtmlElementsAndPickOne​(HtmlPage page, java.lang.String xpath, boolean excludeFirst, boolean excludeLast)
      Finds HTML elements using the given XPath expression on the specified page, selects one of them randomly and returns it.
      static <T extends HtmlElement>
      T
      findSingleHtmlElementByID​(HtmlPage page, java.lang.String id)
      Finds the HTML element with the given ID on the specified page.
      static <T extends HtmlElement>
      T
      findSingleHtmlElementByXPath​(HtmlElement element, java.lang.String xpath)
      Finds a single HTML elements using the given XPath expression within the specified HTML element.
      static <T extends HtmlElement>
      T
      findSingleHtmlElementByXPath​(HtmlPage page, java.lang.String xpath)
      Finds a single HTML elements using the given XPath expression on the specified page.
      static HtmlAnchor getAnchorWithText​(HtmlPage page, java.lang.String anchorText)
      Returns the HTML anchor element with the passed anchor text.
      static java.util.List<HtmlForm> getFormsByIDRegExp​(HtmlPage page, java.util.regex.Pattern pattern)
      Returns a list of forms the ID of which matches the given regular expression.
      static java.util.List<HtmlForm> getFormsByNameRegExp​(HtmlPage page, java.util.regex.Pattern pattern)
      Returns a list of forms the name of which matches the given regular expression.
      static HtmlPage getFramePage​(HtmlPage page, java.lang.String... frameNames)
      Returns the HTML page contained in a nested frame window.
      static <T extends HtmlInput>
      T
      getInputEndingWith​(HtmlForm form, java.lang.String suffix)
      Returns the first input element that ends with this suffix.
      static <T extends HtmlInput>
      T
      getInputStartingWith​(HtmlForm form, java.lang.String prefix)
      Returns the first input element that starts with this prefix.
      static HtmlSelect getSelectEndingWith​(HtmlForm form, java.lang.String suffix)
      Returns the first select element that ends with this suffix.
      static HtmlSelect getSelectStartingWith​(HtmlForm form, java.lang.String prefix)
      Returns the first select element that starts with this prefix.
      static boolean isElementPresent​(HtmlElement element, java.lang.String xpath)
      Returns whether or not a HTML element exists for the given XPath expression.
      static boolean isElementPresent​(HtmlPage page, java.lang.String xpath)
      Returns whether or not a HTML element exists for the given XPath expression.
      static void select​(HtmlForm form, java.lang.String selectName, java.lang.String optionValue)
      Finds the HTML select element with the given name in the specified form and selects the option with the passed value.
      static void selectRandomly​(HtmlForm form, java.lang.String selectName)
      Finds the HTML select element with the given name in the specified form and selects one of the options randomly.
      static void selectRandomly​(HtmlForm form, java.lang.String selectName, boolean excludeFirst)
      Finds the HTML select element with the given name in the specified form and selects one of the options randomly.
      static void selectRandomly​(HtmlForm form, java.lang.String selectName, boolean excludeFirst, boolean excludeLast)
      Finds the HTML select element with the given name in the specified form and set one of the options selected randomly.
      static void setCheckBoxValue​(HtmlForm form, java.lang.String checkBoxName, boolean isChecked)
      Finds the HTML check box input element with the given name in the specified form and sets its value.
      static void setInputValue​(HtmlForm form, java.lang.String inputName, java.lang.String value)
      Finds the HTML input element with the given name in the specified form and sets its value.
      static <T extends HtmlElement>
      java.util.List<T>
      waitForHtmlElements​(HtmlPage page, java.lang.String xpath, long timeout)
      Waits until at least one HTML element can be located on the given page using the specified XPath expression and returns the list of matching elements.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • HtmlPageUtils

        public HtmlPageUtils()
    • Method Detail

      • checkRadioButton

        public static void checkRadioButton​(HtmlForm form,
                                            java.lang.String radioButtonName,
                                            int index)
        Checks the HTML radio button input element with the given name and index in the specified form. All other radio buttons with the same name are left unchecked.
        Parameters:
        form - the form with the radio buttons
        radioButtonName - the name of the radio button group
        index - the index of the radio button to check (starts with 0)
      • checkRadioButton

        public static void checkRadioButton​(HtmlForm form,
                                            java.lang.String radioButtonName,
                                            java.lang.String value)
        Checks the HTML radio button input element with the given name and value in the specified form. All other radio buttons with the same name are left unchecked.
        Parameters:
        form - the form with the radio buttons
        radioButtonName - the name of the radio button group
        value - the value of the radio button to check
      • checkRadioButtonRandomly

        public static void checkRadioButtonRandomly​(HtmlForm form,
                                                    java.lang.String radioButtonName)
        Checks one of the radio buttons with the given name in the specified form. All other radio buttons with the same name are left unchecked.
        Parameters:
        form - the form with the radio buttons
        radioButtonName - the name of the radio button group
      • checkRadioButtonRandomly

        public static void checkRadioButtonRandomly​(HtmlForm form,
                                                    java.lang.String radioButtonName,
                                                    boolean excludeFirst,
                                                    boolean excludeLast)
        Checks one of the radio buttons with the given name in the specified form. All other radio buttons with the same name are unchecked.
        Parameters:
        form - the form with the radio buttons
        radioButtonName - the name of the radio button group
        excludeFirst - whether to exclude the first radio button in the group
        excludeLast - whether to exclude the last radio button in the group
      • createHtmlElement

        public static <T extends HtmlElement> T createHtmlElement​(java.lang.String tagName,
                                                                  HtmlElement parent)
        Creates a new HTML element with the specified tag name and adds it as a child to the given parent element.

        Note that input elements should be created using createInput(HtmlForm, String, String, String).

        Parameters:
        tagName - the tag name
        parent - the parent
        Returns:
        the HTML element just created
      • createInput

        public static HtmlInput createInput​(HtmlForm form,
                                            java.lang.String type,
                                            java.lang.String name,
                                            java.lang.String value)
        Creates a new input HTML element, initializes it with the specified type, name, and value, and inserts it to the given form.
        Parameters:
        form - the form
        type - the type
        name - the name
        value - the value
        Returns:
        the input element just created
      • findHtmlElements

        public static <T extends HtmlElement> java.util.List<T> findHtmlElements​(HtmlPage page,
                                                                                 java.lang.String xpath)
        Finds HTML elements using the given XPath expression on the specified page.
        Type Parameters:
        T - the expected type of the elements found
        Parameters:
        page - the page to search
        xpath - the XPath expression specifying the elements
        Returns:
        the list of elements found
        Throws:
        java.lang.AssertionError - if no elements were found
      • findHtmlElementsAndPickOne

        public static <T> T findHtmlElementsAndPickOne​(HtmlPage page,
                                                       java.lang.String xpath)
        Finds HTML elements using the given XPath expression on the specified page, selects one of them randomly and returns it.
        Type Parameters:
        T - the expected type of the elements found
        Parameters:
        page - the page to search
        xpath - the XPath expression specifying the elements
        Returns:
        the list of elements found
        Throws:
        java.lang.AssertionError - if no elements were found
      • findHtmlElementsAndPickOne

        public static <T> T findHtmlElementsAndPickOne​(HtmlPage page,
                                                       java.lang.String xpath,
                                                       boolean excludeFirst)
        Finds HTML elements using the given XPath expression on the specified page, selects one of them randomly and returns it.
        Type Parameters:
        T - the expected type of the elements found
        Parameters:
        page - the page to search
        xpath - the XPath expression specifying the elements
        excludeFirst - whether to exclude the first element found
        Returns:
        the list of elements found
        Throws:
        java.lang.AssertionError - if no elements were found
      • findHtmlElementsAndPickOne

        public static <T extends HtmlElement> T findHtmlElementsAndPickOne​(HtmlPage page,
                                                                           java.lang.String xpath,
                                                                           boolean excludeFirst,
                                                                           boolean excludeLast)
        Finds HTML elements using the given XPath expression on the specified page, selects one of them randomly and returns it.
        Type Parameters:
        T - the expected type of the elements found
        Parameters:
        page - the page to search
        xpath - the XPath expression specifying the elements
        excludeFirst - whether to exclude the first element found
        excludeLast - whether to exclude the last element found
        Returns:
        the list of elements found
        Throws:
        java.lang.AssertionError - if no elements were found
      • findSingleHtmlElementByID

        public static <T extends HtmlElement> T findSingleHtmlElementByID​(HtmlPage page,
                                                                          java.lang.String id)
        Finds the HTML element with the given ID on the specified page.
        Type Parameters:
        T - the expected type of the element found
        Parameters:
        page - the page to search
        id - the ID of the element
        Returns:
        the element found
        Throws:
        java.lang.AssertionError - if no or more than one element with this ID was found
      • findSingleHtmlElementByXPath

        public static <T extends HtmlElement> T findSingleHtmlElementByXPath​(HtmlPage page,
                                                                             java.lang.String xpath)
        Finds a single HTML elements using the given XPath expression on the specified page.
        Type Parameters:
        T - the expected type of the elements found
        Parameters:
        page - the page to search
        xpath - the XPath expression specifying the elements
        Returns:
        the element found
        Throws:
        java.lang.AssertionError - if no or more than one element was found
      • findHtmlElements

        public static <T extends HtmlElement> java.util.List<T> findHtmlElements​(HtmlElement element,
                                                                                 java.lang.String xpath)
        Finds HTML elements using the given XPath expression within the specified HTML element.
        Type Parameters:
        T - the expected type of the elements found
        Parameters:
        element - the HTML element to search
        xpath - the XPath expression specifying the elements (must not contain leading slash)
        Returns:
        the list of elements found
        Throws:
        java.lang.AssertionError - if no elements were found
      • findHtmlElementsAndPickOne

        public static <T extends HtmlElement> T findHtmlElementsAndPickOne​(HtmlElement element,
                                                                           java.lang.String xpath)
        Finds HTML elements using the given XPath expression within the specified HTML element, selects one of them randomly and returns it.
        Type Parameters:
        T - the expected type of the elements found
        Parameters:
        element - the HTML element to search
        xpath - the XPath expression specifying the elements
        Returns:
        the list of elements found
        Throws:
        java.lang.AssertionError - if no elements were found
      • findHtmlElementsAndPickOne

        public static <T extends HtmlElement> T findHtmlElementsAndPickOne​(HtmlElement element,
                                                                           java.lang.String xpath,
                                                                           boolean excludeFirst)
        Finds HTML elements using the given XPath expression within the specified HTML element, selects one of them randomly and returns it.
        Type Parameters:
        T - the expected type of the elements found
        Parameters:
        element - the HTML element to search
        xpath - the XPath expression specifying the elements
        excludeFirst - whether to exclude the first element found
        Returns:
        the list of elements found
        Throws:
        java.lang.AssertionError - if no elements were found
      • findHtmlElementsAndPickOne

        public static <T extends HtmlElement> T findHtmlElementsAndPickOne​(HtmlElement element,
                                                                           java.lang.String xpath,
                                                                           boolean excludeFirst,
                                                                           boolean excludeLast)
        Finds HTML elements using the given XPath expression within the specified HTML element, selects one of them randomly and returns it.
        Type Parameters:
        T - the expected type of the elements found
        Parameters:
        element - the HTML element to search
        xpath - the XPath expression specifying the elements
        excludeFirst - whether to exclude the first element found
        excludeLast - whether to exclude the last element found
        Returns:
        the list of elements found
        Throws:
        java.lang.AssertionError - if no elements were found
      • findSingleHtmlElementByXPath

        public static <T extends HtmlElement> T findSingleHtmlElementByXPath​(HtmlElement element,
                                                                             java.lang.String xpath)
        Finds a single HTML elements using the given XPath expression within the specified HTML element.
        Type Parameters:
        T - the expected type of the elements found
        Parameters:
        element - the HTML element to search
        xpath - the XPath expression specifying the elements
        Returns:
        the element found
        Throws:
        java.lang.AssertionError - if no or more than one element was found
      • getAnchorWithText

        public static HtmlAnchor getAnchorWithText​(HtmlPage page,
                                                   java.lang.String anchorText)
        Returns the HTML anchor element with the passed anchor text.
        Parameters:
        page - the page to search
        anchorText - the anchor text
        Returns:
        the anchor found
        Throws:
        java.lang.AssertionError - if no or more than one anchor was found
      • getFramePage

        public static HtmlPage getFramePage​(HtmlPage page,
                                            java.lang.String... frameNames)
        Returns the HTML page contained in a nested frame window. The path to the respective frame is given as an array of frame names, starting with the name of the outermost frame.
        Parameters:
        page - the page to search
        frameNames - the list of frame names
        Returns:
        the frame page found
        Throws:
        ElementNotFoundException - if there is no frame with the given name at the respective nesting level
      • getInputEndingWith

        public static <T extends HtmlInput> T getInputEndingWith​(HtmlForm form,
                                                                 java.lang.String suffix)
        Returns the first input element that ends with this suffix.
        Parameters:
        form - the form to use
        suffix - the suffix of the input elements to search
        Returns:
        the first found input element or null otherwise
      • getInputStartingWith

        public static <T extends HtmlInput> T getInputStartingWith​(HtmlForm form,
                                                                   java.lang.String prefix)
        Returns the first input element that starts with this prefix.
        Parameters:
        form - the form to use
        prefix - the prefix of the input elements to search
        Returns:
        the first found input element or null otherwise
      • getSelectEndingWith

        public static HtmlSelect getSelectEndingWith​(HtmlForm form,
                                                     java.lang.String suffix)
        Returns the first select element that ends with this suffix.
        Parameters:
        form - the form to use
        suffix - the suffix of the select elements to search
        Returns:
        the first found select element or null otherwise
      • getSelectStartingWith

        public static HtmlSelect getSelectStartingWith​(HtmlForm form,
                                                       java.lang.String prefix)
        Returns the first select element that starts with this prefix.
        Parameters:
        form - the form to use
        prefix - the prefix of the select elements to search
        Returns:
        the first found select element or null otherwise
      • select

        public static void select​(HtmlForm form,
                                  java.lang.String selectName,
                                  java.lang.String optionValue)
        Finds the HTML select element with the given name in the specified form and selects the option with the passed value.
        Parameters:
        form - the form to search
        selectName - the name of the select element
        optionValue - the value of the option element to select
      • selectRandomly

        public static void selectRandomly​(HtmlForm form,
                                          java.lang.String selectName)
        Finds the HTML select element with the given name in the specified form and selects one of the options randomly.
        Parameters:
        form - the form to search
        selectName - the name of the select element
      • selectRandomly

        public static void selectRandomly​(HtmlForm form,
                                          java.lang.String selectName,
                                          boolean excludeFirst)
        Finds the HTML select element with the given name in the specified form and selects one of the options randomly.
        Parameters:
        form - the form to search
        selectName - the name of the select element
        excludeFirst - whether to exclude the first option element
      • selectRandomly

        public static void selectRandomly​(HtmlForm form,
                                          java.lang.String selectName,
                                          boolean excludeFirst,
                                          boolean excludeLast)
        Finds the HTML select element with the given name in the specified form and set one of the options selected randomly. Disabled option will be ignored.
        Parameters:
        form - the form to search
        selectName - the name of the select element
        excludeFirst - whether to exclude the first option element
        excludeLast - whether to exclude the last option element
      • setCheckBoxValue

        public static void setCheckBoxValue​(HtmlForm form,
                                            java.lang.String checkBoxName,
                                            boolean isChecked)
        Finds the HTML check box input element with the given name in the specified form and sets its value.
        Parameters:
        form - the form to search
        checkBoxName - the name of the check box
        isChecked - the new check box value
      • setInputValue

        public static void setInputValue​(HtmlForm form,
                                         java.lang.String inputName,
                                         java.lang.String value)
        Finds the HTML input element with the given name in the specified form and sets its value.
        Parameters:
        form - the form to search
        inputName - the name of the input element
        value - the new value
      • getFormsByIDRegExp

        public static java.util.List<HtmlForm> getFormsByIDRegExp​(HtmlPage page,
                                                                  java.util.regex.Pattern pattern)
        Returns a list of forms the ID of which matches the given regular expression.
        Parameters:
        page - the page to search
        pattern - the regex pattern to match the form ID
        Returns:
        the list of forms found
      • getFormsByNameRegExp

        public static java.util.List<HtmlForm> getFormsByNameRegExp​(HtmlPage page,
                                                                    java.util.regex.Pattern pattern)
        Returns a list of forms the name of which matches the given regular expression.
        Parameters:
        page - the page to search
        pattern - the regex pattern to match the form name
        Returns:
        the list of forms found
      • isElementPresent

        public static boolean isElementPresent​(HtmlPage page,
                                               java.lang.String xpath)
        Returns whether or not a HTML element exists for the given XPath expression.
        Parameters:
        page - the HTML page
        xpath - the XPath expression
        Returns:
        true if at least one HTML element matches the given XPath expression, false otherwise
      • isElementPresent

        public static boolean isElementPresent​(HtmlElement element,
                                               java.lang.String xpath)
        Returns whether or not a HTML element exists for the given XPath expression.
        Parameters:
        element - the HTML element
        xpath - the XPath expression
        Returns:
        true if at least one HTML element matches the given XPath expression, false otherwise
      • countElementsByXPath

        public static int countElementsByXPath​(HtmlPage page,
                                               java.lang.String xpath)
        Returns the number of elements that match the given XPath expression.
        Parameters:
        page - the HTML page
        xpath - the XPath expression
        Returns:
        number of matching elements
      • waitForHtmlElements

        public static <T extends HtmlElement> java.util.List<T> waitForHtmlElements​(HtmlPage page,
                                                                                    java.lang.String xpath,
                                                                                    long timeout)
                                                                             throws java.lang.InterruptedException
        Waits until at least one HTML element can be located on the given page using the specified XPath expression and returns the list of matching elements. If the waiting time exceeds the given timeout value, an AssertionError is thrown. This method can be used to wait for HTML elements that appear on the page only after some JavaScript code has finished to run.
        Parameters:
        page - the HTML page
        xpath - the XPath expression
        timeout - the timeout value [ms]
        Returns:
        the list of matching elements
        Throws:
        java.lang.InterruptedException - if the current thread is interrupted while sleeping
        java.lang.AssertionError - if no elements were found in the given time period