Introduction to Playwright
Playwright drives a real web browser from your code. Your tests can open a page, click things and check what the user sees.
Here’s how it works:
- You find an element on the page.
- You do something to it, like click it.
- You check that the page now looks the way it should.
Why unit tests aren’t enough
You might already know Django’s test client. It sends a request to a view and gives you back the response:
def test_toggle(client):
item = TodoItem.objects.create(title="Buy milk")
response = client.post(f"/todo/{item.pk}/toggle/")
assert "line-through" in response.content.decode()This test is useful. It proves the view saves the change and sends back the right HTML. But it never runs a browser, so a lot can break without it noticing:
- If the checkbox’s
hx-postpoints at the wrong URL, nothing is saved, and the test still passes. - If
hx-targetpoints at the wrong element, the new list lands in the wrong place, and the test still passes. - If HTMX or Alpine fails to load, nothing on the page reacts to clicks, and the test still passes.
- Alpine’s
x-showhides descriptions in the browser. The HTML from the server always contains them. The test client can’t tell you whether a description is visible.
In goals 1 to 4, a lot of our app’s behaviour moved into HTML attributes that only do something inside a browser. To test that behaviour, we need a browser.
No. Unit tests are fast, and they’re the right tool for models, forms and views. Browser tests are slow, because they start a real browser. Use them to check that the pieces work together in the browser, and use unit tests for the rest.
You don’t need to memorise this
Playwright code can feel like a whole new language at first. You don’t need to learn it by heart. Playwright has tools built in that help you write it, and we’ll use one of them, codegen, on the next page.
For now, focus on what Playwright can do. The next three sections cover the basics: finding elements, interacting with them, and checking that they have the state or value you expect.
1. Find an element
A locator is how Playwright finds an element on the page. The best locators find elements the way a user would, by what they say or what they are:
page.get_by_role("button", name="Add")
page.get_by_text("Buy milk")get_by_rolefinds an element by its role, likebutton,checkboxorheading.get_by_textfinds an element by the text inside it.
You can also use a CSS selector with locator, and narrow a locator down with filter:
item = page.locator("#todo-list > div").filter(has_text="Buy milk")
item.get_by_role("checkbox")The first line finds the todo item that contains “Buy milk”. The second line finds the checkbox inside that item.
2. Do something
Once you have a locator, you can act on it, just like a user would:
page.get_by_role("button", name="Add").click()
page.get_by_label("Title").fill("Buy milk")See actions for the full list.
3. Check the result
Use expect to check what’s on the page:
expect(page.get_by_text("Buy milk")).to_be_visible()
expect(page.get_by_role("checkbox")).to_be_checked()expect waits. If the page isn’t right yet, Playwright keeps checking for a few seconds before it gives up. That matters for us, because HTMX takes a moment to get a response from the server and swap it in. You don’t need to add any waiting code yourself.
Running Playwright with pytest
We use Playwright through pytest, with two plugins:
- pytest-playwright gives each test a
pagefixture: a fresh browser tab, ready to use. - pytest-django gives us a
live_serverfixture: a real Django server, running in the background while the tests run, with its own empty test database.
A test asks for both fixtures by name:
def test_home_page_loads(page: Page, live_server):
page.goto(live_server.url)
expect(page.get_by_role("heading", name="Get this done")).to_be_visible()Headless and headed
By default, Playwright runs the browser in headless mode. The browser runs, but with no window, so you don’t see anything. That’s faster, and it’s what you want most of the time, and on a server that has no screen at all.
In headed mode, the browser window opens and you can watch your test click through the page. That’s handy when a test fails and you want to see why.