Playwright Python vs JavaScript
Playwright in Python and JavaScript
Playwright for Python and Playwright for JavaScript are the same tool, used from two different languages.
Both control the same browsers through the same engine, and both have the same features.
Locators, assertions, auto-waiting, tracing and network mocking work the same way in both languages.
The differences are in how the code is written.
This chapter lists them, so you can follow the JavaScript chapters of this tutorial in Python.
Name Comparison
Python uses snake_case names where JavaScript uses camelCase.
Options that JavaScript passes in an object are keyword arguments in Python.
| JavaScript | Python |
|---|---|
| page.getByRole('button', { name: 'Add' }) | page.get_by_role("button", name="Add") |
| page.getByPlaceholder('Email') | page.get_by_placeholder("Email") |
| page.getByTestId('todo-count') | page.get_by_test_id("todo-count") |
| await expect(locator).toHaveText('1 item left') | expect(locator).to_have_text("1 item left") |
| await expect(locator).toBeVisible() | expect(locator).to_be_visible() |
| await expect(locator).not.toBeVisible() | expect(locator).not_to_be_visible() |
| await expect(page).toHaveURL(/login/) | expect(page).to_have_url(re.compile("login")) |
| await page.screenshot({ path: 'a.png', fullPage: true }) | page.screenshot(path="a.png", full_page=True) |
| await page.waitForURL('**/done') | page.wait_for_url("**/done") |
| true / false / null | True / False / None |
The rule is simple: write the JavaScript name in snake_case, and you have the Python name.
Tests, Await and Running
| JavaScript | Python | |
|---|---|---|
| Import | import { test, expect } from '@playwright/test' | from playwright.sync_api import Page, expect |
| Test runner | Playwright Test (built in) | pytest with the pytest-playwright plugin |
| A test | test('name', async ({ page }) => { ... }) | def test_name(page: Page): |
| Await | await in front of every action and assertion | None in the sync API |
| Test files | *.spec.js | test_*.py |
| Run the tests | npx playwright test | pytest |
| Headed | npx playwright test --headed | pytest --headed |
| Choose a browser | --project=chromium | --browser chromium |
| Settings | playwright.config.js | pytest.ini, conftest.py and command line options |
| Base URL | use: { baseURL: '...' } in the config | --base-url ... |
| Trace | use: { trace: 'on' } in the config | --tracing on |
| Record code | npx playwright codegen | playwright codegen --target python-pytest |
The playwright command in Python has the same tools as npx playwright: codegen, show-trace, install and more.
Codegen writes plain Python scripts by default.
Add --target python-pytest to get pytest tests, or --target python-async for the async API.
The Same Test in Both Languages
This test adds a todo, marks it as done, and checks that the Done filter shows it.
It also sets the size of the browser window.
In JavaScript:
Example: JavaScript
tests/todo.spec.js
// @ts-check
import { test, expect } from '@playwright/test';
test.use({ viewport: { width: 800, height: 600 } });
test('can complete a todo', async ({ page }) => {
await page.goto('https://www.w3schools.com/playwright/practice/todo.html');
await page.getByPlaceholder('What needs to be done?').fill('Learn Playwright');
await page.getByRole('button', { name: 'Add' }).click();
await page.getByRole('checkbox', { name: 'Complete Learn Playwright' }).check();
await page.getByRole('button', { name: 'Done' }).click();
await expect(page.getByRole('checkbox')).toHaveCount(1);
});
In Python:
Example: Python
tests/test_todo.py
from playwright.sync_api import Page, expect
def test_can_complete_todo(page: Page):
page.goto("https://www.w3schools.com/playwright/practice/todo.html")
page.get_by_placeholder("What needs to be done?").fill("Learn Playwright")
page.get_by_role("button", name="Add").click()
page.get_by_role("checkbox", name="Complete Learn Playwright").check()
page.get_by_role("button", name="Done").click()
expect(page.get_by_role("checkbox")).to_have_count(1)
Example: Python
tests/conftest.py
import pytest
@pytest.fixture(scope="session")
def browser_context_args(browser_context_args):
return {**browser_context_args, "viewport": {"width": 800, "height": 600}}
Both tests pass:
Output:
Running 1 test using 1 worker
[1/1] [chromium] › tests/todo.spec.js:6:1 › can complete a todo
1 passed (2.5s)
Output:
============================= test session starts =============================
platform win32 -- Python 3.9.13, pytest-8.4.2, pluggy-1.6.0
rootdir: /home/user/my-project
plugins: base-url-2.1.0, playwright-0.7.1
collected 1 item
tests/test_todo.py . [100%]
============================== 1 passed in 2.23s ==============================
The lines inside the two tests are the same, line by line.
The setting is where they differ: JavaScript uses test.use(), and Python overrides a fixture (replaces it with its own version, see below).
Fixtures
Both test runners use fixtures: the test asks for page, and the runner creates it.
In Python, these are normal pytest fixtures, and you put your own in conftest.py.
pytest-playwright gives you these fixtures:
| Fixture | Description |
|---|---|
| page | A new tab in a new context, for each test |
| context | A new browser context (a separate browser session with its own cookies and storage), for each test |
| browser | The browser, shared by all tests in the run |
| browser_name | The name of the browser: "chromium", "firefox" or "webkit" |
| browser_context_args | The options for every new context, such as viewport and base_url. Override it to change them |
| browser_type_launch_args | The options for launching the browser. Override it to change them |
The conftest.py above overrides browser_context_args.
It takes the default options and adds a viewport, so every test in the folder gets an 800 x 600 window.
The Concepts Are the Same
Everything you learn in the JavaScript chapters works in Python too.
Read them for the ideas, and write the code with the Python names:
- Locators and Filter Locators: the same
get_by_*methods, filters and chaining - Actions:
fill(),click(),check(),select_option()and the rest - Assertions:
expect()withto_have_text(),to_be_visible()and more - Auto-Waiting: Python waits for elements the same way, with the same 5 second assertion timeout
- Codegen: record your actions as Python code
- Trace Viewer: the same viewer opens Python traces
- Page Object Model: page classes are Python classes
- Network Mocking:
page.route()works the same way - API Testing: the request API is there in Python too
The Python reference lists every method with its Python name: playwright.dev/python.