Playwright JavaScript Setup: Step-by-Step Guide (2026) | TechSarvam

1. Introduction to Playwright JavaScript Setup

Setting up a browser automation framework correctly is the first step toward building reliable automated tests. Without the right Node.js version, browser binaries, dependencies, and project configuration, even a simple test may fail before execution begins.

This Playwright JavaScript setup guide explains how to prepare a complete automation environment using JavaScript and Playwright Test.

You will learn how to install Playwright, configure the project, download Chromium, Firefox, and WebKit, write your first test, execute tests in different modes, and generate HTML reports.

The examples use JavaScript (.js) rather than TypeScript (.ts). This makes the tutorial suitable for freshers, JavaScript developers, QA Automation Engineers, and Selenium testers transitioning to Playwright.

By the end, you will have a working project structure that you can extend for regression testing and enterprise automation.

2. What Is Playwright, and Why Use JavaScript?

Playwright is an open-source browser automation framework developed by Microsoft. It supports Chromium, Firefox, and WebKit using a consistent API.

It provides two main options:

For a typical QA automation project, Playwright Test is a good choice because it includes the tools needed to organise and execute automated test scripts.

JavaScript is useful because it:

The Playwright JavaScript setup process is straightforward, and you do not need TypeScript to create a professional automation project.

3. Prerequisites: Node.js, npm, JavaScript Basics, and VS Code

Before installing Playwright, prepare your system.

1. Node.js

Node.js provides the runtime required to execute JavaScript outside a browser.

For the current Playwright documentation, use a supported Node.js release. The documented versions include the latest 22.x, 24.x, and 26.x releases. <Cite refs={[“turn884682search0”]}/>

Download it from https://nodejs.org/.

2. npm

npm is the Node.js package manager. It installs Playwright and manages project dependencies. It is included with the Node.js installation.

3. Visual Studio Code

VS Code is a convenient editor for creating JavaScript files, managing project folders, and running terminal commands.

4. JavaScript basics

You should understand variables, functions, objects, arrays, promises, and async/await.

Verify Node.js and npm

Open Command Prompt, PowerShell, macOS Terminal, or Linux Terminal.

Run:

node -v

npm -v

Example output:

v22.x.x

10.x.x

The exact version numbers depend on your installed releases.

If both commands display versions, your Node.js environment is ready.

4. Installing Playwright Using npm init playwright@latest

The recommended way to create a new Playwright Test project is to use the official project initializer.

Step 1: Create a project folder

mkdir playwright-javascript-setup

cd playwright-javascript-setup

Step 2: Run the Playwright initializer

npm init playwright@latest

The initializer asks several questions.

PromptRecommended selection
LanguageJavaScript
Test foldertests
GitHub Actions workflowYes, if using GitHub
Install browsersYes

Important: The initializer may default to TypeScript. Select JavaScript when prompted because this tutorial uses .js files.

The initializer installs Playwright Test and creates the basic project files.

Step 3: Verify the installation

npx playwright –version

This command displays the installed Playwright version.

If the version appears, Playwright Test is installed successfully.

You can rerun the initializer later to add Playwright to an existing project. It does not overwrite existing tests. <Cite refs={[“turn884682search0”]}/>

5. Understanding the Generated Project Structure

After installation, your project will contain files similar to these:

playwright-javascript-setup/

│

├── tests/

│   └── example.spec.js

│

├── playwright.config.js

├── package.json

├── package-lock.json

├── .gitignore

│

└── node_modules/

The exact generated files may vary with the Playwright version and your setup selections.

tests/

This folder contains automated test scripts. Each test file usually follows the .spec.js naming convention.

playwright.config.js

This file controls browser selection, test directories, timeouts, retries, reporters, and other execution settings.

package.json

This file stores project metadata, dependencies, and npm scripts.

package-lock.json

This file records the dependency versions installed for the project. Commit it to source control so team members and CI can reproduce the dependency installation.

node_modules/

This folder contains installed packages. Do not commit it to Git.

A good Playwright JavaScript setup keeps configuration and test code separate so the project can grow without becoming difficult to maintain.

6. Configuring playwright.config.js and package.json

The configuration file controls how Playwright executes tests.

Open playwright.config.js and use this example:

// @ts-check

const { defineConfig, devices } = require(‘@playwright/test’);

module.exports = defineConfig({

  testDir: ‘./tests’,

  fullyParallel: true,

  forbidOnly: !!process.env.CI,

  retries: process.env.CI ? 2 : 0,

  workers: process.env.CI ? 2 : undefined,

  reporter: [

    [‘list’],

    [‘html’, { open: ‘never’ }]

  ],

  use: {

    baseURL: ‘https://example.com’,

    headless: true,

    screenshot: ‘only-on-failure’,

    video: ‘retain-on-failure’,

    trace: ‘retain-on-failure’

  },

  projects: [

    {

      name: ‘chromium’,

      use: { …devices[‘Desktop Chrome’] }

    },

    {

      name: ‘firefox’,

      use: { …devices[‘Desktop Firefox’] }

    },

    {

      name: ‘webkit’,

      use: { …devices[‘Desktop Safari’] }

    }

  ]

});

This configuration provides:

  • A dedicated test folder.
  • Parallel test execution.
  • Additional retries in CI.
  • HTML and terminal reporting.
  • Screenshots and traces for failures.
  • Three browser projects.

Replace https://example.com with your application’s URL when testing another website.

Configure package.json scripts

You can add convenient commands to the scripts section:

{

  “scripts”: {

    “test”: “playwright test”,

    “test:headed”: “playwright test –headed,

    “test:debug”: “playwright test –debug,

    “test:chromium”: “playwright test –project=chromium,

    “report”: “playwright show-report”

  }

}

Now you can execute tests using:

npm test

npm run test:headed

npm run test:debug

npm run test:chromium

npm run report

These scripts make the Playwright Test Setup easier for beginners and development teams.

7. Installing Chromium, Firefox, and WebKit Browsers

Playwright requires browser binaries that match the installed Playwright version.

Install all default browsers:

npx playwright install

Install an individual browser:

npx playwright install chromium

npx playwright install firefox

npx playwright install webkit

Browser installation on Windows

Open PowerShell or Command Prompt inside your project directory:

npx playwright install

If installation fails, check your internet connection, permissions, and available disk space.

Browser installation on macOS

Open Terminal:

npx playwright install

Browser installation on Linux

Install browsers and their required operating-system dependencies:

npx playwright install –with-deps

This is particularly useful on CI machines where browser libraries may not already be installed.

Why should you reinstall browsers after updating Playwright?

Playwright versions depend on specific browser binaries. After upgrading the Playwright package, run the browser installation command again when required:

npm install -D @playwright/test@latest

npx playwright install

This helps avoid browser-version mismatches. <Cite refs={[“turn884682search9”]}/>

8. Writing and Running Your First JavaScript Test

After completing the Playwright JavaScript setup, create your first test.

Create tests/homepage.spec.js:

const { test, expect } = require(‘@playwright/test’);

test(‘homepage should display the correct title’, async ({ page }) => {

  await page.goto(‘/’);

  await expect(page).toHaveTitle(/Example Domain/);

});

test(‘homepage should display its main heading’, async ({ page }) => {

  await page.goto(‘/’);

  await expect(

    page.getByRole(‘heading’, { name: ‘Example Domain’ })

  ).toBeVisible();

});

The example uses the baseURL configured earlier.

Understanding the code:

  • test() defines a test case.
  • page is a built-in Playwright fixture.
  • page.goto() navigates to a URL.
  • getByRole() locates an element by its accessible role.
  • expect() checks whether the expected condition is true.

Run the test:

npx playwright test

Example terminal output:

Running 2 tests using 2 workers

  ✓ homepage should display the correct title

  ✓ homepage should display its main heading

  2 passed

The exact output depends on the Playwright version and execution environment. These results illustrate what a successful run may look like.

9. Running Tests in Headed, Headless, and Debug Modes

Playwright supports multiple execution modes.

Headless mode

The browser runs without displaying its graphical window. This is the default mode.

npx playwright test

It is useful for CI/CD pipelines and unattended regression testing.

Headed mode

The browser window appears while tests execute.

npx playwright test –headed

This is helpful when beginners want to observe browser actions.

Debug mode

npx playwright test –debug

This launches Playwright Inspector, allowing you to pause execution, inspect locators, and step through actions.

Run a single test file

npx playwright test tests/homepage.spec.js

Run only Chromium

npx playwright test –project=chromium

Run with four workers

npx playwright test –workers=4

Parallel execution can reduce test duration, but tests should use independent data to avoid conflicts.

10. Generating HTML Reports and Viewing Test Results

Reporting is important because QA teams need to understand test outcomes, not just execute automated scripts.

The HTML reporter is included with Playwright Test.

Configure it in playwright.config.js:

reporter: [

  [‘list’],

  [‘html’, {

    outputFolder: ‘playwright-report’,

    open: ‘never’

  }]

]

Run the tests:

npx playwright test

Open the report:

npx playwright show-report

The HTML report provides:

You can also inspect test execution using Trace Viewer.

Enable tracing:

use: {

  trace: ‘retain-on-failure’

}

After a failed test, open its trace file:

npx playwright show-trace test-results/path-to-trace.zip

Replace the example path with the actual trace file generated by your test run.

Reports are generated locally by default. For team access, upload the report folder as a CI artifact or publish it through an approved hosting service.

11. Troubleshooting Common Installation and Configuration Errors

Even a correct Playwright JavaScript setup can encounter environment-related problems.

Problem 1: Node.js or npm command not recognised

Reason: Node.js may not be installed correctly, or its installation directory may not be available in the system PATH.

Solution: Install or repair Node.js, restart the terminal, and verify:

node -v

npm -v

Problem 2: Browser executable does not exist

Reason: Browser binaries may not have been downloaded.

Solution:

npx playwright install

Problem 3: Browser installation fails on Linux

Reason: Required operating-system libraries may be missing.

Solution:

npx playwright install –with-deps

Use an account with the necessary permissions.

Problem 4: Cannot find module ‘@playwright/test’

Reason: Playwright Test may not be installed in the current project, or the terminal may be open in the wrong directory.

Solution:

npm install -D @playwright/test

Then run:

npx playwright –version

Problem 5: Test file is not discovered

Reason: The file may be outside the configured test directory or may not match the configured test naming pattern.

Solution: Keep test files inside tests/ and use names such as login.spec.js.

Problem 6: Test works locally but fails in CI

Check browser dependencies, environment variables, application availability, permissions, and test data. Enable traces to investigate failures.

12. Preparing the Setup for Regression Testing and CI/CD

Once the basic Playwright JavaScript setup works, prepare it for team usage.

For regression testing:

  • Separate tests by application module.
  • Use meaningful test names.
  • Keep test data independent.
  • Configure browser projects.
  • Capture traces for failures.
  • Use retries carefully rather than hiding unstable tests.

GitHub Actions example

Create .github/workflows/playwright.yml:

name: Playwright Tests

on:

  push:

    branches: [main]

  pull_request:

    branches: [main]

jobs:

  test:

    runs-on: ubuntu-latest

    steps:

      – uses: actions/checkout@v4

      – uses: actions/setup-node@v4

        with:

          node-version: 22

          cache: npm

      – name: Install dependencies

        run: npm ci

      – name: Install browsers

        run: npx playwright install –with-deps

      – name: Run tests

        run: npx playwright test

      – name: Upload HTML report

        if: ${{ !cancelled() }}

        uses: actions/upload-artifact@v4

        with:

          name: playwright-report

          path: playwright-report/

          retention-days: 14

This workflow installs dependencies, downloads browsers, executes tests, and stores the HTML report as a workflow artifact.

For Jenkins, create stages for checkout, dependency installation, browser installation, and test execution. Use a post { always { … } } block to archive playwright-report/** even when tests fail.

For larger frameworks, explore Playwright Framework Design and Playwright JavaScript CI/CD.

13. Best Practices for Playwright JavaScript Setup

Follow these practices when preparing an automation environment:

  1. Use a supported Node.js version.
  2. Commit package-lock.json for reproducible npm installs.
  3. Do not commit node_modules.
  4. Install matching browser binaries after Playwright updates.
  5. Use a central configuration file.
  6. Prefer accessible locators over fragile selectors.
  7. Avoid unnecessary fixed waits.
  8. Keep credentials in environment variables or CI secrets.
  9. Use isolated test data for parallel execution.
  10. Capture screenshots and traces for failures.

For the next stage of learning, explore Playwright JavaScript Tutorial, Playwright JavaScript Project Structure, Playwright JavaScript Configuration, Playwright JavaScript Locators, Playwright JavaScript Assertions, Playwright JavaScript Page Object Model, and Playwright JavaScript Debugging.

14. Interview Preparation for QA Automation Engineers and SDETs

For Indian QA Automation Engineer and SDET interviews, prepare to explain your setup decisions and demonstrate the commands.

Common interview questions include:

  1. How do you install Playwright using npm?
  2. What is the difference between Playwright Library and Playwright Test?
  3. What is the purpose of playwright.config.js?
  4. How do you install Chromium, Firefox, and WebKit?
  5. What is the difference between headed and headless execution?
  6. How do you run a single test file?
  7. How do you generate an HTML report?
  8. How do you debug a failed test?
  9. How do you configure Playwright for GitHub Actions?
  10. How do you prepare a Playwright project for regression testing?

Starter repository: Add your verified GitHub repository URL here after publishing the sample project. Include the JavaScript configuration, sample tests, package files, and CI workflow. Do not publish real credentials or authentication files.

15. Frequently Asked Questions

1. What is Playwright JavaScript setup?

Playwright JavaScript setup is the process of preparing Node.js, installing Playwright Test and browser binaries, creating a JavaScript project, and configuring the environment to run automated browser tests.

2. What are the benefits of Playwright JavaScript setup?

It provides a ready-to-use environment for browser automation, end-to-end testing, regression testing, cross-browser execution, debugging, and reporting.

3. Is Playwright JavaScript setup suitable for beginners?

Yes. Beginners with basic JavaScript knowledge can follow the installation steps and start with simple browser tests before learning advanced framework concepts.

4. Can I use Playwright JavaScript on Windows, macOS, and Linux?

Yes. Playwright supports all three operating-system families, subject to its documented system requirements and browser dependencies.

5. Do I need TypeScript to use Playwright?

No. Playwright supports JavaScript and TypeScript. You can build a complete automation project using JavaScript files.

Leave a Comment

Your email address will not be published. Required fields are marked *