Manuals / Playwright with Python / Ch 7

Part 1 · FoundationsBeginner45 min read

2. Environment Setup

Playwright with Python · 244 pages source format

venv hygiene, install pytest-playwright + browser binaries, and a starter folder structure that won’t fight you when POM arrives.

What you'll learn

  • Always use a virtual environment
  • pip install vs playwright install
  • Starter project layout

Python, pip, and virtual environments

Assuming Python is installed, the critical habit here is virtual environments (venv). Every project should get its own isolated environment.

Create and activate a venv
python -m venv venv
source venv/bin/activate      # Mac/Linux
venv\Scripts\activate         # Windows
Interactive study board
Python, pip, and virtual environmentsDrag 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?

Installing Playwright + browser binaries

Two separate steps that beginners often miss the distinction between.

Package, then browsers
pip install pytest-playwright
playwright install

Pro tip. Playwright ships its own browsers on purpose — same version on every laptop and CI machine.

Interactive study board
Installing Playwright + browser binariesDrag 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?

Project folder structure

Even a simple starting structure pays off later (preview of Chapter 14’s POM and Chapter 29’s scalable architecture). Starting with this loose structure — rather than dumping every test file flat in one folder — means you won’t need a painful reorganization once the suite grows past a handful of tests.

Starter layout
project/
├── tests/
├── pages/          # page object classes — comes later
├── conftest.py
├── pytest.ini
└── requirements.txt

Do this now

Create this folder skeleton in a new repo and commit an empty tests/.gitkeep.

Interactive study board
Project folder structureDrag 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