order-delete.spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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('删除订单测试', () => {
  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. const message = await successToast.textContent();
  164. expect(message).toBeDefined();
  165. expect(message?.length).toBeGreaterThan(0);
  166. // 验证消息包含删除相关的关键词
  167. expect(message).toMatch(/删除|成功|已删除/);
  168. // 验证订单被删除
  169. const exists = await orderManagementPage.orderExists(testOrderName);
  170. expect(exists).toBe(false);
  171. });
  172. test('删除确认对话框应该正确显示', async ({ orderManagementPage, page }) => {
  173. await orderManagementPage.openDeleteDialog(testOrderName);
  174. // 验证对话框可见
  175. const dialog = page.locator('[role="alertdialog"]');
  176. await expect(dialog).toBeVisible();
  177. // 验证确认按钮存在(支持多种可能的按钮名称)
  178. const confirmButton = page.locator('[role="alertdialog"]').getByRole('button', {
  179. name: /^(确认删除|删除|确定|确认)$/
  180. });
  181. await expect(confirmButton).toBeVisible();
  182. // 验证取消按钮存在
  183. const cancelButton = page.locator('[role="alertdialog"]').getByRole('button', { name: '取消' });
  184. await expect(cancelButton).toBeVisible();
  185. // 取消删除以清理
  186. await orderManagementPage.cancelDelete();
  187. });
  188. });
  189. test.describe('取消删除', () => {
  190. test('应该能在确认对话框中取消删除', async ({ orderManagementPage }) => {
  191. // 打开删除确认对话框
  192. await orderManagementPage.openDeleteDialog(testOrderName);
  193. // 取消删除
  194. await orderManagementPage.cancelDelete();
  195. // 验证订单仍然存在
  196. const exists = await orderManagementPage.orderExists(testOrderName);
  197. expect(exists).toBe(true);
  198. });
  199. test('取消删除后订单应该保持不变', async ({ orderManagementPage, page }) => {
  200. // 获取删除前的订单行(用于后续验证)
  201. const orderRowBefore = page.locator('table tbody tr').filter({ hasText: testOrderName });
  202. await expect(orderRowBefore).toBeVisible();
  203. // 打开删除确认对话框
  204. await orderManagementPage.openDeleteDialog(testOrderName);
  205. // 取消删除
  206. await orderManagementPage.cancelDelete();
  207. // 等待对话框关闭
  208. await page.waitForTimeout(500);
  209. // 验证订单仍然在列表中
  210. const orderRowAfter = page.locator('table tbody tr').filter({ hasText: testOrderName });
  211. await expect(orderRowAfter).toBeVisible();
  212. });
  213. test('应该能通过关闭对话框取消删除', async ({ orderManagementPage, page }) => {
  214. // 打开删除确认对话框
  215. await orderManagementPage.openDeleteDialog(testOrderName);
  216. // 按 Escape 键关闭对话框
  217. await page.keyboard.press('Escape');
  218. // 等待对话框关闭
  219. const dialog = page.locator('[role="alertdialog"]');
  220. await dialog.waitFor({ state: 'hidden', timeout: 5000 }).catch(() => {
  221. console.debug('对话框关闭超时,可能已经关闭');
  222. });
  223. // 验证订单仍然存在
  224. const exists = await orderManagementPage.orderExists(testOrderName);
  225. expect(exists).toBe(true);
  226. });
  227. });
  228. test.describe('删除有关联人员的订单', () => {
  229. let orderWithPersonName: string;
  230. test.beforeEach(async ({ orderManagementPage }) => {
  231. // 创建专用于此测试套件的订单(确保测试隔离)
  232. const timestamp = Date.now();
  233. orderWithPersonName = `删除测试_人员_${timestamp}`;
  234. const created = await createTestOrder(orderManagementPage.page, orderWithPersonName);
  235. if (!created) {
  236. test.skip(true, '无法创建测试订单用于人员关联测试');
  237. }
  238. // 验证订单出现在列表中
  239. await expect(async () => {
  240. const exists = await orderManagementPage.orderExists(orderWithPersonName);
  241. expect(exists).toBe(true);
  242. }).toPass({ timeout: 5000 });
  243. // 打开人员管理对话框并添加人员
  244. await orderManagementPage.openPersonManagementDialog(orderWithPersonName);
  245. // 检查是否已经有人员(如果有则使用现有人员,否则尝试添加)
  246. const personTable = orderManagementPage.page.locator('[role="dialog"]').locator('table tbody tr');
  247. const personCount = await personTable.count();
  248. if (personCount === 0) {
  249. // 尝试添加人员
  250. try {
  251. await orderManagementPage.addPersonToOrder({
  252. disabledPersonName: '测试', // 使用名称而不是硬编码 ID
  253. });
  254. console.debug('✓ 已添加人员到订单');
  255. } catch (error) {
  256. console.debug('添加人员失败,可能没有可用数据:', error);
  257. }
  258. } else {
  259. console.debug(`✓ 订单已有 ${personCount} 个关联人员`);
  260. }
  261. // 关闭人员管理对话框
  262. await orderManagementPage.page.keyboard.press('Escape');
  263. await orderManagementPage.page.waitForTimeout(500);
  264. });
  265. test('应该能删除有人员的订单(级联删除)', async ({ orderManagementPage }) => {
  266. // 尝试删除有关联人员的订单
  267. await orderManagementPage.deleteOrder(orderWithPersonName);
  268. // 验证结果 - 根据实际业务逻辑,可能成功或失败
  269. const successToast = orderManagementPage.page.locator('[data-sonner-toast][data-type="success"]');
  270. const errorToast = orderManagementPage.page.locator('[data-sonner-toast][data-type="error"]');
  271. const hasSuccess = await successToast.count() > 0;
  272. const hasError = await errorToast.count() > 0;
  273. if (hasSuccess) {
  274. // 级联删除成功
  275. console.debug('✓ 订单及其关联人员已被删除');
  276. const exists = await orderManagementPage.orderExists(orderWithPersonName);
  277. expect(exists).toBe(false);
  278. } else if (hasError) {
  279. // 禁止删除,显示错误消息
  280. const errorMessage = await errorToast.textContent();
  281. console.debug('删除失败消息:', errorMessage);
  282. expect(errorMessage).toBeDefined();
  283. // 订单应该仍然存在
  284. const exists = await orderManagementPage.orderExists(orderWithPersonName);
  285. expect(exists).toBe(true);
  286. } else {
  287. // 如果没有明确的成功或失败消息,检查订单状态
  288. const exists = await orderManagementPage.orderExists(orderWithPersonName);
  289. console.debug(`删除后订单状态: ${exists ? '仍存在' : '已删除'}`);
  290. }
  291. });
  292. test('删除失败应该显示错误消息', async ({ orderManagementPage }) => {
  293. // 打开删除对话框
  294. await orderManagementPage.openDeleteDialog(orderWithPersonName);
  295. // 确认删除
  296. await orderManagementPage.confirmDelete();
  297. // 等待响应
  298. await orderManagementPage.page.waitForTimeout(2000);
  299. // 检查结果
  300. const successToast = orderManagementPage.page.locator('[data-sonner-toast][data-type="success"]');
  301. const errorToast = orderManagementPage.page.locator('[data-sonner-toast][data-type="error"]');
  302. const hasSuccess = await successToast.count() > 0;
  303. const hasError = await errorToast.count() > 0;
  304. // 根据业务逻辑验证结果
  305. if (hasError) {
  306. const errorMessage = await errorToast.textContent();
  307. console.debug('预期错误消息:', errorMessage);
  308. expect(errorMessage).toBeDefined();
  309. expect(errorMessage?.length).toBeGreaterThan(0);
  310. } else if (!hasSuccess) {
  311. // 如果没有明确的 Toast 消息,检查其他形式的反馈
  312. console.debug('没有检测到 Toast 消息,检查其他反馈形式');
  313. }
  314. // 清理:如果删除成功,则不需要操作;如果删除失败,订单仍在
  315. const exists = await orderManagementPage.orderExists(orderWithPersonName);
  316. if (exists) {
  317. console.debug('订单仍然存在,业务规则可能禁止删除有人员的订单');
  318. }
  319. });
  320. });
  321. test.describe('删除后列表更新验证', () => {
  322. test('删除后列表应该不再显示该订单', async ({ orderManagementPage }) => {
  323. // 验证订单在删除前存在
  324. const existsBefore = await orderManagementPage.orderExists(testOrderName);
  325. expect(existsBefore).toBe(true);
  326. // 执行删除
  327. await orderManagementPage.deleteOrder(testOrderName);
  328. // 验证订单在删除后不存在
  329. await expect(async () => {
  330. const existsAfter = await orderManagementPage.orderExists(testOrderName);
  331. expect(existsAfter).toBe(false);
  332. }).toPass({ timeout: 5000 });
  333. });
  334. test('删除后列表应该正确更新', async ({ orderManagementPage, page }) => {
  335. // 获取删除前的行数
  336. const tableBody = page.locator('table tbody');
  337. const rowsBefore = await tableBody.locator('tr').count();
  338. // 执行删除
  339. await orderManagementPage.deleteOrder(testOrderName);
  340. // 等待列表更新
  341. await page.waitForTimeout(1000);
  342. // 验证行数减少
  343. const rowsAfter = await tableBody.locator('tr').count();
  344. expect(rowsAfter).toBe(rowsBefore - 1);
  345. });
  346. });
  347. test.describe('Toast 消息验证', () => {
  348. test('成功删除应该显示正确的成功消息', async ({ orderManagementPage }) => {
  349. await orderManagementPage.deleteOrder(testOrderName);
  350. const successToast = orderManagementPage.page.locator('[data-sonner-toast][data-type="success"]');
  351. await expect(successToast).toBeVisible();
  352. const message = await successToast.textContent();
  353. expect(message).toBeDefined();
  354. expect(message?.length).toBeGreaterThan(0);
  355. // 验证消息包含删除相关的关键词
  356. expect(message).toMatch(/删除|成功|已删除/);
  357. console.debug('删除成功消息:', message);
  358. });
  359. test('Toast 消息应该自动消失', async ({ orderManagementPage }) => {
  360. await orderManagementPage.deleteOrder(testOrderName);
  361. const successToast = orderManagementPage.page.locator('[data-sonner-toast][data-type="success"]');
  362. // 等待 Toast 消息消失 - 使用更合理的超时时间
  363. await successToast.waitFor({ state: 'hidden', timeout: 6000 }).catch(() => {
  364. console.debug('Toast 消息可能在 6 秒内未消失');
  365. });
  366. // 验证消息不再可见
  367. const isVisible = await successToast.isVisible().catch(() => false);
  368. expect(isVisible).toBe(false);
  369. });
  370. });
  371. });