2
0

region-list.spec.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. const provinces = regionManagementPage.treeContainer.getByText(/^[\u4e00-\u9fa5]+省$/);
  37. const count = await provinces.count();
  38. expect(count).toBeGreaterThan(0);
  39. // 验证第一个省份名称存在
  40. const firstProvince = await provinces.first().textContent();
  41. expect(firstProvince).toBeTruthy();
  42. expect(firstProvince!.trim()).toMatch(/^[\u4e00-\u9fa5]+省$/);
  43. });
  44. test('应该能获取区域状态', async ({ regionManagementPage, page }) => {
  45. await regionManagementPage.waitForTreeLoaded();
  46. // 获取树中的第一个区域节点
  47. const firstRegion = regionManagementPage.treeContainer.getByText(/^[\u4e00-\u9fa5]+省$/).first();
  48. const count = await firstRegion.count();
  49. if (count > 0) {
  50. const regionName = await firstRegion.textContent();
  51. if (regionName) {
  52. const status = await regionManagementPage.getRegionStatus(regionName.trim());
  53. // 验证状态是有效的值(启用、禁用或null)
  54. expect(status === null || status === '启用' || status === '禁用').toBe(true);
  55. }
  56. }
  57. });
  58. test('应该能展开和收起区域节点', async ({ regionManagementPage }) => {
  59. await regionManagementPage.waitForTreeLoaded();
  60. // 查找一个有子节点的省份
  61. const province = regionManagementPage.treeContainer.getByText(/^[\u4e00-\u9fa5]+省$/).first();
  62. const count = await province.count();
  63. if (count > 0) {
  64. const provinceName = await province.textContent();
  65. if (provinceName) {
  66. const name = provinceName.trim();
  67. // 尝试展开节点
  68. await regionManagementPage.expandNode(name);
  69. console.debug(`已展开节点: ${name}`);
  70. // 等待一下让展开动画完成
  71. await regionManagementPage.page.waitForTimeout(500);
  72. // 尝试收起节点
  73. await regionManagementPage.collapseNode(name);
  74. console.debug(`已收起节点: ${name}`);
  75. }
  76. }
  77. });
  78. test('应该能检查区域是否存在', async ({ regionManagementPage }) => {
  79. await regionManagementPage.waitForTreeLoaded();
  80. // 测试检查不存在的区域
  81. const notExists = await regionManagementPage.regionExists('不存在的测试区域XYZ123');
  82. expect(notExists).toBe(false);
  83. // 获取树中的实际区域名称进行测试
  84. const firstRegion = regionManagementPage.treeContainer.getByText(/^[\u4e00-\u9fa5]+省$/).first();
  85. const count = await firstRegion.count();
  86. if (count > 0) {
  87. const regionName = await firstRegion.textContent();
  88. if (regionName) {
  89. // 测试检查存在的区域
  90. const exists = await regionManagementPage.regionExists(regionName.trim());
  91. expect(exists).toBe(true);
  92. }
  93. }
  94. });
  95. });
  96. test.describe('树形结构交互', () => {
  97. test('应该能连续展开多个省份', async ({ regionManagementPage }) => {
  98. await regionManagementPage.waitForTreeLoaded();
  99. // 获取所有省份
  100. const provinces = regionManagementPage.treeContainer.getByText(/^[\u4e00-\u9fa5]+省$/);
  101. const count = await provinces.count();
  102. if (count > 0) {
  103. // 最多展开前3个省份
  104. const maxExpand = Math.min(count, 3);
  105. for (let i = 0; i < maxExpand; i++) {
  106. const province = provinces.nth(i);
  107. const provinceName = await province.textContent();
  108. if (provinceName) {
  109. await regionManagementPage.expandNode(provinceName.trim());
  110. console.debug(`展开省份 ${i + 1}: ${provinceName}`);
  111. await regionManagementPage.page.waitForTimeout(300);
  112. }
  113. }
  114. }
  115. });
  116. test('页面刷新后树结构应该正常显示', async ({ regionManagementPage, page }) => {
  117. await regionManagementPage.waitForTreeLoaded();
  118. // 刷新页面
  119. await page.reload();
  120. await page.waitForLoadState('domcontentloaded');
  121. // 重新导航到区域管理页面
  122. await regionManagementPage.goto();
  123. // 验证树结构再次加载
  124. await regionManagementPage.waitForTreeLoaded();
  125. await expect(regionManagementPage.treeContainer).toBeVisible();
  126. });
  127. });
  128. test.describe('导航功能', () => {
  129. test('应该能从其他页面导航到区域管理', async ({ adminLoginPage, regionManagementPage, page }) => {
  130. // 先访问其他页面
  131. await page.goto('/admin/dashboard');
  132. await page.waitForLoadState('domcontentloaded');
  133. // 然后导航到区域管理页面
  134. await regionManagementPage.goto();
  135. // 验证页面正常加载
  136. await expect(regionManagementPage.pageTitle).toBeVisible();
  137. await expect(regionManagementPage.treeContainer).toBeVisible();
  138. });
  139. });
  140. test.describe('区域层级标识验证', () => {
  141. test('应该能验证省份层级标识', async ({ regionManagementPage }) => {
  142. await regionManagementPage.waitForTreeLoaded();
  143. // 验证省份存在(以"省"结尾)
  144. const provinces = regionManagementPage.treeContainer.getByText(/^[\u4e00-\u9fa5]+省$/);
  145. const count = await provinces.count();
  146. expect(count).toBeGreaterThan(0);
  147. console.debug(`找到 ${count} 个省份`);
  148. });
  149. test('应该能展开省份并验证市级子节点', async ({ regionManagementPage }) => {
  150. await regionManagementPage.waitForTreeLoaded();
  151. // 查找第一个省份
  152. const province = regionManagementPage.treeContainer.getByText(/^[\u4e00-\u9fa5]+省$/).first();
  153. const provinceCount = await province.count();
  154. if (provinceCount > 0) {
  155. const provinceName = await province.textContent();
  156. if (provinceName) {
  157. // 展开省份
  158. await regionManagementPage.expandNode(provinceName.trim());
  159. await regionManagementPage.page.waitForTimeout(500);
  160. // 验证市级子节点存在(以"市"结尾)
  161. const cities = regionManagementPage.treeContainer.getByText(/^[\u4e00-\u9fa5]+市$/);
  162. const cityCount = await cities.count();
  163. console.debug(`展开 ${provinceName.trim()} 后找到 ${cityCount} 个市`);
  164. // 验证至少有一个市(如果省份有子节点)
  165. if (cityCount > 0) {
  166. const firstCity = await cities.first().textContent();
  167. expect(firstCity).toBeTruthy();
  168. expect(firstCity!.trim()).toMatch(/^[\u4e00-\u9fa5]+市$/);
  169. }
  170. }
  171. }
  172. });
  173. test('应该能展开市并验证区级子节点', async ({ regionManagementPage }) => {
  174. await regionManagementPage.waitForTreeLoaded();
  175. // 查找第一个省份并展开
  176. const province = regionManagementPage.treeContainer.getByText(/^[\u4e00-\u9fa5]+省$/).first();
  177. const provinceCount = await province.count();
  178. if (provinceCount > 0) {
  179. const provinceName = await province.textContent();
  180. if (provinceName) {
  181. await regionManagementPage.expandNode(provinceName.trim());
  182. await regionManagementPage.page.waitForTimeout(500);
  183. // 查找第一个市并展开
  184. const city = regionManagementPage.treeContainer.getByText(/^[\u4e00-\u9fa5]+市$/).first();
  185. const cityCount = await city.count();
  186. if (cityCount > 0) {
  187. const cityName = await city.textContent();
  188. if (cityName) {
  189. await regionManagementPage.expandNode(cityName.trim());
  190. await regionManagementPage.page.waitForTimeout(500);
  191. // 验证区级子节点存在(以"区"或"县"结尾)
  192. const districts = regionManagementPage.treeContainer.getByText(/^[\u4e00-\u9fa5]+(区|县)$/);
  193. const districtCount = await districts.count();
  194. console.debug(`展开 ${cityName.trim()} 后找到 ${districtCount} 个区/县`);
  195. if (districtCount > 0) {
  196. const firstDistrict = await districts.first().textContent();
  197. expect(firstDistrict).toBeTruthy();
  198. }
  199. }
  200. }
  201. }
  202. }
  203. });
  204. });
  205. });