Skip to main content

夹具

Playwright 测试基于 测试治具 的概念。测试夹具用于为每个测试建立环境,为测试提供所需的一切,仅此而已。

Playwright Test is based on the concept of the test fixtures. Test fixtures are used to establish environment for each test, giving the test everything it needs and nothing else.

Playwright Test 查看每个测试声明,分析测试所需的夹具集,并专门为测试准备这些夹具。由装置准备的值被合并到单个对象中,该对象可作为第一个参数供 test、钩子、注释和其他装置使用。

Playwright Test looks at each test declaration, analyses the set of fixtures the test needs and prepares those fixtures specifically for the test. Values prepared by the fixtures are merged into a single object that is available to the test, hooks, annotations and other fixtures as a first parameter.

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

test('basic test', async ({ page }) => {
// ...
});

根据上面的测试,Playwright Test 将在运行测试之前设置 page 夹具,并在测试完成后将其拆除。page 夹具提供了可用于测试的 页面 对象。

Given the test above, Playwright Test will set up the page fixture before running the test, and tear it down after the test has finished. page fixture provides a Page object that is available to the test.

Playwright Test 附带下面列出的内置装置,你也可以添加自己的装置。Playwright 测试还通过 提供选项 来配置 fixtures.browserfixtures.contextfixtures.page

Playwright Test comes with builtin fixtures listed below, and you can add your own fixtures as well. Playwright Test also provides options to configure fixtures.browser, fixtures.context and fixtures.page.


属性

Properties

browser

Added in: v1.10 fixtures.browser

浏览器 实例在 同一个工作线程 中的所有测试之间共享 - 这使得测试变得高效。然而,每个测试都在隔离的 BrowserContext 中运行并获得一个全新的环境。

Browser instance is shared between all tests in the same worker - this makes testing efficient. However, each test runs in an isolated BrowserContext and gets a fresh environment.

了解如何 配置浏览器 并查看 可用选项

Learn how to configure browser and see available options.

用法

Usage

test.beforeAll(async ({ browser }) => {
const page = await browser.newPage();
// ...
});

类型

Type


browserName

Added in: v1.10 fixtures.browserName

运行测试的浏览器的名称。默认为 'chromium'。对 注释测试 基于浏览器很有用。

Name of the browser that runs tests. Defaults to 'chromium'. Useful to annotate tests based on the browser.

用法

Usage

test('skip this test in Firefox', async ({ page, browserName }) => {
test.skip(browserName === 'firefox', 'Still working on it');
// ...
});

类型

Type

  • "chromium"|"firefox"|"webkit"

context

Added in: v1.10 fixtures.context

为每个测试创建的隔离 BrowserContext 实例。由于上下文彼此隔离,因此每个测试都会获得一个全新的环境,即使多个测试在单个 浏览器 中运行以实现最高效率也是如此。

Isolated BrowserContext instance, created for each test. Since contexts are isolated between each other, every test gets a fresh environment, even when multiple tests run in a single Browser for maximum efficiency.

了解如何 配置上下文 并查看 可用选项

Learn how to configure context and see available options.

默认 fixtures.page 属于此上下文。

Default fixtures.page belongs to this context.

用法

Usage

test('example test', async ({ page, context }) => {
await context.route('*external.com/*', route => route.abort());
// ...
});

类型

Type


page

Added in: v1.10 fixtures.page

为每个测试创建的隔离 页面 实例。由于 fixtures.context 隔离,测试之间的页面是隔离的。

Isolated Page instance, created for each test. Pages are isolated between tests due to fixtures.context isolation.

这是测试中最常用的夹具。

This is the most common fixture used in a test.

用法

Usage

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

test('basic test', async ({ page }) => {
await page.goto('/signin');
await page.getByLabel('User Name').fill('user');
await page.getByLabel('Password').fill('password');
await page.getByText('Sign in').click();
// ...
});

类型

Type


request

Added in: v1.10 fixtures.request

每个测试隔离 APIRequestContext 个实例。

Isolated APIRequestContext instance for each test.

用法

Usage

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

test('basic test', async ({ request }) => {
await request.post('/signin', {
data: {
username: 'user',
password: 'password'
}
});
// ...
});

类型

Type