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
filePlease make sure to use the same version for all VIVIDUS dependencies. Example 1. build.gradleimplementation(group: 'org.vividus', name: 'vividus-plugin-html')
-
If the project was imported to the IDE before adding new dependency, re-generate the configuration files for the used IDE and then refresh the project in the used IDE.
Table Transformers
FROM_HTML
FROM_HTML
transformer generates a table based on the text content, HTML content or attributes of HTML elements found in the requested HTML page.
Parameter Name | Description | ||||
---|---|---|---|---|---|
|
The URL of the page to build the table upon.
|
||||
|
The name of the variable containing source HTML, only variables of scopes
|
||||
|
The HTML resource name or the HTML file path.
|
||||
|
The column name in the generated table. |
||||
|
The XPath selector to select HTML elements in the HTML page. By using XPath selector we can extract element’s HTML content, attributes and text content like its shown in the following example:
|
||||
|
The base URI can be used to get an absolute URL from an attribute that may be a relative URL: make sure to add the prefix |
Property Name | Acceptable values | Default | Description |
---|---|---|---|
|
Set of headers to set when requesting the page.
|
<!DOCTYPE html>
<html>
<body>
<a href="/r">R</a>
<a href="/g">G</a>
<a href="/b">B</a>
</body>
</html>
Examples:
{transformer=FROM_HTML, column=relative-url, pageUrl=https://mypage.com, xpathSelector=//a/@href}
|relative-url|
|/r |
|/g |
|/b |
transformer=FROM_HTML, column=relative-url, path=data/index.html, xpathSelector=//a/@href
Steps
Validate elements
Validates the number of elements found by the locator matches the specified rules.
Then number of elements found by $htmlLocatorType `$htmlLocator` in HTML `$html` is $comparisonRule `$number`
-
$htmlLocatorType
- The HTML locator type, eitherCSS selector
orXPath
. -
$htmlLocator
- The actual locator. -
$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 $htmlLocatorType `$htmlLocator` in HTML `$html` contains text `$expectedText`
-
$htmlLocatorType
- The HTML locator type, eitherCSS selector
orXPath
. -
$htmlLocator
- The actual locator. -
$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 $htmlLocatorType `$htmlLocator` in HTML `$html` to $scopes variable `$variableName`
-
$attributeName
- The name of the attribute. -
$htmlLocatorType
- The HTML locator type, eitherCSS selector
orXPath
. -
$htmlLocator
- The actual locator. -
$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 $htmlLocatorType `$htmlLocator` in HTML `$html` to $scopes variable `variableName`
-
$dataType
- Eitherdata
ortext
-
$html
- The HTML document to find element. -
$htmlLocatorType
- The HTML locator type, eitherCSS selector
orXPath
. -
$htmlLocator
- The actual locator.-
$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 |
Save number of elements
Saves the number of elements contained in HTML document.
When I save number of elements found by $htmlLocatorType `$htmlLocator` in HTML `$html` to $scopes variable `$variableName`
-
$htmlLocatorType
- The HTML locator type, eitherCSS selector
orXPath
. -
$htmlLocator
- The actual locator. -
$html
- The HTML document. -
$variableName
- The name of the variable to save the number of elements.
Given I initialize story variable `html` with value
`
<html>
<body>
<h1>This is a Heading</h1>
<p title="paragraph">This is a paragraph.</p>
</body>
</html>
`
When I save number of elements found by XPath `//h1` in HTML `${html}` to scenario variable `heading1`
Then `${heading1}` is equal to `1`