在 Playwrigth 中组合灯具

问题描述 投票:0回答:1

我想要什么

我想要一种在用户登录的情况下运行测试的方法,并使用该用户使用固定装置进行 POM 操作。 例如,

  1. 管理员用户的第一个场景:我将创建一个{Page}并使用管理员用户信息设置他的存储状态,然后在另一个装置中使用此页面进行POM操作
  2. Operator 用户的第二个场景:我将创建一个 {Page} 并使用 Operator 用户数据设置他的 storageState,然后在另一个固定装置中使用此页面进行 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)
  },
})

作为最后一步,我想在测试中使用它们,其中:

  1. 首先我设置了一个记录的{Page}
  2. 使用记录的{Page}进行POM操作; 像这样的:
**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) => { }
playwright
1个回答
0
投票

只需使用合并

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

© www.soinside.com 2019 - 2024. All rights reserved.