bank-name-status-toggle-final.spec.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /**
  9. * 验证银行名称状态切换功能的 E2E 测试
  10. */
  11. test.describe.serial('银行名称状态切换验证', () => {
  12. test('应该能够成功切换银行名称状态', async ({ page }) => {
  13. console.log('=== 银行名称状态切换测试开始 ===');
  14. // 登录
  15. console.log('1. 登录管理员账户...');
  16. await page.goto('/admin/login');
  17. await page.fill('[placeholder="请输入用户名"]', 'admin');
  18. await page.fill('[placeholder="请输入密码"]', 'admin123');
  19. await page.click('button:has-text("登录")');
  20. // 等待登录完成 - 使用更长的等待时间
  21. await page.waitForURL('**/admin/dashboard', { timeout: 10000 });
  22. await page.waitForTimeout(2000);
  23. console.log(' ✓ 登录成功,当前 URL: ' + page.url());
  24. // 导航到银行名称管理页面
  25. console.log('2. 导航到银行名称管理页面...');
  26. await page.goto('/admin/bank-names');
  27. await page.waitForLoadState('domcontentloaded');
  28. // 等待数据加载 - 等待表格出现
  29. console.log(' 等待表格数据加载...');
  30. await page.waitForSelector('tbody tr', { timeout: 15000 }).catch(() => {
  31. console.log(' ⚠️ 等待表格超时,继续执行...');
  32. });
  33. await page.waitForTimeout(5000);
  34. await page.screenshot({ path: 'test-results/bank-status-toggle-1-initial.png' });
  35. console.log(' ✓ 初始页面截图已保存');
  36. // 查找银行记录
  37. console.log('3. 查找银行记录...');
  38. const tableRows = await page.locator('tbody tr').all();
  39. console.log(' 找到 ' + tableRows.length + ' 条银行名称记录');
  40. // 如果没有找到行,检查页面状态
  41. if (tableRows.length === 0) {
  42. console.log(' ⚠️ 未找到表格行,检查页面状态...');
  43. const tableExists = await page.locator('table').isVisible().catch(() => false);
  44. console.log(' 表格是否存在: ' + tableExists);
  45. const tbodyHtml = await page.locator('tbody').innerHTML().catch(() => 'N/A');
  46. console.log(' tbody HTML 长度: ' + tbodyHtml.length);
  47. const pageText = await page.locator('#root').innerText();
  48. console.log(' 页面是否包含"银行名称": ' + pageText.includes('银行名称'));
  49. console.log(' 页面是否包含"暂无": ' + pageText.includes('暂无'));
  50. }
  51. expect(tableRows.length, '应该找到至少一条银行名称记录').toBeGreaterThan(0);
  52. // 获取第一条记录
  53. const firstRow = tableRows[0];
  54. // 从行中提取 ID - 查找编辑按钮的 data-testid
  55. const editButton = firstRow.locator('[data-testid^="edit-button-"]');
  56. const testId = await editButton.getAttribute('data-testid');
  57. const recordId = testId?.replace('edit-button-', '');
  58. console.log(' 使用记录 ID: ' + recordId);
  59. // 获取当前状态
  60. const rowText = await firstRow.innerText();
  61. const initialIsEnabled = rowText.includes('启用');
  62. console.log(' 初始状态: ' + (initialIsEnabled ? '启用' : '禁用'));
  63. // 点击编辑按钮
  64. console.log('4. 点击编辑按钮打开编辑对话框...');
  65. await editButton.click();
  66. await page.waitForTimeout(2000);
  67. await page.screenshot({ path: 'test-results/bank-status-toggle-2-edit-dialog.png' });
  68. console.log(' ✓ 编辑对话框截图已保存');
  69. // 验证编辑对话框出现
  70. const modal = page.locator('[data-testid="type-modal"]');
  71. await expect(modal, '编辑对话框应该出现').toBeVisible({ timeout: 5000 });
  72. console.log(' ✓ 编辑对话框已出现');
  73. // 查找状态开关
  74. const statusSwitch = page.locator('[data-testid="edit-status-switch"]');
  75. await expect(statusSwitch, '状态开关应该存在').toBeVisible();
  76. console.log(' ✓ 找到状态开关');
  77. // 获取开关初始状态
  78. const switchElement = statusSwitch.locator('[data-slot="switch"]');
  79. const initialDataState = await switchElement.getAttribute('data-state');
  80. console.log(' 开关初始状态: ' + initialDataState);
  81. // 点击开关
  82. console.log('5. 点击状态开关...');
  83. await switchElement.click();
  84. await page.waitForTimeout(1500);
  85. const afterClickDataState = await switchElement.getAttribute('data-state');
  86. console.log(' 点击后状态: ' + afterClickDataState);
  87. expect(afterClickDataState, '开关状态应该改变').not.toBe(initialDataState);
  88. console.log(' ✓ 开关状态已改变');
  89. await page.screenshot({ path: 'test-results/bank-status-toggle-3-after-toggle.png' });
  90. console.log(' ✓ 切换后截图已保存');
  91. // 点击更新按钮
  92. console.log('6. 点击更新按钮保存更改...');
  93. const updateButton = page.locator('button[type="submit"]').filter({ hasText: '更新' });
  94. await updateButton.click();
  95. // 等待更新完成
  96. await page.waitForTimeout(5000);
  97. await page.screenshot({ path: 'test-results/bank-status-toggle-4-after-update.png' });
  98. console.log(' ✓ 更新后截图已保存');
  99. // 检查是否有成功提示
  100. const successToast = page.locator('[data-sonner-toast][data-type="success"]');
  101. const hasSuccess = await successToast.isVisible({ timeout: 3000 }).catch(() => false);
  102. if (hasSuccess) {
  103. const toastText = await successToast.innerText();
  104. console.log(' ✓ 成功提示: "' + toastText + '"');
  105. }
  106. // 刷新页面验证状态
  107. console.log('7. 刷新页面验证状态保持...');
  108. await page.reload({ waitUntil: 'domcontentloaded' });
  109. await page.waitForTimeout(5000);
  110. await page.screenshot({ path: 'test-results/bank-status-toggle-5-after-refresh.png' });
  111. console.log(' ✓ 刷新后截图已保存');
  112. // 获取刷新后的状态
  113. const refreshedRows = await page.locator('tbody tr').all();
  114. expect(refreshedRows.length, '刷新后应该仍有数据').toBeGreaterThan(0);
  115. const refreshedRow = refreshedRows[0];
  116. const finalRowText = await refreshedRow.innerText();
  117. const finalIsEnabled = finalRowText.includes('启用');
  118. console.log(' 刷新后状态: ' + (finalIsEnabled ? '启用' : '禁用'));
  119. // 验证状态已保持
  120. const expectedStatus = !initialIsEnabled;
  121. expect(finalIsEnabled, '刷新后状态应该与切换后的状态一致').toBe(expectedStatus);
  122. console.log(' ✓ 状态已正确保持');
  123. console.log('=== 测试完成 ===');
  124. });
  125. });