๐ฆ Module 2 โ Installing Playwright
| Detail | Info |
|---|---|
| Difficulty | |
| Time Required | โฑ๏ธ 15-20 minutes |
| Prerequisites | Node.js installed |
| Outcome | Fully 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
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
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.jsonfile - 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"
}
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:
| Prompt | Recommended Choice | Explanation |
|---|---|---|
| TypeScript or JavaScript? | JavaScript | Easier for beginners; this course uses JS |
| Name of site/tests folder? | Press Enter (default: tests) | Standard convention |
| Add GitHub Actions workflow? | No | We'll customize this later |
| Install browsers? | Yes | Essential for running tests locally |
Choose:
โข JavaScript
โข Default test folder
โข Install browsers โ Yes
โข GitHub Actions โ No
Playwright installs these components:
@playwright/testโ Main testing framework- Browsers: Chromium, Firefox, WebKit
playwright.config.jsโ Configuration file- Example test files โ To get started quickly
- Trace Viewer โ Debugging tool
Total space used: ~300-400 MB (includes browser binaries)
โฑ๏ธ Installation Time Expectationsโ
| Component | Time 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
๐ 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:
| Setting | Value | Purpose |
|---|---|---|
testDir | './tests' | Where to find test files |
fullyParallel | true | Run tests simultaneously |
retries | 0 locally / 2 on CI | Retry 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:
- Running command from wrong directory
- Test files not in
tests/folder - 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 โ โ
| Item | Status |
|---|---|
| Node.js v18+ installed | โ |
| npm available via terminal | โ |
| Project folder created | โ |
package.json initialized | โ |
| Playwright installed successfully | โ |
| Browsers downloaded (Chromium/Firefox/WebKit) | โ |
Validation Tests โ โ
| Item | Status |
|---|---|
Ran npx playwright test | โ |
| Saw passing tests ("X passed") | โ |
Opened HTML report (show-report) | โ |
| Report loaded in browser correctly | โ |
Configuration Verified โ โ
| Item | Status |
|---|---|
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:
- Always run commands from inside
playwright-tests/folder, not the parent directory npm init -yโnpm init playwright@latest= standard setup sequence- Choose JavaScript (not TypeScript) if following this exact course
- Install browsers during setup (unless using CI docker images)
- Verify installation by running:
npx playwright test - 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:
| Component | Size |
|---|---|
| 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โ
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:
- ๐งช Practice Exercises โ hands-on tasks to build real skills
- ๐๏ธ Real Projects โ real-world automation examples