Playwright Testing
This chapter guides you through writing and running your first Playwright test, understanding the basic composition and execution flow of a test.
Running the Sample Test
After initializing the project, first run the auto-generated sample test to confirm everything works:
# Run all tests
npx playwright test
After running it you will see output similar to the following:
Running 6 tests using 4 workers
✓ 1 [chromium] › tests/example.spec.ts:3:1 › has title (2.1s)
✓ 2 [chromium] › tests/example.spec.ts:10:1 › get started link (1.8s)
✓ 3 [firefox] › tests/example.spec.ts:3:1 › has title (1.9s)
✓ 4 [firefox] › tests/example.spec.ts:10:1 › get started link (1.7s)
✓ 5 [webkit] › tests/example.spec.ts:3:1 › has title (2.3s)
✓ 6 [webkit] › tests/example.spec.ts:10:1 › get started link (2.0s)
6 passed (12s)
From the output you can see that the test ran 2 tests on each of 3 browsers, for a total of 6 tests, all passing.
Creating Your First Test File
clear tests/example.spec.ts , let's write a test from scratch.
Example
// File path: tests/first-test.spec.ts
import
{
test
,
expect
}
from
'@playwright/test'
;
// test(name, callback) defines a test
// name: the test name, shown in the test report
// The callback parameter { page } is a fixture provided by Playwright
test
(
'Visit the RUNOOB homepage and check the title'
,
async
(
{
page
}
)
=>
{
// Navigate to the specified URL
await page.
goto
(
'https://www.runoob.com/'
)
;
// Assertion: the page title contains "RUNOOB"
await expect
(
page
)
.
toHaveTitle
(
/RUNOOB/
)
;
}
)
;
Run this test:
npx playwright test tests/first-test.spec.ts
Expected output:
Running 1 test using 1 worker
✓ 1 [chromium] › tests/first-test.spec.ts:4:1 › 访问 RUNOOB 首页并检查标题 (2.5s)
1 passed (2.5s)
The test() Function in Detail
test() is the core function of Playwright Test, used to define a test case.
Basic Syntax
Example
import
{
test
,
expect
}
from
'@playwright/test'
;
// test(test name, test function)
test
(
'A descriptive name for the test'
,
async
(
{
page
}
)
=>
{
// Operations in the test
await page.
goto
(
'https://example.com'
)
;
// Assertions in the test
await expect
(
page
)
.
toHaveTitle
(
/Example/
)
;
}
)
;
Parameter Description
| Parameters | Type | Description |
|---|---|---|
| First Parameter (Test Name) | string |
A descriptive name for the test, shown in test reports and logs |
| Second Parameter (Test Function) | async (fixtures) => {} |
An async function containing the test's actual logic |
What Is the page Fixture
{ page } is destructuring assignment in the test function parameters; it extracts from the fixtures object provided by Playwright page。
page is a Page object representing a browser tab.
Each test has its own independent page Example:
| fixture | Type | Description |
|---|---|---|
page |
Page Object | An independent browser tab for navigation, actions, and assertions |
context |
BrowserContext Object | Browser context, managing cookies, storage, etc. |
browser |
Browser Object | Browser instance, rarely used directly |
request |
APIRequestContext Object | Used to send HTTP requests (not through the browser) |
The most commonly used is page, and the vast majority of operations are done through it.
page.goto() Navigation
page.goto(url) is one of the most basic operations in Playwright, used to navigate to a specified URL.
Example
// Basic usage
await page.
goto
(
'https://www.runoob.com/'
)
;
// Navigation with options
await page.
goto
(
'https://www.runoob.com/'
,
{
// Wait until the 'load' event fires (default)
waitUntil
:
'load'
,
// Operation timeout (milliseconds)
timeout
:
30000
,
// Referrer (Referer header)
referer
:
'https://www.google.com/'
,
}
)
;
waitUntil The option has three possible values:
| Value | Meaning | Applicable scenarios |
|---|---|---|
'load' |
Wait for the load event to fire | The default value, suitable for most pages |
'domcontentloaded' |
Wait for the DOMContentLoaded event | Just wait for the basic page structure to load |
'networkidle' |
Wait for the network to be idle (no new requests within 500 ms) | Needed when you must wait for all async data to load |
In the vast majority of cases, use the default
'load'and that's enough; Playwright's auto-waiting mechanism handles subsequent element availability issues, so there is no need to use'networkidle'。
expect() Assertions
expect() is Playwright's assertion method, used to verify whether test results meet expectations.
Example
// Page title assertion
await expect
(
page
)
.
toHaveTitle
(
/RUNOOB/
)
;
// Page URL assertion
await expect
(
page
)
.
toHaveURL
(
'https://www.runoob.com/'
)
;
// Element visibility assertion
await expect
(
page.
getByText
(
'RUNOOB'
)
)
.
toBeVisible
(
)
;
Playwright's assertions have a key feature: automatic retry.
If an assertion condition is temporarily unmet, Playwright keeps retrying until the condition is met or a timeout occurs.
This means you don't need to write waitFor or sleep to wait for the page to be ready.
The async/await Pattern in Tests
All Playwright operations are asynchronous, so the test function must be declared as async。
All browser actions (goto、click、fill etc.) and assertions (expect) must use await The const keyword.
Example
// Correct usage
test
(
'Correct Example'
,
async
(
{
page
}
)
=>
{
await page.
goto
(
'https://www.runoob.com/'
)
;
await expect
(
page
)
.
toHaveTitle
(
/RUNOOB/
)
;
}
)
;
// Incorrect usage (missing await)
test
(
'Incorrect Example'
,
async
(
{
page
}
)
=>
{
page.
goto
(
'https://www.runoob.com/'
)
;
// No await!
expect
(
page
)
.
toHaveTitle
(
/RUNOOB/
)
;
// No await!
}
)
;
If you forget to write
await, the test may assert at the wrong time, causing flakiness or unexpected passes. Be sure to addawait。
Understanding Test Output
Let's look at another test with a deliberately failing assertion to understand test output:
Example
// File path: tests/fail-demo.spec.ts
import
{
test
,
expect
}
from
'@playwright/test'
;
test
(
'A Deliberately Failing Test'
,
async
(
{
page
}
)
=>
{
await page.
goto
(
'https://www.runoob.com/'
)
;
// Assert the title contains nonexistent content; expected to fail
await expect
(
page
)
.
toHaveTitle
(
/nonexistent text/
)
;
}
)
;
After running, the output is roughly as follows:
Running 1 test using 1 worker
✘ 1 [chromium] › tests/fail-demo.spec.ts:4:1 › 一个故意失败的测试 (5.2s)
1 failed
tests/fail-demo.spec.ts:4:1 › 一个故意失败的测试
An HTML report is also opened automatically, showing detailed failure information and error screenshots.