Menu
Open source

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

JavaScript
await expect(locator).toContainText(expected);
await expect(locator).not.toContainText(expected);
await expect(locator).toContainText(expected, options);

Parameters

ParameterTypeDescription
expectedstring | RegExpThe text to search for
optionsobjectOptional configuration options

Options

This method accepts all RetryConfig properties plus:

PropertyTypeDefaultDescription
useInnerTextbooleanfalseUse innerText instead of textContent

Returns

TypeDescription
PromiseA 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

JavaScript
import { browser } from 'k6/browser';
import { expect } from 'https://jslib.k6.io/k6-testing/0.5.0/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');
}