import { expect, Page } from '@playwright/test' export const USERNAME = process.env.ERP_USER || 'admin' export const PASSWORD = process.env.ERP_PASS || 'admin123' const PATH_TO_MENU_TEXT: Record = { '/production/report/need': '采购计划需求表', '/production/plan-order': '生产计划单', '/production/purchase-plan': '采购计划单', '/production/work-order': '生产订单', '/purchasing/checkin': '采购到货单', '/purchasing/order': '采购订单', '/rd/ebom': '产品BOM', '/warehouse/issue': '生产领料单' } export async function closeMessageBox(page: Page) { const closeBtn = page.getByRole('button', { name: /确 定|确定|关 闭|关闭|知道了/ }).first() if (await closeBtn.count() > 0) { try { await closeBtn.click() await page.waitForTimeout(300) } catch (e) { } } const overlay = page.locator('.el-overlay-message-box') if (await overlay.count() > 0) { try { await page.press('Escape') await page.waitForTimeout(300) } catch (e) { } } } export async function login(page: Page) { await page.goto('/') await page.waitForLoadState('networkidle') const accountInput = page.getByPlaceholder('账号').first() if (!(await accountInput.count())) { await page.waitForLoadState('networkidle') return } await accountInput.fill(USERNAME) const passwordInput = page.getByPlaceholder('密码').first() if (await passwordInput.count()) { await passwordInput.fill(PASSWORD) } const codeInput = page.getByPlaceholder('验证码').first() if (await codeInput.count()) { await codeInput.fill('0000') } await page.getByRole('button', { name: /登 录/ }).click() await expect(page).toHaveURL(/\/(dashboard|$)/) await page.waitForLoadState('networkidle') await page.waitForTimeout(1000) await closeMessageBox(page) } export async function clickSubMenu(page: Page, title: string) { await closeMessageBox(page) const subMenu = page.locator('.el-sub-menu__title', { hasText: title }) await subMenu.waitFor({ state: 'visible', timeout: 10000 }) await subMenu.click() await page.waitForTimeout(300) } export async function clickMenuItem(page: Page, path: string) { await closeMessageBox(page) const menuText = PATH_TO_MENU_TEXT[path] || path const menuItem = page.locator('.el-menu-item', { hasText: menuText }) await menuItem.waitFor({ state: 'visible', timeout: 10000 }) await menuItem.click() await page.waitForLoadState('networkidle') await page.waitForTimeout(500) await closeMessageBox(page) } export async function expectBasicList(page: Page) { await closeMessageBox(page) await expect(page.locator('.el-form').first()).toBeVisible() await expect(page.locator('.el-table').first()).toBeVisible() }