top of page

Playwright Python expect Assertions: Build and Verify a Search Test

12 hours ago
3 min read

In the last lesson, the test clicked a button but didn't check anything. A test that doesn't verify anything isn't really a test. In this lesson you build a complete search scenario on the demo shop and verify the results with Playwright's expect assertions.



Before you start: this builds on locators and auto-waiting. If you haven't covered those yet, read Playwright Python Locators and Auto-Waiting first.


The scenario

Searching for a product is one of the most common things a user does in a shop, so that's what you automate:

  1. Open the demo shop

  2. Type a keyword into the search box

  3. Click the search button

  4. Check that search results are shown


Step 1: Type into the search box

In tests/concepts, create a new file called test_search.py. Start the same way as before: define a test function and open the demo shop.

To find the search field, inspect it in the browser. Its role is searchbox, so get_by_role finds it, and .fill() types the keyword. Auto-waiting makes sure the field is ready before Playwright types into it.

def test_search_products(page):
    page.goto("https://qa-cart.com/demoshop/")
    page.get_by_role("searchbox").fill("fresh")

Step 2: Click the search button

The search button has the role button and the name search, so it's the same pattern you used for Add To Cart:

    page.get_by_role("button", name="search").click()

After the click, the shop shows the search results page. Now you need to verify it.


Step 3: Verify with expect

Interacting with the page isn't enough. You need to check that something actually happened. Playwright has its own assertions for this. Import expect at the top of the file:

from playwright.sync_api import expect

This imports expect from Playwright's Python library. It's Playwright's built-in way of writing assertions.

expect or assert?

Python's plain assert, which you used in Lesson 2, still works. But assert is generic: you can use it anywhere, in unit tests, API tests or any Python code. expect is designed for UI testing, and that makes a real difference, as Step 4 shows.

Find the text with get_by_text

What you want to verify is that the text Search results appears. get_by_text finds an element by the text it contains. It can match a substring, an exact string or a regular expression. Here you pass the substring Search results, wrap the locator in expect, and check that it's visible:

    expect(page.get_by_text("Search results")).to_be_visible()

Step 4: Why expect waits, and assert doesn't

This connects to auto-waiting from the last lesson. expect automatically waits for the condition to become true. After you click search, the results might take a moment to load. Playwright doesn't check once and give up. It keeps retrying until the text is visible, or until the timeout runs out.

  • If Search results appears within the timeout, the test passes.

  • If it doesn't appear in time, the test fails.

Playwright doesn't only wait before actions like click. It also waits during assertions.

So the test is fill, click, then expect, with no manual waits anywhere. There's also a short video on expect vs assert that shows the difference side by side.


Step 5: Run it and fix the strict mode violation

Save the file and run the test headed, with a delay so you can watch it:

pytest tests/concepts/test_search.py --headed --slowmo 500

It fails, with an error you have seen before: a strict mode violation, because the text resolves to two elements. On the results page, Search results appears twice, once in the breadcrumb and once in the heading.

As in the last lesson, be specific. For now, take one of them with .last:

    expect(page.get_by_text("Search results").last).to_be_visible()

Run it again and the test passes. Here is the complete test:

from playwright.sync_api import expect

def test_search_products(page):
    page.goto("https://qa-cart.com/demoshop/")
    page.get_by_role("searchbox").fill("fresh")
    page.get_by_role("button", name="search").click()
    expect(page.get_by_text("Search results").last).to_be_visible()

This check is deliberately simple. It confirms the results section shows up. In a real project you would go further, for example checking that the products shown actually match the keyword.


What you covered

  • Building a full user scenario from locators: fill, click, verify

  • Importing and using expect, Playwright's assertion for UI tests

  • get_by_text to find an element by its text

  • expect retries until the condition is true or the timeout runs out

  • Fixing another strict mode violation with .last

Right now the test searches for one keyword, fresh. To try organic and farm as well, you don't need a separate test for each. The next lesson reuses the same test with different data, using Python lists and dictionaries.


The code for the whole course is in the playwright-python-course-yt repo on GitHub.


Get Started with Playwright Python: the free series

This post is Lesson 4 of my free Playwright Python course on YouTube.

bottom of page