参数化测试
example.spec.ts
[
{ name: 'Alice', expected: 'Hello, Alice!' },
{ name: 'Bob', expected: 'Hello, Bob!' },
{ name: 'Charlie', expected: 'Hello, Charlie!' },
].forEach(({ name, expected }) => {
// You can also do it with test.describe() or with multiple tests as long the test name is unique.
test(testing with ${name}
, async ({ page }) => {
await page.goto(https://example.com/greet?name=${name}
);
await expect(page.getByRole('heading')).toHaveText(expected);
});
});
钩子前后
大多数情况下,你应该将 beforeEach、beforeAll、afterEach 和 afterAll 钩子放在 forEach 之外,这样钩子只会执行一次:
test.beforeEach(async ({ page }) => {
// ...
});
test.afterEach(async ({ page }) => {
// ...
});
[
{ name: 'Alice', expected: 'Hello, Alice!' },
{ name: 'Bob', expected: 'Hello, Bob!' },
{ name: 'Charlie', expected: 'Hello, Charlie!' },
].forEach(({ name, expected }) => {
test(testing with ${name}
, async ({ page }) => {
await page.goto(https://example.com/greet?name=${name}
);
await expect(page.getByRole('heading')).toHaveText(expected);
});
});
如果你希望每个测试都有钩子,你可以将它们放在 describe() 中 - 因此它们会在每次迭代/每个单独的测试中执行:
example.spec.ts
[
{ name: 'Alice', expected: 'Hello, Alice!' },
{ name: 'Bob', expected: 'Hello, Bob!' },
{ name: 'Charlie', expected: 'Hello, Charlie!' },
].forEach(({ name, expected }) => {
test.describe(() => {
test.beforeEach(async ({ page }) => {
await page.goto(https://example.com/greet?name=${name}
);
});
test(testing with ${expected}
, async ({ page }) => {
await expect(page.getByRole('heading')).toHaveText(expected);
});
});
});
参数化项目
Playwright Test 支持同时运行多个测试项目。在下面的示例中,我们将运行两个具有不同选项的项目。
我们声明选项 person 并在配置中设置该值。第一个项目以值 Alice 运行,第二个项目以值 Bob 运行。
TypeScript
JavaScript
my-test.ts
import { test as base } from '@playwright/test';
export type TestOptions = {
person: string;
};
export const test = base.extend({
// Define an option and provide a default value.
// We can later override it in the config.
person: ['John', { option: true }],
});
我们可以在测试中使用该选项,与 fixtures 类似。
example.spec.ts
import { test } from './my-test';
test('test 1', async ({ page, person }) => {
await page.goto(/index.html
);
await expect(page.locator('#node')).toContainText(person);
// ...
});
现在,我们可以使用项目在多种配置中运行测试。
TypeScript
JavaScript
playwright.config.ts
import { defineConfig } from '@playwright/test';
import type { TestOptions } from './my-test';
export default defineConfig({
projects: [
{
name: 'alice',
use: { person: 'Alice' },
},
{
name: 'bob',
use: { person: 'Bob' },
},
]
});
我们还可以在夹具中使用该选项。了解有关 fixtures 的更多信息。
my-test.ts
import { test as base } from '@playwright/test';
export type TestOptions = {
person: string;
};
export const test = base.extend({
// Define an option and provide a default value.
// We can later override it in the config.
person: ['John', { option: true }],
// Override default "page" fixture.
page: async ({ page, person }, use) => {
await page.goto('/chat');
// We use "person" parameter as a "name" for the chat room.
await page.getByLabel('User Name').fill(person);
await page.getByText('Enter chat room').click();
// Each test will get a "page" that already has the person name.
await use(page);
},
});
注意
参数化项目行为在版本 1.18 中已更改。了解更多。
传递环境变量
你可以使用环境变量从命令行配置测试。