route-management.page.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import { Page, Locator, expect } from '@playwright/test';
  2. export class RouteManagementPage {
  3. readonly page: Page;
  4. readonly pageTitle: Locator;
  5. readonly createRouteButton: Locator;
  6. readonly searchInput: Locator;
  7. readonly searchButton: Locator;
  8. readonly routeTable: Locator;
  9. readonly editButtons: Locator;
  10. readonly deleteButtons: Locator;
  11. readonly statusToggleButtons: Locator;
  12. readonly pagination: Locator;
  13. readonly vehicleTypeFilter: Locator;
  14. constructor(page: Page) {
  15. this.page = page;
  16. this.pageTitle = page.locator('[data-testid="route-management-title"]');
  17. this.createRouteButton = page.locator('[data-testid="create-route-button"]');
  18. this.searchInput = page.locator('[data-testid="route-search-input"]');
  19. this.searchButton = page.getByRole('button', { name: '搜索' });
  20. this.routeTable = page.locator('[data-testid="route-table"]');
  21. this.editButtons = page.locator('[data-testid^="edit-route-"]');
  22. this.deleteButtons = page.locator('[data-testid^="delete-route-"]');
  23. this.statusToggleButtons = page.locator('[data-testid^="toggle-route-"]');
  24. this.pagination = page.locator('[data-slot="pagination"]');
  25. this.vehicleTypeFilter = page.locator('[data-testid="route-vehicle-type-filter"]');
  26. }
  27. async goto() {
  28. // 直接导航到路线管理页面
  29. await this.page.goto('/admin/routes');
  30. // 等待页面完全加载
  31. await this.page.waitForLoadState('domcontentloaded');
  32. // 等待路线管理标题出现
  33. await this.page.waitForSelector('[data-testid="route-management-title"]', { state: 'visible', timeout: 15000 });
  34. // 等待表格数据加载完成
  35. await this.page.waitForSelector('[data-testid="route-table"] tbody tr', { state: 'visible', timeout: 20000 });
  36. await this.expectToBeVisible();
  37. }
  38. async expectToBeVisible() {
  39. // 等待页面完全加载
  40. await expect(this.pageTitle).toBeVisible({ timeout: 15000 });
  41. await expect(this.createRouteButton).toBeVisible({ timeout: 10000 });
  42. // 等待至少一行路线数据加载完成
  43. await expect(this.routeTable.locator('tbody tr').first()).toBeVisible({ timeout: 20000 });
  44. }
  45. async searchRoutes(keyword: string) {
  46. await this.searchInput.fill(keyword);
  47. await this.searchButton.click();
  48. await this.page.waitForLoadState('networkidle');
  49. }
  50. async filterByVehicleType(type: '大巴' | '中巴' | '小车') {
  51. await this.vehicleTypeFilter.click();
  52. await this.page.getByRole('option', { name: type }).click();
  53. await this.page.waitForLoadState('networkidle');
  54. }
  55. async createRoute(routeData: {
  56. name: string;
  57. startPoint: string;
  58. endPoint: string;
  59. vehicleType: 'bus' | 'van' | 'car';
  60. price: number;
  61. seatCount: number;
  62. departureTime: string;
  63. activityId: number;
  64. }) {
  65. await this.createRouteButton.click();
  66. // 填写路线表单
  67. await this.page.getByLabel('路线名称').fill(routeData.name);
  68. await this.page.getByLabel('出发地').fill(routeData.startPoint);
  69. await this.page.getByLabel('目的地').fill(routeData.endPoint);
  70. // 选择车型
  71. await this.page.getByLabel('车型').click();
  72. const vehicleTypeMap = {
  73. 'bus': '大巴',
  74. 'van': '中巴',
  75. 'car': '小车'
  76. };
  77. await this.page.getByRole('option', { name: vehicleTypeMap[routeData.vehicleType] }).click();
  78. // 填写价格和座位数
  79. await this.page.getByLabel('价格').fill(routeData.price.toString());
  80. await this.page.getByLabel('座位数').fill(routeData.seatCount.toString());
  81. // 填写出发时间
  82. await this.page.getByLabel('出发时间').fill(routeData.departureTime);
  83. // 选择活动
  84. await this.page.getByLabel('关联活动').click();
  85. await this.page.getByRole('option').first().click();
  86. // 提交表单 - 使用模态框中的创建按钮
  87. await this.page.locator('[role="dialog"]').getByRole('button', { name: '创建路线' }).click();
  88. await this.page.waitForLoadState('networkidle');
  89. // 等待路线创建结果提示
  90. try {
  91. await Promise.race([
  92. this.page.waitForSelector('text=创建成功', { timeout: 10000 }),
  93. this.page.waitForSelector('text=创建失败', { timeout: 10000 })
  94. ]);
  95. // 检查是否有错误提示
  96. const errorVisible = await this.page.locator('text=创建失败').isVisible().catch(() => false);
  97. if (errorVisible) {
  98. return;
  99. }
  100. // 如果是创建成功,刷新页面
  101. await this.page.waitForTimeout(1000);
  102. await this.page.reload();
  103. await this.page.waitForLoadState('networkidle');
  104. await this.expectToBeVisible();
  105. } catch (error) {
  106. // 如果没有提示出现,继续执行
  107. console.log('创建操作没有显示提示信息,继续执行');
  108. await this.page.reload();
  109. await this.page.waitForLoadState('networkidle');
  110. await this.expectToBeVisible();
  111. }
  112. }
  113. async getRouteCount(): Promise<number> {
  114. const rows = await this.routeTable.locator('tbody tr').count();
  115. return rows;
  116. }
  117. async getRouteByName(name: string): Promise<Locator | null> {
  118. const routeRow = this.routeTable.locator('tbody tr').filter({ hasText: name }).first();
  119. return (await routeRow.count()) > 0 ? routeRow : null;
  120. }
  121. async routeExists(name: string): Promise<boolean> {
  122. const routeRow = this.routeTable.locator('tbody tr').filter({ hasText: name }).first();
  123. return (await routeRow.count()) > 0;
  124. }
  125. async editRoute(name: string, updates: {
  126. name?: string;
  127. startPoint?: string;
  128. endPoint?: string;
  129. vehicleType?: 'bus' | 'van' | 'car';
  130. price?: number;
  131. seatCount?: number;
  132. departureTime?: string;
  133. }) {
  134. const routeRow = await this.getRouteByName(name);
  135. if (!routeRow) throw new Error(`Route ${name} not found`);
  136. // 编辑按钮
  137. const editButton = routeRow.locator('[data-testid^="edit-route-"]');
  138. await editButton.waitFor({ state: 'visible', timeout: 10000 });
  139. await editButton.click();
  140. // 等待编辑模态框出现
  141. await this.page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: 10000 });
  142. // 更新字段
  143. if (updates.name) {
  144. await this.page.getByLabel('路线名称').fill(updates.name);
  145. }
  146. if (updates.startPoint) {
  147. await this.page.getByLabel('出发地').fill(updates.startPoint);
  148. }
  149. if (updates.endPoint) {
  150. await this.page.getByLabel('目的地').fill(updates.endPoint);
  151. }
  152. if (updates.vehicleType) {
  153. await this.page.getByLabel('车型').click();
  154. const vehicleTypeMap = {
  155. 'bus': '大巴',
  156. 'van': '中巴',
  157. 'car': '小车'
  158. };
  159. await this.page.getByRole('option', { name: vehicleTypeMap[updates.vehicleType] }).click();
  160. }
  161. if (updates.price) {
  162. await this.page.getByLabel('价格').fill(updates.price.toString());
  163. }
  164. if (updates.seatCount) {
  165. await this.page.getByLabel('座位数').fill(updates.seatCount.toString());
  166. }
  167. if (updates.departureTime) {
  168. await this.page.getByLabel('出发时间').fill(updates.departureTime);
  169. }
  170. // 提交更新
  171. await this.page.locator('[role="dialog"]').getByRole('button', { name: '更新路线' }).click();
  172. await this.page.waitForLoadState('networkidle');
  173. // 等待操作完成
  174. await this.page.waitForTimeout(1000);
  175. }
  176. async deleteRoute(name: string) {
  177. const routeRow = await this.getRouteByName(name);
  178. if (!routeRow) throw new Error(`Route ${name} not found`);
  179. // 删除按钮
  180. const deleteButton = routeRow.locator('[data-testid^="delete-route-"]');
  181. await deleteButton.waitFor({ state: 'visible', timeout: 10000 });
  182. await deleteButton.click();
  183. // 确认删除对话框
  184. await this.page.getByRole('button', { name: '删除' }).click();
  185. // 等待删除操作完成
  186. try {
  187. await Promise.race([
  188. this.page.waitForSelector('text=删除成功', { timeout: 10000 }),
  189. this.page.waitForSelector('text=删除失败', { timeout: 10000 })
  190. ]);
  191. const errorVisible = await this.page.locator('text=删除失败').isVisible().catch(() => false);
  192. if (errorVisible) {
  193. throw new Error('删除操作失败:前端显示删除失败提示');
  194. }
  195. } catch (error) {
  196. console.log('删除操作没有显示提示信息,继续执行');
  197. }
  198. // 刷新页面确认路线是否被删除
  199. await this.page.reload();
  200. await this.page.waitForLoadState('networkidle');
  201. await this.expectToBeVisible();
  202. }
  203. async toggleRouteStatus(name: string) {
  204. const routeRow = await this.getRouteByName(name);
  205. if (!routeRow) throw new Error(`Route ${name} not found`);
  206. // 状态切换按钮
  207. const statusButton = routeRow.locator('[data-testid^="toggle-route-"]');
  208. await statusButton.waitFor({ state: 'visible', timeout: 10000 });
  209. const currentStatus = await statusButton.textContent();
  210. await statusButton.click();
  211. // 确认状态切换对话框
  212. await this.page.getByRole('button', { name: '确认' }).click();
  213. // 等待操作完成
  214. await this.page.waitForLoadState('networkidle');
  215. await this.page.waitForTimeout(1000);
  216. return currentStatus;
  217. }
  218. async expectRouteExists(name: string) {
  219. const exists = await this.routeExists(name);
  220. expect(exists).toBe(true);
  221. }
  222. async expectRouteNotExists(name: string) {
  223. const exists = await this.routeExists(name);
  224. expect(exists).toBe(false);
  225. }
  226. async getRouteStatus(name: string): Promise<string | null> {
  227. const routeRow = await this.getRouteByName(name);
  228. if (!routeRow) return null;
  229. const statusButton = routeRow.locator('[data-testid^="toggle-route-"]');
  230. return await statusButton.textContent();
  231. }
  232. }