region-list.spec.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.serial('区域列表查看测试', () => {
  9. test.beforeEach(async ({ adminLoginPage, regionManagementPage }) => {
  10. // 以管理员身份登录后台
  11. await adminLoginPage.goto();
  12. await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
  13. await adminLoginPage.expectLoginSuccess();
  14. await regionManagementPage.goto();
  15. });
  16. test.describe('页面加载验证', () => {
  17. test('应该显示区域列表页面标题', async ({ regionManagementPage }) => {
  18. await expect(regionManagementPage.pageTitle).toBeVisible();
  19. await expect(regionManagementPage.pageTitle).toContainText('省市区树形管理');
  20. });
  21. test('应该显示新增省按钮', async ({ regionManagementPage }) => {
  22. await expect(regionManagementPage.addProvinceButton).toBeVisible();
  23. await expect(regionManagementPage.addProvinceButton).toContainText('新增省');
  24. });
  25. test('应该等待树结构加载完成', async ({ regionManagementPage }) => {
  26. await regionManagementPage.waitForTreeLoaded();
  27. await expect(regionManagementPage.treeContainer).toBeVisible();
  28. });
  29. });
  30. test.describe('区域数据展示验证', () => {
  31. test('应该显示默认省份数据', async ({ regionManagementPage }) => {
  32. await regionManagementPage.waitForTreeLoaded();
  33. // 验证树形容器可见
  34. await expect(regionManagementPage.treeContainer).toBeVisible();
  35. // 验证树结构中包含一些区域(至少要有省份存在)
  36. // 这里不验证具体的省份名称,因为测试数据可能不同
  37. const treeContent = regionManagementPage.treeContainer;
  38. await expect(treeContent).toBeVisible();
  39. });
  40. test('应该能获取区域状态', async ({ regionManagementPage, page }) => {
  41. await regionManagementPage.waitForTreeLoaded();
  42. // 获取树中的第一个区域节点
  43. const firstRegion = regionManagementPage.treeContainer.getByText(/^[\u4e00-\u9fa5]+省$/).first();
  44. const count = await firstRegion.count();
  45. if (count > 0) {
  46. const regionName = await firstRegion.textContent();
  47. if (regionName) {
  48. const status = await regionManagementPage.getRegionStatus(regionName.trim());
  49. // 验证状态是有效的值(启用、禁用或null)
  50. expect(status === null || status === '启用' || status === '禁用').toBe(true);
  51. }
  52. }
  53. });
  54. test('应该能展开和收起区域节点', async ({ regionManagementPage }) => {
  55. await regionManagementPage.waitForTreeLoaded();
  56. // 查找一个有子节点的省份
  57. const province = regionManagementPage.treeContainer.getByText(/^[\u4e00-\u9fa5]+省$/).first();
  58. const count = await province.count();
  59. if (count > 0) {
  60. const provinceName = await province.textContent();
  61. if (provinceName) {
  62. const name = provinceName.trim();
  63. // 尝试展开节点
  64. await regionManagementPage.expandNode(name);
  65. console.debug(`已展开节点: ${name}`);
  66. // 等待一下让展开动画完成
  67. await regionManagementPage.page.waitForTimeout(500);
  68. // 尝试收起节点
  69. await regionManagementPage.collapseNode(name);
  70. console.debug(`已收起节点: ${name}`);
  71. }
  72. }
  73. });
  74. test('应该能检查区域是否存在', async ({ regionManagementPage }) => {
  75. await regionManagementPage.waitForTreeLoaded();
  76. // 测试检查不存在的区域
  77. const notExists = await regionManagementPage.regionExists('不存在的测试区域XYZ123');
  78. expect(notExists).toBe(false);
  79. // 获取树中的实际区域名称进行测试
  80. const firstRegion = regionManagementPage.treeContainer.getByText(/^[\u4e00-\u9fa5]+省$/).first();
  81. const count = await firstRegion.count();
  82. if (count > 0) {
  83. const regionName = await firstRegion.textContent();
  84. if (regionName) {
  85. // 测试检查存在的区域
  86. const exists = await regionManagementPage.regionExists(regionName.trim());
  87. expect(exists).toBe(true);
  88. }
  89. }
  90. });
  91. });
  92. test.describe('树形结构交互', () => {
  93. test('应该能连续展开多个省份', async ({ regionManagementPage }) => {
  94. await regionManagementPage.waitForTreeLoaded();
  95. // 获取所有省份
  96. const provinces = regionManagementPage.treeContainer.getByText(/^[\u4e00-\u9fa5]+省$/);
  97. const count = await provinces.count();
  98. if (count > 0) {
  99. // 最多展开前3个省份
  100. const maxExpand = Math.min(count, 3);
  101. for (let i = 0; i < maxExpand; i++) {
  102. const province = provinces.nth(i);
  103. const provinceName = await province.textContent();
  104. if (provinceName) {
  105. await regionManagementPage.expandNode(provinceName.trim());
  106. console.debug(`展开省份 ${i + 1}: ${provinceName}`);
  107. await regionManagementPage.page.waitForTimeout(300);
  108. }
  109. }
  110. }
  111. });
  112. test('页面刷新后树结构应该正常显示', async ({ regionManagementPage, page }) => {
  113. await regionManagementPage.waitForTreeLoaded();
  114. // 刷新页面
  115. await page.reload();
  116. await page.waitForLoadState('domcontentloaded');
  117. // 重新导航到区域管理页面
  118. await regionManagementPage.goto();
  119. // 验证树结构再次加载
  120. await regionManagementPage.waitForTreeLoaded();
  121. await expect(regionManagementPage.treeContainer).toBeVisible();
  122. });
  123. });
  124. test.describe('导航功能', () => {
  125. test('应该能从其他页面导航到区域管理', async ({ adminLoginPage, regionManagementPage, page }) => {
  126. // 先访问其他页面
  127. await page.goto('/admin/dashboard');
  128. await page.waitForLoadState('domcontentloaded');
  129. // 然后导航到区域管理页面
  130. await regionManagementPage.goto();
  131. // 验证页面正常加载
  132. await expect(regionManagementPage.pageTitle).toBeVisible();
  133. await expect(regionManagementPage.treeContainer).toBeVisible();
  134. });
  135. });
  136. });