2
0

order-delete.spec.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /**
  2. * 删除订单 E2E 测试
  3. *
  4. * 测试范围:
  5. * - 删除草稿状态订单
  6. * - 删除有关联人员的订单
  7. * - 取消删除操作
  8. * - 删除后列表更新验证
  9. * - Toast 消息验证
  10. *
  11. * @packageDocumentation
  12. */
  13. import { test, expect, Page } from '../../utils/test-setup';
  14. /**
  15. * 辅助函数:获取订单列表中第一个订单的名称
  16. *
  17. * @param page - Playwright Page 对象
  18. * @returns 第一个订单的名称,如果没有则返回 null
  19. */
  20. async function getFirstOrderName(page: Page): Promise<string | null> {
  21. const table = page.locator('table tbody tr');
  22. // 等待表格数据加载完成(跳过"加载中"等占位符文本)
  23. await page.waitForTimeout(1000);
  24. const count = await table.count();
  25. if (count === 0) {
  26. return null;
  27. }
  28. // 查找第一个有效的订单行(排除"加载中"等占位符)
  29. for (let i = 0; i < count; i++) {
  30. const row = table.nth(i);
  31. const nameCell = row.locator('td').first();
  32. const name = await nameCell.textContent();
  33. const trimmedName = name?.trim() || '';
  34. // 跳过占位符文本
  35. if (trimmedName && trimmedName !== '加载中...' && trimmedName !== '暂无数据' && !trimmedName.includes('加载')) {
  36. return trimmedName;
  37. }
  38. }
  39. return null;
  40. }
  41. /**
  42. * 辅助函数:在创建订单对话框中选择残疾人
  43. *
  44. * @param page - Playwright Page 对象
  45. * @returns 是否成功选择了残疾人
  46. */
  47. async function selectDisabledPersonForOrder(page: Page): Promise<boolean> {
  48. const selectPersonButton = page.getByRole('button', { name: '选择残疾人' });
  49. await selectPersonButton.click();
  50. await page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: 5000 });
  51. let hasData = false;
  52. try {
  53. const firstCheckbox = page.locator('table tbody tr').first().locator('input[type="checkbox"]').first();
  54. await firstCheckbox.waitFor({ state: 'visible', timeout: 3000 });
  55. await firstCheckbox.check();
  56. console.debug('✓ 已选择第一个残疾人');
  57. hasData = true;
  58. } catch (error) {
  59. console.debug('没有可用的残疾人数据');
  60. hasData = false;
  61. }
  62. if (hasData) {
  63. const confirmButton = page.getByRole('button', { name: /^(确定|确认|选择)$/ });
  64. await confirmButton.click().catch(() => {
  65. console.debug('没有找到确认按钮,尝试关闭对话框');
  66. page.keyboard.press('Escape');
  67. });
  68. } else {
  69. await page.keyboard.press('Escape').catch(() => {
  70. console.debug('无法关闭对话框,可能已经自动关闭');
  71. });
  72. }
  73. await page.waitForTimeout(500);
  74. return hasData;
  75. }
  76. /**
  77. * 辅助函数:创建测试订单
  78. *
  79. * @param page - Playwright Page 对象
  80. * @param orderName - 订单名称
  81. * @returns 是否成功创建
  82. */
  83. async function createTestOrder(page: Page, orderName: string): Promise<boolean> {
  84. // 打开创建对话框
  85. const addOrderButton = page.getByTestId('create-order-button');
  86. await addOrderButton.click();
  87. await page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: 5000 });
  88. // 填写必填字段
  89. await page.getByLabel(/订单名称|名称/).fill(orderName);
  90. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  91. // 选择残疾人(必填)
  92. const hasDisabledPerson = await selectDisabledPersonForOrder(page);
  93. if (!hasDisabledPerson) {
  94. await page.keyboard.press('Escape');
  95. return false;
  96. }
  97. // 提交表单
  98. const submitButton = page.getByRole('button', { name: /^(创建|更新|保存)$/ });
  99. await submitButton.click();
  100. // 等待网络请求完成
  101. try {
  102. await page.waitForLoadState('networkidle', { timeout: 5000 });
  103. } catch {
  104. console.debug('networkidle 超时,继续检查 Toast 消息');
  105. }
  106. await page.waitForTimeout(2000);
  107. // 检查成功消息
  108. const successToast = page.locator('[data-sonner-toast][data-type="success"]');
  109. const hasSuccess = await successToast.count() > 0;
  110. // 等待对话框关闭
  111. const dialog = page.locator('[role="dialog"]');
  112. await dialog.waitFor({ state: 'hidden', timeout: 5000 }).catch(() => {
  113. console.debug('对话框关闭超时,可能已经关闭');
  114. });
  115. return hasSuccess;
  116. }
  117. test.describe.serial('删除订单测试', () => {
  118. let testOrderName: string;
  119. test.beforeEach(async ({ adminLoginPage, orderManagementPage, testUsers }) => {
  120. // 以管理员身份登录后台
  121. await adminLoginPage.goto();
  122. await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
  123. await adminLoginPage.expectLoginSuccess();
  124. await orderManagementPage.goto();
  125. // 尝试使用现有订单
  126. const existingOrder = await getFirstOrderName(orderManagementPage.page);
  127. if (existingOrder) {
  128. testOrderName = existingOrder;
  129. console.debug(`✓ 使用现有订单: ${testOrderName}`);
  130. } else {
  131. // 如果没有现有订单,创建一个
  132. const timestamp = Date.now();
  133. testOrderName = `删除测试_${timestamp}`;
  134. const created = await createTestOrder(orderManagementPage.page, testOrderName);
  135. if (!created) {
  136. // 没有残疾人数据时,跳过测试
  137. test.skip(true, '没有残疾人数据,无法创建测试订单');
  138. }
  139. // 验证订单出现在列表中
  140. await expect(async () => {
  141. const exists = await orderManagementPage.orderExists(testOrderName);
  142. expect(exists).toBe(true);
  143. }).toPass({ timeout: 5000 });
  144. }
  145. });
  146. test.describe('删除草稿状态订单', () => {
  147. test('应该成功删除草稿订单', async ({ orderManagementPage }) => {
  148. // 打开删除确认对话框
  149. await orderManagementPage.openDeleteDialog(testOrderName);
  150. // 确认删除
  151. await orderManagementPage.confirmDelete();
  152. // 验证订单不再存在
  153. await expect(async () => {
  154. const exists = await orderManagementPage.orderExists(testOrderName);
  155. expect(exists).toBe(false);
  156. }).toPass({ timeout: 5000 });
  157. });
  158. test('应该在删除后显示成功提示', async ({ orderManagementPage }) => {
  159. await orderManagementPage.deleteOrder(testOrderName);
  160. // 验证 Toast 成功消息
  161. const successToast = orderManagementPage.page.locator('[data-sonner-toast][data-type="success"]');
  162. await expect(successToast).toBeVisible();
  163. expect(await successToast.textContent()).toContain('成功');
  164. // 验证订单被删除
  165. const exists = await orderManagementPage.orderExists(testOrderName);
  166. expect(exists).toBe(false);
  167. });
  168. test('删除确认对话框应该正确显示', async ({ orderManagementPage, page }) => {
  169. await orderManagementPage.openDeleteDialog(testOrderName);
  170. // 验证对话框可见
  171. const dialog = page.locator('[role="alertdialog"]');
  172. await expect(dialog).toBeVisible();
  173. // 验证确认按钮存在(支持多种可能的按钮名称)
  174. const confirmButton = page.locator('[role="alertdialog"]').getByRole('button', {
  175. name: /^(确认删除|删除|确定|确认)$/
  176. });
  177. await expect(confirmButton).toBeVisible();
  178. // 验证取消按钮存在
  179. const cancelButton = page.locator('[role="alertdialog"]').getByRole('button', { name: '取消' });
  180. await expect(cancelButton).toBeVisible();
  181. // 取消删除以清理
  182. await orderManagementPage.cancelDelete();
  183. });
  184. });
  185. test.describe('取消删除', () => {
  186. test('应该能在确认对话框中取消删除', async ({ orderManagementPage }) => {
  187. // 打开删除确认对话框
  188. await orderManagementPage.openDeleteDialog(testOrderName);
  189. // 取消删除
  190. await orderManagementPage.cancelDelete();
  191. // 验证订单仍然存在
  192. const exists = await orderManagementPage.orderExists(testOrderName);
  193. expect(exists).toBe(true);
  194. });
  195. test('取消删除后订单应该保持不变', async ({ orderManagementPage, page }) => {
  196. // 获取删除前的订单行(用于后续验证)
  197. const orderRowBefore = page.locator('table tbody tr').filter({ hasText: testOrderName });
  198. await expect(orderRowBefore).toBeVisible();
  199. // 打开删除确认对话框
  200. await orderManagementPage.openDeleteDialog(testOrderName);
  201. // 取消删除
  202. await orderManagementPage.cancelDelete();
  203. // 等待对话框关闭
  204. await page.waitForTimeout(500);
  205. // 验证订单仍然在列表中
  206. const orderRowAfter = page.locator('table tbody tr').filter({ hasText: testOrderName });
  207. await expect(orderRowAfter).toBeVisible();
  208. });
  209. test('应该能通过关闭对话框取消删除', async ({ orderManagementPage, page }) => {
  210. // 打开删除确认对话框
  211. await orderManagementPage.openDeleteDialog(testOrderName);
  212. // 按 Escape 键关闭对话框
  213. await page.keyboard.press('Escape');
  214. // 等待对话框关闭
  215. const dialog = page.locator('[role="alertdialog"]');
  216. await dialog.waitFor({ state: 'hidden', timeout: 5000 }).catch(() => {
  217. console.debug('对话框关闭超时,可能已经关闭');
  218. });
  219. // 验证订单仍然存在
  220. const exists = await orderManagementPage.orderExists(testOrderName);
  221. expect(exists).toBe(true);
  222. });
  223. });
  224. test.describe('删除有关联人员的订单', () => {
  225. let orderWithPersonName: string;
  226. test.beforeEach(async ({ orderManagementPage }) => {
  227. // 尝试找到一个有人员的订单,或者使用现有订单并添加人员
  228. // 首先尝试使用不同的现有订单
  229. const table = orderManagementPage.page.locator('table tbody tr');
  230. const count = await table.count();
  231. if (count > 1) {
  232. // 如果有多个订单,使用第二个(避免与主测试订单冲突)
  233. const secondRow = table.nth(1);
  234. const nameCell = secondRow.locator('td').first();
  235. const name = await nameCell.textContent();
  236. orderWithPersonName = name?.trim() || '';
  237. console.debug(`✓ 使用第二个现有订单: ${orderWithPersonName}`);
  238. } else {
  239. // 如果只有一个订单,尝试创建新的
  240. const timestamp = Date.now();
  241. orderWithPersonName = `删除测试_人员_${timestamp}`;
  242. const created = await createTestOrder(orderManagementPage.page, orderWithPersonName);
  243. if (!created) {
  244. test.skip(true, '无法创建测试订单用于人员关联测试');
  245. }
  246. // 验证订单出现在列表中
  247. await expect(async () => {
  248. const exists = await orderManagementPage.orderExists(orderWithPersonName);
  249. expect(exists).toBe(true);
  250. }).toPass({ timeout: 5000 });
  251. }
  252. // 尝试打开人员管理对话框并添加人员
  253. // 注意:这个测试可能因为没有残疾人数据而跳过
  254. await orderManagementPage.openPersonManagementDialog(orderWithPersonName);
  255. // 检查是否已经有人员(如果有则使用现有人员,否则尝试添加)
  256. const personTable = orderManagementPage.page.locator('[role="dialog"]').locator('table tbody tr');
  257. const personCount = await personTable.count();
  258. if (personCount === 0) {
  259. // 尝试添加人员
  260. try {
  261. await orderManagementPage.addPersonToOrder({
  262. disabledPersonId: 1,
  263. });
  264. console.debug('✓ 已添加人员到订单');
  265. } catch (error) {
  266. console.debug('添加人员失败,可能没有可用数据:', error);
  267. }
  268. } else {
  269. console.debug(`✓ 订单已有 ${personCount} 个关联人员`);
  270. }
  271. // 关闭人员管理对话框
  272. await orderManagementPage.page.keyboard.press('Escape');
  273. await orderManagementPage.page.waitForTimeout(500);
  274. });
  275. test('应该能删除有人员的订单(级联删除)', async ({ orderManagementPage }) => {
  276. // 尝试删除有关联人员的订单
  277. await orderManagementPage.deleteOrder(orderWithPersonName);
  278. // 验证结果 - 根据实际业务逻辑,可能成功或失败
  279. const successToast = orderManagementPage.page.locator('[data-sonner-toast][data-type="success"]');
  280. const errorToast = orderManagementPage.page.locator('[data-sonner-toast][data-type="error"]');
  281. const hasSuccess = await successToast.count() > 0;
  282. const hasError = await errorToast.count() > 0;
  283. if (hasSuccess) {
  284. // 级联删除成功
  285. console.debug('✓ 订单及其关联人员已被删除');
  286. const exists = await orderManagementPage.orderExists(orderWithPersonName);
  287. expect(exists).toBe(false);
  288. } else if (hasError) {
  289. // 禁止删除,显示错误消息
  290. const errorMessage = await errorToast.textContent();
  291. console.debug('删除失败消息:', errorMessage);
  292. expect(errorMessage).toBeDefined();
  293. // 订单应该仍然存在
  294. const exists = await orderManagementPage.orderExists(orderWithPersonName);
  295. expect(exists).toBe(true);
  296. } else {
  297. // 如果没有明确的成功或失败消息,检查订单状态
  298. const exists = await orderManagementPage.orderExists(orderWithPersonName);
  299. console.debug(`删除后订单状态: ${exists ? '仍存在' : '已删除'}`);
  300. }
  301. });
  302. test('删除失败应该显示错误消息', async ({ orderManagementPage }) => {
  303. // 打开删除对话框
  304. await orderManagementPage.openDeleteDialog(orderWithPersonName);
  305. // 确认删除
  306. await orderManagementPage.confirmDelete();
  307. // 等待响应
  308. await orderManagementPage.page.waitForTimeout(2000);
  309. // 检查结果
  310. const successToast = orderManagementPage.page.locator('[data-sonner-toast][data-type="success"]');
  311. const errorToast = orderManagementPage.page.locator('[data-sonner-toast][data-type="error"]');
  312. const hasSuccess = await successToast.count() > 0;
  313. const hasError = await errorToast.count() > 0;
  314. // 根据业务逻辑验证结果
  315. if (hasError) {
  316. const errorMessage = await errorToast.textContent();
  317. console.debug('预期错误消息:', errorMessage);
  318. expect(errorMessage).toBeDefined();
  319. expect(errorMessage?.length).toBeGreaterThan(0);
  320. } else if (!hasSuccess) {
  321. // 如果没有明确的 Toast 消息,检查其他形式的反馈
  322. console.debug('没有检测到 Toast 消息,检查其他反馈形式');
  323. }
  324. // 清理:如果删除成功,则不需要操作;如果删除失败,订单仍在
  325. const exists = await orderManagementPage.orderExists(orderWithPersonName);
  326. if (exists) {
  327. console.debug('订单仍然存在,业务规则可能禁止删除有人员的订单');
  328. }
  329. });
  330. });
  331. test.describe('删除后列表更新验证', () => {
  332. test('删除后列表应该不再显示该订单', async ({ orderManagementPage }) => {
  333. // 验证订单在删除前存在
  334. const existsBefore = await orderManagementPage.orderExists(testOrderName);
  335. expect(existsBefore).toBe(true);
  336. // 执行删除
  337. await orderManagementPage.deleteOrder(testOrderName);
  338. // 验证订单在删除后不存在
  339. await expect(async () => {
  340. const existsAfter = await orderManagementPage.orderExists(testOrderName);
  341. expect(existsAfter).toBe(false);
  342. }).toPass({ timeout: 5000 });
  343. });
  344. test('删除后列表应该正确更新', async ({ orderManagementPage, page }) => {
  345. // 获取删除前的行数
  346. const tableBody = page.locator('table tbody');
  347. const rowsBefore = await tableBody.locator('tr').count();
  348. // 执行删除
  349. await orderManagementPage.deleteOrder(testOrderName);
  350. // 等待列表更新
  351. await page.waitForTimeout(1000);
  352. // 验证行数减少
  353. const rowsAfter = await tableBody.locator('tr').count();
  354. expect(rowsAfter).toBe(rowsBefore - 1);
  355. });
  356. });
  357. test.describe('Toast 消息验证', () => {
  358. test('成功删除应该显示正确的成功消息', async ({ orderManagementPage }) => {
  359. await orderManagementPage.deleteOrder(testOrderName);
  360. const successToast = orderManagementPage.page.locator('[data-sonner-toast][data-type="success"]');
  361. await expect(successToast).toBeVisible();
  362. const message = await successToast.textContent();
  363. expect(message).toBeDefined();
  364. expect(message?.length).toBeGreaterThan(0);
  365. console.debug('删除成功消息:', message);
  366. });
  367. test('Toast 消息应该自动消失', async ({ orderManagementPage }) => {
  368. await orderManagementPage.deleteOrder(testOrderName);
  369. const successToast = orderManagementPage.page.locator('[data-sonner-toast][data-type="success"]');
  370. // 等待 Toast 消息消失
  371. await successToast.waitFor({ state: 'hidden', timeout: 10000 }).catch(() => {
  372. console.debug('Toast 消息可能在 10 秒内未消失');
  373. });
  374. // 验证消息不再可见
  375. const isVisible = await successToast.isVisible().catch(() => false);
  376. expect(isVisible).toBe(false);
  377. });
  378. });
  379. });