Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS MATH

Playwright First Test


Share

Your First Playwright Test

A Playwright test is a JavaScript function that controls a browser.

It opens a page, does what a user would do, and checks that the page shows the right result.

In this chapter you will test a small todo list app.

The app is one of our practice pages, made for this tutorial: Todo List.


Write the Test

Create a file named todo.spec.js in the tests folder of your project.

Playwright finds test files by their name: every file ending in .spec.js is a test file.

Example

tests/todo.spec.js

// @ts-check
import { test, expect } from '@playwright/test';

test('has title', async ({ page }) => {
  await page.goto('https://www.w3schools.com/playwright/practice/todo.html');
  await expect(page).toHaveTitle(/Todo List/);
});

test('can add a todo', async ({ page }) => {
  await page.goto('https://www.w3schools.com/playwright/practice/todo.html');
  await page.getByPlaceholder('What needs to be done?').fill('Learn Playwright');
  await page.getByRole('button', { name: 'Add' }).click();
  await expect(page.getByText('Learn Playwright')).toBeVisible();
  await expect(page.getByTestId('todo-count')).toHaveText('1 item left');
});


Example Explained

  • import { test, expect } loads the two functions you need from Playwright
  • test('has title', ...) creates a test with a name and a function
  • async ({ page }) gives the test a page, a fresh browser tab
  • page.goto() opens a URL
  • expect(page).toHaveTitle() checks the page title
  • getByPlaceholder(), getByRole(), getByText() and getByTestId() find elements on the page
  • fill() types text into a field, and click() clicks
  • toBeVisible() and toHaveText() check the result

Each test gets its own clean browser tab.

The todo you add in the second test is gone when the next test starts.

Note: Every Playwright action returns a promise, so you write await in front of it.

If you forget await, the test moves on before the action is done.


Run the Test

Run all tests from the project folder:

Example

npx playwright test

Playwright runs every test in every browser listed in playwright.config.js.

This project tests in Chromium only, so the two tests run once each:

Output:

Running 2 tests using 2 workers

[1/2] [chromium] › tests/todo.spec.js:4:1 › has title
[2/2] [chromium] › tests/todo.spec.js:9:1 › can add a todo
  2 passed (3.2s)

By default the tests run headless: the browser works in the background, and you do not see a window.

A worker is a separate process that runs tests.

The two tests ran at the same time, in two workers.


When a Test Fails

Change the expected text in the second test from '1 item left' to '2 items left', and run the test again.

Example

await expect(page.getByTestId('todo-count')).toHaveText('2 items left');

Now Playwright reports a failure, and shows what it expected and what it got:

Output:

Running 2 tests using 2 workers

[1/2] [chromium] › tests/todo.spec.js:9:1 › can add a todo
[2/2] [chromium] › tests/todo.spec.js:4:1 › has title
  1) [chromium] › tests/todo.spec.js:9:1 › can add a todo

    Error: expect(locator).toHaveText(expected) failed

    Locator:  getByTestId('todo-count')
    Expected: "2 items left"
    Received: "1 item left"
    Timeout:  5000ms

      12 |   await page.getByRole('button', { name: 'Add' }).click();
      13 |   await expect(page.getByText('Learn Playwright')).toBeVisible();
    > 14 |   await expect(page.getByTestId('todo-count')).toHaveText('2 items left');
         |                                                ^
      15 | });

  1 failed
    [chromium] › tests/todo.spec.js:9:1 › can add a todo
  1 passed (7.4s)

Playwright keeps checking for 5 seconds before it reports a failure.

This gives the page time to update, so you do not need to add waits yourself.

You will learn more about this in the Auto-Waiting chapter.


See the Report

After a run, Playwright saves an HTML report with every test and every error.

Open it with this command:

Example

npx playwright show-report

The report opens in your browser.

Click a test to see its steps, the error, and where in the code it failed.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.