Installing Playwright and Creating Your First Test

Getting Playwright running locally is the first step toward using it in real projects. A clean installation and a simple first test help you verify that browsers, drivers, and the test runner are all wired correctly.

Installing Playwright and Scaffolding a Project

Playwright projects are usually TypeScript or JavaScript Node.js projects. The recommended way to start is with the official init command, which sets up the test runner, configuration, and example tests for you.

# 1. Create a new folder and initialise a Node.js project
mkdir playwright-demo
cd playwright-demo
npm init -y

# 2. Install Playwright test runner
npm install -D @playwright/test

# 3. Install browsers and scaffold config
npx playwright install
npx playwright test --init

# 4. Run the example tests
npx playwright test

# 5. Open the HTML report
npx playwright show-report
Note: The npx playwright test --init command creates a standard folder structure (tests, playwright.config, example tests) that follows Playwright’s best practices.
Tip: Commit your generated playwright.config and example tests to version control so new team members can run npx playwright test immediately after cloning.
Warning: Avoid installing Playwright globally; keep it as a dev dependency in each project so versions can be upgraded and tracked per repository.

After running the example suite, you can rename the sample spec file and start adding your own tests. This iterative approach helps you evolve the project without losing the scaffold’s structure.

Common Mistakes

Mistake 1 β€” Skipping the official init flow

This leads to ad hoc setups.

❌ Wrong: Manually creating random folders and configs with no standard layout.

βœ… Correct: Use the built-in init command and then customise as needed.

Mistake 2 β€” Forgetting to install browsers

Tests then fail unexpectedly.

❌ Wrong: Installing @playwright/test but never running npx playwright install.

βœ… Correct: Always install the browser binaries once per environment before running tests.

🧠 Test Yourself

What is the recommended way to start a new Playwright project?