2
0

order-create.spec.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. import { TIMEOUTS } from '../../utils/timeouts';
  2. import { test, expect } from '../../utils/test-setup';
  3. import { readFileSync } from 'fs';
  4. import { join, dirname } from 'path';
  5. import { fileURLToPath } from 'url';
  6. const __filename = fileURLToPath(import.meta.url);
  7. const __dirname = dirname(__filename);
  8. const testUsers = JSON.parse(readFileSync(join(__dirname, '../../fixtures/test-users.json'), 'utf-8'));
  9. /**
  10. * 辅助函数:在创建订单对话框中选择残疾人
  11. * @param page Playwright Page 对象
  12. * @returns 是否成功选择了残疾人
  13. */
  14. async function selectDisabledPersonForOrder(page: Parameters<typeof test>[0]['prototype']): Promise<boolean> {
  15. // 点击"选择残疾人"按钮
  16. const selectPersonButton = page.getByRole('button', { name: '选择残疾人' });
  17. await selectPersonButton.click();
  18. // 等待残疾人选择对话框出现
  19. await page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: TIMEOUTS.DIALOG });
  20. // 尝试选择第一个可用的残疾人
  21. let hasData = false;
  22. try {
  23. // 查找残疾人列表中的第一行复选框
  24. const firstCheckbox = page.locator('table tbody tr').first().locator('input[type="checkbox"]').first();
  25. await firstCheckbox.waitFor({ state: 'visible', timeout: TIMEOUTS.ELEMENT_VISIBLE_SHORT });
  26. await firstCheckbox.check();
  27. console.debug('✓ 已选择第一个残疾人');
  28. hasData = true;
  29. } catch (error) {
  30. console.debug('没有可用的残疾人数据');
  31. hasData = false;
  32. }
  33. if (hasData) {
  34. // 有数据时,点击确认按钮关闭选择对话框
  35. const confirmButton = page.getByRole('button', { name: /^(确定|确认|选择)$/ });
  36. await confirmButton.click().catch(() => {
  37. console.debug('没有找到确认按钮,尝试关闭对话框');
  38. page.keyboard.press('Escape');
  39. });
  40. } else {
  41. // 没有数据时,关闭空对话框
  42. await page.keyboard.press('Escape').catch(() => {
  43. console.debug('无法关闭对话框,可能已经自动关闭');
  44. });
  45. }
  46. // 等待选择对话框关闭
  47. await page.waitForTimeout(TIMEOUTS.MEDIUM);
  48. return hasData;
  49. }
  50. test.describe.serial('创建订单测试', () => {
  51. test.beforeEach(async ({ adminLoginPage, orderManagementPage }) => {
  52. // 以管理员身份登录后台
  53. await adminLoginPage.goto();
  54. await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
  55. await adminLoginPage.expectLoginSuccess();
  56. await orderManagementPage.goto();
  57. });
  58. test.describe('基本创建订单', () => {
  59. test('应该能创建只填写必填字段的订单', async ({ orderManagementPage, page }) => {
  60. // 生成唯一订单名称
  61. const orderName = `测试订单_${Date.now()}`;
  62. // 打开创建对话框
  63. await orderManagementPage.openCreateDialog();
  64. // 填写必填字段
  65. await page.getByLabel(/订单名称|名称/).fill(orderName);
  66. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  67. // 选择残疾人(必填)
  68. const hasDisabledPerson = await selectDisabledPersonForOrder(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(orderName);
  80. expect(exists).toBe(true);
  81. }).toPass({ timeout: TIMEOUTS.DIALOG });
  82. } else {
  83. // 没有残疾人数据时,跳过测试验证
  84. // 注意:完整的订单创建流程需要残疾人数据
  85. console.debug('没有残疾人数据,跳过订单创建验证');
  86. await orderManagementPage.cancelDialog();
  87. }
  88. });
  89. test('应该显示创建成功的提示消息', async ({ orderManagementPage, page }) => {
  90. const orderName = `测试订单_${Date.now()}`;
  91. // 打开创建对话框
  92. await orderManagementPage.openCreateDialog();
  93. // 填写必填字段
  94. await page.getByLabel(/订单名称|名称/).fill(orderName);
  95. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  96. // 选择残疾人(必填)
  97. const hasDisabledPerson = await selectDisabledPersonForOrder(page);
  98. if (hasDisabledPerson) {
  99. // 有残疾人数据时,提交表单并验证成功消息
  100. const result = await orderManagementPage.submitForm();
  101. // 验证成功消息
  102. expect(result.successMessage).toBeDefined();
  103. expect(result.successMessage?.length).toBeGreaterThan(0);
  104. console.debug('创建订单成功消息:', result.successMessage);
  105. } else {
  106. // 没有数据时跳过验证
  107. console.debug('没有残疾人数据,跳过成功消息验证');
  108. await orderManagementPage.cancelDialog();
  109. }
  110. });
  111. });
  112. test.describe('创建订单并选择平台', () => {
  113. test('应该能创建订单时选择平台', async ({ orderManagementPage, page }) => {
  114. const orderName = `测试订单_平台_${Date.now()}`;
  115. // 打开创建对话框
  116. await orderManagementPage.openCreateDialog();
  117. // 填写必填字段
  118. await page.getByLabel(/订单名称|名称/).fill(orderName);
  119. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  120. // 选择平台(使用 selectRadixOption 工具)
  121. try {
  122. // 点击平台下拉框(通过文本定位)
  123. const platformTrigger = page.locator('[data-testid="platform-search-select"]');
  124. if (await platformTrigger.count() > 0) {
  125. await platformTrigger.click({ force: true });
  126. // 等待平台选项列表出现
  127. const platformOption = page.getByRole('option').first();
  128. await platformOption.waitFor({ state: 'visible', timeout: TIMEOUTS.ELEMENT_VISIBLE_SHORT });
  129. // 选择第一个可用平台
  130. await platformOption.click();
  131. } else {
  132. console.debug('平台选择器未找到,跳过平台选择');
  133. }
  134. } catch (error) {
  135. console.debug('平台选择器无可用选项,跳过平台选择:', error);
  136. }
  137. // 选择残疾人(必填)
  138. const hasDisabledPerson = await selectDisabledPersonForOrder(page);
  139. if (hasDisabledPerson) {
  140. // 提交表单
  141. const result = await orderManagementPage.submitForm();
  142. // 验证创建成功
  143. expect(result.hasSuccess).toBe(true);
  144. expect(result.hasError).toBe(false);
  145. // 等待对话框关闭
  146. await orderManagementPage.waitForDialogClosed();
  147. // 验证订单出现在列表中
  148. await expect(async () => {
  149. const exists = await orderManagementPage.orderExists(orderName);
  150. expect(exists).toBe(true);
  151. }).toPass({ timeout: TIMEOUTS.DIALOG });
  152. } else {
  153. console.debug('没有残疾人数据,跳过平台选择测试验证');
  154. }
  155. });
  156. });
  157. test.describe('创建订单并选择公司', () => {
  158. test('应该能创建订单时选择公司', async ({ orderManagementPage, page }) => {
  159. const orderName = `测试订单_公司_${Date.now()}`;
  160. // 打开创建对话框
  161. await orderManagementPage.openCreateDialog();
  162. // 填写必填字段
  163. await page.getByLabel(/订单名称|名称/).fill(orderName);
  164. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  165. // 选择公司(使用 data-testid 定位)
  166. try {
  167. const companyTrigger = page.locator('[data-testid="company-search-select"]');
  168. if (await companyTrigger.count() > 0) {
  169. await companyTrigger.click({ force: true });
  170. // 等待公司选项列表出现
  171. const companyOption = page.getByRole('option').first();
  172. await companyOption.waitFor({ state: 'visible', timeout: TIMEOUTS.ELEMENT_VISIBLE_SHORT });
  173. // 选择第一个可用公司
  174. await companyOption.click();
  175. } else {
  176. console.debug('公司选择器未找到,跳过公司选择');
  177. }
  178. } catch (error) {
  179. console.debug('公司选择器无可用选项,跳过公司选择:', error);
  180. }
  181. // 选择残疾人(必填)
  182. const hasDisabledPerson = await selectDisabledPersonForOrder(page);
  183. if (hasDisabledPerson) {
  184. // 提交表单
  185. const result = await orderManagementPage.submitForm();
  186. // 验证创建成功
  187. expect(result.hasSuccess).toBe(true);
  188. expect(result.hasError).toBe(false);
  189. // 等待对话框关闭
  190. await orderManagementPage.waitForDialogClosed();
  191. // 验证订单出现在列表中
  192. await expect(async () => {
  193. const exists = await orderManagementPage.orderExists(orderName);
  194. expect(exists).toBe(true);
  195. }).toPass({ timeout: TIMEOUTS.DIALOG });
  196. } else {
  197. console.debug('没有残疾人数据,跳过公司选择测试验证');
  198. }
  199. });
  200. });
  201. test.describe('创建订单并选择渠道', () => {
  202. test('应该能创建订单时选择渠道', async ({ orderManagementPage, page }) => {
  203. const orderName = `测试订单_渠道_${Date.now()}`;
  204. // 打开创建对话框
  205. await orderManagementPage.openCreateDialog();
  206. // 填写必填字段
  207. await page.getByLabel(/订单名称|名称/).fill(orderName);
  208. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  209. // 选择渠道(使用 data-testid 定位)
  210. try {
  211. const channelTrigger = page.locator('[data-testid="channel-search-select"]');
  212. if (await channelTrigger.count() > 0) {
  213. await channelTrigger.click({ force: true });
  214. // 等待渠道选项列表出现
  215. const channelOption = page.getByRole('option').first();
  216. await channelOption.waitFor({ state: 'visible', timeout: TIMEOUTS.ELEMENT_VISIBLE_SHORT });
  217. // 选择第一个可用渠道
  218. await channelOption.click();
  219. } else {
  220. console.debug('渠道选择器未找到,跳过渠道选择');
  221. }
  222. } catch (error) {
  223. console.debug('渠道选择器无可用选项,跳过渠道选择:', error);
  224. }
  225. // 选择残疾人(必填)
  226. const hasDisabledPerson = await selectDisabledPersonForOrder(page);
  227. if (hasDisabledPerson) {
  228. // 提交表单
  229. const result = await orderManagementPage.submitForm();
  230. // 验证创建成功
  231. expect(result.hasSuccess).toBe(true);
  232. expect(result.hasError).toBe(false);
  233. // 等待对话框关闭
  234. await orderManagementPage.waitForDialogClosed();
  235. // 验证订单出现在列表中
  236. await expect(async () => {
  237. const exists = await orderManagementPage.orderExists(orderName);
  238. expect(exists).toBe(true);
  239. }).toPass({ timeout: TIMEOUTS.DIALOG });
  240. } else {
  241. console.debug('没有残疾人数据,跳过渠道选择测试验证');
  242. }
  243. });
  244. });
  245. test.describe('创建完整订单(所有字段)', () => {
  246. test('应该能填写所有字段创建订单', async ({ orderManagementPage, page }) => {
  247. const orderName = `测试订单_完整_${Date.now()}`;
  248. // 打开创建对话框
  249. await orderManagementPage.openCreateDialog();
  250. // 填写所有字段
  251. await page.getByLabel(/订单名称|名称/).fill(orderName);
  252. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  253. // 尝试选择平台
  254. try {
  255. const platformTrigger = page.locator('[data-testid="platform-search-select"]');
  256. if (await platformTrigger.count() > 0) {
  257. await platformTrigger.click({ force: true });
  258. const platformOption = page.getByRole('option').first();
  259. await platformOption.waitFor({ state: 'visible', timeout: TIMEOUTS.ELEMENT_VISIBLE_SHORT });
  260. await platformOption.click();
  261. } else {
  262. console.debug('平台选择器未找到');
  263. }
  264. } catch {
  265. console.debug('平台选择器无可用选项');
  266. }
  267. // 尝试选择公司
  268. try {
  269. const companyTrigger = page.locator('[data-testid="company-search-select"]');
  270. if (await companyTrigger.count() > 0) {
  271. await companyTrigger.click({ force: true });
  272. const companyOption = page.getByRole('option').first();
  273. await companyOption.waitFor({ state: 'visible', timeout: TIMEOUTS.ELEMENT_VISIBLE_SHORT });
  274. await companyOption.click();
  275. } else {
  276. console.debug('公司选择器未找到');
  277. }
  278. } catch {
  279. console.debug('公司选择器无可用选项');
  280. }
  281. // 尝试选择渠道
  282. try {
  283. const channelTrigger = page.locator('[data-testid="channel-search-select"]');
  284. if (await channelTrigger.count() > 0) {
  285. await channelTrigger.click({ force: true });
  286. const channelOption = page.getByRole('option').first();
  287. await channelOption.waitFor({ state: 'visible', timeout: TIMEOUTS.ELEMENT_VISIBLE_SHORT });
  288. await channelOption.click();
  289. } else {
  290. console.debug('渠道选择器未找到');
  291. }
  292. } catch {
  293. console.debug('渠道选择器无可用选项');
  294. }
  295. // 选择残疾人(必填)
  296. const hasDisabledPerson = await selectDisabledPersonForOrder(page);
  297. if (hasDisabledPerson) {
  298. // 提交表单
  299. const result = await orderManagementPage.submitForm();
  300. // 验证创建成功
  301. expect(result.hasSuccess).toBe(true);
  302. expect(result.hasError).toBe(false);
  303. // 等待对话框关闭
  304. await orderManagementPage.waitForDialogClosed();
  305. // 验证订单出现在列表中
  306. await expect(async () => {
  307. const exists = await orderManagementPage.orderExists(orderName);
  308. expect(exists).toBe(true);
  309. }).toPass({ timeout: TIMEOUTS.DIALOG });
  310. } else {
  311. console.debug('没有残疾人数据,跳过完整订单测试验证');
  312. }
  313. });
  314. });
  315. test.describe('表单验证测试', () => {
  316. test('应该能尝试提交表单', async ({ orderManagementPage, page }) => {
  317. // 注意:这个测试验证表单提交的基本流程
  318. // 完整的订单创建需要残疾人数据
  319. // 打开创建对话框
  320. await orderManagementPage.openCreateDialog();
  321. // 填写基本字段
  322. await page.getByLabel(/订单名称|名称/).fill('表单验证测试');
  323. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  324. // 验证对话框仍然打开(未提交)
  325. const dialog = page.locator('[role="dialog"]');
  326. await expect(dialog).toBeVisible();
  327. // 关闭对话框
  328. await orderManagementPage.cancelDialog();
  329. });
  330. test('应该能取消创建订单操作', async ({ orderManagementPage, page }) => {
  331. // 打开创建对话框
  332. await orderManagementPage.openCreateDialog();
  333. // 填写一些字段
  334. await page.getByLabel(/订单名称|名称/).fill('取消测试订单');
  335. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  336. // 验证对话框是打开的
  337. const dialog = page.locator('[role="dialog"]');
  338. await expect(dialog).toBeVisible();
  339. // 取消对话框
  340. await orderManagementPage.cancelDialog();
  341. // 验证对话框已关闭
  342. await expect(dialog).not.toBeVisible();
  343. // 验证订单没有出现在列表中
  344. const exists = await orderManagementPage.orderExists('取消测试订单');
  345. expect(exists).toBe(false);
  346. });
  347. test('应该能通过关闭对话框取消创建', async ({ orderManagementPage, page }) => {
  348. // 打开创建对话框
  349. await orderManagementPage.openCreateDialog();
  350. // 填写一些字段
  351. await page.getByLabel(/订单名称|名称/).fill('关闭测试订单');
  352. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  353. // 验证对话框是打开的
  354. const dialog = page.locator('[role="dialog"]');
  355. await expect(dialog).toBeVisible();
  356. // 点击对话框外的遮罩层或按 ESC 键关闭
  357. await page.keyboard.press('Escape');
  358. // 等待对话框关闭
  359. await orderManagementPage.waitForDialogClosed();
  360. // 验证订单没有出现在列表中
  361. const exists = await orderManagementPage.orderExists('关闭测试订单');
  362. expect(exists).toBe(false);
  363. });
  364. });
  365. test.describe('对话框元素验证', () => {
  366. test('应该显示创建订单对话框的所有字段', async ({ orderManagementPage, page }) => {
  367. // 打开创建对话框
  368. await orderManagementPage.openCreateDialog();
  369. // 验证对话框存在
  370. const dialog = page.locator('[role="dialog"]');
  371. await expect(dialog).toBeVisible();
  372. // 验证必填字段存在
  373. await expect(page.getByLabel(/订单名称|名称/)).toBeVisible();
  374. await expect(page.getByLabel(/预计开始日期|开始日期/)).toBeVisible();
  375. // 验证选择残疾人按钮存在
  376. await expect(page.getByRole('button', { name: '选择残疾人' })).toBeVisible();
  377. // 验证提示文本存在
  378. await expect(page.getByText(/创建订单时必须至少选择一名残疾人/)).toBeVisible();
  379. // 验证可选字段存在(在对话框内查找,使用 first() 避免匹配多个元素)
  380. await expect(dialog.getByText('平台').first()).toBeVisible();
  381. await expect(dialog.getByText('公司').first()).toBeVisible();
  382. await expect(dialog.getByText('渠道').first()).toBeVisible();
  383. // 验证按钮存在
  384. await expect(page.getByRole('button', { name: '创建' })).toBeVisible();
  385. await expect(page.getByRole('button', { name: '取消' })).toBeVisible();
  386. // 关闭对话框
  387. await orderManagementPage.cancelDialog();
  388. });
  389. });
  390. });