1
0
Files
nex/frontend/e2e/validation.spec.ts
lanyuanxiaoyao 59179094ed feat: E2E 测试集成真实后端
- Playwright 双 webServer 模式自动启动 Go 后端 + Vite 前端
- 后端使用临时 SQLite 数据库隔离,固定端口 19026
- vite.config.ts proxy target 动态读取环境变量
- 新增 sql.js 依赖用于 SQLite 统计数据 seed
- 新增 e2e/fixtures.ts 共享工具模块(API seed + SQLite seed)
- 拆分测试文件 5→7(providers/models/stats/navigation/validation)
- 删除旧文件 crud.spec.ts/sidebar.spec.ts/stats-cards.spec.ts
- E2E 测试尚有部分用例需调试修复
2026-04-22 00:31:35 +08:00

76 lines
2.8 KiB
TypeScript

import { test, expect } from '@playwright/test'
function formInputs(page: import('@playwright/test').Page) {
const dialog = page.locator('.t-dialog:visible')
return {
id: dialog.locator('input[placeholder="例如: openai"]'),
name: dialog.locator('input[placeholder="例如: OpenAI"]'),
apiKey: dialog.locator('input[type="password"]'),
baseUrl: dialog.locator('input[placeholder="例如: https://api.openai.com/v1"]'),
saveBtn: dialog.locator('.t-dialog__footer').getByRole('button', { name: '保存' }),
cancelBtn: dialog.locator('.t-dialog__footer').getByRole('button', { name: '取消' }),
}
}
test.describe('供应商表单验证', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/providers')
await expect(page.getByRole('heading', { name: '供应商管理' })).toBeVisible()
})
test('应显示必填字段验证', async ({ page }) => {
await page.getByRole('button', { name: '添加供应商' }).click()
await expect(page.locator('.t-dialog')).toBeVisible()
await formInputs(page).saveBtn.click()
await expect(page.getByText('请输入供应商 ID')).toBeVisible()
await expect(page.getByText('请输入名称')).toBeVisible()
await expect(page.getByText('请输入 API Key')).toBeVisible()
await expect(page.getByText('请输入 Base URL')).toBeVisible()
})
test('应验证URL格式', async ({ page }) => {
await page.getByRole('button', { name: '添加供应商' }).click()
await expect(page.locator('.t-dialog')).toBeVisible()
const inputs = formInputs(page)
await inputs.id.fill('test_url')
await inputs.name.fill('Test')
await inputs.apiKey.fill('sk_test')
await inputs.baseUrl.fill('not-a-url')
await inputs.saveBtn.click()
await expect(page.getByText('请输入有效的 URL')).toBeVisible()
})
test('取消后表单应重置', async ({ page }) => {
await page.getByRole('button', { name: '添加供应商' }).click()
await expect(page.locator('.t-dialog')).toBeVisible()
let inputs = formInputs(page)
await inputs.id.fill('should_be_reset')
await inputs.name.fill('Should Be Reset')
await inputs.cancelBtn.click()
await expect(page.locator('.t-dialog')).not.toBeVisible()
await page.getByRole('button', { name: '添加供应商' }).click()
await expect(page.locator('.t-dialog')).toBeVisible()
inputs = formInputs(page)
await expect(inputs.id).toHaveValue('')
await expect(inputs.name).toHaveValue('')
})
test('快速连续点击只打开一个对话框', async ({ page }) => {
await page.getByRole('button', { name: '添加供应商' }).click()
await expect(page.locator('.t-dialog')).toBeVisible()
expect(await page.locator('.t-dialog').count()).toBe(1)
await formInputs(page).cancelBtn.click()
await expect(page.locator('.t-dialog')).not.toBeVisible()
})
})