Skip to content

Playwright Text Assertion Not Matching Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Playwright Text Assertion Not Matching Fix. We cover key concepts, practical examples, and best practices.

Your toHaveText() assertion fails — the element has text but the matcher reports a mismatch, even though the text looks correct.

The Problem

// WRONG — toHaveText requires exact match
test('verify heading', async ({ page }) => {
  await expect(page.locator('h1')).toHaveText('Welcome!');
  // Fails if there's extra whitespace or child element text
});
Expected: "Welcome!"
Received: "  Welcome!  "

toHaveText() checks for an exact match. Extra whitespace, hidden text, or child element text causes failure.

Step-by-Step Fix

1. Use toContainText for partial matching

// RIGHT — toContainText checks substring
import { test, expect } from '@playwright/test';

test('heading contains text', async ({ page }) => {
  await page.goto('https://example.com');

  await expect(page.locator('h1')).toContainText('Welcome');

  // Also works with an array of texts
  await expect(page.locator('ul li')).toContainText(['Item 1', 'Item 2']);
});

2. Use toHaveText with options

// RIGHT — toHaveText with exact and ignoreCase
test('heading has exact text', async ({ page }) => {
  await expect(page.locator('h1')).toHaveText('Welcome!', {
    useInnerText: true,     // Use innerText instead of textContent
    timeout: 10000,
  });

  // Case-insensitive matching
  await expect(page.locator('h1')).toHaveText(/welcome!/i);
});

3. Use regex patterns

// RIGHT — regex for flexible matching
test('match with regex', async ({ page }) => {
  await expect(page.locator('.price')).toHaveText(/\$\d+\.\d{2}/);
  await expect(page.locator('.email')).toContainText(/@example\.com$/);
});

4. Get text for custom assertions

// RIGHT — get text content for custom validation
test('custom text validation', async ({ page }) => {
  const text = await page.locator('.message').textContent();
  expect(text.trim()).toBe('Operation successful');
  expect(text.length).toBeGreaterThan(0);
});

Expected output:

✓ heading contains text (0.5s)
✓ heading has exact text (0.4s)
✓ match with regex (0.6s)

Prevention Tips

  • Use toContainText() for substring matching
  • Use toHaveText() for exact matching (with options)
  • Use regex patterns for flexible or dynamic text
  • Use useInnerText: true when hidden text causes issues
  • Use textContent() for custom assertions

Common Mistakes with assert text

  1. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  2. Using return to exit a function early instead of wrapping a pure value in the monad
  3. Mixing let bindings with <- bindings in do notation, producing type errors

These mistakes appear frequently in real-world PLAYWRIGHT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What's the difference between toHaveText and toContainText?

toHaveText checks if the element's text exactly matches (or matches a regex). toContainText checks if the text contains a substring. Use toContainText when you don't care about surrounding whitespace or child element text.

Does toHaveText include child element text?

By default, toHaveText uses textContent which includes all child text. Use { useInnerText: true } to get only visible text.

Can I assert on multiple elements' text at once?

Yes. await expect(page.locator('li')).toHaveText(['Item 1', 'Item 2', 'Item 3']) checks each element matches the corresponding text in order.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro