2
0

order-edit.spec.ts 20 KB

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