Frontend Development 5 min read

Using Playwright's expect API for Web Page Assertions

This article explains how to use Playwright's expect methods to assert page URL, title, and element visibility, provides Python async code examples for common assertions, and lists the full set of locator‑based expect functions for comprehensive web testing.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Using Playwright's expect API for Web Page Assertions

Playwright provides an expect method that can be used to assert various page states such as whether the page URL matches an expected value, whether the page title matches, and whether an element matched by a CSS selector is visible.

Example code (Python async) demonstrates opening a page with page.goto() , calling a test_assertions function that uses assert statements with page.url() , page.title() , and page.is_visible() , and finally closing the browser:

import asyncio
from playwright.async_api import async_playwright

async def test_assertions(page):
    # Assert page URL
    assert page.url() == 'https://www.example.com'
    # Assert page title
    assert page.title() == 'Example Domain'
    # Assert element visibility
    assert page.is_visible(selector='div')

async def main():
    async with async_playwright() as playwright:
        page = await playwright.chromium().new_page()
        await page.goto('https://www.example.com')
        await test_assertions(page)
        await page.close()

asyncio.run(main())

Playwright also offers a rich set of locator‑based expect methods, including Expect(Locator).toBeCheckedAsync() , toBeDisabledAsync() , toBeEditableAsync() , toBeEmptyAsync() , toBeEnabledAsync() , toBeFocusedAsync() , toBeHiddenAsync() , toBeVisibleAsync() , toContainTextAsync() , toHaveAttributeAsync() , toHaveClassAsync() , toHaveCountAsync() , toHaveCSSAsync() , toHaveIdAsync() , and toHaveJSPropertyAsync() . These can be used to verify checkboxes, disabled state, editability, emptiness, enablement, focus, visibility, text content, attributes, classes, counts, CSS values, IDs, and JavaScript properties of elements.

A second code example shows how to assert that a page title equals an expected string and that a specific element is visible:

import asyncio
from playwright.async_api import async_playwright

# Define a test function to verify title and element existence
async def test_example(page):
    # Assert page title
    assert page.title() == "Expected Page Title"
    # Assert element visibility
    assert await page.locator("element_selector").is_visible()

async def main():
    async with async_playwright() as playwright:
        page = await playwright.chromium().new_page()
        await page.goto("https://www.example.com")
        await test_example(page)
        await page.close()

asyncio.run(main())

In this example, the main function creates a new browser page, navigates to the target URL, runs the test_example function, and closes the page; any failed assertions cause the test to fail and report an error.

PythonAutomationPlaywrightweb testingAssertions
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.