Package com.xceptance.xlt.api.util
Class HtmlPageUtils
- java.lang.Object
-
- com.xceptance.xlt.api.util.BasicPageUtils
-
- com.xceptance.xlt.api.util.HtmlPageUtils
-
public class HtmlPageUtils extends BasicPageUtils
TheHtmlPageUtils
class provides some useful helper methods to make dealing withHtmlPage
objects easier. When using the plain HtmlUnit API, similar pieces of code have to be written again and again. Using this class, test case actions are often shorter and easier to understand.- See Also:
AbstractHtmlPageAction
,LightweightHtmlPageUtils
-
-
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>
TcreateHtmlElement(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>
TfindHtmlElementsAndPickOne(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>
TfindHtmlElementsAndPickOne(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>
TfindHtmlElementsAndPickOne(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>
TfindHtmlElementsAndPickOne(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>
TfindSingleHtmlElementByID(HtmlPage page, java.lang.String id)
Finds the HTML element with the given ID on the specified page.static <T extends HtmlElement>
TfindSingleHtmlElementByXPath(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>
TfindSingleHtmlElementByXPath(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>
TgetInputEndingWith(HtmlForm form, java.lang.String suffix)
Returns the first input element that ends with this suffix.static <T extends HtmlInput>
TgetInputStartingWith(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 com.xceptance.xlt.api.util.BasicPageUtils
getAbsoluteUrl, getAbsoluteUrl, pickOneRandomly, pickOneRandomly, pickOneRandomly
-
-
-
-
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 buttonsradioButtonName
- the name of the radio button groupindex
- 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 buttonsradioButtonName
- the name of the radio button groupvalue
- 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 buttonsradioButtonName
- 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 buttonsradioButtonName
- the name of the radio button groupexcludeFirst
- whether to exclude the first radio button in the groupexcludeLast
- 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 nameparent
- 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 formtype
- the typename
- the namevalue
- 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 searchxpath
- 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 searchxpath
- 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 searchxpath
- the XPath expression specifying the elementsexcludeFirst
- 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 searchxpath
- the XPath expression specifying the elementsexcludeFirst
- whether to exclude the first element foundexcludeLast
- 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 searchid
- 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 searchxpath
- 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 searchxpath
- 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 searchxpath
- 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 searchxpath
- the XPath expression specifying the elementsexcludeFirst
- 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 searchxpath
- the XPath expression specifying the elementsexcludeFirst
- whether to exclude the first element foundexcludeLast
- 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 searchxpath
- 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 searchanchorText
- 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 searchframeNames
- 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 usesuffix
- 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 useprefix
- 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 usesuffix
- 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 useprefix
- 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 searchselectName
- the name of the select elementoptionValue
- 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 searchselectName
- 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 searchselectName
- the name of the select elementexcludeFirst
- 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 searchselectName
- the name of the select elementexcludeFirst
- whether to exclude the first option elementexcludeLast
- 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 searchcheckBoxName
- the name of the check boxisChecked
- 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 searchinputName
- the name of the input elementvalue
- 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 searchpattern
- 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 searchpattern
- 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 pagexpath
- 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 elementxpath
- 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 pagexpath
- 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, anAssertionError
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 pagexpath
- the XPath expressiontimeout
- the timeout value [ms]- Returns:
- the list of matching elements
- Throws:
java.lang.InterruptedException
- if the current thread is interrupted while sleepingjava.lang.AssertionError
- if no elements were found in the given time period
-
-