【问题标题】:Playwright save storage state only for certain files剧作家只为某些文件保存存储状态
【发布时间】:2022-02-16 20:30:22
【问题描述】:

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

                storageState: 'myauth.json',

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

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

【问题讨论】:

    标签: playwright playwright-test


    【解决方案1】:

    您可以使用固定装置,并将您使用的固定装置设置为默认值 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' });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      • 2016-04-21
      • 2022-11-14
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      相关资源
      最近更新 更多