---
title: "toContainText() | Grafana k6 documentation"
description: "Asserts that an element contains specific text"
---

# 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 ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

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

## Parameters

Expand table

| Parameter | Type             | Description                    |
|-----------|------------------|--------------------------------|
| expected  | string \| RegExp | The text to search for         |
| options   | object           | Optional configuration options |

### Options

This method accepts all [RetryConfig](/docs/k6/latest/javascript-api/jslib/testing/retrying-assertions/retryconfig/) properties plus:

Expand table

| Property     | Type    | Default | Description                          |
|--------------|---------|---------|--------------------------------------|
| useInnerText | boolean | `false` | Use innerText instead of textContent |

## Returns

Expand table

| 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

JavaScript ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```javascript
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');
}
```
