पाठ 4 / 25
Tests and Fixtures
test, page and isolation.
What a Playwright test receives
You import test and expect from @playwright/test. Each test is an async function that destructures fixtures from its first argument. page is the most common: a fresh tab in a fresh browser context, which is like a new incognito profile, so cookies and storage never leak between tests. Other built-in fixtures include context, browser, browserName and request (an API client). Hooks such as test.beforeEach run setup, and test.describe groups tests. Tests in a file run in order by default, while files run in parallel across workers.
Find elements like a user, assert like a user
Playwright tests receive fixtures such as page, locate elements by role and label, and wait automatically.
A test with a hook and a group
Each test gets its own isolated page.
import { test, expect } from '@playwright/test';
test.describe('todo list', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/todos');
});
test('adds a todo', async ({ page }) => {
await page.getByPlaceholder('What needs to be done?').fill('Buy milk');
await page.getByPlaceholder('What needs to be done?').press('Enter');
await expect(page.getByRole('listitem')).toHaveText(['Buy milk']);
});
test('starts empty', async ({ page }) => {
await expect(page.getByRole('listitem')).toHaveCount(0);
});
});A clean hotel room for every guest
Each test checks into a freshly cleaned room (a new browser context): no leftover cookies, no previous guest's luggage in local storage.
त्वरित जाँच: Why does each Playwright test get a new browser context?
- To download a new browser each time
- To isolate cookies and storage so tests do not affect each other
- To share login state automatically between tests
- To make tests run in a fixed order
Answer
To isolate cookies and storage so tests do not affect each other — Isolation is the default.