user.spec.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { test, expect } from '@playwright/test'
  2. // 测试账号配置
  3. const TEST_USERNAME = process.env.TEST_USERNAME || 'admin'
  4. const TEST_PASSWORD = process.env.TEST_PASSWORD || '123456'
  5. test.describe('用户管理测试', () => {
  6. test.beforeEach(async ({ page }) => {
  7. // 先登录
  8. await page.goto('/login')
  9. await page.getByPlaceholder('用户名').fill(TEST_USERNAME)
  10. await page.getByPlaceholder('密码').fill(TEST_PASSWORD)
  11. await page.getByRole('button', { name: '登录' }).click()
  12. // 等待登录成功
  13. await page.waitForURL(/^(?!.*\/login).*$/, { timeout: 15000 })
  14. // 进入用户管理页面
  15. await page.goto('/user')
  16. await page.waitForLoadState('networkidle')
  17. })
  18. test('用户管理页面正确显示', async ({ page }) => {
  19. // 验证页面元素
  20. await expect(page.getByText('新增用户')).toBeVisible()
  21. await expect(page.getByText('刷新列表')).toBeVisible()
  22. // 验证表格存在
  23. await expect(page.locator('.el-table')).toBeVisible()
  24. })
  25. test('搜索功能正常工作', async ({ page }) => {
  26. // 输入搜索关键词
  27. const searchInput = page.getByPlaceholder('请输入用户名')
  28. if (await searchInput.isVisible()) {
  29. await searchInput.fill('admin')
  30. // 点击搜索
  31. await page.getByRole('button', { name: '搜索' }).click()
  32. await page.waitForTimeout(1000)
  33. // 验证表格中包含 admin
  34. await expect(page.locator('.el-table')).toContainText('admin')
  35. }
  36. })
  37. test('重置按钮清空搜索条件', async ({ page }) => {
  38. // 输入搜索关键词
  39. const searchInput = page.getByPlaceholder('请输入用户名')
  40. if (await searchInput.isVisible()) {
  41. await searchInput.fill('test')
  42. // 点击重置
  43. await page.getByRole('button', { name: '重置' }).click()
  44. await page.waitForTimeout(500)
  45. // 验证搜索框被清空
  46. await expect(searchInput).toHaveValue('')
  47. }
  48. })
  49. test('新增用户弹窗可以打开', async ({ page }) => {
  50. // 点击新增用户
  51. await page.getByRole('button', { name: '新增用户' }).click()
  52. // 验证弹窗显示
  53. await expect(page.getByRole('dialog')).toBeVisible()
  54. await expect(page.locator('.el-dialog__title')).toContainText(/新增用户|编辑用户/)
  55. })
  56. test('新增用户表单验证', async ({ page }) => {
  57. // 打开新增弹窗
  58. await page.getByRole('button', { name: '新增用户' }).click()
  59. await expect(page.getByRole('dialog')).toBeVisible()
  60. // 直接点击确定,触发验证
  61. await page.getByRole('button', { name: '确定' }).click()
  62. await page.waitForTimeout(500)
  63. // 验证表单验证信息
  64. // 根据实际的验证消息调整
  65. await expect(page.getByRole('dialog')).toBeVisible()
  66. })
  67. test('新增用户弹窗可以关闭', async ({ page }) => {
  68. // 打开新增弹窗
  69. await page.getByRole('button', { name: '新增用户' }).click()
  70. await expect(page.getByRole('dialog')).toBeVisible()
  71. // 点击取消
  72. await page.getByRole('button', { name: '取消' }).click()
  73. // 验证弹窗关闭
  74. await expect(page.getByRole('dialog')).not.toBeVisible()
  75. })
  76. test('刷新列表功能正常', async ({ page }) => {
  77. // 点击刷新列表
  78. await page.getByRole('button', { name: '刷新列表' }).click()
  79. // 等待加载完成
  80. await page.waitForTimeout(1000)
  81. // 验证表格仍然存在
  82. await expect(page.locator('.el-table')).toBeVisible()
  83. })
  84. test('角色筛选功能正常', async ({ page }) => {
  85. // 查找角色选择器
  86. const roleSelect = page
  87. .locator('.el-select')
  88. .filter({ hasText: /角色|请选择角色/ })
  89. .first()
  90. if (await roleSelect.isVisible()) {
  91. await roleSelect.click()
  92. await page.waitForTimeout(300)
  93. // 选择管理员
  94. const adminOption = page.getByRole('option', { name: '管理员' })
  95. if (await adminOption.isVisible()) {
  96. await adminOption.click()
  97. await page.waitForTimeout(500)
  98. }
  99. }
  100. })
  101. test('分页功能存在', async ({ page }) => {
  102. // 验证分页组件存在
  103. await expect(page.locator('.el-pagination')).toBeVisible()
  104. })
  105. test('表格显示用户信息', async ({ page }) => {
  106. // 等待表格数据加载
  107. await page.waitForTimeout(1000)
  108. // 验证表格列标题
  109. await expect(page.locator('.el-table')).toContainText('用户名')
  110. await expect(page.locator('.el-table')).toContainText('角色')
  111. })
  112. })