order-edit.spec.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /**
  2. * 编辑订单 E2E 测试
  3. *
  4. * 测试范围:
  5. * - 编辑订单基本信息(名称、预计开始日期)
  6. * - 编辑订单关联信息(平台、公司、渠道)
  7. * - 编辑后列表更新验证
  8. * - 对话框交互验证
  9. * - 错误场景测试
  10. *
  11. * @packageDocumentation
  12. */
  13. import { TIMEOUTS } from '../../utils/timeouts';
  14. import { test, expect, Page } from '../../utils/test-setup';
  15. import { selectRadixOption } from '@d8d/e2e-test-utils';
  16. /**
  17. * 辅助函数:在创建订单对话框中选择残疾人
  18. *
  19. * @param page - Playwright Page 对象
  20. * @returns 是否成功选择了残疾人
  21. */
  22. async function selectDisabledPersonForOrder(page: Page): Promise<boolean> {
  23. const selectPersonButton = page.getByRole('button', { name: '选择残疾人' });
  24. await selectPersonButton.click();
  25. await page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: TIMEOUTS.DIALOG });
  26. let hasData = false;
  27. try {
  28. const firstCheckbox = page.locator('table tbody tr').first().locator('input[type="checkbox"]').first();
  29. await firstCheckbox.waitFor({ state: 'visible', timeout: TIMEOUTS.ELEMENT_VISIBLE_SHORT });
  30. await firstCheckbox.check();
  31. console.debug('✓ 已选择第一个残疾人');
  32. hasData = true;
  33. } catch (error) {
  34. console.debug('没有可用的残疾人数据');
  35. hasData = false;
  36. }
  37. if (hasData) {
  38. const confirmButton = page.getByRole('button', { name: /^(确定|确认|选择)$/ });
  39. await confirmButton.click().catch(() => {
  40. console.debug('没有找到确认按钮,尝试关闭对话框');
  41. page.keyboard.press('Escape');
  42. });
  43. } else {
  44. await page.keyboard.press('Escape').catch(() => {
  45. console.debug('无法关闭对话框,可能已经自动关闭');
  46. });
  47. }
  48. await page.waitForTimeout(TIMEOUTS.MEDIUM);
  49. return hasData;
  50. }
  51. /**
  52. * 辅助函数:尝试更改 Radix Select 的值
  53. *
  54. * 用于测试平台、公司、渠道等下拉框的更换功能
  55. *
  56. * @param page - Playwright Page 对象
  57. * @param dataTestId - 下拉框的 data-testid 属性值
  58. * @param label - 下拉框的标签(用于 selectRadixOption)
  59. * @returns 是否成功更改了值
  60. */
  61. async function tryChangeSelectValue(
  62. page: Page,
  63. dataTestId: string,
  64. label: string
  65. ): Promise<{ changed: boolean; newValue?: string }> {
  66. try {
  67. // 点击下拉框
  68. const trigger = page.locator(`[data-testid="${dataTestId}"]`);
  69. const count = await trigger.count();
  70. if (count === 0) {
  71. console.debug(`${label}选择器未找到`);
  72. return { changed: false };
  73. }
  74. // 获取当前值(通过触发器的文本内容)
  75. const currentValue = await trigger.textContent() || '';
  76. // 打开下拉框
  77. await trigger.click({ force: true });
  78. // 等待选项出现
  79. await page.waitForTimeout(TIMEOUTS.SHORT);
  80. // 获取所有可用选项
  81. const options = page.getByRole('option');
  82. const optionCount = await options.count();
  83. if (optionCount <= 1) {
  84. console.debug(`只有一个${label}选项,无法测试更换`);
  85. await page.keyboard.press('Escape'); // 关闭下拉框
  86. return { changed: false };
  87. }
  88. // 获取所有选项的文本
  89. const availableValues: string[] = [];
  90. for (let i = 0; i < optionCount; i++) {
  91. const text = await options.nth(i).textContent();
  92. if (text) availableValues.push(text);
  93. }
  94. // 选择与当前值不同的选项
  95. const newValue = availableValues.find(v => v !== currentValue && v.trim() !== '') ?? availableValues[1];
  96. if (newValue && newValue !== currentValue) {
  97. // 使用 selectRadixOption 工具进行选择
  98. await selectRadixOption(page, label, newValue);
  99. console.debug(`✓ 已选择不同的${label}: ${newValue}`);
  100. // 验证选择器确实更新到了新值
  101. const updatedValue = await trigger.textContent();
  102. if (updatedValue?.includes(newValue)) {
  103. return { changed: true, newValue };
  104. }
  105. }
  106. // 关闭下拉框
  107. await page.keyboard.press('Escape');
  108. return { changed: false };
  109. } catch (error) {
  110. console.debug(`${label}选择失败:`, error);
  111. return { changed: false };
  112. }
  113. }
  114. /**
  115. * 辅助函数:创建测试订单
  116. *
  117. * @param page - Playwright Page 对象
  118. * @param orderName - 订单名称
  119. * @returns 是否成功创建
  120. */
  121. async function createTestOrder(page: Page, orderName: string): Promise<boolean> {
  122. // 打开创建对话框
  123. const addOrderButton = page.getByTestId('create-order-button');
  124. await addOrderButton.click();
  125. await page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: TIMEOUTS.DIALOG });
  126. // 填写必填字段
  127. await page.getByLabel(/订单名称|名称/).fill(orderName);
  128. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  129. // 选择残疾人(必填)
  130. const hasDisabledPerson = await selectDisabledPersonForOrder(page);
  131. if (!hasDisabledPerson) {
  132. await page.keyboard.press('Escape');
  133. return false;
  134. }
  135. // 提交表单
  136. const submitButton = page.getByRole('button', { name: /^(创建|更新|保存)$/ });
  137. await submitButton.click();
  138. // 等待网络请求完成
  139. try {
  140. await page.waitForLoadState('networkidle', { timeout: TIMEOUTS.DIALOG });
  141. } catch {
  142. console.debug('networkidle 超时,继续检查 Toast 消息');
  143. }
  144. await page.waitForTimeout(TIMEOUTS.VERY_LONG);
  145. // 检查成功消息
  146. const successToast = page.locator('[data-sonner-toast][data-type="success"]');
  147. const hasSuccess = await successToast.count() > 0;
  148. // 等待对话框关闭
  149. const dialog = page.locator('[role="dialog"]');
  150. await dialog.waitFor({ state: 'hidden', timeout: TIMEOUTS.DIALOG }).catch(() => {
  151. console.debug('对话框关闭超时,可能已经关闭');
  152. });
  153. return hasSuccess;
  154. }
  155. /**
  156. * 辅助函数:获取订单列表中的预计开始日期
  157. *
  158. * @param page - Playwright Page 对象
  159. * @param orderName - 订单名称
  160. * @returns 预计开始日期字符串
  161. */
  162. async function getOrderExpectedStartDate(page: Page, orderName: string): Promise<string | null> {
  163. const orderRow = page.locator('table tbody tr').filter({ hasText: orderName });
  164. // 查找包含日期的单元格(通常是第3或第4列)
  165. const cells = orderRow.locator('td');
  166. const cellCount = await cells.count();
  167. // 遍历单元格查找日期格式的文本
  168. for (let i = 0; i < cellCount; i++) {
  169. const text = await cells.nth(i).textContent();
  170. if (text && /^\d{4}-\d{2}-\d{2}$/.test(text.trim())) {
  171. return text.trim();
  172. }
  173. }
  174. return null;
  175. }
  176. test.describe.serial('编辑订单测试', () => {
  177. let testOrderName: string;
  178. test.beforeEach(async ({ adminLoginPage, orderManagementPage, testUsers }) => {
  179. // 以管理员身份登录后台
  180. await adminLoginPage.goto();
  181. await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
  182. await adminLoginPage.expectLoginSuccess();
  183. await orderManagementPage.goto();
  184. // 创建测试订单
  185. const timestamp = Date.now();
  186. testOrderName = `编辑测试_${timestamp}`;
  187. const created = await createTestOrder(orderManagementPage.page, testOrderName);
  188. if (!created) {
  189. // 没有残疾人数据时,跳过测试
  190. test.skip(true, '没有残疾人数据,无法创建测试订单');
  191. }
  192. // 验证订单出现在列表中
  193. await expect(async () => {
  194. const exists = await orderManagementPage.orderExists(testOrderName);
  195. expect(exists).toBe(true);
  196. }).toPass({ timeout: TIMEOUTS.DIALOG });
  197. });
  198. test.afterEach(async ({ orderManagementPage }) => {
  199. // 清理测试数据
  200. try {
  201. const exists = await orderManagementPage.orderExists(testOrderName);
  202. if (exists) {
  203. await orderManagementPage.deleteOrder(testOrderName);
  204. console.debug(`✓ 已清理测试订单: ${testOrderName}`);
  205. }
  206. } catch (error) {
  207. console.debug(`清理测试订单失败:`, error);
  208. }
  209. });
  210. test.describe('编辑订单基本信息', () => {
  211. test('应该能修改订单名称', async ({ orderManagementPage }) => {
  212. const timestamp = Date.now();
  213. const newName = `${testOrderName}_修改${timestamp}`;
  214. const result = await orderManagementPage.editOrder(testOrderName, {
  215. name: newName,
  216. });
  217. expect(result.hasSuccess).toBe(true);
  218. expect(result.hasError).toBe(false);
  219. await expect(async () => {
  220. const exists = await orderManagementPage.orderExists(newName);
  221. expect(exists).toBe(true);
  222. }).toPass({ timeout: TIMEOUTS.DIALOG });
  223. // 更新测试订单名称供后续清理使用
  224. testOrderName = newName;
  225. });
  226. test('应该能修改预计开始日期', async ({ orderManagementPage }) => {
  227. const newDate = '2025-02-20';
  228. const result = await orderManagementPage.editOrder(testOrderName, {
  229. expectedStartDate: newDate,
  230. });
  231. expect(result.hasSuccess).toBe(true);
  232. expect(result.hasError).toBe(false);
  233. // 验证列表中日期确实更新了
  234. await expect(async () => {
  235. const actualDate = await getOrderExpectedStartDate(orderManagementPage.page, testOrderName);
  236. expect(actualDate).toBe(newDate);
  237. }).toPass({ timeout: TIMEOUTS.DIALOG });
  238. });
  239. test('应该能同时修改多个基本信息', async ({ orderManagementPage }) => {
  240. const timestamp = Date.now();
  241. const newName = `${testOrderName}_批量${timestamp}`;
  242. const newDate = '2025-03-15';
  243. const result = await orderManagementPage.editOrder(testOrderName, {
  244. name: newName,
  245. expectedStartDate: newDate,
  246. });
  247. expect(result.hasSuccess).toBe(true);
  248. expect(result.hasError).toBe(false);
  249. await expect(async () => {
  250. const exists = await orderManagementPage.orderExists(newName);
  251. expect(exists).toBe(true);
  252. }).toPass({ timeout: TIMEOUTS.DIALOG });
  253. // 验证日期也更新了
  254. const actualDate = await getOrderExpectedStartDate(orderManagementPage.page, newName);
  255. expect(actualDate).toBe(newDate);
  256. testOrderName = newName;
  257. });
  258. });
  259. test.describe('编辑订单关联信息', () => {
  260. test('应该能更换平台', async ({ orderManagementPage, page }) => {
  261. await orderManagementPage.openEditDialog(testOrderName);
  262. const result = await tryChangeSelectValue(page, 'platform-search-select', '平台');
  263. if (!result.changed) {
  264. await orderManagementPage.cancelDialog();
  265. test.skip(true, '无法更换平台(可能只有一个选项)');
  266. return;
  267. }
  268. // 提交表单
  269. const submitResult = await orderManagementPage.submitForm();
  270. expect(submitResult.hasSuccess).toBe(true);
  271. expect(submitResult.hasError).toBe(false);
  272. await orderManagementPage.waitForDialogClosed();
  273. const exists = await orderManagementPage.orderExists(testOrderName);
  274. expect(exists).toBe(true);
  275. });
  276. test('应该能更换公司', async ({ orderManagementPage, page }) => {
  277. await orderManagementPage.openEditDialog(testOrderName);
  278. const result = await tryChangeSelectValue(page, 'company-search-select', '公司');
  279. if (!result.changed) {
  280. await orderManagementPage.cancelDialog();
  281. test.skip(true, '无法更换公司(可能只有一个选项)');
  282. return;
  283. }
  284. const submitResult = await orderManagementPage.submitForm();
  285. expect(submitResult.hasSuccess).toBe(true);
  286. expect(submitResult.hasError).toBe(false);
  287. await orderManagementPage.waitForDialogClosed();
  288. const exists = await orderManagementPage.orderExists(testOrderName);
  289. expect(exists).toBe(true);
  290. });
  291. test('应该能更换渠道', async ({ orderManagementPage, page }) => {
  292. await orderManagementPage.openEditDialog(testOrderName);
  293. const result = await tryChangeSelectValue(page, 'channel-search-select', '渠道');
  294. if (!result.changed) {
  295. await orderManagementPage.cancelDialog();
  296. test.skip(true, '无法更换渠道(可能只有一个选项)');
  297. return;
  298. }
  299. const submitResult = await orderManagementPage.submitForm();
  300. expect(submitResult.hasSuccess).toBe(true);
  301. expect(submitResult.hasError).toBe(false);
  302. await orderManagementPage.waitForDialogClosed();
  303. const exists = await orderManagementPage.orderExists(testOrderName);
  304. expect(exists).toBe(true);
  305. });
  306. test('应该能同时更换多个关联信息', async ({ orderManagementPage, page }) => {
  307. await orderManagementPage.openEditDialog(testOrderName);
  308. let changesMade = 0;
  309. // 尝试更换平台
  310. const platformResult = await tryChangeSelectValue(page, 'platform-search-select', '平台');
  311. if (platformResult.changed) changesMade++;
  312. // 尝试更换公司
  313. const companyResult = await tryChangeSelectValue(page, 'company-search-select', '公司');
  314. if (companyResult.changed) changesMade++;
  315. // 尝试更换渠道
  316. const channelResult = await tryChangeSelectValue(page, 'channel-search-select', '渠道');
  317. if (channelResult.changed) changesMade++;
  318. if (changesMade === 0) {
  319. await orderManagementPage.cancelDialog();
  320. test.skip(true, '没有任何可选择的关联信息选项');
  321. return;
  322. }
  323. const submitResult = await orderManagementPage.submitForm();
  324. expect(submitResult.hasSuccess).toBe(true);
  325. expect(submitResult.hasError).toBe(false);
  326. await orderManagementPage.waitForDialogClosed();
  327. const exists = await orderManagementPage.orderExists(testOrderName);
  328. expect(exists).toBe(true);
  329. });
  330. });
  331. test.describe('编辑后列表更新验证', () => {
  332. test('应该显示更新后的订单名称', async ({ orderManagementPage }) => {
  333. const newName = `${testOrderName}_列表验证`;
  334. await orderManagementPage.editOrder(testOrderName, {
  335. name: newName,
  336. });
  337. await expect(async () => {
  338. const exists = await orderManagementPage.orderExists(newName);
  339. expect(exists).toBe(true);
  340. }).toPass({ timeout: TIMEOUTS.DIALOG });
  341. // 验证旧名称不再存在
  342. const oldExists = await orderManagementPage.orderExists(testOrderName);
  343. expect(oldExists).toBe(false);
  344. testOrderName = newName;
  345. });
  346. test('应该显示更新后的预计开始日期', async ({ orderManagementPage }) => {
  347. const newDate = '2025-04-10';
  348. await orderManagementPage.editOrder(testOrderName, {
  349. expectedStartDate: newDate,
  350. });
  351. // 验证列表中的日期确实更新了
  352. await expect(async () => {
  353. const actualDate = await getOrderExpectedStartDate(orderManagementPage.page, testOrderName);
  354. expect(actualDate).toBe(newDate);
  355. }).toPass({ timeout: TIMEOUTS.DIALOG });
  356. });
  357. test('编辑后返回列表应该显示更新后的信息', async ({ orderManagementPage }) => {
  358. const newName = `${testOrderName}_完整验证`;
  359. const newDate = '2025-05-20';
  360. await orderManagementPage.editOrder(testOrderName, {
  361. name: newName,
  362. expectedStartDate: newDate,
  363. });
  364. await expect(async () => {
  365. const exists = await orderManagementPage.orderExists(newName);
  366. expect(exists).toBe(true);
  367. }).toPass({ timeout: TIMEOUTS.DIALOG });
  368. const oldExists = await orderManagementPage.orderExists(testOrderName);
  369. expect(oldExists).toBe(false);
  370. const actualDate = await getOrderExpectedStartDate(orderManagementPage.page, newName);
  371. expect(actualDate).toBe(newDate);
  372. testOrderName = newName;
  373. });
  374. });
  375. test.describe('编辑对话框交互验证', () => {
  376. test('编辑对话框应该预填充现有数据', async ({ orderManagementPage, page }) => {
  377. await orderManagementPage.openEditDialog(testOrderName);
  378. const dialog = page.locator('[role="dialog"]');
  379. await expect(dialog).toBeVisible();
  380. const nameInput = page.getByLabel(/订单名称|名称/);
  381. await expect(nameInput).toBeVisible();
  382. const nameValue = await nameInput.inputValue();
  383. expect(nameValue).toBe(testOrderName);
  384. await expect(page.getByLabel(/预计开始日期|开始日期/)).toBeVisible();
  385. await expect(page.getByRole('button', { name: '更新' })).toBeVisible();
  386. await expect(page.getByRole('button', { name: '取消' })).toBeVisible();
  387. await orderManagementPage.cancelDialog();
  388. });
  389. test('应该能取消编辑操作', async ({ orderManagementPage, page }) => {
  390. await orderManagementPage.openEditDialog(testOrderName);
  391. const modifiedName = `${testOrderName}_已取消`;
  392. await page.getByLabel(/订单名称|名称/).fill(modifiedName);
  393. const dialog = page.locator('[role="dialog"]');
  394. await expect(dialog).toBeVisible();
  395. await orderManagementPage.cancelDialog();
  396. await expect(dialog).not.toBeVisible();
  397. const exists = await orderManagementPage.orderExists(testOrderName);
  398. expect(exists).toBe(true);
  399. const modifiedExists = await orderManagementPage.orderExists(modifiedName);
  400. expect(modifiedExists).toBe(false);
  401. });
  402. test('应该显示编辑成功的提示消息', async ({ orderManagementPage }) => {
  403. const result = await orderManagementPage.editOrder(testOrderName, {
  404. expectedStartDate: '2025-06-15',
  405. });
  406. expect(result.successMessage).toBeDefined();
  407. expect(result.successMessage?.length).toBeGreaterThan(0);
  408. console.debug('编辑订单成功消息:', result.successMessage);
  409. });
  410. test('应该能通过关闭对话框取消编辑', async ({ orderManagementPage, page }) => {
  411. await orderManagementPage.openEditDialog(testOrderName);
  412. const modifiedName = `${testOrderName}_已关闭`;
  413. await page.getByLabel(/订单名称|名称/).fill(modifiedName);
  414. const dialog = page.locator('[role="dialog"]');
  415. await expect(dialog).toBeVisible();
  416. await page.keyboard.press('Escape');
  417. await orderManagementPage.waitForDialogClosed();
  418. const exists = await orderManagementPage.orderExists(testOrderName);
  419. expect(exists).toBe(true);
  420. const modifiedExists = await orderManagementPage.orderExists(modifiedName);
  421. expect(modifiedExists).toBe(false);
  422. });
  423. });
  424. test.describe('错误场景测试', () => {
  425. test('清空必填字段后应该显示验证错误', async ({ orderManagementPage, page }) => {
  426. await orderManagementPage.openEditDialog(testOrderName);
  427. // 清空订单名称
  428. await page.getByLabel(/订单名称|名称/).fill('');
  429. // 尝试提交
  430. const submitButton = page.getByRole('button', { name: /^(创建|更新|保存)$/ });
  431. await submitButton.click();
  432. await page.waitForTimeout(TIMEOUTS.LONG);
  433. // 验证错误提示
  434. const errorToast = page.locator('[data-sonner-toast][data-type="error"]');
  435. const hasError = await errorToast.count() > 0;
  436. // 验证对话框仍然打开(提交失败)
  437. const dialog = page.locator('[role="dialog"]');
  438. const isDialogOpen = await dialog.count() > 0;
  439. expect(hasError || isDialogOpen).toBe(true);
  440. await orderManagementPage.cancelDialog();
  441. });
  442. test('编辑为已存在的订单名称应该显示错误', async ({ orderManagementPage, page }) => {
  443. // 先创建另一个订单
  444. const existingOrderName = `已存在订单_${Date.now()}`;
  445. const created = await createTestOrder(page, existingOrderName);
  446. if (!created) {
  447. test.skip(true, '无法创建额外的测试订单');
  448. return;
  449. }
  450. // 尝试编辑当前订单为已存在的名称
  451. await orderManagementPage.openEditDialog(testOrderName);
  452. await page.getByLabel(/订单名称|名称/).fill(existingOrderName);
  453. const result = await orderManagementPage.submitForm();
  454. // 应该显示错误或失败
  455. expect(result.hasError).toBe(true);
  456. // 清理创建的订单
  457. await orderManagementPage.waitForDialogClosed();
  458. await orderManagementPage.deleteOrder(existingOrderName);
  459. });
  460. test('网络错误时应该显示错误提示', async ({ orderManagementPage, page }) => {
  461. await orderManagementPage.openEditDialog(testOrderName);
  462. // 模拟网络离线
  463. await page.context().setOffline(true);
  464. // 尝试提交
  465. const result = await orderManagementPage.submitForm();
  466. // 恢复网络
  467. await page.context().setOffline(false);
  468. // 验证错误消息或网络错误
  469. expect(result.hasError).toBe(true || result.hasSuccess === false);
  470. await orderManagementPage.cancelDialog();
  471. });
  472. });
  473. });