You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
haoliang-net/frontend/e2e/simulator.spec.ts

107 lines
4.0 KiB
TypeScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* CNC管理后台 - 模拟采集模块 Playwright E2E测试
*
* 测试范围IIS模式真实后端模拟器未启动
* - 侧边栏导航:模拟采集菜单项可见可点击
* - 总览页(/simulator断连状态UI——未连接提示、按钮禁用
*
* 运行方式:`npx playwright test e2e/simulator.spec.ts --project=chromium`
* 前提IIS站点已部署最新前后端代码无需启动CncSimulator
*/
import { test, expect, type Page } from '@playwright/test'
// === 登录辅助函数 ===
// 使用完整URL避免baseURL(127.0.0.1)与IIS绑定(192.168.1.202)不匹配
const BASE = 'http://192.168.1.202/admin'
async function login(page: Page) {
await page.goto(`${BASE}/login`)
await page.waitForLoadState('networkidle')
await page.waitForSelector('input', { timeout: 10000 })
const inputs = page.locator('input')
await inputs.nth(0).fill('admin')
await inputs.nth(1).fill('admin123')
await page.locator('button').last().click()
await page.waitForURL(/\/(dashboard|admin\/?$)/, { timeout: 15000 })
}
// ============================================================
// 套件1侧边栏导航
// ============================================================
test.describe('模拟采集侧边栏导航', () => {
test.beforeEach(async ({ page }) => {
await login(page)
})
test('侧边栏有"模拟采集"菜单项', async ({ page }) => {
const menuItems = page.locator('.el-menu-item')
const simulatorItem = menuItems.filter({ hasText: '模拟采集' })
await expect(simulatorItem).toBeVisible()
})
test('点击"模拟采集"菜单跳转到总览页', async ({ page }) => {
const menuItems = page.locator('.el-menu-item')
const simulatorItem = menuItems.filter({ hasText: '模拟采集' })
await simulatorItem.click()
await page.waitForURL(/\/simulator$/, { timeout: 10000 })
expect(page.url()).toContain('/simulator')
})
})
// ============================================================
// 套件2总览页——模拟器未连接状态
// ============================================================
test.describe('模拟采集总览页(模拟器未连接)', () => {
test.beforeEach(async ({ page }) => {
await login(page)
// 通过侧边栏菜单导航到模拟采集页面
const menuItems = page.locator('.el-menu-item')
const simulatorItem = menuItems.filter({ hasText: '模拟采集' })
await simulatorItem.click()
await page.waitForURL(/\/simulator$/, { timeout: 10000 })
// 等待ping API调用完成
await page.waitForTimeout(2000)
})
test('页面显示"模拟器未连接"', async ({ page }) => {
await expect(page.locator('text=模拟器未连接')).toBeVisible()
})
test('页面显示友好提示信息', async ({ page }) => {
await expect(page.locator('text=模拟器未启动')).toBeVisible()
})
test('三个操作按钮正确禁用', async ({ page }) => {
await expect(page.locator('button:has-text("全部启动")')).toBeDisabled()
await expect(page.locator('button:has-text("全部停止")')).toBeDisabled()
await expect(page.locator('button:has-text("刷新配置")')).toBeDisabled()
})
test('面包屑导航显示"首页 / 模拟采集"', async ({ page }) => {
const breadcrumb = page.locator('.el-breadcrumb')
await expect(breadcrumb).toBeVisible()
await expect(breadcrumb.locator('text=首页')).toBeVisible()
await expect(breadcrumb.locator('text=模拟采集')).toBeVisible()
})
test('页面无JavaScript异常排除预期的网络错误', async ({ page }) => {
const errors: string[] = []
page.on('console', (msg) => {
if (msg.type() === 'error') errors.push(msg.text())
})
await page.reload({ waitUntil: 'networkidle' })
await page.waitForTimeout(3000)
// 过滤掉模拟器不可达的预期错误
const realErrors = errors.filter(e =>
!e.includes('simulator') &&
!e.includes('50001') &&
!e.includes('Network Error') &&
!e.includes('ERR_CONNECTION_REFUSED') &&
!e.includes('404')
)
expect(realErrors).toEqual([])
})
})