Skip to content

Automating your first test with Playwright and Python

Matthew Heusser Jan 11, 2022 Automation

If you are a Python programmer and you need a browser automation framework for end-to-end testing, you might consider adding Playwright to your project. Playwright is a cross-platform, cross-browser automation platform that prioritizes reliability, capability, and speed. The next question is where to get started - the step-by-step walkthrough. By the end of this article, that's exactly what you'll have; follow along to create your first test in Playwright with Python.

Assumptions and Requirements

This guide is aimed at programmers. A knowledge of Python 3 is not required, as the tutorial provides the exact code to use. The problem for people new to testing will be the next step, building more complex tests, building a proper suite of tests, and structuring the test code as it grows.

The examples in this tutorial were written for Python 3.8. If you don't have Python 3.8+ already, you can install it on Windows, *nix, and macOS. After the install, running python3 -V from the command line should result in a version of 3.8.2 or higher. Once a proper version of Python is installed, it is time to download and install Playwright.

Installing the software

There are just three steps to set up Playwright on a development machine. The script below uses pip3, the built-in Python package installer, to download and install Playwright, then has Playwright download browser binaries for Chromium, Firefox, and Webkit. Once that is done the setup script installs an extension for Playwright testing called pytest-playwright. If you are already writing unit tests in pytest, then Playwright will feel like a simple extension to unit testing, with the browser as an "object" you can send commands to and evaluate results.

pip3 install playwright
playwright install
pip3 install pytest-playwright

Sanity check the installation

With Playwright installed, it's possible to create a program that launches a browser. The WonderProxy folder for this on Github has a simple program,, to check the Playwright install.

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False, slow_mo=500)
    page = browser.new_page()

Run the program with python3, which will launch a Chromium browser, sending it to the Playwright homepage.

You can check the versions of pytest by running pytest from the command line. With no python files in the directory, pytest will generate something like this:

mheusser@Matthews-MBP-2 Shared % pytest
test session starts
platform darwin -- Python 3.8.2, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/Shared
plugins: playwright-0.2.2, base-url-1.4.2
collected 0 items

no tests ran in 0.01s
mheusser@Matthews-MBP-2 Shared %

Once Pytest and Playwright are installed and functional, you can combine them into an actual test.

Your first Playwright test

Here's the code for You can cut/paste and save to a local file, view the folder on Github, or download the entire Github project.

import pytest

def test_home_page_title(page):
    # Simple test, load homepage and check title
    assert page.title() == 'Localization testing with confidence - WonderProxy'

@pytest.mark.parametrize("test_input", ["lansing", "orlando", "perth", "knoxville"])
def test_check_server_status(page, test_input):
    # Check server status for multiple servers
    server_status = page.inner_text("//a[@href='/servers/" + test_input + "']/ancestor::li/span[5]")
    assert server_status == "up"

The code above creates a browser, sends it to a page, then checks the status on four different servers, making sure they are "up." Pytest will run these tests automatically, as long as the test filename begins with test_ and ends in .py, and Pytest runs in the same directory as the test code. Here's some sample output.

test session starts
platform darwin -- Python 3.8.2, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/mheusser/Desktop/code/example
plugins: playwright-0.2.2, base-url-1.4.2
collected 5 items .....
5 passed in 8.50s

Where to go next

Once the test passes, the next challenge is to apply the ideas to your application. To do that, review the Playwright documentation for Python and the documentation for the Pytest framework. If your code behaves differently based on the locale, you may want to simulate different locales in your tests automatically using Playwright and WonderProxy. While the examples are in NodeJS, the general principles apply to Python (and any other Playwright toolkit).

Matthew Heusser

The managing director of Excelon Development, Matt Heusser writes and consults on software delivery with a focus on quality.