📖 The Story: The Dependency Hell

Meet Alex. Alex was a brilliant manual tester who decided to step into automation. His first day, his lead told him, "Set up the Selenium environment." Eight hours later, Alex was staring at a terminal full of red text. He had Java version 21, but the framework needed 11. He had downloaded the ChromeDriver, but it was for Chrome 120 while his browser had auto-updated to 122. Path variables were broken. His spirit was broken.

This is called "Dependency Hell." It's the dark forest where aspiring automation engineers get lost before they even write a single line of test code.

Then, Alex discovered Playwright. He installed Node.js, ran one command, and Playwright intelligently downloaded the exact browsers it needed, pinned to the exact versions that matched the library. In 3 minutes, he was ready. No path variables. No driver mismatches. Just pure automation bliss. In this chapter, we will get your machine to that exact same state.

🎯 Why Proper Installation Matters

A faulty setup leads to flaky tests, CI/CD failures, and wasted hours debugging environment issues instead of writing tests. A clean installation guarantees:

🧩

Reproducibility

Tests run the exact same way on your laptop, your colleague's laptop, and the CI server.

📦

Isolation

Project dependencies don't clash with each other. Project A uses Playwright v1.40, Project B uses v1.49. No problem.

🚀

Speed

Using the right package manager and Node version makes test execution significantly faster.

🌍 Real-World Example: The Onboarding Script

In modern engineering teams, new hires are expected to run the test suite on day one. Here is the standard sequence of commands they run:

bash · onboarding.sh
# 1. Clone the project repository
git clone https://github.com/company/qa-automation.git
cd qa-automation

# 2. Install all Node.js dependencies (including Playwright)
npm install

# 3. Download the specific browser binaries defined in package-lock
npx playwright install

# 4. Run the tests to verify setup
npm test

Why npx? The npx command executes a package from the local node_modules without having to install it globally. It ensures you are using the exact version of Playwright pinned in your package.json.

🧒 Explain Like I'm 10

Imagine you want to build a Lego city (your test suite). To build it, you need two things:

  1. A workbench (Node.js). This is the surface you work on.
  2. The Lego box (Playwright). This contains the bricks and the instruction manual.
  3. The workers (Browsers). These are the tiny robots that will actually move the Lego pieces around.

First, we set up the workbench (install Node.js). Then, we open the Lego box (run npm init playwright). Finally, we unpack the tiny robots (run playwright install). Once they are all on the table, you can start building!

🎓 Professional Explanation

Playwright is a Node.js library distributed via the npm (Node Package Manager) registry. To use it, you must have the Node.js runtime installed on your system. Playwright relies heavily on asynchronous I/O, so a modern version of Node.js (v18 or higher) is required to support the latest JavaScript features and performance optimizations.

When you run the Playwright installation command, two distinct things happen:

  • npm package installation: The Playwright API code (@playwright/test) is downloaded into your project's node_modules folder.
  • Browser binary installation: Playwright reaches out to its CDN and downloads patched versions of Chromium, Firefox, and WebKit. These are stored in a global cache (e.g., ~/.cache/ms-playwright on macOS/Linux) so they can be shared across multiple projects without re-downloading.

📦 Step-by-Step Installation Guide

Step 1: Install Node.js

Go to nodejs.org and download the LTS (Long Term Support) version. Run the installer. Verify it works by opening your terminal:

bash
node --version
# Should output something like: v20.11.0

npm --version
# Should output something like: 10.2.4

Step 2: Initialize Your Project

Create a folder for your automation project and initialize it:

bash
mkdir playwright-cseian
cd playwright-cseian
npm init -y

The -y flag creates a package.json file with default values, skipping the interactive questionnaire.

Step 3: Install Playwright

This is the magic command. It sets up everything for you.

bash
npm init playwright@latest

Playwright will ask you a few setup questions. Choose these options for a modern setup:

  • Choose between TypeScript or JavaScript: Select TypeScript (industry standard).
  • Name of your test folder: Press Enter to accept the default (tests).
  • Add GitHub Actions CI workflow: Press Y (we will use this later in the course).
  • Install Playwright browsers: Press Y (this downloads Chromium, Firefox, WebKit).

💼 Interview Questions

Beginner
What is Node.js and why do we need it for Playwright?
Show answer
Node.js is a JavaScript runtime environment that allows JS to run outside a web browser. We need it because Playwright is built on Node.js; it provides the environment and package manager (npm) required to install and execute the Playwright library.
Intermediate
Why should you avoid installing Playwright globally?
Show answer
Installing globally causes version conflicts. If Project A needs Playwright 1.40 and Project B needs 1.49, a single global install cannot serve both. Local installation ensures each project defines its exact version in package.json, guaranteeing reproducibility.
Advanced
How do you handle Playwright installation in a strict, air-gapped CI/CD environment?
Show answer
You can set the environment variable PLAYWRIGHT_BROWSERS_PATH=0 to force the binaries to install directly into the project's node_modules folder. Then, you zip the entire node_modules folder, transfer it to the air-gapped network, and unzip it there. The tests will run without needing internet access.

🧠 Quiz: Test Your Setup Knowledge

1. Which command is the recommended way to install Playwright in a new project?
npm install -g playwright
npm init playwright@latest
apt-get install playwright
npx create-playwright
2. What is the minimum recommended Node.js version for modern Playwright?
Node.js 8
Node.js 12
Node.js 18+
Node.js 22+
3. Where are the patched browser binaries stored by default?
Inside the project's tests/ folder
In a global OS-specific cache directory
In the system's Program Files
They are compiled at runtime
4. What does the npm init playwright@latest command do?
Only downloads the Playwright API
Creates config files, example tests, and downloads browsers
Installs Node.js on your machine
Launches the Playwright UI mode
5. Why should you avoid using sudo with npm in Playwright installation?
It slows down the download speed
It causes permission issues and security risks
Playwright doesn't support root users
It installs the 32-bit version instead of 64-bit

📌 Summary

🎯 Key Takeaways

  • Node.js (v18+) is the foundation; you cannot use Playwright without it.
  • The npm init playwright@latest command scaffolds the entire project structure.
  • Playwright downloads its own browsers to a global cache, preventing version mismatches.
  • Never install Playwright globally; keep it local to the project for reproducibility.
  • Use PLAYWRIGHT_BROWSERS_PATH=0 for self-contained CI/CD pipelines.