order-edit.spec.ts 20 KB

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