Skip to main content

๐Ÿ“ฆ Module 2 โ€“ Installing Playwright

Module Overview
DetailInfo
DifficultyBeginner
Time Requiredโฑ๏ธ 15-20 minutes
PrerequisitesNode.js installed
OutcomeFully functional Playwright test project

โœ… Learning Objectivesโ€‹

After completing this module, you will be able to:

  • Create a new Playwright project structure
  • Initialize Node.js configuration
  • Install Playwright with browser support
  • Configure test settings properly
  • Run your first automated test suite
  • Understand the folder structure of a Playwright project

๐Ÿ› ๏ธ Prerequisites Checkโ€‹

Before starting, verify you have these tools installed:

Step 1: Verify Node.js is Installedโ€‹

Open your terminal and run:

node --version

You should see output similar to:

v18.x.x or higher

If you don't have Node.js, download it from: nodejs.org

Pro Tip

Use Node.js version 18 or higher for best compatibility with latest Playwright features.

Step 2: Verify npm is Availableโ€‹

npm --version

Expected output:

9.x.x or higher

npm comes bundled with Node.js, so installing Node usually handles this automatically.


๐Ÿš€ Installation Stepsโ€‹

Follow these steps in order to set up your Playwright environment.


๐Ÿ“ Step 1: Create a Project Folderโ€‹

Create a dedicated directory for your automation tests:

# Create the main project directory
mkdir playwright-tests

# Navigate into it
cd playwright-tests
Why Create Separate Folders?

We keep our course website (Docusaurus) separate from our test projects (Playwright) because:

  • Clean separation of concerns
  • Easier version control (git)
  • Can host tests on different CI pipelines
  • No node_modules conflicts

๐Ÿ“„ Step 2: Initialize Node Projectโ€‹

Initialize a new Node.js package:

npm init -y

What this does:

  • Creates package.json file
  • Sets up default project metadata
  • Enables npm dependency management

Generated package.json looks like:

{
"name": "playwright-tests",
"version": "1.0.0",
"main": "index.js",
"scripts": {},
"keywords": [],
"author": "",
"license": "ISC"
}
DON'T SKIP THIS STEP

Without initializing Node, you cannot install Playwright packages or run scripts properly!


๐ŸŽฏ Step 3: Install Playwrightโ€‹

Install Playwright along with its dependencies:

npm init playwright@latest

During installation, you'll be prompted with questions:

PromptRecommended ChoiceExplanation
TypeScript or JavaScript?JavaScriptEasier for beginners; this course uses JS
Name of site/tests folder?Press Enter (default: tests)Standard convention
Add GitHub Actions workflow?NoWe'll customize this later
Install browsers?YesEssential for running tests locally
Choose:
โ€ข JavaScript
โ€ข Default test folder
โ€ข Install browsers โ†’ Yes
โ€ข GitHub Actions โ†’ No
What Gets Installed

Playwright installs these components:

  1. @playwright/test โ€“ Main testing framework
  2. Browsers: Chromium, Firefox, WebKit
  3. playwright.config.js โ€“ Configuration file
  4. Example test files โ€“ To get started quickly
  5. Trace Viewer โ€“ Debugging tool

Total space used: ~300-400 MB (includes browser binaries)


โฑ๏ธ Installation Time Expectationsโ€‹

ComponentTime Required
Downloading packages~30 seconds
Installing dependencies~45 seconds
Downloading browser binaries~2-3 minutes
TOTAL~4-5 minutes

Actual time varies based on internet speed.


๐Ÿงช Step 4: Run Example Tests (Verify Installation)โ€‹

Once installation completes, validate everything works:

npx playwright test

Expected successful output:

Running 3 tests using 3 workers

โœ“ example.spec.js:4:1 โ€บ has title (619ms)
โœ“ example.spec.js:9:1 โ€บ get started link (471ms)
โœ“ example.spec.js:12:1 โ€บ check cta button (498ms)

3 passed (2s)

To open last HTML report run:
npx playwright show-report
Test Passed!

๐ŸŽ‰ Congratulations! Your Playwright environment is now ready!

If you see "3 passed", your installation was successful!


๐Ÿ‘€ Step 5: Open Test Reportโ€‹

View the beautiful HTML report generated by Playwright:

npx playwright show-report

If port 9323 is busy (common error), use:

npx playwright show-report --port=9324

Then visit in browser:

http://localhost:9324

Report features you'll see:

  • Pass/fail status with colors
  • Screenshot thumbnails per step
  • Timeline visualization
  • Network request logs
  • Console logs captured

๐Ÿ“‚ Understanding Project Structureโ€‹

After installation, your playwright-tests/ folder should look like this:

playwright-tests/
โ”‚
โ”œโ”€โ”€ node_modules/ โ† Dependencies (don't edit manually)
โ”‚ โ””โ”€โ”€ @playwright/
โ”‚ โ””โ”€โ”€ test/
โ”‚
โ”œโ”€โ”€ tests/ โ† Your test files go here โœ๏ธ
โ”‚ โ””โ”€โ”€ example.spec.js โ† Sample test (can delete later)
โ”‚
โ”œโ”€โ”€ playwright.config.js โ† Configuration settings โš™๏ธ
โ”‚
โ”œโ”€โ”€ package.json โ† Project metadata & scripts
โ”œโ”€โ”€ package-lock.json โ† Exact dependency versions
โ”‚
โ””โ”€โ”€ playwright-report/ โ† Auto-generated reports ๐Ÿ“Š

โš™๏ธ Quick Config Overviewโ€‹

Your default playwright.config.js includes:

SettingValuePurpose
testDir'./tests'Where to find test files
fullyParalleltrueRun tests simultaneously
retries0 locally / 2 on CIRetry failed tests
reporter'html'Generate HTML report
projects[chromium, firefox, webkit]Browser targets

๐Ÿ”ง Common Installation Issues & Fixesโ€‹

Issue 1: Port Already in Useโ€‹

Error message:

Error: listen EADDRINUSE: address already in use ::1:9323

Solution:

# Use a different port
npx playwright show-report --port=9325

Or kill existing process:

lsof -i :9323 | grep LISTEN
kill -9 <PID>

Issue 2: Permission Denied (Mac/Linux)โ€‹

Error message:

EACCES: permission denied, mkdir '/usr/local/lib/node_modules'

Solution:

# Use npx instead of global install
npx playwright test

# Or fix permissions
sudo chown -R $(whoami) ~/.npm
sudo chown -R $(whoami) /usr/local/lib/node_modules

Issue 3: Old Node Versionโ€‹

Error message:

Playwright requires Node.js >= 14

Solution: Upgrade Node.js:

  • Download latest LTS from nodejs.org
  • Or use version manager: nvm install --lts

Issue 4: Tests Not Foundโ€‹

Error message:

No tests found.

Common causes:

  1. Running command from wrong directory
  2. Test files not in tests/ folder
  3. File extension mismatch (.spec.js vs .test.js)

Fix:

cd playwright-tests # Always run from here!
npx playwright test --list # Check what tests are found

๐Ÿ“Š Installation Checklistโ€‹

Before proceeding to Module 3, confirm each item:

Environment Setup โœ…โ€‹

ItemStatus
Node.js v18+ installedโ˜
npm available via terminalโ˜
Project folder createdโ˜
package.json initializedโ˜
Playwright installed successfullyโ˜
Browsers downloaded (Chromium/Firefox/WebKit)โ˜

Validation Tests โœ…โ€‹

ItemStatus
Ran npx playwright testโ˜
Saw passing tests ("X passed")โ˜
Opened HTML report (show-report)โ˜
Report loaded in browser correctlyโ˜

Configuration Verified โœ…โ€‹

ItemStatus
Checked playwright.config.js existsโ˜
Understand testDir: './tests' locationโ˜
Know how to run single test fileโ˜

๐ŸŽ“ Key Takeaways from This Moduleโ€‹

TL;DR โ€“ What You Need to Remember:

  1. Always run commands from inside playwright-tests/ folder, not the parent directory
  2. npm init -y โ†’ npm init playwright@latest = standard setup sequence
  3. Choose JavaScript (not TypeScript) if following this exact course
  4. Install browsers during setup (unless using CI docker images)
  5. Verify installation by running: npx playwright test
  6. Check reports with: npx playwright show-report

๐Ÿ”— Quick Navigationโ€‹

Previous Module: โ† Module 1 โ€“ Introduction to Playwright

Up Next: โ†’ Module 3 โ€“ Writing Your First Test


โ“ Frequently Asked Questionsโ€‹

Q: Can I use Yarn instead of npm?

Yes! Replace all npm commands with yarn:

yarn init
yarn create @playwright/latest
yarn add @playwright/test
yarn playwright test

Both work perfectly with Playwright.

Q: Do I need to install all 3 browsers?

Not necessarily. For this beginner course, Chromium is sufficient.

You can limit to Chromium-only config:

// playwright.config.js
projects: [
{ name: 'chromium', use: { ...devices['Desktop Chrome'] } },
],

Saves ~200MB disk space.

Q: How much disk space does Playwright need?

Approximate sizes:

ComponentSize
Playwright npm package~15 MB
Chromium binary~150 MB
Firefox binary~80 MB
WebKit binary~70 MB
Total~315 MB
Q: Should I commit node_modules to Git?

NO! Add node_modules/ to .gitignore.

Team members/can install dependencies themselves via:

npm ci # Install exact versions from lockfile

๐Ÿšจ Module Completion Badgeโ€‹

โœ… MODULE 2 COMPLETE!

You've successfully installed Playwright and can now:

  • โœ… Create Playwright projects from scratch
  • โœ… Initialize proper project structure
  • โœ… Run automated browser tests
  • โœ… View detailed HTML test reports

Ready for Module 3: Writing Your First Custom Test! โ†’


๐Ÿš€ Keep Goingโ€‹

Apply what you learned in this module: