top of page

Your First Playwright Python Test: Write It, Run It, Break It

12 hours ago
4 min read

In this lesson you write your first Playwright test in Python, run it, watch it pass, and then break it on purpose so you know what a failure looks like.

If you have used Playwright with TypeScript, you may expect an example test to start from. npm init playwright@latest generates one for you. pytest-playwright doesn't, so every test in this course is written from scratch, and you understand every line.



Before you start: you need Python, VS Code, an active virtual environment and pytest-playwright with its browsers installed. If you haven't done that yet, follow How to Install Playwright with Python first.


Step 1: Create the test folder and file

From your project's root folder, create a tests folder with a concepts folder inside it, in one step:

mkdir -p tests/concepts

The -p flag creates both folders at once. Inside tests/concepts, create a new file called test_homepage.py. Your first test goes here.


Step 2: What pytest does

pytest finds your test functions, runs them and reports which passed and which failed. It isn't specific to Playwright. It runs any Python test: unit tests, API tests, browser tests. Playwright plugs into it.

pytest is the runner. Playwright is what it runs.

Step 3: Write the test, line by line

Here is the complete test. It opens my demo store and checks the page title:

def test_homepage_loads(page):
    page.goto("https://qa-cart.com")
    assert page.title() == "QA TEST AUTOMATION DEMO STORE BY ANURADHA AGARWAL"

Now line by line, including the bits of Python syntax you need.

def and the test name

def is the Python keyword for defining a function. The name, test_homepage_loads, can be almost anything, but it must start with test. That prefix is how pytest finds your tests. A function without it is just a function, and pytest won't run it.

The colon and the indentation

The colon at the end of the def line and the indented lines below it are how Python marks code as belonging to the function. Remove the indentation and page.goto is no longer part of the test. Press Tab to indent.

The page parameter is a fixture

A parameter is an input the function expects before it runs. You never create page yourself. It's a fixture: a piece of setup pytest prepares and hands to any test that asks for it. You just write page in the parentheses and pytest does the rest.

This fixture comes from the pytest-playwright plugin you installed in Lesson 1. Behind the scenes it launches a browser, opens a fresh page, hands it to your test, and closes everything when the test finishes. You never write code to open or close the browser. The Playwright docs on the pytest plugin cover this in more detail.

page.goto

page.goto tells Playwright to open that URL in the browser.

assert and page.title()

page.title() reads the browser's current page title as plain text. To get the exact title, open the site and use View Page Source, then copy the text inside the title tag. The == operator compares two values for equality.

assert is plain Python, nothing specific to testing. It checks a condition. If the condition is true, nothing happens and execution continues. If it's false, Python raises an error, and pytest catches it and marks the test as failed.

Every check in this course, whether it's a title, a URL, text on the page or the number of items in a cart, comes down to this same pattern.


Step 4: Run the test

Save the file, then run it with pytest, pointing at the file (press Tab to autocomplete the path):

pytest tests/concepts/test_homepage.py

You'll see a green line saying passed. That's pytest's short way of saying the test ran and its assertion held.


Step 5: Break it on purpose

A test you have never seen fail is a test you can't fully trust. Comment out the real check with a hash and add one with a wrong title:

def test_homepage_loads(page):
    page.goto("https://qa-cart.com")
    # assert page.title() == "QA TEST AUTOMATION DEMO STORE BY ANURADHA AGARWAL"
    assert page.title() == "Wrong Title"

Save and run it again. This time the output is red. Here is how to read it:

  • pytest points to the exact line that failed.

  • It shows the two values side by side. The minus (-) line is what the test expected, here Wrong Title. The plus (+) line is what the page actually had.

  • The test name shows as test_homepage_loads[chromium]. pytest-playwright runs each test once per configured browser, and right now that's only Chromium. Running across more browsers comes later in the course.

Put the real assertion back before you move on.


What you covered

  • What pytest does: it finds, runs and reports tests

  • The naming rule: test functions start with test

  • What a fixture is, and where page comes from

  • How to read a failure: expected versus actual

Next you deal with the timing problems that make browser tests flaky, using locators and auto-waiting.


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

bottom of page