Playwright + TS · Lesson 15 of 36

0/36 done

Module 15: Installation & Setup

15. Initializing Playwright Test & Runner Configuration

15 minlesson

Try it yourself

Implement parseExecutionMode:

  1. Accept args: { isHeaded?: boolean; isParallel?: boolean }.
  2. Return ExecutionSettings { headless: !args.isHeaded, workers: args.isParallel ? 4 : 1 }.

The editor has a worked example. Read it, tweak it, then press Mark as complete.

Quick Setup Command

Initialize a brand new Playwright TypeScript test project:

bash
npm init playwright@latest

Prompts will ask:

  • TypeScript or JavaScript? -> Choose TypeScript
  • Where to put tests? -> tests
  • Add GitHub Actions workflow? -> False
  • Install Playwright browsers? -> Yes

Writing Your First Test (tests/example.spec.ts)

typescript
import { test, expect } from "@playwright/test";

test("has title and navigates to docs", async ({ page }) => {
  await page.goto("https://playwright.dev/");

  // Expect a title "to contain" a substring.
  await expect(page).toHaveTitle(/Playwright/);

  // Click getting started link
  await page.getByRole("link", { name: "Get started" }).click();

  // Expects page to have a heading with the name of Installation.
  await expect(page.getByRole("heading", { name: "Installation" })).toBeVisible();
});

Running Tests from CLI

bash
# Run all tests (headless by default):
npx playwright test

# Run tests in headed browser mode (watch browser window):
npx playwright test --headed

# Run only chromium:
npx playwright test --project=chromium

# Run UI mode (interactive test explorer with time travel):
npx playwright test --ui

Key Takeaways & Walkthrough Summary

Setting up Playwright is incredibly fast. In this lesson, we will install Playwright, explore the playwright.config.ts configuration file, write our very first test spec, and learn how to run tests in headed and headless modes using both the terminal and UI mode. Let's start testing!

Part of the Playwright + TS course (Playwright with TypeScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Why does Playwright default to headless mode?

Headless mode runs faster and uses significantly fewer CPU and GPU resources, making it ideal for high-throughput local runs and CI pipelines.

How do I run a specific test file?

Run 'npx playwright test tests/login.spec.ts' or filter by test title with 'npx playwright test -g "login"'.

Finished reading?

Review the worked example in the editor, then mark the lesson complete.

TypeScript
Skip