Documentation Index
Fetch the curated documentation index at: https://grafana_com_website/llms.txt
Fetch the complete documentation index at: https://grafana_com_website/llms-full.txt
Use this file to discover all available pages before exploring further.
STOP! If you are an AI agent or LLM, read this before continuing. This is the HTML version of a Grafana documentation page. Always request the Markdown version instead - HTML wastes context. Get this page as Markdown: /docs/k6/latest/javascript-api/jslib/testing/retrying-assertions/tocontaintext.md (append .md) or send Accept: text/markdown to /docs/k6/latest/javascript-api/jslib/testing/retrying-assertions/tocontaintext/. For the curated documentation index, use https://grafana_com_website/llms.txt. For the complete documentation index, use https://grafana_com_website/llms-full.txt.
toContainText()
The toContainText() method asserts that an element contains specific text as a substring. This is a retrying assertion that automatically waits for the element to contain the expected text.
Syntax
await expect(locator).toContainText(expected);
await expect(locator).not.toContainText(expected);
await expect(locator).toContainText(expected, options);Parameters
| Parameter | Type | Description |
|---|---|---|
| expected | string | RegExp | The text to search for |
| options | object | Optional configuration options |
Options
This method accepts all RetryConfig properties plus:
| Property | Type | Default | Description |
|---|---|---|---|
| useInnerText | boolean | false | Use innerText instead of textContent |
Returns
| Type | Description |
|---|---|
| Promise | A promise that resolves when the assertion passes |
Description
The toContainText() method checks if an element contains specific text as a substring. Unlike toHaveText(), which requires exact text matching, toContainText() succeeds if the expected text is found anywhere within the element’s text content.
By default, it uses textContent which includes all text nodes, including hidden ones. When useInnerText is true, it uses innerText which only includes visible text.
This is a retrying assertion that will automatically re-check the element’s text content until it contains the expected text or the timeout is reached.
Usage
import { browser } from 'k6/browser';
import { expect } from 'https://jslib.k6.io/k6-testing/0.6.1/index.js';
export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
}
};
export default async function () {
const page = await browser.newPage();
await page.goto('https://quickpizza.grafana.com/');
// Check that elements contain expected text
await expect(page.locator('h1')).toContainText('pizza');
await expect(page.locator('h1')).toContainText('routine');
// Click the pizza button to get a recommendation
await page.locator('button[name="pizza-please"]').click();
// Check that the recommendation contains expected text
await expect(page.locator('h2')).toContainText('QuickPizza');
}Was this page helpful?
Related resources from Grafana Labs

