Playwright Visibility Assertion Not Working Fix
In this tutorial, you'll learn about Playwright Visibility Assertion Not Working Fix. We cover key concepts, practical examples, and best practices.
Your toBeVisible() assertion fails even though the element is on the page — or passes when the element is hidden.
The Problem
// WRONG — element exists in DOM but is not visible
test('check element visible', async ({ page }) => {
await expect(page.locator('.hidden-element')).toBeVisible();
// Fails — element has display: none or visibility: hidden
});
TimeoutError: expect.toBeVisible: Timeout 5000ms exceeded.
locator('.hidden-element') is hidden from accessibility tree
The element exists in the DOM but is hidden with CSS (display: none, visibility: hidden, opacity: 0, or width/height: 0).
Step-by-Step Fix
1. Use toBeVisible for visibility checks
// RIGHT — toBeVisible checks actual visibility
import { test, expect } from '@playwright/test';
test('button is visible after click', async ({ page }) => {
await page.goto('https://example.com');
await page.getByRole('button', { name: 'Show' }).click();
await expect(page.getByText('Hidden content')).toBeVisible();
});
2. Use toBeHidden for absence checks
// RIGHT — check element is hidden
test('modal is hidden initially', async ({ page }) => {
await page.goto('https://example.com');
await expect(page.locator('.modal')).toBeHidden();
// Or check element doesn't exist in DOM
await expect(page.locator('.modal')).not.toBeVisible();
});
3. Check element presence vs visibility
// RIGHT — distinguish existence from visibility
test('check element states', async ({ page }) => {
// Element exists in DOM but is hidden
await expect(page.locator('#loading-spinner')).toBeHidden();
// Element does not exist in DOM
await expect(page.locator('#results')).not.toBeAttached();
// Element exists and is visible
await expect(page.locator('#header')).toBeVisible();
});
4. Wait for visibility changes
// RIGHT — wait for element to become visible
test('wait for element to appear', async ({ page }) => {
await page.getByRole('button', { name: 'Load' }).click();
// Playwright auto-waits for visibility
await expect(page.locator('.results')).toBeVisible({ timeout: 10000 });
});
Expected output:
✓ button is visible after click (0.8s)
✓ modal is hidden initially (0.4s)
✓ check element states (0.5s)
Prevention Tips
- Use
toBeVisible()for visibility checks (not existence) - Use
toBeHidden()to verify hidden elements - Use
not.toBeAttached()to verify DOM absence - Playwright auto-waits for visibility before actions
- Set appropriate timeouts for async visibility changes
Common Mistakes with assert visible
- Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists
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