top of page

Data-Driven Testing in Playwright Python with Lists and Loops

12 hours ago
3 min read

In the last lesson, you searched the demo shop for one keyword, fresh, and verified that results appeared. Real testing goes further: you run the same scenario with different inputs. You might want to search for organic and farm too, but you don't need a new test for each one. You make the one test more flexible, using a Python list and a loop.



Before you start: this lesson reuses the search test from Playwright Python expect Assertions. Have that test working first.


Step 1: Start from the search test

In tests/concepts, create a new file called test_data_driven.py and copy in the search test from the last lesson:

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()

The keyword fresh is hardcoded. That's the part to change.


Step 2: Put the keywords in a list

At the start of the test, before the scenario, create a variable called keywords that holds all three values:

    keywords = ["fresh", "organic", "farm"]

This is a Python list: a collection of values. Here it stores the search keywords you want to try.


Step 3: Loop over the list

To run the scenario once for each keyword, use a for loop. It takes each value from the list in turn, runs everything inside the loop, then moves on to the next value:

    for keyword in keywords:

The loop variable can have any name. keyword just makes it readable. Indent the scenario under the for line so it belongs to the loop, then replace the hardcoded "fresh" with the variable:

from playwright.sync_api import expect

def test_search_products(page):
    keywords = ["fresh", "organic", "farm"]

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

On each pass, keyword holds the current value, so the search box gets fresh, then organic, then farm. The verification stays the same: the search results section is visible for each keyword.


Step 4: Run it

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

Watch the browser: it enters the three keywords one by one and repeats the same search steps for each. That is data-driven testing. Instead of writing three tests, you write one test and pass it several inputs.

Keep the test data separate from the test logic. To try a new keyword, you add it to the list. The steps don't change.

When a list is enough, and when you need a dictionary

A list works here because only one thing changes: the search keyword. And the check is simple, the results section is visible.

A stronger test would also check what each search returns. On the demo shop, the results heading includes the keyword you searched for, and the products match it. To verify that, each input needs its own expected value. That's when a Python dictionary helps, because it keeps an input and its expected outcome together. This lesson stays with the list, and the video shows where a dictionary would fit.


What you covered

  • Storing test data in a Python list

  • Using a for loop to run the same scenario for every value

  • Replacing a hardcoded value with the loop variable

  • What data-driven testing is: one test, many inputs

  • When you would reach for a dictionary instead

Next you look at what happens after a run: the HTML report and the Trace Viewer, for when a test fails and you need to see why.


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 5 of my free Playwright Python course on YouTube.

bottom of page