typescript 如何组成装置在剧作家

bvn4nwqk  于 2023-05-19  发布在  TypeScript
关注(0)|答案(2)|浏览(142)

我想组成固定装置。第一个fixture应该总是可用的(把它看作基类)。第二个fixture在不同的测试文件中会有所不同(把它看作是派生类)我试着按照下面的代码运行,它像我预期的那样工作。是否可以遵循此方法或任何更好的选择?

//baseFixture.js
import { test as base} from '@playwright/test';
interface MyFixtures {
  fixture1: string;
}

export const test = base.extend<MyFixtures>({
  fixture1: "fixture-one"
}, );

//derivedFixture.js

import {test as test1} from 'baseFixture'
interface MyFixtures2 {
  fixture2: string;
}

export const test = test1.extend<MyFixtures2>({
  fixture2: "fixture-two"
}, );

//in test_file.js

import {test} from 'derivedFixture'

  test('should allow me use composed fixture', async ({ page, fixture1, fixture2 }) => {
     console.log(`from first fixture ${fixture1}`)
     console.log(`from second fixture ${fixture2}`)
  });
5cg8jx4n

5cg8jx4n1#

在我看来,你正在使用像POM这样的夹具,并且你正在过度设计测试。**如果它适合你,并取决于你想要的,然后使用它。**如果我的假设是正确的,而不是传递夹具到另一个夹具传递POM,你甚至可以在这里执行步骤,让该页面进入特定状态,这里是从剧作家页面的例子:

// my-test.js
const base = require('@playwright/test');
const { TodoPage } = require('./todo-page');
const { SettingsPage } = require('./settings-page');

// 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.
exports.test = base.test.extend({
  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));
  },
});
exports.expect = base.expect;

然后在你的测试中简单地传递{todoPage} or {settingsPage}或两者:

const { test, expect } = require('./my-test');

test.beforeEach(async ({ settingsPage }) => {
  await settingsPage.switchToDarkMode();
});

test('basic test', async ({ todoPage, page }) => {
  await todoPage.addToDo('something nice');
  await expect(page.locator('.todo-item')).toContainText(['something nice']);
});

你也可以在这里链接你的固定装置并重复使用它们,例如。你可以将todoPage传递给settingsPage fixture:

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

这样,todoPage中的所有内容都将被执行,然后是settingsPage,这就是你传递给测试的内容,我猜这就是你试图实现的。

hc2pp10m

hc2pp10m2#

我的方法是将基础夹具用作衍生夹具中的从属夹具:

import { test as base } from "@playwright/test"
interface MyFixtures1 {
    fixture1: string
}

export const testBase = base.extend<{}, MyFixtures1>({
    fixture1: [
        async ({}, use) => {
            console.log("fixture1 setup once per worker")
            use("one")
            console.log("fixture1 teardown once per worker")
        },
        { scope: "worker" }
    ]
})

interface MyFixtures2 {
    fixture2: string
}

export const test = testBase.extend<MyFixtures2>({
    fixture2: async ({ fixture1 }, use) => {
        console.log("fixture2 setup for each test")
        use(`two-${fixture1}`)
        console.log("fixture2 teardown for each test")
    },
})

test("should allow me use composed fixture", async ({ fixture1, fixture2 }) => {
    console.log(`from first fixture ${fixture1}`)
    console.log(`from second fixture ${fixture2}`)
})

test("should base", async ({ fixture1 }) => {
    console.log(`from first fixture ${fixture1}`)
})

test("should derived", async ({ fixture2 }) => {
    console.log(`from second fixture ${fixture2}`)
})

关于如何在docs中使用fixture的更多信息

相关问题