order-management.page.ts 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  1. import { Page, Locator } from '@playwright/test';
  2. import { selectRadixOption } from '@d8d/e2e-test-utils';
  3. /**
  4. * 订单状态常量
  5. */
  6. export const ORDER_STATUS = {
  7. DRAFT: 'draft',
  8. CONFIRMED: 'confirmed',
  9. IN_PROGRESS: 'in_progress',
  10. COMPLETED: 'completed',
  11. } as const;
  12. /**
  13. * 订单状态类型
  14. */
  15. export type OrderStatus = typeof ORDER_STATUS[keyof typeof ORDER_STATUS];
  16. /**
  17. * 订单状态显示名称映射
  18. */
  19. export const ORDER_STATUS_LABELS: Record<OrderStatus, string> = {
  20. draft: '草稿',
  21. confirmed: '已确认',
  22. in_progress: '进行中',
  23. completed: '已完成',
  24. } as const;
  25. /**
  26. * 工作状态常量
  27. */
  28. export const WORK_STATUS = {
  29. NOT_EMPLOYED: 'not_employed',
  30. PENDING: 'pending',
  31. EMPLOYED: 'employed',
  32. RESIGNED: 'resigned',
  33. } as const;
  34. /**
  35. * 工作状态类型
  36. */
  37. export type WorkStatus = typeof WORK_STATUS[keyof typeof WORK_STATUS];
  38. /**
  39. * 工作状态显示名称映射
  40. */
  41. export const WORK_STATUS_LABELS: Record<WorkStatus, string> = {
  42. not_employed: '未就业',
  43. pending: '待就业',
  44. employed: '已就业',
  45. resigned: '已离职',
  46. } as const;
  47. /**
  48. * 订单数据接口
  49. */
  50. export interface OrderData {
  51. /** 订单名称 */
  52. name: string;
  53. /** 预计开始日期 */
  54. expectedStartDate?: string;
  55. /** 平台ID */
  56. platformId?: number;
  57. /** 平台名称 */
  58. platformName?: string;
  59. /** 公司ID */
  60. companyId?: number;
  61. /** 公司名称 */
  62. companyName?: string;
  63. /** 渠道ID */
  64. channelId?: number;
  65. /** 渠道名称 */
  66. channelName?: string;
  67. /** 订单状态 */
  68. status?: OrderStatus;
  69. /** 工作状态 */
  70. workStatus?: WorkStatus;
  71. }
  72. /**
  73. * 订单人员数据接口
  74. */
  75. export interface OrderPersonData {
  76. /** 残疾人ID */
  77. disabledPersonId: number;
  78. /** 残疾人姓名 */
  79. disabledPersonName?: string;
  80. /** 入职日期 */
  81. hireDate?: string;
  82. /** 薪资 */
  83. salary?: number;
  84. /** 工作状态 */
  85. workStatus?: WorkStatus;
  86. /** 实际入职日期 */
  87. actualHireDate?: string;
  88. /** 离职日期 */
  89. resignDate?: string;
  90. }
  91. /**
  92. * 网络响应数据接口
  93. */
  94. export interface NetworkResponse {
  95. /** 请求URL */
  96. url: string;
  97. /** 请求方法 */
  98. method: string;
  99. /** 响应状态码 */
  100. status: number;
  101. /** 是否成功 */
  102. ok: boolean;
  103. /** 响应头 */
  104. responseHeaders: Record<string, string>;
  105. /** 响应体 */
  106. responseBody: unknown;
  107. }
  108. /**
  109. * 表单提交结果接口
  110. */
  111. export interface FormSubmitResult {
  112. /** 提交是否成功 */
  113. success: boolean;
  114. /** 是否有错误 */
  115. hasError: boolean;
  116. /** 是否有成功消息 */
  117. hasSuccess: boolean;
  118. /** 错误消息 */
  119. errorMessage?: string;
  120. /** 成功消息 */
  121. successMessage?: string;
  122. /** 网络响应列表 */
  123. responses?: NetworkResponse[];
  124. }
  125. /**
  126. * 订单管理 Page Object
  127. *
  128. * 用于订单管理功能的 E2E 测试
  129. * 页面路径: /admin/orders(待确认)
  130. *
  131. * @example
  132. * ```typescript
  133. * const orderPage = new OrderManagementPage(page);
  134. * await orderPage.goto();
  135. * await orderPage.createOrder({ name: '测试订单' });
  136. * ```
  137. */
  138. export class OrderManagementPage {
  139. readonly page: Page;
  140. // ===== 页面级选择器 =====
  141. /** 页面标题 */
  142. readonly pageTitle: Locator;
  143. /** 新增订单按钮 */
  144. readonly addOrderButton: Locator;
  145. /** 订单列表表格 */
  146. readonly orderTable: Locator;
  147. /** 搜索输入框 */
  148. readonly searchInput: Locator;
  149. /** 搜索按钮 */
  150. readonly searchButton: Locator;
  151. constructor(page: Page) {
  152. this.page = page;
  153. // 初始化页面级选择器
  154. // 使用更精确的选择器来定位页面标题(避免与侧边栏按钮冲突)
  155. this.pageTitle = page.locator('[data-slot="card-title"]').getByText('订单管理', { exact: true });
  156. // 使用 data-testid 定位创建订单按钮(按钮文本是"创建订单"不是"新增订单")
  157. this.addOrderButton = page.getByTestId('create-order-button');
  158. this.orderTable = page.locator('table');
  159. // 使用 data-testid 定位搜索输入框
  160. this.searchInput = page.getByTestId('search-order-name-input');
  161. // 使用 data-testid 定位搜索按钮
  162. this.searchButton = page.getByTestId('search-button');
  163. }
  164. // ===== 导航和基础验证 =====
  165. /**
  166. * 导航到订单管理页面
  167. */
  168. async goto() {
  169. await this.page.goto('/admin/orders');
  170. await this.page.waitForLoadState('domcontentloaded');
  171. // 等待页面标题出现
  172. await this.pageTitle.waitFor({ state: 'visible', timeout: 15000 });
  173. // 等待表格数据加载
  174. await this.page.waitForSelector('table tbody tr', { state: 'visible', timeout: 20000 });
  175. await this.expectToBeVisible();
  176. }
  177. /**
  178. * 验证页面关键元素可见
  179. */
  180. async expectToBeVisible() {
  181. await this.pageTitle.waitFor({ state: 'visible', timeout: 15000 });
  182. await this.addOrderButton.waitFor({ state: 'visible', timeout: 10000 });
  183. }
  184. // ===== 搜索和筛选功能 =====
  185. /**
  186. * 按订单名称搜索
  187. * @param name 订单名称
  188. */
  189. async searchByName(name: string) {
  190. await this.searchInput.fill(name);
  191. await this.searchButton.click();
  192. await this.page.waitForLoadState('networkidle');
  193. await this.page.waitForTimeout(1000);
  194. }
  195. /**
  196. * 打开高级筛选对话框
  197. */
  198. async openFilterDialog() {
  199. const filterButton = this.page.getByRole('button', { name: /筛选|高级筛选/ });
  200. await filterButton.click();
  201. await this.page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: 5000 });
  202. }
  203. /**
  204. * 设置筛选条件
  205. * @param filters 筛选条件
  206. */
  207. async setFilters(filters: {
  208. status?: OrderStatus;
  209. workStatus?: WorkStatus;
  210. platformId?: number;
  211. platformName?: string;
  212. companyId?: number;
  213. companyName?: string;
  214. channelId?: number;
  215. channelName?: string;
  216. dateRange?: { start?: string; end?: string };
  217. }) {
  218. // 订单状态筛选
  219. if (filters.status) {
  220. const statusFilter = this.page.getByLabel(/订单状态/);
  221. await statusFilter.click();
  222. const statusLabel = ORDER_STATUS_LABELS[filters.status];
  223. await this.page.getByRole('option', { name: statusLabel }).click();
  224. }
  225. // 工作状态筛选
  226. if (filters.workStatus) {
  227. const workStatusFilter = this.page.getByLabel(/工作状态/);
  228. await workStatusFilter.click();
  229. const workStatusLabel = WORK_STATUS_LABELS[filters.workStatus];
  230. await this.page.getByRole('option', { name: workStatusLabel }).click();
  231. }
  232. // 平台筛选
  233. if (filters.platformName) {
  234. await selectRadixOption(this.page, '平台', filters.platformName);
  235. }
  236. // 公司筛选
  237. if (filters.companyName) {
  238. await selectRadixOption(this.page, '公司', filters.companyName);
  239. }
  240. // 渠道筛选
  241. if (filters.channelName) {
  242. await selectRadixOption(this.page, '渠道', filters.channelName);
  243. }
  244. // 日期范围筛选
  245. if (filters.dateRange) {
  246. if (filters.dateRange.start) {
  247. const startDateInput = this.page.getByLabel(/开始日期|起始日期/);
  248. await startDateInput.fill(filters.dateRange.start);
  249. }
  250. if (filters.dateRange.end) {
  251. const endDateInput = this.page.getByLabel(/结束日期|截止日期/);
  252. await endDateInput.fill(filters.dateRange.end);
  253. }
  254. }
  255. }
  256. /**
  257. * 应用筛选条件
  258. */
  259. async applyFilters() {
  260. const applyButton = this.page.getByRole('button', { name: /应用|确定|筛选/ });
  261. await applyButton.click();
  262. await this.page.waitForLoadState('networkidle');
  263. await this.page.waitForTimeout(1000);
  264. }
  265. /**
  266. * 清空筛选条件
  267. */
  268. async clearFilters() {
  269. const clearButton = this.page.getByRole('button', { name: /重置|清空/ });
  270. await clearButton.click();
  271. await this.page.waitForTimeout(500);
  272. }
  273. // ===== 订单 CRUD 操作 =====
  274. /**
  275. * 打开创建订单对话框
  276. */
  277. async openCreateDialog() {
  278. await this.addOrderButton.click();
  279. await this.page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: 5000 });
  280. }
  281. /**
  282. * 打开编辑订单对话框
  283. * @param orderName 订单名称
  284. */
  285. async openEditDialog(orderName: string) {
  286. // 找到订单行并点击"打开菜单"按钮
  287. const orderRow = this.orderTable.locator('tbody tr').filter({ hasText: orderName });
  288. const menuButton = orderRow.getByRole('button', { name: '打开菜单' });
  289. await menuButton.click();
  290. // 等待菜单出现并点击"编辑"选项
  291. // 使用 data-testid 或 role 定位编辑选项
  292. const editOption = this.page.getByRole('menuitem', { name: '编辑' });
  293. await editOption.waitFor({ state: 'visible', timeout: 3000 });
  294. await editOption.click();
  295. // 等待编辑对话框出现
  296. await this.page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: 5000 });
  297. }
  298. /**
  299. * 打开删除确认对话框
  300. * @param orderName 订单名称
  301. */
  302. async openDeleteDialog(orderName: string) {
  303. // 找到订单行并点击"打开菜单"按钮(与编辑操作相同的模式)
  304. const orderRow = this.orderTable.locator('tbody tr').filter({ hasText: orderName });
  305. const menuButton = orderRow.getByRole('button', { name: '打开菜单' });
  306. await menuButton.click();
  307. // 等待菜单出现并点击"删除"选项
  308. const deleteOption = this.page.getByRole('menuitem', { name: '删除' });
  309. await deleteOption.waitFor({ state: 'visible', timeout: 3000 });
  310. await deleteOption.click();
  311. // 等待删除确认对话框出现
  312. await this.page.waitForSelector('[role="alertdialog"]', { state: 'visible', timeout: 5000 });
  313. }
  314. /**
  315. * 填写订单表单
  316. * @param data 订单数据
  317. */
  318. async fillOrderForm(data: OrderData) {
  319. // 等待表单出现
  320. await this.page.waitForSelector('form', { state: 'visible', timeout: 5000 });
  321. // 填写订单名称
  322. if (data.name) {
  323. await this.page.getByLabel(/订单名称|名称/).fill(data.name);
  324. }
  325. // 填写预计开始日期
  326. if (data.expectedStartDate) {
  327. const dateInput = this.page.getByLabel(/预计开始日期|开始日期/);
  328. await dateInput.fill(data.expectedStartDate);
  329. }
  330. // 选择平台
  331. if (data.platformName) {
  332. await selectRadixOption(this.page, '平台', data.platformName);
  333. }
  334. // 选择公司
  335. if (data.companyName) {
  336. await selectRadixOption(this.page, '公司', data.companyName);
  337. }
  338. // 选择渠道
  339. if (data.channelName) {
  340. await selectRadixOption(this.page, '渠道', data.channelName);
  341. }
  342. // 选择订单状态(如果是编辑模式)
  343. if (data.status) {
  344. const statusLabel = ORDER_STATUS_LABELS[data.status];
  345. await selectRadixOption(this.page, '订单状态', statusLabel);
  346. }
  347. // 选择工作状态(如果是编辑模式)
  348. if (data.workStatus) {
  349. const workStatusLabel = WORK_STATUS_LABELS[data.workStatus];
  350. await selectRadixOption(this.page, '工作状态', workStatusLabel);
  351. }
  352. }
  353. /**
  354. * 提交表单
  355. * @returns 表单提交结果
  356. */
  357. async submitForm(): Promise<FormSubmitResult> {
  358. // 收集网络响应
  359. const responses: NetworkResponse[] = [];
  360. // 监听所有网络请求
  361. const responseHandler = async (response: Response) => {
  362. const url = response.url();
  363. // 监听订单管理相关的 API 请求
  364. if (url.includes('/orders') || url.includes('order')) {
  365. const requestBody = response.request()?.postData();
  366. const responseBody = await response.text().catch(() => '');
  367. let jsonBody = null;
  368. try {
  369. jsonBody = JSON.parse(responseBody);
  370. } catch {
  371. // 不是 JSON 响应
  372. }
  373. responses.push({
  374. url,
  375. method: response.request()?.method() ?? 'UNKNOWN',
  376. status: response.status(),
  377. ok: response.ok(),
  378. responseHeaders: await response.allHeaders().catch(() => ({})),
  379. responseBody: jsonBody || responseBody,
  380. });
  381. }
  382. };
  383. this.page.on('response', responseHandler);
  384. try {
  385. // 点击提交按钮(创建或更新)
  386. const submitButton = this.page.getByRole('button', { name: /^(创建|更新|保存)$/ });
  387. await submitButton.click();
  388. // 等待网络请求完成(使用较宽松的超时,因为有些操作可能不触发网络请求)
  389. try {
  390. await this.page.waitForLoadState('domcontentloaded', { timeout: 5000 });
  391. } catch {
  392. // domcontentloaded 超时不是致命错误,继续检查 Toast 消息
  393. console.debug('domcontentloaded 超时,继续检查 Toast 消息');
  394. }
  395. } finally {
  396. // 确保监听器总是被移除,防止内存泄漏
  397. this.page.off('response', responseHandler);
  398. }
  399. // 等待 Toast 消息显示
  400. await this.page.waitForTimeout(2000);
  401. // 检查 Toast 消息
  402. const errorToast = this.page.locator('[data-sonner-toast][data-type="error"]');
  403. const successToast = this.page.locator('[data-sonner-toast][data-type="success"]');
  404. const hasError = await errorToast.count() > 0;
  405. const hasSuccess = await successToast.count() > 0;
  406. let errorMessage: string | null = null;
  407. let successMessage: string | null = null;
  408. if (hasError) {
  409. errorMessage = await errorToast.first().textContent();
  410. }
  411. if (hasSuccess) {
  412. successMessage = await successToast.first().textContent();
  413. }
  414. return {
  415. success: hasSuccess || (!hasError && !hasSuccess),
  416. hasError,
  417. hasSuccess,
  418. errorMessage: errorMessage ?? undefined,
  419. successMessage: successMessage ?? undefined,
  420. responses,
  421. };
  422. }
  423. /**
  424. * 取消对话框
  425. */
  426. async cancelDialog() {
  427. const cancelButton = this.page.getByRole('button', { name: '取消' });
  428. await cancelButton.click();
  429. await this.waitForDialogClosed();
  430. }
  431. /**
  432. * 等待对话框关闭
  433. */
  434. async waitForDialogClosed() {
  435. // 先等待一段时间让对话框有机会关闭
  436. await this.page.waitForTimeout(1000);
  437. // 检查是否还有对话框可见
  438. const dialogs = this.page.locator('[role="dialog"]');
  439. const dialogCount = await dialogs.count();
  440. if (dialogCount === 0) {
  441. // 没有对话框了,已经关闭
  442. console.debug('对话框已关闭(无对话框元素)');
  443. return;
  444. }
  445. // 尝试等待对话框隐藏或从 DOM 中移除
  446. try {
  447. await dialogs.first().waitFor({ state: 'hidden', timeout: 3000 });
  448. console.debug('对话框已关闭');
  449. } catch {
  450. // 超时不是致命错误,对话框可能已经以其他方式关闭
  451. console.debug('对话框关闭等待超时,继续执行');
  452. }
  453. await this.page.waitForTimeout(500);
  454. }
  455. /**
  456. * 确认删除操作
  457. */
  458. async confirmDelete() {
  459. // 尝试多种可能的按钮名称
  460. const confirmButton = this.page.locator('[role="alertdialog"]').getByRole('button', {
  461. name: /^(确认删除|删除|确定|确认)$/
  462. });
  463. await confirmButton.click();
  464. // 等待确认对话框关闭和网络请求完成
  465. await this.page.waitForSelector('[role="alertdialog"]', { state: 'hidden', timeout: 5000 })
  466. .catch(() => console.debug('删除确认对话框关闭超时'));
  467. await this.page.waitForLoadState('networkidle', { timeout: 10000 });
  468. await this.page.waitForTimeout(1000);
  469. }
  470. /**
  471. * 取消删除操作
  472. */
  473. async cancelDelete() {
  474. const cancelButton = this.page.getByRole('button', { name: '取消' }).and(
  475. this.page.locator('[role="alertdialog"]')
  476. );
  477. await cancelButton.click();
  478. await this.page.waitForSelector('[role="alertdialog"]', { state: 'hidden', timeout: 5000 })
  479. .catch(() => console.debug('删除确认对话框关闭超时(取消操作)'));
  480. }
  481. /**
  482. * 验证订单是否存在
  483. * @param orderName 订单名称
  484. * @returns 订单是否存在
  485. */
  486. async orderExists(orderName: string): Promise<boolean> {
  487. const orderRow = this.orderTable.locator('tbody tr').filter({ hasText: orderName });
  488. return (await orderRow.count()) > 0;
  489. }
  490. // ===== 订单详情 =====
  491. /**
  492. * 打开订单详情对话框
  493. * @param orderName 订单名称
  494. */
  495. async openDetailDialog(orderName: string) {
  496. // 找到订单行
  497. const orderRow = this.orderTable.locator('tbody tr').filter({ hasText: orderName });
  498. // 先点击操作菜单触发按钮("打开菜单" 或 MoreHorizontal 图标)
  499. const menuTrigger = orderRow.getByRole('button', { name: /打开菜单/ });
  500. await menuTrigger.click();
  501. // 等待菜单显示
  502. await this.page.waitForTimeout(200);
  503. // 点击"查看详情"菜单项
  504. const detailButton = this.page.getByRole('menuitem', { name: /查看详情/ });
  505. await detailButton.click();
  506. await this.page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: 5000 });
  507. }
  508. /**
  509. * 获取订单详情中的基本信息
  510. * @returns 订单基本信息
  511. */
  512. async getOrderDetailInfo(): Promise<{
  513. name?: string;
  514. status?: string;
  515. workStatus?: string;
  516. expectedStartDate?: string;
  517. platform?: string;
  518. company?: string;
  519. channel?: string;
  520. }> {
  521. const dialog = this.page.locator('[role="dialog"]');
  522. const result: Record<string, string | undefined> = {};
  523. // 订单名称 - 查找"订单名称"标签后的值
  524. const nameElement = dialog.locator('.text-muted-foreground').filter({ hasText: '订单名称' })
  525. .locator('..').locator('p,span,div').nth(1);
  526. if (await nameElement.count() > 0) {
  527. const text = await nameElement.textContent();
  528. result.name = text || undefined;
  529. }
  530. // 订单状态
  531. const statusElement = dialog.locator('.text-muted-foreground').filter({ hasText: '订单状态' })
  532. .locator('..').locator('p,span,div').nth(1);
  533. if (await statusElement.count() > 0) {
  534. const text = await statusElement.textContent();
  535. result.status = text || undefined;
  536. }
  537. // 工作状态
  538. const workStatusElement = dialog.locator('.text-muted-foreground').filter({ hasText: '工作状态' })
  539. .locator('..').locator('p,span,div').nth(1);
  540. if (await workStatusElement.count() > 0) {
  541. const text = await workStatusElement.textContent();
  542. result.workStatus = text || undefined;
  543. }
  544. // 预计开始日期
  545. const startDateElement = dialog.locator('.text-muted-foreground').filter({ hasText: /预计开始日期|开始日期/ })
  546. .locator('..').locator('p,span,div').nth(1);
  547. if (await startDateElement.count() > 0) {
  548. const text = await startDateElement.textContent();
  549. result.expectedStartDate = text || undefined;
  550. }
  551. // 平台
  552. const platformElement = dialog.locator('.text-muted-foreground').filter({ hasText: '平台' })
  553. .locator('..').locator('p,span,div').nth(1);
  554. if (await platformElement.count() > 0) {
  555. const text = await platformElement.textContent();
  556. result.platform = text || undefined;
  557. }
  558. // 公司
  559. const companyElement = dialog.locator('.text-muted-foreground').filter({ hasText: '公司' })
  560. .locator('..').locator('p,span,div').nth(1);
  561. if (await companyElement.count() > 0) {
  562. const text = await companyElement.textContent();
  563. result.company = text || undefined;
  564. }
  565. // 渠道
  566. const channelElement = dialog.locator('.text-muted-foreground').filter({ hasText: '渠道' })
  567. .locator('..').locator('p,span,div').nth(1);
  568. if (await channelElement.count() > 0) {
  569. const text = await channelElement.textContent();
  570. result.channel = text || undefined;
  571. }
  572. return result;
  573. }
  574. /**
  575. * 从订单详情对话框中获取关联人员列表
  576. * @returns 人员信息列表
  577. */
  578. async getPersonListFromDetail(): Promise<Array<{
  579. name?: string;
  580. workStatus?: string;
  581. hireDate?: string;
  582. salary?: string;
  583. }>> {
  584. const dialog = this.page.locator('[role="dialog"]');
  585. const result: Array<{ name?: string; workStatus?: string; hireDate?: string; salary?: string }> = [];
  586. // 查找人员列表区域(通常在详情对话框中有一个表格或列表展示人员)
  587. // 尝试多种可能的定位策略
  588. const personTable = dialog.locator('table').filter({ hasText: /人员|员工/ });
  589. const personList = dialog.locator('[class*="person"], [class*="employee"], [data-testid*="person"]');
  590. // 优先使用表格形式
  591. if (await personTable.count() > 0) {
  592. const rows = personTable.locator('tbody tr');
  593. const rowCount = await rows.count();
  594. for (let i = 0; i < rowCount; i++) {
  595. const row = rows.nth(i);
  596. const cells = row.locator('td');
  597. const cellCount = await cells.count();
  598. const personInfo: { name?: string; workStatus?: string; hireDate?: string; salary?: string } = {};
  599. // 根据列数量和数据类型提取信息
  600. for (let j = 0; j < cellCount; j++) {
  601. const cellText = await cells.nth(j).textContent();
  602. if (!cellText) continue;
  603. const trimmedText = cellText.trim();
  604. // 尝试识别列内容
  605. // 姓名通常在第一列
  606. if (j === 0 && trimmedText) {
  607. personInfo.name = trimmedText;
  608. }
  609. // 工作状态检查
  610. for (const [statusValue, statusLabel] of Object.entries(WORK_STATUS_LABELS)) {
  611. if (trimmedText.includes(statusLabel)) {
  612. personInfo.workStatus = statusLabel;
  613. break;
  614. }
  615. }
  616. // 薪资检查(包含数字)
  617. if (/^\d+(\.\d+)?$/.test(trimmedText.replace(/,/g, ''))) {
  618. personInfo.salary = trimmedText;
  619. }
  620. // 日期检查(符合日期格式)
  621. if (/^\d{4}-\d{2}-\d{2}$/.test(trimmedText) || /^\d{4}\/\d{2}\/\d{2}$/.test(trimmedText)) {
  622. if (!personInfo.hireDate) {
  623. personInfo.hireDate = trimmedText;
  624. }
  625. }
  626. }
  627. if (personInfo.name || personInfo.workStatus) {
  628. result.push(personInfo);
  629. }
  630. }
  631. } else if (await personList.count() > 0) {
  632. // 如果是列表形式而非表格
  633. const listItems = personList.locator('[class*="item"], [class*="row"], li, div');
  634. const itemCount = await listItems.count();
  635. for (let i = 0; i < itemCount; i++) {
  636. const item = listItems.nth(i);
  637. const itemText = await item.textContent();
  638. if (itemText && itemText.trim()) {
  639. result.push({ name: itemText.trim() });
  640. }
  641. }
  642. }
  643. return result;
  644. }
  645. /**
  646. * 从订单详情对话框中获取附件列表
  647. * @returns 附件信息列表
  648. */
  649. async getAttachmentListFromDetail(): Promise<Array<{
  650. fileName?: string;
  651. uploadDate?: string;
  652. uploader?: string;
  653. }>> {
  654. const dialog = this.page.locator('[role="dialog"]');
  655. const result: Array<{ fileName?: string; uploadDate?: string; uploader?: string }> = [];
  656. // 查找附件列表区域
  657. // 尝试多种可能的定位策略
  658. const attachmentTable = dialog.locator('table').filter({ hasText: /附件|文件/ });
  659. const attachmentList = dialog.locator('[class*="attachment"], [class*="file"], [data-testid*="attachment"]');
  660. // 优先使用表格形式
  661. if (await attachmentTable.count() > 0) {
  662. const rows = attachmentTable.locator('tbody tr');
  663. const rowCount = await rows.count();
  664. for (let i = 0; i < rowCount; i++) {
  665. const row = rows.nth(i);
  666. const cells = row.locator('td');
  667. const cellCount = await cells.count();
  668. const attachmentInfo: { fileName?: string; uploadDate?: string; uploader?: string } = {};
  669. for (let j = 0; j < cellCount; j++) {
  670. const cellText = await cells.nth(j).textContent();
  671. if (!cellText) continue;
  672. const trimmedText = cellText.trim();
  673. // 文件名通常在第一列
  674. if (j === 0 && trimmedText) {
  675. attachmentInfo.fileName = trimmedText;
  676. }
  677. // 日期检查
  678. if (/^\d{4}-\d{2}-\d{2}/.test(trimmedText) || /^\d{4}\/\d{2}\/\d{2}/.test(trimmedText)) {
  679. if (!attachmentInfo.uploadDate) {
  680. attachmentInfo.uploadDate = trimmedText;
  681. }
  682. }
  683. // 上传者通常是文本用户名
  684. if (j > 0 && trimmedText && !attachmentInfo.uploader && !attachmentInfo.uploadDate && !/^\d{4}/.test(trimmedText)) {
  685. attachmentInfo.uploader = trimmedText;
  686. }
  687. }
  688. if (attachmentInfo.fileName) {
  689. result.push(attachmentInfo);
  690. }
  691. }
  692. } else if (await attachmentList.count() > 0) {
  693. // 如果是列表形式
  694. const listItems = attachmentList.locator('[class*="item"], [class*="row"], li, div');
  695. const itemCount = await listItems.count();
  696. for (let i = 0; i < itemCount; i++) {
  697. const item = listItems.nth(i);
  698. const itemText = await item.textContent();
  699. if (itemText && itemText.trim()) {
  700. result.push({ fileName: itemText.trim() });
  701. }
  702. }
  703. }
  704. return result;
  705. }
  706. /**
  707. * 关闭订单详情对话框
  708. */
  709. async closeDetailDialog(): Promise<void> {
  710. // 尝试多种关闭方式
  711. // 方式1: 点击右上角 X 按钮
  712. const closeButton = this.page.locator('[role="dialog"]').getByRole('button', { name: '关闭' }).first();
  713. const closeButtonCount = await closeButton.count();
  714. if (closeButtonCount > 0) {
  715. await closeButton.click();
  716. } else {
  717. // 方式2: 点击取消按钮
  718. const cancelButton = this.page.locator('[role="dialog"]').getByRole('button', { name: '取消' }).first();
  719. const cancelButtonCount = await cancelButton.count();
  720. if (cancelButtonCount > 0) {
  721. await cancelButton.click();
  722. } else {
  723. // 方式3: 按 Escape 键
  724. await this.page.keyboard.press('Escape');
  725. }
  726. }
  727. // 等待对话框关闭
  728. await this.waitForDialogClosed();
  729. }
  730. // ===== 人员关联管理 =====
  731. /**
  732. * 打开人员管理对话框
  733. *
  734. * **使用场景:**
  735. * - **从订单列表页打开**: 传入 `orderName` 参数,方法会先找到对应订单行,再点击人员管理按钮
  736. * - **从订单详情页打开**: 不传参数,方法会直接点击页面中的人员管理按钮
  737. *
  738. * @param orderName 订单名称(可选)。从列表页打开时需要传入,从详情页打开时不传
  739. *
  740. * @example
  741. * ```typescript
  742. * // 从订单列表页打开
  743. * await orderPage.openPersonManagementDialog('测试订单');
  744. *
  745. * // 从订单详情页打开
  746. * await orderPage.openDetailDialog('测试订单');
  747. * await orderPage.openPersonManagementDialog();
  748. * ```
  749. */
  750. async openPersonManagementDialog(orderName?: string) {
  751. // 人员管理功能直接集成在订单详情对话框中
  752. // 如果提供了订单名称,打开订单详情对话框
  753. if (orderName) {
  754. await this.openDetailDialog(orderName);
  755. }
  756. // 人员管理功能已在详情对话框中,无需额外操作
  757. }
  758. /**
  759. * 添加人员到订单
  760. * @param personData 人员数据
  761. */
  762. async addPersonToOrder(personData: OrderPersonData) {
  763. // 点击添加人员按钮
  764. const addButton = this.page.getByRole('button', { name: /添加人员|新增人员/ });
  765. await addButton.click();
  766. await this.page.waitForTimeout(300);
  767. // 选择残疾人(支持通过名称选择)
  768. if (personData.disabledPersonName) {
  769. await selectRadixOption(this.page, '残疾人|选择残疾人', personData.disabledPersonName);
  770. } else if (personData.disabledPersonId) {
  771. // 如果只提供了 ID,尝试在对话框中选择第一个残疾人
  772. const firstCheckbox = this.page.locator('[role="dialog"]').locator('table tbody tr').first().locator('input[type="checkbox"]').first();
  773. try {
  774. await firstCheckbox.waitFor({ state: 'visible', timeout: 3000 });
  775. await firstCheckbox.check();
  776. } catch {
  777. console.debug('没有可用的残疾人数据');
  778. }
  779. }
  780. // 填写入职日期
  781. if (personData.hireDate) {
  782. const hireDateInput = this.page.getByLabel(/入职日期/);
  783. await hireDateInput.fill(personData.hireDate);
  784. }
  785. // 填写薪资
  786. if (personData.salary !== undefined) {
  787. const salaryInput = this.page.getByLabel(/薪资|工资/);
  788. await salaryInput.fill(String(personData.salary));
  789. }
  790. // 选择工作状态
  791. if (personData.workStatus) {
  792. const workStatusLabel = WORK_STATUS_LABELS[personData.workStatus];
  793. await selectRadixOption(this.page, '工作状态', workStatusLabel);
  794. }
  795. // 提交
  796. const submitButton = this.page.getByRole('button', { name: /^(添加|确定|保存)$/ });
  797. await submitButton.click();
  798. await this.page.waitForLoadState('networkidle');
  799. await this.page.waitForTimeout(1000);
  800. }
  801. /**
  802. * 修改人员工作状态
  803. * @param personName 人员姓名
  804. * @param newStatus 新的工作状态
  805. */
  806. async updatePersonWorkStatus(personName: string, newStatus: WorkStatus) {
  807. const dialog = this.page.locator('[role="dialog"]');
  808. // 找到所有表格,选择第二个表格(绑定人员列表)
  809. // 第一个表格是待添加人员列表,第二个是绑定人员列表
  810. const allTables = await dialog.locator('table').all();
  811. const boundPersonsTable = allTables[1]; // 第二个表格
  812. // 在该表格中找到包含人员姓名的行
  813. const allRows = boundPersonsTable.locator('tbody tr');
  814. const targetRow = allRows.filter({ hasText: personName });
  815. // 工作状态在倒数第二列(最后一列是薪资)
  816. const cells = targetRow.locator('td');
  817. const cellCount = await cells.count();
  818. // 工作状态是倒数第二列(索引 cellCount - 2)
  819. const workStatusCell = cells.nth(cellCount - 2);
  820. const workStatusSelect = workStatusCell.locator('[role="combobox"]');
  821. await workStatusSelect.click();
  822. // 等待下拉选项显示
  823. await this.page.waitForTimeout(300);
  824. // 使用中文标签选择选项
  825. const workStatusLabel = WORK_STATUS_LABELS[newStatus];
  826. const option = this.page.getByRole('option').filter({ hasText: workStatusLabel });
  827. await option.click();
  828. await this.page.waitForLoadState('networkidle');
  829. await this.page.waitForTimeout(500);
  830. }
  831. // ===== 附件管理 =====
  832. /**
  833. * 打开添加附件对话框
  834. */
  835. async openAddAttachmentDialog() {
  836. const attachmentButton = this.page.getByRole('button', { name: /添加附件|上传附件/ });
  837. await attachmentButton.click();
  838. await this.page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: 5000 });
  839. }
  840. /**
  841. * 上传附件
  842. * @param personName 人员姓名
  843. * @param fileName 文件名
  844. * @param mimeType 文件类型(默认为 image/jpeg)
  845. */
  846. async uploadAttachment(personName: string, fileName: string, mimeType: string = 'image/jpeg') {
  847. // 选择订单人员
  848. const personSelect = this.page.getByLabel(/选择人员|订单人员/);
  849. await personSelect.click();
  850. await this.page.getByRole('option', { name: personName }).click();
  851. // 查找文件上传输入框
  852. const fileInput = this.page.locator('input[type="file"]');
  853. await fileInput.setInputFiles({
  854. name: fileName,
  855. mimeType,
  856. buffer: Buffer.from(`fake ${fileName} content`),
  857. });
  858. // 等待上传处理
  859. await this.page.waitForTimeout(500);
  860. // 提交
  861. const submitButton = this.page.getByRole('button', { name: /^(上传|确定|保存)$/ });
  862. await submitButton.click();
  863. await this.page.waitForLoadState('networkidle');
  864. await this.page.waitForTimeout(1000);
  865. }
  866. // ===== 高级操作 =====
  867. /**
  868. * 创建订单(完整流程)
  869. * @param data 订单数据
  870. * @returns 表单提交结果
  871. */
  872. async createOrder(data: OrderData): Promise<FormSubmitResult> {
  873. await this.openCreateDialog();
  874. await this.fillOrderForm(data);
  875. const result = await this.submitForm();
  876. await this.waitForDialogClosed();
  877. return result;
  878. }
  879. /**
  880. * 编辑订单(完整流程)
  881. * @param orderName 订单名称
  882. * @param data 更新的订单数据
  883. * @returns 表单提交结果
  884. */
  885. async editOrder(orderName: string, data: OrderData): Promise<FormSubmitResult> {
  886. await this.openEditDialog(orderName);
  887. await this.fillOrderForm(data);
  888. const result = await this.submitForm();
  889. await this.waitForDialogClosed();
  890. return result;
  891. }
  892. /**
  893. * 删除订单(完整流程)
  894. * @param orderName 订单名称
  895. * @returns 是否成功删除
  896. */
  897. async deleteOrder(orderName: string): Promise<boolean> {
  898. await this.openDeleteDialog(orderName);
  899. await this.confirmDelete();
  900. // 等待并检查 Toast 消息
  901. await this.page.waitForTimeout(1000);
  902. const successToast = this.page.locator('[data-sonner-toast][data-type="success"]');
  903. const hasSuccess = await successToast.count() > 0;
  904. return hasSuccess;
  905. }
  906. // ===== 订单状态流转操作 =====
  907. /**
  908. * 打开激活订单确认对话框
  909. * @param orderName 订单名称
  910. */
  911. async openActivateDialog(orderName: string): Promise<void> {
  912. // 找到订单行并点击"打开菜单"按钮(与编辑/删除操作相同的模式)
  913. const orderRow = this.orderTable.locator('tbody tr').filter({ hasText: orderName });
  914. const menuButton = orderRow.getByRole('button', { name: '打开菜单' });
  915. await menuButton.click();
  916. // 等待菜单出现并点击"激活"选项
  917. const activateOption = this.page.getByRole('menuitem', { name: /激活|激活订单/ });
  918. await activateOption.waitFor({ state: 'visible', timeout: 3000 });
  919. await activateOption.click();
  920. // 等待确认对话框出现
  921. await this.page.waitForSelector('[role="alertdialog"]', { state: 'visible', timeout: 5000 });
  922. }
  923. /**
  924. * 确认激活订单
  925. */
  926. async confirmActivate(): Promise<void> {
  927. // 尝试多种可能的按钮名称
  928. const confirmButton = this.page.locator('[role="alertdialog"]').getByRole('button', {
  929. name: /^(确认激活|激活|确定|确认)$/
  930. });
  931. await confirmButton.click();
  932. // 等待确认对话框关闭和网络请求完成
  933. await this.page.waitForSelector('[role="alertdialog"]', { state: 'hidden', timeout: 5000 })
  934. .catch(() => console.debug('激活确认对话框关闭超时'));
  935. await this.page.waitForLoadState('networkidle', { timeout: 10000 });
  936. await this.page.waitForTimeout(1000);
  937. }
  938. /**
  939. * 激活订单(完整流程)
  940. * @param orderName 订单名称
  941. * @returns 是否成功激活
  942. */
  943. async activateOrder(orderName: string): Promise<boolean> {
  944. await this.openActivateDialog(orderName);
  945. await this.confirmActivate();
  946. // 等待并检查 Toast 消息
  947. await this.page.waitForTimeout(1000);
  948. const successToast = this.page.locator('[data-sonner-toast][data-type="success"]');
  949. const hasSuccess = await successToast.count() > 0;
  950. return hasSuccess;
  951. }
  952. /**
  953. * 打开关闭订单确认对话框
  954. * @param orderName 订单名称
  955. */
  956. async openCloseDialog(orderName: string): Promise<void> {
  957. // 找到订单行并点击"打开菜单"按钮
  958. const orderRow = this.orderTable.locator('tbody tr').filter({ hasText: orderName });
  959. const menuButton = orderRow.getByRole('button', { name: '打开菜单' });
  960. await menuButton.click();
  961. // 等待菜单出现并点击"关闭"选项
  962. const closeOption = this.page.getByRole('menuitem', { name: /关闭|关闭订单|完成/ });
  963. await closeOption.waitFor({ state: 'visible', timeout: 3000 });
  964. await closeOption.click();
  965. // 等待确认对话框出现
  966. await this.page.waitForSelector('[role="alertdialog"]', { state: 'visible', timeout: 5000 });
  967. }
  968. /**
  969. * 确认关闭订单
  970. */
  971. async confirmClose(): Promise<void> {
  972. // 尝试多种可能的按钮名称
  973. const confirmButton = this.page.locator('[role="alertdialog"]').getByRole('button', {
  974. name: /^(确认关闭|关闭|确定|确认)$/
  975. });
  976. await confirmButton.click();
  977. // 等待确认对话框关闭和网络请求完成
  978. await this.page.waitForSelector('[role="alertdialog"]', { state: 'hidden', timeout: 5000 })
  979. .catch(() => console.debug('关闭确认对话框关闭超时'));
  980. await this.page.waitForLoadState('networkidle', { timeout: 10000 });
  981. await this.page.waitForTimeout(1000);
  982. }
  983. /**
  984. * 关闭订单(完整流程)
  985. * @param orderName 订单名称
  986. * @returns 是否成功关闭
  987. */
  988. async closeOrder(orderName: string): Promise<boolean> {
  989. await this.openCloseDialog(orderName);
  990. await this.confirmClose();
  991. // 等待并检查 Toast 消息
  992. await this.page.waitForTimeout(1000);
  993. const successToast = this.page.locator('[data-sonner-toast][data-type="success"]');
  994. const hasSuccess = await successToast.count() > 0;
  995. return hasSuccess;
  996. }
  997. /**
  998. * 获取订单的当前状态(从列表页面)
  999. * @param orderName 订单名称
  1000. * @returns 订单状态值或 null
  1001. */
  1002. async getOrderStatus(orderName: string): Promise<OrderStatus | null> {
  1003. const orderRow = this.orderTable.locator('tbody tr').filter({ hasText: orderName });
  1004. // 等待行可见
  1005. await orderRow.waitFor({ state: 'visible', timeout: 3000 }).catch(() => {
  1006. console.debug(`订单 "${orderName}" 行不可见`);
  1007. });
  1008. const rowCount = await orderRow.count();
  1009. if (rowCount === 0) {
  1010. console.debug(`订单 "${orderName}" 不存在`);
  1011. return null;
  1012. }
  1013. // 尝试多种策略定位状态列
  1014. // 策略1: 查找包含状态文本的单元格(但排除订单名称列)
  1015. const allCells = orderRow.locator('td');
  1016. const cellCount = await allCells.count();
  1017. for (let i = 1; i < cellCount; i++) { // 跳过第一列(通常是订单名称)
  1018. const cell = allCells.nth(i);
  1019. const cellText = await cell.textContent();
  1020. if (cellText) {
  1021. // 检查是否包含完整的状态标签(避免部分匹配)
  1022. for (const [statusValue, statusLabel] of Object.entries(ORDER_STATUS_LABELS)) {
  1023. // 使用更严格的匹配:必须是状态标签本身或包含完整标签
  1024. const trimmedText = cellText.trim();
  1025. if (trimmedText === statusLabel || trimmedText.includes(`${statusLabel}`)) {
  1026. // 验证不是订单名称列(额外检查)
  1027. const firstCellText = await allCells.nth(0).textContent();
  1028. if (firstCellText && !firstCellText.includes(orderName.substring(0, 3))) {
  1029. // 第一列不包含订单名称开头,说明列结构可能不同
  1030. return statusValue as OrderStatus;
  1031. }
  1032. // 跳过第一列后找到的状态标签才返回
  1033. return statusValue as OrderStatus;
  1034. }
  1035. }
  1036. }
  1037. }
  1038. // 策略2: 如果上述方法失败,尝试查找状态徽章/标签元素
  1039. // 查找具有状态样式特征的元素
  1040. const statusBadge = orderRow.locator('[class*="status"], [class*="badge"], span').filter({
  1041. hasText: Object.values(ORDER_STATUS_LABELS)
  1042. });
  1043. if (await statusBadge.count() > 0) {
  1044. const badgeText = await statusBadge.first().textContent();
  1045. if (badgeText) {
  1046. for (const [statusValue, statusLabel] of Object.entries(ORDER_STATUS_LABELS)) {
  1047. if (badgeText.includes(statusLabel)) {
  1048. return statusValue as OrderStatus;
  1049. }
  1050. }
  1051. }
  1052. }
  1053. console.debug(`无法从订单 "${orderName}" 中解析状态`);
  1054. return null;
  1055. }
  1056. /**
  1057. * 验证订单状态
  1058. * @param orderName 订单名称
  1059. * @param expectedStatus 期望的状态
  1060. */
  1061. async expectOrderStatus(orderName: string, expectedStatus: OrderStatus): Promise<void> {
  1062. const actualStatus = await this.getOrderStatus(orderName);
  1063. if (actualStatus === null) {
  1064. throw new Error(`订单 "${orderName}" 未找到或状态列无法识别`);
  1065. }
  1066. if (actualStatus !== expectedStatus) {
  1067. throw new Error(
  1068. `订单 "${orderName}" 状态不匹配: 期望 "${ORDER_STATUS_LABELS[expectedStatus]}", 实际 "${ORDER_STATUS_LABELS[actualStatus]}"`
  1069. );
  1070. }
  1071. }
  1072. /**
  1073. * 检查激活按钮是否可用
  1074. *
  1075. * **注意**: 此方法会打开和关闭菜单,属于有副作用的操作
  1076. *
  1077. * @param orderName 订单名称
  1078. * @returns 按钮是否可用
  1079. */
  1080. async checkActivateButtonEnabled(orderName: string): Promise<boolean> {
  1081. // 找到订单行并打开菜单
  1082. const orderRow = this.orderTable.locator('tbody tr').filter({ hasText: orderName });
  1083. // 检查订单是否存在
  1084. const orderCount = await orderRow.count();
  1085. if (orderCount === 0) {
  1086. console.debug(`订单 "${orderName}" 不存在`);
  1087. return false;
  1088. }
  1089. const menuButton = orderRow.getByRole('button', { name: '打开菜单' });
  1090. try {
  1091. await menuButton.click();
  1092. } catch (error) {
  1093. console.debug(`无法打开订单 "${orderName}" 的菜单:`, error);
  1094. return false;
  1095. }
  1096. // 检查激活菜单项是否可点击
  1097. const activateOption = this.page.getByRole('menuitem', { name: /激活|激活订单/ });
  1098. const isVisible = await activateOption.isVisible().catch(() => false);
  1099. let isEnabled = false;
  1100. if (isVisible) {
  1101. // 检查是否有禁用属性或样式
  1102. const isDisabled = await activateOption.isDisabled().catch(() => false);
  1103. isEnabled = !isDisabled;
  1104. }
  1105. // 关闭菜单以便后续操作
  1106. await this.page.keyboard.press('Escape');
  1107. await this.page.waitForTimeout(300);
  1108. return isEnabled;
  1109. }
  1110. /**
  1111. * 检查关闭按钮是否可用
  1112. *
  1113. * **注意**: 此方法会打开和关闭菜单,属于有副作用的操作
  1114. *
  1115. * @param orderName 订单名称
  1116. * @returns 按钮是否可用
  1117. */
  1118. async checkCloseButtonEnabled(orderName: string): Promise<boolean> {
  1119. // 找到订单行并打开菜单
  1120. const orderRow = this.orderTable.locator('tbody tr').filter({ hasText: orderName });
  1121. // 检查订单是否存在
  1122. const orderCount = await orderRow.count();
  1123. if (orderCount === 0) {
  1124. console.debug(`订单 "${orderName}" 不存在`);
  1125. return false;
  1126. }
  1127. const menuButton = orderRow.getByRole('button', { name: '打开菜单' });
  1128. try {
  1129. await menuButton.click();
  1130. } catch (error) {
  1131. console.debug(`无法打开订单 "${orderName}" 的菜单:`, error);
  1132. return false;
  1133. }
  1134. // 检查关闭菜单项是否可点击
  1135. const closeOption = this.page.getByRole('menuitem', { name: /关闭|关闭订单|完成/ });
  1136. const isVisible = await closeOption.isVisible().catch(() => false);
  1137. let isEnabled = false;
  1138. if (isVisible) {
  1139. // 检查是否有禁用属性或样式
  1140. const isDisabled = await closeOption.isDisabled().catch(() => false);
  1141. isEnabled = !isDisabled;
  1142. }
  1143. // 关闭菜单以便后续操作
  1144. await this.page.keyboard.press('Escape');
  1145. await this.page.waitForTimeout(300);
  1146. return isEnabled;
  1147. }
  1148. }