login.spec.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { test, expect } from '@playwright/test'
  2. /**
  3. * 登录功能 E2E 测试
  4. */
  5. test.describe('Login', () => {
  6. test.beforeEach(async ({ page }) => {
  7. // 每个测试前导航到登录页
  8. await page.goto('/login')
  9. })
  10. test('should display login form', async ({ page }) => {
  11. // 验证登录页面元素存在
  12. await expect(page.getByRole('heading', { name: '登录' })).toBeVisible()
  13. await expect(page.getByPlaceholder('请输入租户名称')).toBeVisible()
  14. await expect(page.getByPlaceholder('请输入用户名')).toBeVisible()
  15. await expect(page.getByPlaceholder('请输入密码')).toBeVisible()
  16. await expect(page.getByRole('button', { name: '登录' })).toBeVisible()
  17. })
  18. test('should have default values filled', async ({ page }) => {
  19. // 验证默认值
  20. const tenantInput = page.getByPlaceholder('请输入租户名称')
  21. const usernameInput = page.getByPlaceholder('请输入用户名')
  22. const passwordInput = page.getByPlaceholder('请输入密码')
  23. await expect(tenantInput).toHaveValue('yshop')
  24. await expect(usernameInput).toHaveValue('admin')
  25. await expect(passwordInput).toHaveValue('admin123')
  26. })
  27. test('should login successfully with valid credentials', async ({ page }) => {
  28. // 点击登录按钮
  29. await page.getByRole('button', { name: '登录' }).click()
  30. // 等待登录成功并跳转到首页
  31. await expect(page).toHaveURL(/\/index/, { timeout: 10000 })
  32. // 验证首页元素 - 使用更精确的定位器
  33. await expect(page.getByRole('link', { name: '首页' })).toBeVisible()
  34. await expect(page.getByText('会员总数')).toBeVisible()
  35. })
  36. test('should show error with invalid tenant', async ({ page }) => {
  37. // 清空并输入无效的租户名称
  38. const tenantInput = page.getByPlaceholder('请输入租户名称')
  39. await tenantInput.clear()
  40. await tenantInput.fill('invalid_tenant')
  41. // 点击登录按钮
  42. await page.getByRole('button', { name: '登录' }).click()
  43. // 应该显示错误提示(停留在登录页)
  44. await expect(page).toHaveURL(/\/login/, { timeout: 5000 })
  45. })
  46. test('should toggle password visibility', async ({ page }) => {
  47. const passwordInput = page.getByPlaceholder('请输入密码')
  48. // 默认密码应该是隐藏的
  49. await expect(passwordInput).toHaveAttribute('type', 'password')
  50. // 点击显示密码图标(眼睛图标)
  51. const toggleButton = page.locator('.el-input__suffix').filter({ has: passwordInput.locator('..') }).locator('img').last()
  52. if (await toggleButton.isVisible()) {
  53. await toggleButton.click()
  54. // 密码应该变成可见
  55. await expect(passwordInput).toHaveAttribute('type', 'text')
  56. }
  57. })
  58. test('should remember me checkbox works', async ({ page }) => {
  59. const rememberMeCheckbox = page.getByText('记住我')
  60. // 验证记住我选项存在
  61. await expect(rememberMeCheckbox).toBeVisible()
  62. })
  63. test('should redirect to original page after login', async ({ page }) => {
  64. // 访问需要登录的页面
  65. await page.goto('/index')
  66. // 应该被重定向到登录页,带有 redirect 参数
  67. await expect(page).toHaveURL(/\/login\?redirect/)
  68. // 登录
  69. await page.getByRole('button', { name: '登录' }).click()
  70. // 登录后应该跳转回原页面
  71. await expect(page).toHaveURL(/\/index/, { timeout: 10000 })
  72. })
  73. })