k6/html module provides functionality for parsing and manipulating HTML content in k6 scripts. It offers a jQuery-like API for selecting and extracting data from HTML responses.
Overview
Use this module to:- Parse HTML responses from HTTP requests
- Extract data using CSS selectors
- Validate HTML structure and content
- Scrape data for parameterization
Importing the Module
API Reference
Functions
function
Selection Class
The Selection object provides a jQuery-like API for traversing and manipulating HTML documents.Common Methods
function
Returns elements matching the CSS selector.Parameters:
string
required
CSS selector (e.g.,
"div.class", "#id", "a[href]")function
Returns the text content of the selection.Returns:
string
Combined text content of all matched elements
function
function
Returns the value of an attribute.Parameters:
string
required
Attribute name (e.g.,
"href", "src", "class")function
Iterates over matched elements.Parameters:
function
required
Function called for each element:
(index, element) => voidfunction
Returns the element at the specified index.Parameters:
number
required
Zero-based index of the element
Traversal Methods
function
Returns child elements, optionally filtered by selector.
function
Returns parent elements, optionally filtered by selector.
function
Returns sibling elements, optionally filtered by selector.
function
Returns the next sibling element.
function
Returns the previous sibling element.
function
Returns the closest ancestor matching the selector.
Element Class
Represents a single HTML DOM element.string
The tag name of the element (e.g.,
"div", "a", "span")number
The type of the node (1 for element nodes)
string
The text content of the element
string
The HTML content of the element
object
Map of attribute names to values
Examples
Parsing HTTP Response
Data Extraction and Validation
Working with Tables
Form Data Extraction
CSS Selector Examples
Content Verification
Common Use Cases
Web Scraping for Test Data
Extract dynamic values from HTML to use in subsequent requests:- CSRF tokens from forms
- Session identifiers from hidden fields
- Product IDs from listings
- Dynamic URLs from navigation
HTML Structure Validation
Verify that pages contain expected elements:- Forms have required fields
- Navigation links are present
- Images have alt text
- Headers follow hierarchy
Content Verification
Validate page content matches requirements:- Check for specific text or messages
- Verify data is displayed correctly
- Ensure error messages appear when expected
- Validate dynamic content rendering
Performance Considerations
HTML parsing has overhead. For high-performance tests:
- Only parse when you need to extract data
- Use
httpresponse checks for simple validation - Cache parsed documents if parsing the same content multiple times
- Consider using
check()on response body strings for simple text matching
Related Resources
HTTP Module
Make HTTP requests
Checks
Validate responses
CSS Selectors
Learn CSS selector syntax
Data Correlation
Extract and reuse dynamic data