Manuals / Playwright with Python / Ch 31

Part 4 · Advanced TechniquesAdvanced40 min read

23. Cross-browser & Cross-device Testing

Playwright with Python · 244 pages source format

Running on Chromium, Firefox, WebKit pytest --browser chromium pytest --browser firefox pytest --browser webkit Or parametrized directly in code/config to run against all three in one CI pipeline run. --browser (pytest-playwright CLI flag) What it does: Selects which browser engine the test session launches.

What you'll learn

  • Running on Chromium, Firefox, WebKit
  • Mobile emulation (device descriptors, viewport, geolocation)

Running on Chromium, Firefox, WebKit

Or parametrized directly in code/config to run against all three in one CI pipeline run.

--browser (pytest-playwright CLI flag)

What it does: Selects which browser engine the test session launches.

Types/params: "chromium" (default), "firefox", "webkit".

Pointers: Running the full suite three times (once per browser) in CI catches engine-specific bugs before real users do — a common setup is a CI matrix job that runs the same suite once per browser value in parallel.

Run / study this snippet
pytest --browser chromium
pytest --browser firefox
pytest --browser webkit
Interactive study board
Running on Chromium, Firefox, WebKitDrag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Page under testdrag · tap →Multi-browserdrag · tap →Automation pathdrag · tap →Tooling nodedrag · tap →
Clear?

Mobile emulation (device descriptors, viewport, geolocation)

iphone = p.devices["iPhone 13"] context = browser.new_context(**iphone) page = context.new_page()

Interactive study board
Mobile emulation (device descriptors, viewport, geolocation)Drag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Page under testdrag · tap →Multi-browserdrag · tap →Automation pathdrag · tap →Tooling nodedrag · tap →
Clear?

page.goto("https://app.example.com")

context = browser.new_context(

viewport={"width": 390, "height": 844}, geolocation={"latitude": 27.7172, "longitude": 85.3240}, permissions=["geolocation"],

)

playwright.devices["<device name>"]

What it does: A dictionary of preset settings (viewport, user-agent, touch support, device pixel ratio) for a named real device.

Types/params: String key matching a supported device name, e.g. "iPhone 13",

"Pixel 5", "iPad Pro 11".

Pointers: Unpack it directly into new_context(**device_dict) — no need to set each property manually. Full list of supported devices is in Playwright's own source/docs since it's updated over time.

permissions=...)

What it does: Sets custom device-like properties without using a full preset.

Types/params:

Pointers: Forgetting permissions=["geolocation"] is a common gotcha — setting geolocation alone does nothing if the page's geolocation request isn't permitted.

  • viewport (dict {width, height}, optional)
  • geolocation (dict {latitude, longitude}, optional)
  • permissions (list of strings, optional) — must explicitly grant "geolocation" or the browser will block the geolocation API regardless of the coordinates provided
Run / study this snippet
# Custom viewport and geolocation, without a full device descriptor
Interactive study board
page.goto("https://app.example.com")Drag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Page under testdrag · tap →Multi-browserdrag · tap →Automation pathdrag · tap →Tooling nodedrag · tap →
Clear?

Checklist