Skip to content

Playwright Visibility Assertion Not Working Fix

DodaTech Updated 2026-06-24 3 min read

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

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead 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

### What does "visible" mean in Playwright?

An element is visible when it has non-zero size, isn't hidden by CSS (display: none, visibility: hidden, opacity: 0), and is within the viewport. Elements behind other elements are still considered visible.

What's the difference between toBeVisible and toBeAttached?

toBeVisible checks the element is visible on the page. toBeAttached checks the element exists in the DOM regardless of visibility. Use toBeAttached when you need to confirm an element exists even if hidden.

How do I check if an element exists in the DOM?

Use await expect(locator).toBeAttached() to check existence, or await expect(locator).not.toBeAttached() to verify absence. Count locators with await expect(locator).toHaveCount(0).

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro