Playwright
  • Introduction
    • Playwright With TS
  • Automation the right things with Playwright
  • Setup
    • Setting Up Your Environment
    • Typescript fundamental
    • Setup Playwright TS Project in VS code
    • Record and Playback with Code Generation using VS Code Playwright Extension
    • Record and Playback using Playwright Codegen with Inspector
  • Playwright config file
  • Implement Test Cases
    • Open Browser
    • Fake Geolocation
    • Form Authentication
    • Checkboxes
    • Dropdown
    • Hyperlink
    • Web Table
    • Drag Drop
    • Frame
    • Horizontal Slide
    • Context Click - Right Click
  • Hover
  • Take Screenshot
  • Automatic screenshots on failure:
  • Page Object Model
    • Understanding the Page Object Model pattern
  • Implementing POM with Login Page
  • Combine POM with Playwright Fixture
  • API Testing
    • API Docs
  • GET
  • API POST
  • PUT
  • DELETE
  • CI/CD And Reporting
    • Setup Allure Report
    • Run in Github Actions
  • Playwright & AI
    • Debug & Generate Playwright Tests with AI
Powered by GitBook
On this page
  1. Setup

Setting Up Your Environment

PreviousAutomation the right things with PlaywrightNextTypescript fundamental

Last updated 15 days ago

Installing Node.js and npm

  1. Download and install Node.js from

  2. Verify installation:bash

    node --version
    npm --version

Setting up a new project

  1. Create a new directory for your project:bash

    mkdir playwright-project
    cd playwright-project
  2. Initialize a new Node.js project:bash

    npm init -y

Installing Playwright with TypeScript

Playwright provides an installer that sets up everything you need

npm init playwright@latest

During the setup, select the following options:

  • Choose TypeScript when prompted

  • Select browsers you want to test on (Chromium, Firefox, WebKit)

  • Add GitHub Actions workflow if you plan to use CI

Configuring tsconfig.json

The Playwright installer creates a basic tsconfig.json file. Here's an explanation of key settings:

{
  "compilerOptions": {
    "target": "ES2021",        // ECMAScript target version
    "module": "commonjs",      // Module system
    "moduleResolution": "node", // How modules are resolved
    "strict": true,            // Enable all strict type checking options
    "esModuleInterop": true,   // Enables compatibility with Babel
    "skipLibCheck": true,      // Skip type checking of declaration files
    "forceConsistentCasingInFileNames": true, // Ensure consistent casing in imports
    "resolveJsonModule": true  // Allow importing JSON files
  }
}

Understanding project structure

After installation, your project should have the following structure:

playwright-project/
├── node_modules/
├── playwright-report/     // Test reports are stored here
├── test-results/          // Screenshots, videos, and traces
├── tests/                 // Your test files go here
│   └── example.spec.ts    // Example test
├── playwright.config.ts   // Playwright configuration
├── package.json           // Project dependencies
├── package-lock.json
└── tsconfig.json          // TypeScript configuration

Key files:

  • playwright.config.ts: Configuration for your tests, including browsers, timeouts, and reporter settings.

  • tests/example.spec.ts: Example test file created by the installer.

  • package.json: Contains scripts to run your tests and project dependencies.

Running your first test

  • The installer creates an example test. Run it with:

    npx playwright test

  • To see a report of your test run:

    npx playwright show-report

  • To run tests with UI mode (for debugging):

    npx playwright test --ui

nodejs.org