使用 Playwright 在单个测试中使用多个测试装置

Twi*_*per 6 typescript playwright playwright-test

// foo.ts
import { test as base } from "@playwright/test";

const test = base.extend<{foo: string}>({
    foo: "hello"
});

export { test };
Run Code Online (Sandbox Code Playgroud)
// bar.ts
import { test as base } from "@playwright/test";

const test = base.extend<{bar: string}>({
    bar: "bye"
});

export { test };
Run Code Online (Sandbox Code Playgroud)
// index.ts
import { test } from /* BOTH_FOO_AND_BAR??? */;

test("Basic test", async ({ foo, bar }) => { // <-- I want to be able to use both foo and bar fixture here
    console.log(foo);
    console.log(bar);
});
Run Code Online (Sandbox Code Playgroud)

能达到上面的效果吗?或者我必须让一个像这样依赖另一个?

// bar.ts
import { test as base } from "./foo";
Run Code Online (Sandbox Code Playgroud)
// index.ts
import { test } from "./bar";
Run Code Online (Sandbox Code Playgroud)

如果我有很多文件并且导入最后一个文件将导入所有文件,这将创建一个长链。如果可能的话,我更喜欢挑选和匹配。

pba*_*ski 3

从 Playwright 1.39 开始,您可以简单地合并固定装置https://playwright.dev/docs/release-notes#merge-test-fixtures

固定装置.ts

import { mergeTests } from '@playwright/test';
import { test as dbTest } from 'database-test-utils';
import { test as a11yTest } from 'a11y-test-utils';

export const test = mergeTests(dbTest, a11yTest);
Run Code Online (Sandbox Code Playgroud)

测试规格

import { test } from './fixtures';

test('passes', async ({ database, page, a11y }) => {
  // use database and a11y fixtures.
});
Run Code Online (Sandbox Code Playgroud)