Playwright

Lesson 1 of 17

Lesson ยท 15 min

Your first test

A program that uses a browser

Playwright is a tool that controls a web browser with code. A test opens a page, does what a person would do, and checks the result. You write the test once. You can run it as often as you like, and it does not get tired.

On the right of this lesson is the practice browser. It shows a small sample app called Absolute Path Todos. When you press Run tests, your tests control that browser. You can watch each test move through the app. A banner above the page shows which test is running.

The app is slow on purpose. It waits a short time before it shows the todos, as a real server does. This matters later in the course.

A test file

A test file has a standard shape.

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

test('opens the todo app', async ({ page }) => {
  await page.goto('/');
  await expect(page).toHaveTitle('Absolute Path Todos');
});

Read it from the top.

  • The import line brings in test and expect. Every Playwright file starts with it.
  • test('title', async ({ page }) => { ... }) defines one test. The title tells a person what the test checks.
  • page is the browser tab. Playwright gives a new, clean page to each test.
  • await page.goto('/') opens the first page of the sample app. In a real project you write the full address, or you set a baseURL once.
  • expect(page).toHaveTitle(...) is an assertion. It checks a fact. When the fact is wrong, the test fails.

Most Playwright calls take time, so they return a promise. Write await in front of them. A call without await runs in the background, and the test can end before it finishes.

Read the report

The output shows one line for each test, and a count at the end.

When a test fails, the report shows the line of your code, then Expected and Received. Expected is what your test wanted. Received is what the page had. Read these two lines first. Most failures are a difference between them.

Your turn

The file has one test, and it has a mistake. Run it, read the report, and fix it. Then add two tests of your own. Give each test its own page.goto('/').

Tasks

3 to do

  1. Task 1, not checked yet. Press Run tests. The test fails. Read the report to find the real title of the page. Then fix the expected title so that the test passes.

  2. Task 2, not checked yet. Add a second test named shows the heading. It opens the app and checks that the heading Absolute Path Todos is visible.

  3. Task 3, not checked yet. Add a third test named shows the first todo. It opens the app and checks that the text Buy milk is visible.

Task 1 of 3

Press Run tests. The test fails. Read the report to find the real title of the page. Then fix the expected title so that the test passes.

All tasks

example.spec.js

Ctrl+Enter runs your tests. Ctrl+Shift+Enter checks it. Press Esc, then Tab, to leave the editor.

Practice browser

The sample app is ready

The results of your tests show here. Press Run tests to watch them run in the practice browser.