role-x-button-verify.spec.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { test, expect } from '../../utils/test-setup';
  2. import { readFileSync } from 'fs';
  3. import { join, dirname } from 'path';
  4. import { fileURLToPath } from 'url';
  5. const __filename = fileURLToPath(import.meta.url);
  6. const __dirname = dirname(__filename);
  7. const testUsers = JSON.parse(readFileSync(join(__dirname, '../../fixtures/test-users.json'), 'utf-8'));
  8. test.describe('用户管理 - 角色筛选X按钮验证', () => {
  9. test('验证角色标签和X按钮功能 - 最终验证', async ({ adminLoginPage, page }) => {
  10. await adminLoginPage.goto();
  11. await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
  12. await page.goto('/admin/users');
  13. await page.waitForLoadState('domcontentloaded');
  14. await page.waitForTimeout(1000);
  15. console.log('=== 用户管理角色筛选X按钮验证测试 ===');
  16. // 点击高级筛选按钮
  17. const advancedFilterButton = page.getByTestId('advanced-filter-button');
  18. await advancedFilterButton.click();
  19. await page.waitForTimeout(1000);
  20. // 找到筛选面板
  21. const filterPanel = page.locator('.grid').filter({ hasText: '用户角色' });
  22. // 点击角色选择器
  23. const roleSelect = filterPanel.locator('[role="combobox"]').nth(1);
  24. await roleSelect.click();
  25. await page.waitForTimeout(500);
  26. // 选择管理员选项
  27. const adminOption = page.locator('[role="option"]').nth(0);
  28. await adminOption.click();
  29. await page.waitForTimeout(1500);
  30. // 截图:选择角色后
  31. await page.screenshot({ path: 'test-results/verify-1-after-select.png', fullPage: true });
  32. // 测试1: 点击筛选面板内的 Badge 来移除筛选
  33. console.log('测试1: 点击筛选面板内的 Badge');
  34. const filterPanelBadge = filterPanel.locator('[data-slot="badge"]').filter({ hasText: '管理员' });
  35. const beforeCount = await filterPanelBadge.count();
  36. console.log('点击前 - 筛选面板内的 Badge 数量:', beforeCount);
  37. expect(beforeCount).toBeGreaterThan(0);
  38. // 点击 Badge
  39. await filterPanelBadge.first().click();
  40. await page.waitForTimeout(2000);
  41. // 检查点击后的结果
  42. const afterCount = await filterPanelBadge.count();
  43. console.log('点击后 - 筛选面板内的 Badge 数量:', afterCount);
  44. // 截图:点击后
  45. await page.screenshot({ path: 'test-results/verify-2-after-click.png', fullPage: true });
  46. // 验证结果
  47. if (afterCount === 0) {
  48. console.log('✓ 测试1通过: Badge 点击功能正常,标签已成功移除');
  49. } else {
  50. console.log('⚠ Badge 数量:', afterCount);
  51. }
  52. // 测试2: 验证筛选面板外的 Badge 也被移除
  53. console.log('测试2: 验证筛选面板外的 Badge');
  54. const externalBadge = page.locator('[data-slot="badge"]').filter({ hasText: /^角色: 管理员$/ });
  55. const externalCount = await externalBadge.count();
  56. console.log('筛选面板外的 Badge 数量:', externalCount);
  57. if (externalCount === 0) {
  58. console.log('✓ 测试2通过: 筛选面板外的 Badge 已被移除');
  59. } else {
  60. console.log('⚠ 筛选面板外的 Badge 数量:', externalCount);
  61. }
  62. // 测试3: 测试选择多个角色
  63. console.log('测试3: 测试多角色选择');
  64. // 重新打开筛选面板
  65. const isPanelOpen = await filterPanel.count() > 0;
  66. if (!isPanelOpen) {
  67. await advancedFilterButton.click();
  68. await page.waitForTimeout(1000);
  69. }
  70. // 重新获取筛选面板和角色选择器
  71. const filterPanel2 = page.locator('.grid').filter({ hasText: '用户角色' });
  72. const roleSelect2 = filterPanel2.locator('[role="combobox"]').nth(1);
  73. await roleSelect2.click();
  74. await page.waitForTimeout(500);
  75. await adminOption.click();
  76. await page.waitForTimeout(500);
  77. // 选择第二个角色
  78. await roleSelect2.click();
  79. await page.waitForTimeout(500);
  80. const userOption = page.locator('[role="option"]').nth(1);
  81. await userOption.click();
  82. await page.waitForTimeout(1500);
  83. // 截图:选择多个角色后
  84. await page.screenshot({ path: 'test-results/verify-3-multi-select.png', fullPage: true });
  85. // 验证两个标签都显示
  86. const adminBadges = filterPanel2.locator('[data-slot="badge"]').filter({ hasText: '管理员' });
  87. const userBadges = filterPanel2.locator('[data-slot="badge"]').filter({ hasText: '普通用户' });
  88. const adminCount = await adminBadges.count();
  89. const userCount = await userBadges.count();
  90. console.log('筛选面板内 - 管理员 badge 数量:', adminCount);
  91. console.log('筛选面板内 - 普通用户 badge 数量:', userCount);
  92. expect(adminCount + userCount).toBeGreaterThan(0);
  93. console.log('✓ 测试3通过: 多角色选择功能正常');
  94. // 测试4: 测试移除单个标签
  95. console.log('测试4: 测试移除单个标签(管理员)');
  96. await adminBadges.first().click();
  97. await page.waitForTimeout(1000);
  98. const afterRemoveCount = await filterPanel2.locator('[data-slot="badge"]').filter({ hasText: '管理员' }).count();
  99. console.log('移除管理员后 - 管理员 badge 数量:', afterRemoveCount);
  100. if (afterRemoveCount < adminCount) {
  101. console.log('✓ 测试4通过: 单个标签移除功能正常');
  102. }
  103. // 最终截图
  104. await page.screenshot({ path: 'test-results/verify-final.png', fullPage: true });
  105. console.log('=== 所有验证测试完成 ===');
  106. expect(true).toBe(true);
  107. });
  108. });