region-management.page.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. import { Page, Locator } from '@playwright/test';
  2. /**
  3. * 区域数据接口
  4. */
  5. export interface RegionData {
  6. /** 区域名称 */
  7. name: string;
  8. /** 行政区划代码 */
  9. code?: string;
  10. /** 区域层级(1=省, 2=市, 3=区) */
  11. level?: 1 | 2 | 3;
  12. /** 父级区域ID */
  13. parentId?: number | null;
  14. /** 状态(0=启用, 1=禁用) */
  15. isDisabled?: 0 | 1;
  16. }
  17. /**
  18. * 表单提交结果
  19. */
  20. export interface FormSubmitResult {
  21. success: boolean;
  22. hasError: boolean;
  23. hasSuccess: boolean;
  24. errorMessage?: string;
  25. successMessage?: string;
  26. }
  27. /**
  28. * 区域管理 Page Object
  29. *
  30. * 用于管理省市区树形结构的 E2E 测试
  31. * 页面路径: /admin/areas
  32. *
  33. * @example
  34. * ```typescript
  35. * const regionPage = new RegionManagementPage(page);
  36. * await regionPage.goto();
  37. * await regionPage.createProvince({ name: '测试省', code: '110000' });
  38. * ```
  39. */
  40. export class RegionManagementPage {
  41. readonly page: Page;
  42. // 页面级选择器
  43. readonly pageTitle: Locator;
  44. readonly addProvinceButton: Locator;
  45. readonly treeContainer: Locator;
  46. constructor(page: Page) {
  47. this.page = page;
  48. this.pageTitle = page.getByText('省市区树形管理');
  49. this.addProvinceButton = page.getByRole('button', { name: '新增省' });
  50. this.treeContainer = page.locator('.border.rounded-lg.bg-background');
  51. }
  52. /**
  53. * 导航到区域管理页面
  54. */
  55. async goto() {
  56. await this.page.goto('/admin/areas');
  57. await this.page.waitForLoadState('domcontentloaded');
  58. // 等待页面标题出现
  59. await this.pageTitle.waitFor({ state: 'visible', timeout: 15000 });
  60. // 等待树形结构加载
  61. await this.treeContainer.waitFor({ state: 'visible', timeout: 20000 });
  62. await this.expectToBeVisible();
  63. }
  64. /**
  65. * 验证页面关键元素可见
  66. */
  67. async expectToBeVisible() {
  68. await this.pageTitle.waitFor({ state: 'visible', timeout: 15000 });
  69. await this.addProvinceButton.waitFor({ state: 'visible', timeout: 10000 });
  70. }
  71. /**
  72. * 打开新增省对话框
  73. */
  74. async openCreateProvinceDialog() {
  75. await this.addProvinceButton.click();
  76. // 等待对话框出现
  77. await this.page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: 5000 });
  78. }
  79. /**
  80. * 打开新增子区域对话框
  81. * @param parentName 父级区域名称
  82. * @param childType 子区域类型('市' 或 '区')
  83. */
  84. async openAddChildDialog(parentName: string, childType: '市' | '区') {
  85. // 找到父级节点并点击对应的"新增市"或"新增区"按钮
  86. const button = this.treeContainer.getByText(parentName)
  87. .locator('../../..')
  88. .getByRole('button', { name: childType === '市' ? '新增市' : '新增区' });
  89. await button.click();
  90. // 等待对话框出现
  91. await this.page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: 5000 });
  92. }
  93. /**
  94. * 打开编辑区域对话框
  95. * @param regionName 区域名称
  96. */
  97. async openEditDialog(regionName: string) {
  98. // 找到区域节点并点击"编辑"按钮
  99. const button = this.treeContainer.getByText(regionName)
  100. .locator('../../..')
  101. .getByRole('button', { name: '编辑' });
  102. await button.click();
  103. // 等待对话框出现
  104. await this.page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: 5000 });
  105. }
  106. /**
  107. * 打开删除确认对话框
  108. * @param regionName 区域名称
  109. */
  110. async openDeleteDialog(regionName: string) {
  111. // 找到区域节点并点击"删除"按钮
  112. const button = this.treeContainer.getByText(regionName)
  113. .locator('../../..')
  114. .getByRole('button', { name: '删除' });
  115. await button.click();
  116. // 等待删除确认对话框出现
  117. await this.page.waitForSelector('[role="alertdialog"]', { state: 'visible', timeout: 5000 });
  118. }
  119. /**
  120. * 打开状态切换确认对话框
  121. * @param regionName 区域名称
  122. */
  123. async openToggleStatusDialog(regionName: string) {
  124. // 找到区域节点并点击"启用"或"禁用"按钮
  125. const button = this.treeContainer.getByText(regionName)
  126. .locator('../../..')
  127. .locator('button', { hasText: /^(启用|禁用)$/ });
  128. await button.click();
  129. // 等待状态切换确认对话框出现
  130. await this.page.waitForSelector('[role="alertdialog"]', { state: 'visible', timeout: 5000 });
  131. }
  132. /**
  133. * 填写区域表单
  134. * @param data 区域数据
  135. */
  136. async fillRegionForm(data: RegionData) {
  137. // 等待表单出现
  138. await this.page.waitForSelector('form', { state: 'visible', timeout: 5000 });
  139. // 填写区域名称
  140. if (data.name) {
  141. await this.page.getByLabel('区域名称').fill(data.name);
  142. }
  143. // 填写行政区划代码
  144. if (data.code) {
  145. await this.page.getByLabel('行政区划代码').fill(data.code);
  146. }
  147. }
  148. /**
  149. * 提交表单
  150. * @returns 表单提交结果
  151. */
  152. async submitForm(): Promise<FormSubmitResult> {
  153. // 点击提交按钮(创建或更新)
  154. const submitButton = this.page.getByRole('button', { name: /^(创建|更新)$/ });
  155. await submitButton.click();
  156. // 等待网络请求完成
  157. await this.page.waitForLoadState('networkidle', { timeout: 10000 });
  158. // 等待 Toast 消息显示
  159. await this.page.waitForTimeout(2000);
  160. // 检查 Toast 消息
  161. const errorToast = this.page.locator('[data-sonner-toast][data-type="error"]');
  162. const successToast = this.page.locator('[data-sonner-toast][data-type="success"]');
  163. const hasError = await errorToast.count() > 0;
  164. const hasSuccess = await successToast.count() > 0;
  165. let errorMessage = null;
  166. let successMessage = null;
  167. if (hasError) {
  168. errorMessage = await errorToast.first().textContent();
  169. }
  170. if (hasSuccess) {
  171. successMessage = await successToast.first().textContent();
  172. }
  173. return {
  174. success: hasSuccess || (!hasError && !hasSuccess),
  175. hasError,
  176. hasSuccess,
  177. errorMessage: errorMessage ?? undefined,
  178. successMessage: successMessage ?? undefined,
  179. };
  180. }
  181. /**
  182. * 取消对话框
  183. */
  184. async cancelDialog() {
  185. const cancelButton = this.page.getByRole('button', { name: '取消' });
  186. await cancelButton.click();
  187. await this.waitForDialogClosed();
  188. }
  189. /**
  190. * 等待对话框关闭
  191. */
  192. async waitForDialogClosed() {
  193. const dialog = this.page.locator('[role="dialog"]');
  194. await dialog.waitFor({ state: 'hidden', timeout: 5000 }).catch(() => {});
  195. await this.page.waitForTimeout(500);
  196. }
  197. /**
  198. * 确认删除操作
  199. */
  200. async confirmDelete() {
  201. const confirmButton = this.page.getByRole('button', { name: /^确认删除$/ });
  202. await confirmButton.click();
  203. // 等待确认对话框关闭和网络请求完成
  204. await this.page.waitForSelector('[role="alertdialog"]', { state: 'hidden', timeout: 5000 }).catch(() => {});
  205. await this.page.waitForLoadState('networkidle', { timeout: 10000 });
  206. await this.page.waitForTimeout(1000);
  207. }
  208. /**
  209. * 取消删除操作
  210. */
  211. async cancelDelete() {
  212. const cancelButton = this.page.getByRole('button', { name: '取消' }).and(
  213. this.page.locator('[role="alertdialog"]')
  214. );
  215. await cancelButton.click();
  216. await this.page.waitForSelector('[role="alertdialog"]', { state: 'hidden', timeout: 5000 }).catch(() => {});
  217. }
  218. /**
  219. * 确认状态切换操作
  220. */
  221. async confirmToggleStatus() {
  222. const confirmButton = this.page.locator('[role="alertdialog"]').getByRole('button', { name: '确认' });
  223. await confirmButton.click();
  224. // 等待确认对话框关闭和网络请求完成
  225. await this.page.waitForSelector('[role="alertdialog"]', { state: 'hidden', timeout: 5000 }).catch(() => {});
  226. await this.page.waitForLoadState('networkidle', { timeout: 10000 });
  227. await this.page.waitForTimeout(1000);
  228. }
  229. /**
  230. * 取消状态切换操作
  231. */
  232. async cancelToggleStatus() {
  233. const cancelButton = this.page.locator('[role="alertdialog"]').getByRole('button', { name: '取消' });
  234. await cancelButton.click();
  235. await this.page.waitForSelector('[role="alertdialog"]', { state: 'hidden', timeout: 5000 }).catch(() => {});
  236. }
  237. /**
  238. * 验证区域是否存在
  239. * @param regionName 区域名称
  240. * @returns 区域是否存在
  241. */
  242. async regionExists(regionName: string): Promise<boolean> {
  243. const regionElement = this.treeContainer.getByText(regionName);
  244. return (await regionElement.count()) > 0;
  245. }
  246. /**
  247. * 展开区域节点
  248. * @param regionName 区域名称
  249. */
  250. async expandNode(regionName: string) {
  251. // 找到区域节点的展开按钮(向右的箭头图标)
  252. const expandButton = this.treeContainer.getByText(regionName)
  253. .locator('../../..')
  254. .locator('button')
  255. .filter({ has: this.page.locator('svg[data-lucide="chevron-right"]') });
  256. const count = await expandButton.count();
  257. if (count > 0) {
  258. await expandButton.first().click();
  259. await this.page.waitForTimeout(500);
  260. }
  261. }
  262. /**
  263. * 收起区域节点
  264. * @param regionName 区域名称
  265. */
  266. async collapseNode(regionName: string) {
  267. // 找到区域节点的收起按钮(向下的箭头图标)
  268. const collapseButton = this.treeContainer.getByText(regionName)
  269. .locator('../../..')
  270. .locator('button')
  271. .filter({ has: this.page.locator('svg[data-lucide="chevron-down"]') });
  272. const count = await collapseButton.count();
  273. if (count > 0) {
  274. await collapseButton.first().click();
  275. await this.page.waitForTimeout(500);
  276. }
  277. }
  278. /**
  279. * 获取区域的状态
  280. * @param regionName 区域名称
  281. * @returns 区域状态('启用' 或 '禁用')
  282. */
  283. async getRegionStatus(regionName: string): Promise<'启用' | '禁用' | null> {
  284. const regionRow = this.treeContainer.getByText(regionName).locator('../../..');
  285. const statusBadge = regionRow.locator('.badge').filter({ hasText: /^(启用|禁用)$/ });
  286. const count = await statusBadge.count();
  287. if (count === 0) return null;
  288. const text = await statusBadge.first().textContent();
  289. return (text === '启用' || text === '禁用') ? text : null;
  290. }
  291. /**
  292. * 创建省
  293. * @param data 省份数据
  294. * @returns 表单提交结果
  295. */
  296. async createProvince(data: RegionData): Promise<FormSubmitResult> {
  297. await this.openCreateProvinceDialog();
  298. await this.fillRegionForm(data);
  299. const result = await this.submitForm();
  300. await this.waitForDialogClosed();
  301. return result;
  302. }
  303. /**
  304. * 创建子区域(市或区)
  305. * @param parentName 父级区域名称
  306. * @param childType 子区域类型
  307. * @param data 子区域数据
  308. * @returns 表单提交结果
  309. */
  310. async createChildRegion(
  311. parentName: string,
  312. childType: '市' | '区',
  313. data: RegionData
  314. ): Promise<FormSubmitResult> {
  315. await this.openAddChildDialog(parentName, childType);
  316. await this.fillRegionForm(data);
  317. const result = await this.submitForm();
  318. await this.waitForDialogClosed();
  319. return result;
  320. }
  321. /**
  322. * 编辑区域
  323. * @param regionName 区域名称
  324. * @param data 更新的区域数据
  325. * @returns 表单提交结果
  326. */
  327. async editRegion(regionName: string, data: RegionData): Promise<FormSubmitResult> {
  328. await this.openEditDialog(regionName);
  329. await this.fillRegionForm(data);
  330. const result = await this.submitForm();
  331. await this.waitForDialogClosed();
  332. return result;
  333. }
  334. /**
  335. * 删除区域
  336. * @param regionName 区域名称
  337. * @returns 是否成功(true = 成功删除, false = 删除失败或取消)
  338. */
  339. async deleteRegion(regionName: string): Promise<boolean> {
  340. await this.openDeleteDialog(regionName);
  341. await this.confirmDelete();
  342. // 等待并检查 Toast 消息
  343. await this.page.waitForTimeout(1000);
  344. const successToast = this.page.locator('[data-sonner-toast][data-type="success"]');
  345. const hasSuccess = await successToast.count() > 0;
  346. return hasSuccess;
  347. }
  348. /**
  349. * 切换区域状态(启用/禁用)
  350. * @param regionName 区域名称
  351. * @returns 是否成功
  352. */
  353. async toggleRegionStatus(regionName: string): Promise<boolean> {
  354. await this.openToggleStatusDialog(regionName);
  355. await this.confirmToggleStatus();
  356. // 等待并检查 Toast 消息
  357. await this.page.waitForTimeout(1000);
  358. const successToast = this.page.locator('[data-sonner-toast][data-type="success"]');
  359. const hasSuccess = await successToast.count() > 0;
  360. return hasSuccess;
  361. }
  362. /**
  363. * 等待树形结构加载完成
  364. */
  365. async waitForTreeLoaded() {
  366. await this.treeContainer.waitFor({ state: 'visible', timeout: 20000 });
  367. // 等待加载文本消失
  368. await this.page.waitForSelector('text=加载中...', { state: 'hidden', timeout: 10000 }).catch(() => {});
  369. }
  370. }