HTML Plugin
The plugin provides the ability to work with HTML documents: data extraction and validation.
Installation
-
Copy the below line to
dependencies
section of the projectbuild.gradle
fileExample 1. build.gradleimplementation(group: 'org.vividus', name: 'vividus-plugin-html', version: '0.5.6')
-
If the project was imported to the IDE before adding new plugin, re-generate the configuration files for the used IDE and then refresh the project in the used IDE.
Locators
HTML locators should be used in order to interact with HTML document elements. The following locators are supported:
- CSS selector
- XPath
Steps
Validate elements
Validates the number of elements found by the locator matches the specified rules.
Then number of elements found by $locatorType `$locator` in HTML `$html` is $comparisonRule `$number`
-
$locatorType
- `CSS selector` or `XPath`. -
$locator
- See Locators. -
$html
- The HTML document to validate. -
$comparisonRule
- The comparison rule. -
$quantity
- The expected number of elements.
When I execute HTTP GET request for resource with URL `https://bethesda.net/en/game/starfield`
Then number of elements found by XPath `//h3[contains(., '111122')]` in HTML `${response}` is = `1`
Validate element text
Validates whether the element found by the locator contains the expected text.
Then element found by $locatorType `$locator` in HTML `$html` contains text `$expectedText`
-
$locatorType
- `CSS selector` or `XPath`. -
$locator
- See Locators. -
$html
- The HTML document to validate. -
$expectedText
- The expected element text.
When I execute HTTP GET request for resource with URL `https://bethesda.net/en/game/starfield`
Then element found by XPath `//h3` in HTML `${response}` contains text `111122`
Save attribute value
Saves the attribute value into the variable.
When I save `$attributeName` attribute value of element found by $locatorType `$locator` in HTML `$html` to $scopes variable `$variableName`
-
$attributeName
- The name of the attribute. -
$locatorType
- `CSS selector` or `XPath`. -
$locator
- See Locators. -
$html
- The HTML document to find element. -
$variableName
- The name of the variable to save the attribute value.
Given I initialize story variable `html` with value
`
<html>
<head>
<title>Page Title</title>
<script>//<![CDATA[Here comes the data//]]></script>
</head>
<body>
<h1>This is a Heading</h1>
<p title="paragraph">This is a paragraph.</p>
</body>
</html>
`
When I save `title` attribute value of element found by CSS selector `p` in HTML `${html}` to scenario variable `title`
Then `${title}` is = `paragraph`
Save data/text of element
Saves the data or the text of element. Where the data is characters between the start-tag and end-tag of the element and the text is the textual content of the element without any inner element.
When I save $dataType of element found by $locatorType `$locator` in HTML `$html` to $scopes variable `variableName`
-
$dataType
- Eitherdata
ortext
-
$html
- The HTML document to find element. -
$locatorType
- `CSS selector` or `XPath`. -
$locator
- See Locators.-
$variableName
- The name of the variable to save the data or text.
Given I initialize story variable `html` with value
`
<html>
<head>
<title>Page Title</title>
<script>//<![CDATA[Here comes the data//]]></script>
</head>
<body>
<h1>This is a Heading</h1>
<p title="paragraph">This is a paragraph.</p>
</body>
</html>
`
When I save data of element found by <locatorType> `<script>` in HTML `${html}` to scenario variable `data`
When I save text of element found by <locatorType> `<header>` in HTML `${html}` to scenario variable `text`
Then `${data}` is equal to `//<![CDATA[Here comes the data//]]>`
Then `${text}` is equal to `This is a Heading`
Examples:
|locatorType |script |header|
|CSS selector|script |h1 |
|XPath |//script|//h1 |