我想要一种在用户登录的情况下运行测试的方法,并使用该用户使用固定装置进行 POM 操作。 例如,
我创建了一个固定装置,为每个用户初始化一个新页面及其存储-
**setStorageState.fixture.ts**
type MyFixtures = {
customer_admin_Page: Page
customer_operator_Page: Page
}
const filePath = `${process.env.PROJECT_PATH}/temp/users/${process.env.ENV}/generated/`
import { test as base, type Page } from "@playwright/test"
export * from "@playwright/test"
export const test = base.extend<MyFixtures>({
customer_admin_Page: async ({ browser }, use) => {
const fileName = "rmaintainer.json"
const context = await browser.newContext({ storageState: filePath + fileName })
const newPage = await context.newPage()
await use(newPage)
await context.close()
},
customer_operator_Page: async ({ browser }, use) => {
const fileName = "roperator.json"
const context = await browser.newContext({ storageState: filePath + fileName })
const newPage = await context.newPage()
await use(newPage)
await context.close()
},
})
比我创建另一个遵循 POM 的装置
**machinegroups.fixture.ts**
import { test as base } from "@playwright/test"
import { MachineGroups } from "/src/pom/pages/machinegroups/machinegroups.page"
export const test = base.extend<{ machineGroup: MachineGroups }>({
//Define a fixture
machineGroup: async ({ page }, use) => {
const machineGroup = new MachineGroups(page)
await use(machineGroup)
},
})
作为最后一步,我想在测试中使用它们,其中:
**addgroup.spec.ts**
import { test } from "../fixtures/machinegroups/machinegroups.fixtures"
import { test as pippo } from "../fixtures/users/local/setStorageState.fixtures"
test.describe("Admin user", () => {
test("Admin user should add a new group", async ({ customer_admin_Page, machineGroup }) => {
await machineGroup.clickBtnAddGroup()
})
})
test.describe("Operator user", () => {
test(
"Operator user could not add a new group",
async ({ customer_operator_Page, machineGroup }) => {
await machineGroup.clickBtnAddGroup()
},
)
})
这显然行不通。我的想法是找到将 cutom_operator_page/customer_admin_Page 传递到 machineGroup 夹具中使用的页面的方法。 例如,当我使用管理员用户登录时,page in
machineGroup: async ({ **page** }, use) => { }
将被测试页面自动验证:例如
machineGroup: async ({ **customer_admin_Page** }, use) => { }
只需使用合并
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);
test('passes', async ({ database, page, a11y }) => {
// use database and a11y fixtures.
});
https://playwright.dev/docs/test-fixtures#combine-custom-fixtures-from-multiple-modules