Playwright with Python and pytest
The same Daily List test written in Python: install the pytest plugin, get a page for free, and learn the few names that change from the TypeScript version.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: the same recipe in a different kitchen. A cake recipe says to mix, bake and cool. In a new kitchen the oven has other knobs and the bowls sit in other cupboards. The cake steps stay the same. Only the names of the tools change.
Leo works on a team that writes Python. Tom has seen the Daily List add-a-task test in TypeScript. Daily List is a to-do list app. The steps are the same in Python: open the site, type a title, press the button, check the task.
The picture has limits. A cake recipe is the same in every kitchen. A test also depends on how the runner starts and how it hands over tools. Those parts differ more than the names do.
What it is: Playwright is a free library from Microsoft that drives a real browser from code. It comes in more than one language. This topic is the Python slice. It uses pytest, a that finds tests, runs them and reports the result. The connects the two. You install it with pip install pytest-playwright, then run playwright install once per machine. The sync API is the form where each call finishes before the next starts. API means application programming interface. There is no await. A pytest fixture such as page hands the test a fresh browser tab. The nearest neighbour is the TypeScript test. The journey matches. The spellings and the runner do not.
Why it matters: A Python team should not switch languages just to test the browser. Leo can review get_by_role in the language he already reads. If Tom pastes the TypeScript names, or uses a capital letter for a browser id, the run fails before it tests Daily List. The browser ids are lowercase: chromium, firefox and webkit.
Kept simple: Playwright works the same way underneath in every language. This lesson shows only the Python way.
(SYNTHETIC / TRAINING ONLY: Daily List and every person named in this lesson are invented.)
PLAIN WORDS · 4 in this section
- typescript
- JavaScript with added labels for the kinds of data
- playwright
- a free Microsoft tool that drives a browser for tests
- pytest
- A free Python tool that finds and runs checks.
- lowercase
- small letters, not capitals
PICTURE IT
The picture is the one the lesson above opens with. The table below shows how each part of it matches the software.
THE REAL JOB Writing the same in Python, with the same steps and different names.
- Install
- Name the test
- Ask for page
- Run
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| The same recipe | The same open, fill, click and check steps |
| A different kitchen | Python with pytest in place of TypeScript |
| The new oven knobs | The pytest runner and its flags |
| Tool names with new spelling | The names with underscores |
WHERE THIS PICTURE STOPS
- A recipe does not change when the kitchen changes, but a runner changes how tests are found and set up.
- A cook can see the whole kitchen, while a test sees only what the plugin hands over.
- Knobs are the same on every cooker of one kind, and flags are the same for every run of one runner.
HOOK
Same steps, new spelling.
REMEMBER IT THIS WAY
A sequence: the real steps, in order.
Same steps, new spelling: open, fill, click, check. In Python the tool names use underscores.
| In the trick | What it stands for |
|---|---|
| open | open the site |
| fill | type a title |
| click | press the button |
| check | check the task |
TELL A FRIEND
The Python test has the same steps as the TypeScript one, but it uses pytest and snake case names. A failed setup can leave server data behind, so each change needs its own cleanup.
OFTEN MIXED UP WITH Automation frameworks overview. The earlier lesson compares kinds of automation frameworks. This lesson takes one tool in one language and shows the runner that goes with it.
Sources & standards — 6 sources
- Market practice
Pytest plugin reference (Playwright documentation for Python) ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: Complete extracted primary article; examples inspected, not executed. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.
Primary source opened and operative text inspected on 6 November 2026. Scope: Complete extracted primary article; examples inspected, not executed. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.
- Market practice
Get started (pytest documentation) ↗ — pytest development team · pytest finds test functions that start with test_ in files named test_.py or _test.py, and a plain assert is enough (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
How to parametrize fixtures and test functions (pytest documentation) ↗ — pytest · A test function can be run with many sets of values by marking it with parametrize (registry source; search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Running tests across multiple CPUs (pytest-xdist documentation) ↗ — pytest-xdist · The pytest-xdist plugin spreads tests across several processes (registry source; search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Getting started - Library | Playwright Python ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: Complete extracted primary article; examples inspected, not executed. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.
Primary source opened and operative text inspected on 6 November 2026. Scope: Complete extracted primary article; examples inspected, not executed. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.
- Market practice
How to use fixtures (pytest documentation) ↗ — pytest · Reviewed 6 November 2026: Handling errors and safe teardown only. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.
Primary source opened and operative text inspected on 6 November 2026. Scope: Handling errors and safe teardown only. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.
PRACTISE AND KEEP
Sources for this topic — 7 sources
- Market practice
Pytest plugin reference (Playwright documentation for Python) ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: Complete extracted primary article; examples inspected, not executed. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.
Primary source opened and operative text inspected on 6 November 2026. Scope: Complete extracted primary article; examples inspected, not executed. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.
- Market practice
Get started (pytest documentation) ↗ — pytest development team · pytest finds test functions that start with test_ in files named test_.py or _test.py, and a plain assert is enough (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
How to parametrize fixtures and test functions (pytest documentation) ↗ — pytest · A test function can be run with many sets of values by marking it with parametrize (registry source; search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Running tests across multiple CPUs (pytest-xdist documentation) ↗ — pytest-xdist · The pytest-xdist plugin spreads tests across several processes (registry source; search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Implementation-specific
How to use fixtures (pytest documentation) ↗ — pytest development team · How to use fixtures: Handling errors for yield fixture; Safe teardowns; Safe fixture structure. Primary body opened 22 October 2026.
Primary page opened on the access date. Read the sections on yield errors and safe cleanup. Tool behavior only; no numbered release or guarantee that cleanup always succeeds is claimed.
- Market practice
Getting started - Library | Playwright Python ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: Complete extracted primary article; examples inspected, not executed. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.
Primary source opened and operative text inspected on 6 November 2026. Scope: Complete extracted primary article; examples inspected, not executed. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.
- Market practice
How to use fixtures (pytest documentation) ↗ — pytest · Reviewed 6 November 2026: Handling errors and safe teardown only. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.
Primary source opened and operative text inspected on 6 November 2026. Scope: Handling errors and safe teardown only. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.
Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.