2
0

async-select-test.spec.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { TIMEOUTS } from '../../utils/timeouts';
  2. import { test, expect } from '../../utils/test-setup';
  3. import { readFileSync } from 'fs';
  4. import { join, dirname } from 'path';
  5. import { fileURLToPath } from 'url';
  6. import { selectRadixOptionAsync } from '@d8d/e2e-test-utils';
  7. const __filename = fileURLToPath(import.meta.url);
  8. const __dirname = dirname(__filename);
  9. const testUsers = JSON.parse(readFileSync(join(__dirname, '../../fixtures/test-users.json'), 'utf-8'));
  10. /**
  11. * 专门测试异步 Select 功能的简单测试
  12. * 用于验证 Story 2.3 的 selectRadixOptionAsync 实现
  13. */
  14. test.describe.serial('异步 Select 测试 (Story 2.3)', () => {
  15. test.beforeEach(async ({ adminLoginPage, disabilityPersonPage }) => {
  16. // 以管理员身份登录后台
  17. await adminLoginPage.goto();
  18. await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
  19. await adminLoginPage.expectLoginSuccess();
  20. await disabilityPersonPage.goto();
  21. });
  22. test('测试省份选择(异步加载)', async ({ disabilityPersonPage, page }) => {
  23. console.log('=== 开始测试省份选择 ===');
  24. // 点击新增按钮打开对话框
  25. await disabilityPersonPage.openCreateDialog();
  26. console.log('对话框已打开');
  27. // 使用 selectRadixOptionAsync 选择省份
  28. await selectRadixOptionAsync(page, '省份 *', '广东省');
  29. console.log('✓ 省份选择成功');
  30. // 验证城市选择器已启用(省份选择后城市应该可用了)
  31. const cityCombobox = page.getByRole('combobox', { name: '城市' });
  32. await expect(cityCombobox).not.toBeDisabled();
  33. console.log('✓ 城市选择器已启用');
  34. });
  35. test('测试城市选择(异步加载,依赖省份)', async ({ disabilityPersonPage, page }) => {
  36. console.log('=== 开始测试城市选择 ===');
  37. // 点击新增按钮打开对话框
  38. await disabilityPersonPage.openCreateDialog();
  39. // 先选择省份
  40. await selectRadixOptionAsync(page, '省份 *', '湖北省');
  41. console.log('✓ 省份选择成功');
  42. // 选择城市 - selectRadixOptionAsync 自动处理异步加载,无需 waitForTimeout hack
  43. await selectRadixOptionAsync(page, '城市', '武汉市');
  44. console.log('✓ 城市选择成功');
  45. });
  46. test('测试完整的省份+城市选择流程', async ({ disabilityPersonPage, page }) => {
  47. console.log('=== 开始完整流程测试 ===');
  48. // 点击新增按钮
  49. await disabilityPersonPage.openCreateDialog();
  50. // 填写一些基本信息以到达地址选择区域
  51. await page.getByLabel('姓名 *').fill('测试用户');
  52. await page.getByLabel('性别 *').selectOption('男');
  53. await page.getByLabel('身份证号 *').fill('110101199001011234');
  54. await page.getByLabel('残疾证号 *').fill('CJZ20240001');
  55. await page.getByLabel('联系电话 *').fill('13800138000');
  56. await page.getByLabel('身份证地址 *').fill('测试地址');
  57. console.log('✓ 基本信息已填写');
  58. // 测试省份选择(异步)
  59. const startTime1 = Date.now();
  60. await selectRadixOptionAsync(page, '省份 *', '广东省');
  61. const duration1 = Date.now() - startTime1;
  62. console.log(`✓ 省份选择成功,耗时: ${duration1}ms`);
  63. // 测试城市选择(异步,依赖省份)
  64. const startTime2 = Date.now();
  65. await selectRadixOptionAsync(page, '城市', '深圳市');
  66. const duration2 = Date.now() - startTime2;
  67. console.log(`✓ 城市选择成功,耗时: ${duration2}ms`);
  68. // 验证选择器性能(应该在合理时间内完成)
  69. // 根据 Dev Notes 规范:最大可接受时间为 5 秒
  70. expect(duration1).toBeLessThan(5000); // 省份选择应在 5 秒内完成
  71. expect(duration2).toBeLessThan(5000); // 城市选择应在 5 秒内完成
  72. console.log('✓ 性能验证通过');
  73. });
  74. test.describe('边界场景测试', () => {
  75. test('测试超时场景 - 无效的省份值', async ({ disabilityPersonPage, page }) => {
  76. console.log('=== 测试超时场景 ===');
  77. await disabilityPersonPage.openCreateDialog();
  78. // 尝试选择不存在的省份(应该触发超时或错误)
  79. let errorThrown = false;
  80. try {
  81. await selectRadixOptionAsync(page, '省份 *', '不存在的省份XYZ', { timeout: TIMEOUTS.ELEMENT_VISIBLE_SHORT });
  82. } catch (error: any) {
  83. errorThrown = true;
  84. console.log(`✓ 正确抛出错误: ${error.message}`);
  85. expect(error.message).toContain('selectRadixOptionAsync');
  86. }
  87. expect(errorThrown).toBe(true);
  88. });
  89. test('测试重复选择相同的省份', async ({ disabilityPersonPage, page }) => {
  90. console.log('=== 测试重复选择 ===');
  91. await disabilityPersonPage.openCreateDialog();
  92. // 第一次选择
  93. await selectRadixOptionAsync(page, '省份 *', '广东省');
  94. console.log('✓ 第一次选择成功');
  95. // 重复选择相同省份(应该正常工作)
  96. await selectRadixOptionAsync(page, '省份 *', '广东省');
  97. console.log('✓ 重复选择成功');
  98. });
  99. test('测试城市选择依赖 - 省份变化后城市可用性', async ({ disabilityPersonPage, page }) => {
  100. console.log('=== 测试省份变化场景 ===');
  101. await disabilityPersonPage.openCreateDialog();
  102. // 选择第一个省份
  103. await selectRadixOptionAsync(page, '省份 *', '广东省');
  104. await selectRadixOptionAsync(page, '城市', '广州市');
  105. console.log('✓ 广东省-广州市 选择成功');
  106. // 更换省份
  107. await selectRadixOptionAsync(page, '省份 *', '湖北省');
  108. await selectRadixOptionAsync(page, '城市', '武汉市');
  109. console.log('✓ 湖北省-武汉市 选择成功');
  110. });
  111. });
  112. });