Playwright Text Assertion Not Matching Fix
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: truewhen hidden text causes issues - Use
textContent()for custom assertions
Common Mistakes with assert text
- Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad - 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro