Playwright

Your First Playwright End-to-End Test

Thirdy Gayares
16 min read

🎯 What You Will Learn

Install Playwright Test, write a resilient TodoMVC test, run it headed or in UI mode, and read the HTML report.

  • Initialize Playwright Test in a project
  • Write a complete TodoMVC end-to-end test
  • Use locators and web-first assertions
  • Run headed, UI, debug, and report workflows
Prerequisites: Node.js and npm, plus a project where adding Playwright Test is appropriate.

Initialize Playwright Test

Run the official initializer in your project. Choose TypeScript, keep the tests folder, and install browser binaries when prompted.

Terminal
npm init playwright@latest

Understand the Generated Files

The config controls browsers, retries, reports, and shared options. Test files contain user journeys and assertions.

PathPurpose
playwright.config.tsRunner and browser-project configuration
tests/*.spec.tsEnd-to-end test files
playwright-report/Generated HTML report
test-results/Failure artifacts such as traces and screenshots

Write the Todo Test

The test uses a label-based locator for the input and verifies text plus the remaining-item count.

tests/todo.spec.ts
import { test, expect } from '@playwright/test';

test('adds a todo item', async ({ page }) => {
  await page.goto('https://demo.playwright.dev/todomvc/');

  await page
    .getByPlaceholder('What needs to be done?')
    .fill('Learn Playwright Test');
  await page.keyboard.press('Enter');

  await expect(page.getByText('Learn Playwright Test')).toBeVisible();
  await expect(page.getByText('1 item left!')).toBeVisible();
});

Run the Test

The default command runs configured projects headlessly. Start with one file while learning.

Terminal
npx playwright test tests/todo.spec.ts
1 passed

Watch It in Headed Mode

Headed mode shows the browser and is useful while learning or investigating an interaction.

Terminal
npx playwright test tests/todo.spec.ts --headed

Use UI Mode

UI mode offers watch behavior, step details, filtering, and integrated trace inspection.

Terminal
npx playwright test --ui

Debug One Test

Debug mode opens Playwright Inspector and pauses execution so you can step through the test.

Terminal
npx playwright test tests/todo.spec.ts --debug

Open the HTML Report

After a run, open the report to review outcomes, projects, duration, errors, and attached artifacts.

Terminal
npx playwright show-report

Next improvement: Keep test names behavior-focused and add more assertions only when they protect meaningful user outcomes.

Recap

  • The initializer creates a complete test-runner setup.
  • Tests receive an isolated page fixture.
  • User-facing locators and web-first assertions improve resilience.
  • Headed, UI, debug, and report modes support different stages of development.

Checkpoint: You have completed the workflow and have a repeatable reference for your next Playwright project.

Official Resources

About the Author

TG

Thirdy Gayares

Passionate developer creating custom solutions for everyone. I specialize in building user-friendly tools that solve real-world problems while maintaining the highest standards of security and privacy.