import { expect, type Locator, type Page } from '@playwright/test'; export class HomeNavigation { page: Page; $$moduleList: Locator; subPages: { name: string; url?: string[] }[]; /** * @param {Page} page */ constructor(page: Page) { this.page = page; this.$$moduleList = this.page.locator('.left_box .link_item'); this.subPages = [ { name: '收银' }, { name: '预约' }, { name: '顾客' }, { name: '流水' }, { name: '库存' }, { name: '目标' }, { name: '营销' }, { name: '报表' }, { name: '设置' }, { name: '其他' }, ]; } /** * 跳转到对应模块 * @param {string} pageName 模块名称 * - 收银 * - 预约 * - 顾客 * - 流水 * - 库存 * - 目标 * - 营销 * - 报表 * - 设置 * - 其他 */ gotoModule = async (pageName: string) => { const $active = this.$$moduleList.locator('.active_arrow'); if (!this.subPages.some(item => item.name === pageName)) { throw new Error(`Page ${pageName} does not exist`); } // 模块定位器 const $module = this.$$moduleList.filter({ hasText: pageName }); // 模块选中按钮 const $moduleActive = $module.locator('.active_arrow'); // 等待选中按钮出现 await $active.waitFor(); await expect(async () => { if (!(await $moduleActive.isVisible())) { await $module.click(); } await $moduleActive.waitFor({ timeout: 5000 }); }).toPass({ timeout: 10_000 }); }; }