2
0

profile.spec.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { test, expect } from '../../utils/test-setup';
  2. import testUsers from '../../fixtures/test-users.json' with { type: 'json' };
  3. test.describe('用户个人资料管理', () => {
  4. test.beforeEach(async ({ loginPage }) => {
  5. await loginPage.goto();
  6. await loginPage.login(testUsers.admin.username, testUsers.admin.password);
  7. });
  8. test('查看个人资料页面', async ({ page }) => {
  9. // 导航到个人资料页面(需要根据实际路由调整)
  10. await page.goto('/profile');
  11. await page.waitForLoadState('networkidle');
  12. // 验证页面标题和基本信息
  13. await expect(page.getByRole('heading', { name: '个人资料' })).toBeVisible();
  14. await expect(page.getByText(testUsers.admin.username)).toBeVisible();
  15. });
  16. test('更新个人资料信息', async ({ page }) => {
  17. await page.goto('/profile');
  18. await page.waitForLoadState('networkidle');
  19. // 点击编辑按钮
  20. const editButton = page.getByRole('button', { name: '编辑资料' });
  21. await editButton.click();
  22. // 更新昵称
  23. const nicknameInput = page.getByLabel('昵称');
  24. const newNickname = `测试昵称_${Date.now()}`;
  25. await nicknameInput.fill(newNickname);
  26. // 保存更改
  27. const saveButton = page.getByRole('button', { name: '保存' });
  28. await saveButton.click();
  29. // 验证更新成功
  30. await expect(page.locator('text=资料更新成功')).toBeVisible();
  31. await expect(page.getByText(newNickname)).toBeVisible();
  32. });
  33. test('修改密码', async ({ page }) => {
  34. await page.goto('/profile');
  35. await page.waitForLoadState('networkidle');
  36. // 导航到修改密码页面
  37. const changePasswordTab = page.getByRole('tab', { name: '修改密码' });
  38. await changePasswordTab.click();
  39. // 填写密码表单
  40. const currentPasswordInput = page.getByLabel('当前密码');
  41. const newPasswordInput = page.getByLabel('新密码');
  42. const confirmPasswordInput = page.getByLabel('确认新密码');
  43. await currentPasswordInput.fill(testUsers.admin.password);
  44. await newPasswordInput.fill('NewPassword123!');
  45. await confirmPasswordInput.fill('NewPassword123!');
  46. // 提交修改
  47. const submitButton = page.getByRole('button', { name: '修改密码' });
  48. await submitButton.click();
  49. // 验证密码修改成功
  50. await expect(page.locator('text=密码修改成功')).toBeVisible();
  51. // 使用新密码重新登录验证
  52. const logoutButton = page.getByRole('button', { name: '登出' });
  53. await logoutButton.click();
  54. await page.goto('/login');
  55. await page.getByPlaceholder('请输入用户名').fill(testUsers.admin.username);
  56. await page.getByPlaceholder('请输入密码').fill('NewPassword123!');
  57. await page.getByRole('button', { name: '登录' }).click();
  58. // 验证登录成功
  59. await expect(page).toHaveURL('/');
  60. });
  61. test('密码修改验证 - 当前密码错误', async ({ page }) => {
  62. await page.goto('/profile');
  63. await page.waitForLoadState('networkidle');
  64. const changePasswordTab = page.getByRole('tab', { name: '修改密码' });
  65. await changePasswordTab.click();
  66. const currentPasswordInput = page.getByLabel('当前密码');
  67. const newPasswordInput = page.getByLabel('新密码');
  68. const confirmPasswordInput = page.getByLabel('确认新密码');
  69. await currentPasswordInput.fill('wrongpassword');
  70. await newPasswordInput.fill('NewPassword123!');
  71. await confirmPasswordInput.fill('NewPassword123!');
  72. const submitButton = page.getByRole('button', { name: '修改密码' });
  73. await submitButton.click();
  74. // 验证错误提示
  75. await expect(page.locator('text=当前密码错误')).toBeVisible();
  76. });
  77. test('密码修改验证 - 新密码不一致', async ({ page }) => {
  78. await page.goto('/profile');
  79. await page.waitForLoadState('networkidle');
  80. const changePasswordTab = page.getByRole('tab', { name: '修改密码' });
  81. await changePasswordTab.click();
  82. const currentPasswordInput = page.getByLabel('当前密码');
  83. const newPasswordInput = page.getByLabel('新密码');
  84. const confirmPasswordInput = page.getByLabel('确认新密码');
  85. await currentPasswordInput.fill(testUsers.admin.password);
  86. await newPasswordInput.fill('NewPassword123!');
  87. await confirmPasswordInput.fill('DifferentPassword123!');
  88. const submitButton = page.getByRole('button', { name: '修改密码' });
  89. await submitButton.click();
  90. // 验证错误提示
  91. await expect(page.locator('text=两次密码输入不一致')).toBeVisible();
  92. });
  93. test('查看登录历史', async ({ page }) => {
  94. await page.goto('/profile');
  95. await page.waitForLoadState('networkidle');
  96. // 导航到安全设置页面
  97. const securityTab = page.getByRole('tab', { name: '安全设置' });
  98. await securityTab.click();
  99. // 验证登录历史记录可见
  100. await expect(page.getByText('登录历史')).toBeVisible();
  101. await expect(page.getByText('最近登录')).toBeVisible();
  102. });
  103. });