TOPIC 37/6018 MINNOT STARTED

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.

  1. Install
  2. Name the test
  3. Ask for page
  4. Run
Same recipe, new kitchen

HOW THE PICTURE MAPS

How each part of the picture matches the software
In the pictureIn the software
The same recipeThe same open, fill, click and check steps
A different kitchenPython with pytest in place of TypeScript
The new oven knobsThe pytest runner and its flags
Tool names with new spellingThe 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.

What each part of the trick stands for
In the trickWhat it stands for
openopen the site
filltype a title
clickpress the button
checkcheck 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
  1. 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.

    Says the plugin gives a page fixture (a new page for each test), a browser_name fixture and is_chromium, is_firefox and is_webkit flags; says --headed shows the browser, --browser picks chromium, firefox or webkit and can be repeated; says the options apply only to the default browser, context and page fixtures and can be set in pytest.ini with addopts. Vendor documentation for one tool. Practitioner guidance, not a standard. · Checked 2026-11-06

    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.

  2. 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)

    Says pytest finds test functions whose names start with test_ in files named test_.py or _test.py, and that a plain assert statement is enough because pytest reports the values behind a failed assert. Official language or tool documentation. · Checked 2026-10-18

    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.

  3. 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)

    Says the parametrize marker defines several argument sets so one test function runs once per set, and that fixtures can also be parametrized. Practitioner or vendor guidance, not a standard unless stated. · Checked 2026-10-18

    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.

  4. 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)

    Explains how a plugin spreads tests across worker processes, with modes that send tests to any free worker or keep tests of one file or scope together on the same worker. Practitioner or vendor guidance, not a standard unless stated. · Checked 2026-10-18

    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.

  5. 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.

    Complete extracted primary article; examples inspected, not executed · Checked 2026-11-06

    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.

  6. 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.

    The pytest how-to guide on fixtures: a session-scoped fixture is destroyed at the end of the test session, code after yield is the clean-up, higher-scoped fixtures are set up first, and fixtures should not be imported into test modules. · Checked 2026-11-06

    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
  1. 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.

    Says the plugin gives a page fixture (a new page for each test), a browser_name fixture and is_chromium, is_firefox and is_webkit flags; says --headed shows the browser, --browser picks chromium, firefox or webkit and can be repeated; says the options apply only to the default browser, context and page fixtures and can be set in pytest.ini with addopts. Vendor documentation for one tool. Practitioner guidance, not a standard. · Checked 2026-11-06

    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.

  2. 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)

    Says pytest finds test functions whose names start with test_ in files named test_.py or _test.py, and that a plain assert statement is enough because pytest reports the values behind a failed assert. Official language or tool documentation. · Checked 2026-10-18

    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.

  3. 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)

    Says the parametrize marker defines several argument sets so one test function runs once per set, and that fixtures can also be parametrized. Practitioner or vendor guidance, not a standard unless stated. · Checked 2026-10-18

    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.

  4. 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)

    Explains how a plugin spreads tests across worker processes, with modes that send tests to any free worker or keep tests of one file or scope together on the same worker. Practitioner or vendor guidance, not a standard unless stated. · Checked 2026-10-18

    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.

  5. 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.

    How pytest handles setup failures, post-yield cleanup and cleanup of earlier completed fixtures. · Checked 2026-10-22

    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.

  6. 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.

    Complete extracted primary article; examples inspected, not executed · Checked 2026-11-06

    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.

  7. 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.

    The pytest how-to guide on fixtures: a session-scoped fixture is destroyed at the end of the test session, code after yield is the clean-up, higher-scoped fixtures are set up first, and fixtures should not be imported into test modules. · Checked 2026-11-06

    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.