1

我目前有一个包含 >50 个文件的测试包。其中 49 个文件将使用相同的身份验证,因此我在我的 playwright 配置文件中设置了以下内容:

                storageState: 'myauth.json',

这允许我存储状态并在所有测试中使用它,问题就变成了我不想在我的一个测试中使用这个状态的地方。怎么办?

我知道我可以将状态传递到 49 个文件中并忽略它,但这似乎是错误的想法。

4

1 回答 1

0

您可以使用固定装置,并将您使用 49 次的固定装置设置为默认值。在这种情况下,当您需要其他身份验证时,只需用关键字“use”覆盖它。它在剧作家文档中有描述。https://playwright.dev/docs/test-fixtures

import { TodoPage } from './todo-page';  //In your case auth1 auth2
import { SettingsPage } from './settings-page';

// Declare the types of your fixtures.
type MyFixtures = {
  todoPage: TodoPage;
  settingsPage: SettingsPage;
};

// Extend base test by providing "todoPage" and "settingsPage".
// This new "test" can be used in multiple test files, and each of them will get the fixtures.
export const test = base.extend<MyFixtures>({
  todoPage: async ({ page }, use) => {
    // Set up the fixture.
    const todoPage = new TodoPage(page);
    await todoPage.goto();
    await todoPage.addToDo('item1');
    await todoPage.addToDo('item2');

    // Use the fixture value in the test.
    await use(todoPage);

    // Clean up the fixture.
    await todoPage.removeAll();
  },

  settingsPage: async ({ page }, use) => {
    await use(new SettingsPage(page));
  },
});

当您在测试中分页时使用默认身份验证。当您需要覆盖特定文件 spec.ts 文件时,只需编写:

test.use({ auth: 'reareAuth' });

于 2022-02-16T12:30:22.043 回答