Skip to main content

参数化测试

您可以在测试级别或项目级别参数化测试。

参数化测试

// example.spec.ts
const people = ['Alice', 'Bob'];
for (const name of people) {
test(`testing with ${name}`, async () => {
// ...
});
// 您也可以使用 test.describe() 或多个测试来执行此操作,只要测试名称是唯一的。
}

参数化项目

Playwright Test 支持同时运行多个测试项目。在以下示例中,我们将使用不同的选项运行两个项目。

我们声明选项 person 并在配置中设置值。第一个项目使用值 Alice 运行,第二个使用值 Bob 运行。

// my-test.ts
import { test as base } from '@playwright/test';

export type TestOptions = {
person: string;
};

export const test = base.extend<TestOptions>({
// 定义一个选项并提供默认值。
// 我们稍后可以在配置中覆盖它。
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);
// ...
});

现在,我们可以使用项目在多个配置中运行测试。

// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
import { TestOptions } from './my-test';

const config: PlaywrightTestConfig<TestOptions> = {
projects: [
{
name: 'alice',
use: { person: 'Alice' },
},
{
name: 'bob',
use: { person: 'Bob' },
},
]
};
export default config;

我们还可以在 fixture 中使用该选项。了解更多关于 fixtures 的信息。

// my-test.ts
import { test as base } from '@playwright/test';

export type TestOptions = {
person: string;
};

export const test = base.test.extend<TestOptions>({
// 定义一个选项并提供默认值。
// 我们稍后可以在配置中覆盖它。
person: ['John', { option: true }],

// 覆盖默认的 "page" fixture。
page: async ({ page, person }, use) => {
await page.goto('/chat');
// 我们使用 "person" 参数作为聊天室的 "name"。
await page.getByLabel('User Name').fill(person);
await page.getByText('Enter chat room').click();
// 每个测试将获得一个已经有人名的 "page"。
await use(page);
},
});
note

参数化项目行为在版本 1.18 中已更改。了解更多

传递环境变量

您可以使用环境变量从命令行配置测试。

例如,考虑以下需要用户名和密码的测试文件。通常最好不要将您的秘密存储在源代码中,因此我们需要一种从外部传递秘密的方法。

// example.spec.ts
test(`example test`, async ({ page }) => {
// ...
await page.getByLabel('User Name').fill(process.env.USERNAME);
await page.getByLabel('Password').fill(process.env.PASSWORD);
});

您可以在命令行中设置秘密用户名和密码来运行此测试。

USERNAME=me PASSWORD=secret npx playwright test

同样,配置文件也可以读取通过命令行传递的环境变量。

// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';

const config: PlaywrightTestConfig = {
use: {
baseURL: process.env.STAGING === '1' ? 'http://staging.example.test/' : 'http://example.test/',
}
};
export default config;

现在,您可以针对暂存或生产环境运行测试:

STAGING=1 npx playwright test

.env 文件

为了使环境变量更易于管理,请考虑使用 .env 文件之类的东西。以下是使用 dotenv 包直接在配置文件中读取环境变量的示例。

// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
import dotenv from 'dotenv';
import path from 'path';

// 从默认的 ".env" 文件读取。
dotenv.config();

// 或者,从 "../my.env" 文件读取。
dotenv.config({ path: path.resolve(__dirname, '..', 'my.env') });

const config: PlaywrightTestConfig = {
use: {
baseURL: process.env.STAGING === '1' ? 'http://staging.example.test/' : 'http://example.test/',
}
};
export default config;

现在,您只需编辑 .env 文件即可设置任何您想要的变量。

# .env file
STAGING=0
USERNAME=me
PASSWORD=secret

像往常一样运行测试,您的环境变量应该会被选取。

npx playwright test

通过 CSV 文件创建测试

Playwright 测试运行器在 Node.js 中运行,这意味着您可以直接从文件系统读取文件并使用您喜欢的 CSV 库解析它们。

例如,请参阅此 CSV 文件,在我们的示例中为 input.csv

"test_case","some_value","some_other_value"
"value 1","value 11","foobar1"
"value 2","value 22","foobar21"
"value 3","value 33","foobar321"
"value 4","value 44","foobar4321"

基于此,我们将使用 NPM 的 csv-parse 库生成一些测试:

// foo.spec.ts
import fs from 'fs';
import path from 'path';
import { test } from '@playwright/test';
import { parse } from 'csv-parse/sync';

const records = parse(fs.readFileSync(path.join(__dirname, 'input.csv')), {
columns: true,
skip_empty_lines: true
});

for (const record of records) {
test(`fooo: ${record.test_case}`, async ({ page }) => {
console.log(record.test_case, record.some_value, record.some_other_value);
});
}