auth.spec.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { test, expect } from '@playwright/test'
  2. test.describe('Auth Module E2E Tests', () => {
  3. test.describe('Login Page', () => {
  4. test.beforeEach(async ({ page }) => {
  5. await page.goto('/login')
  6. })
  7. test('should display login form correctly', async ({ page }) => {
  8. await expect(page).toHaveTitle(/摄像头管理系统/)
  9. await expect(page.getByPlaceholder('请输入用户名')).toBeVisible()
  10. await expect(page.getByPlaceholder('请输入密码')).toBeVisible()
  11. await expect(page.getByRole('button', { name: '登 录' })).toBeVisible()
  12. await expect(page.getByText('记住我')).toBeVisible()
  13. })
  14. test('should show validation error when submitting empty form', async ({ page }) => {
  15. await page.getByRole('button', { name: '登 录' }).click()
  16. await expect(page.getByText('请输入用户名')).toBeVisible()
  17. await expect(page.getByText('请输入密码')).toBeVisible()
  18. })
  19. test('should show error with invalid credentials', async ({ page }) => {
  20. await page.getByPlaceholder('请输入用户名').fill('invalid_user')
  21. await page.getByPlaceholder('请输入密码').fill('wrong_password')
  22. await page.getByRole('button', { name: '登 录' }).click()
  23. await expect(page.locator('.el-message--error').first()).toBeVisible({ timeout: 10000 })
  24. })
  25. test('should show help message when clicking forgot password', async ({ page }) => {
  26. await page.getByText('忘记密码?').click()
  27. await expect(page.locator('.el-message--info')).toBeVisible()
  28. await expect(page.getByText('请联系管理员重置密码')).toBeVisible()
  29. })
  30. test('should have remember me checkbox checked by default', async ({ page }) => {
  31. const rememberCheckbox = page.locator('.el-checkbox').filter({ hasText: '记住我' })
  32. await expect(rememberCheckbox).toBeVisible()
  33. await expect(rememberCheckbox).toHaveClass(/is-checked/)
  34. })
  35. })
  36. test.describe('Authenticated Features', () => {
  37. // These tests require valid credentials
  38. // Skip if no test credentials are configured
  39. const username = process.env.TEST_USERNAME || 'admin'
  40. const password = process.env.TEST_PASSWORD || 'admin123'
  41. test.skip('should login and see user dropdown', async ({ page }) => {
  42. await page.goto('/login')
  43. await page.getByPlaceholder('请输入用户名').fill(username)
  44. await page.getByPlaceholder('请输入密码').fill(password)
  45. await page.getByRole('button', { name: '登 录' }).click()
  46. // Wait for redirect and check user dropdown exists
  47. await expect(page).not.toHaveURL(/\/login/, { timeout: 10000 })
  48. await expect(page.locator('.user-info')).toBeVisible()
  49. })
  50. test.skip('should show change password dialog from dropdown', async ({ page }) => {
  51. // Login first
  52. await page.goto('/login')
  53. await page.getByPlaceholder('请输入用户名').fill(username)
  54. await page.getByPlaceholder('请输入密码').fill(password)
  55. await page.getByRole('button', { name: '登 录' }).click()
  56. await expect(page).not.toHaveURL(/\/login/, { timeout: 10000 })
  57. // Open user dropdown
  58. await page.locator('.user-info').click()
  59. await expect(page.getByText('修改密码')).toBeVisible()
  60. // Click change password
  61. await page.getByText('修改密码').click()
  62. // Verify dialog opens
  63. await expect(page.getByRole('dialog')).toBeVisible()
  64. await expect(page.getByText('修改密码').first()).toBeVisible()
  65. await expect(page.getByPlaceholder('请输入原密码')).toBeVisible()
  66. await expect(page.getByPlaceholder('请输入新密码')).toBeVisible()
  67. await expect(page.getByPlaceholder('请再次输入新密码')).toBeVisible()
  68. })
  69. test.skip('should validate change password form', async ({ page }) => {
  70. // Login first
  71. await page.goto('/login')
  72. await page.getByPlaceholder('请输入用户名').fill(username)
  73. await page.getByPlaceholder('请输入密码').fill(password)
  74. await page.getByRole('button', { name: '登 录' }).click()
  75. await expect(page).not.toHaveURL(/\/login/, { timeout: 10000 })
  76. // Open change password dialog
  77. await page.locator('.user-info').click()
  78. await page.getByText('修改密码').click()
  79. // Try to submit empty form
  80. await page.getByRole('dialog').getByRole('button', { name: '确定' }).click()
  81. // Check validation errors
  82. await expect(page.getByText('请输入原密码')).toBeVisible()
  83. await expect(page.getByText('请输入新密码')).toBeVisible()
  84. await expect(page.getByText('请再次输入新密码')).toBeVisible()
  85. })
  86. test.skip('should validate password confirmation match', async ({ page }) => {
  87. // Login first
  88. await page.goto('/login')
  89. await page.getByPlaceholder('请输入用户名').fill(username)
  90. await page.getByPlaceholder('请输入密码').fill(password)
  91. await page.getByRole('button', { name: '登 录' }).click()
  92. await expect(page).not.toHaveURL(/\/login/, { timeout: 10000 })
  93. // Open change password dialog
  94. await page.locator('.user-info').click()
  95. await page.getByText('修改密码').click()
  96. // Fill mismatched passwords
  97. await page.getByPlaceholder('请输入原密码').fill('oldpass')
  98. await page.getByPlaceholder('请输入新密码').fill('newpass123')
  99. await page.getByPlaceholder('请再次输入新密码').fill('differentpass')
  100. await page.getByPlaceholder('请再次输入新密码').blur()
  101. // Check validation error
  102. await expect(page.getByText('两次输入的密码不一致')).toBeVisible()
  103. })
  104. test.skip('should logout successfully', async ({ page }) => {
  105. // Login first
  106. await page.goto('/login')
  107. await page.getByPlaceholder('请输入用户名').fill(username)
  108. await page.getByPlaceholder('请输入密码').fill(password)
  109. await page.getByRole('button', { name: '登 录' }).click()
  110. await expect(page).not.toHaveURL(/\/login/, { timeout: 10000 })
  111. // Open user dropdown and logout
  112. await page.locator('.user-info').click()
  113. await page.getByText('退出登录').click()
  114. // Should redirect to login page
  115. await expect(page).toHaveURL(/\/login/, { timeout: 10000 })
  116. })
  117. })
  118. })