Playwright First Test
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 Playwrighttest('has title', ...)creates a test with a name and a functionasync ({ page })gives the test apage, a fresh browser tabpage.goto()opens a URLexpect(page).toHaveTitle()checks the page titlegetByPlaceholder(),getByRole(),getByText()andgetByTestId()find elements on the pagefill()types text into a field, andclick()clickstoBeVisible()andtoHaveText()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.