2
0

order-create.spec.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. test.beforeEach(async ({ adminLoginPage, orderManagementPage }) => {
  51. // 以管理员身份登录后台
  52. await adminLoginPage.goto();
  53. await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
  54. await adminLoginPage.expectLoginSuccess();
  55. await orderManagementPage.goto();
  56. });
  57. test.describe('基本创建订单', () => {
  58. test('应该能创建只填写必填字段的订单', async ({ orderManagementPage, page }) => {
  59. // 生成唯一订单名称
  60. const orderName = `测试订单_${Date.now()}`;
  61. // 打开创建对话框
  62. await orderManagementPage.openCreateDialog();
  63. // 填写必填字段
  64. await page.getByLabel(/订单名称|名称/).fill(orderName);
  65. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  66. // 选择残疾人(必填)
  67. const hasDisabledPerson = await selectDisabledPersonForOrder(page);
  68. if (hasDisabledPerson) {
  69. // 有残疾人数据时,提交表单并验证
  70. const result = await orderManagementPage.submitForm();
  71. // 验证创建成功
  72. expect(result.hasSuccess).toBe(true);
  73. expect(result.hasError).toBe(false);
  74. // 等待对话框关闭
  75. await orderManagementPage.waitForDialogClosed();
  76. // 验证订单出现在列表中
  77. await expect(async () => {
  78. const exists = await orderManagementPage.orderExists(orderName);
  79. expect(exists).toBe(true);
  80. }).toPass({ timeout: 5000 });
  81. } else {
  82. // 没有残疾人数据时,跳过测试验证
  83. // 注意:完整的订单创建流程需要残疾人数据
  84. console.debug('没有残疾人数据,跳过订单创建验证');
  85. await orderManagementPage.cancelDialog();
  86. }
  87. });
  88. test('应该显示创建成功的提示消息', async ({ orderManagementPage, page }) => {
  89. const orderName = `测试订单_${Date.now()}`;
  90. // 打开创建对话框
  91. await orderManagementPage.openCreateDialog();
  92. // 填写必填字段
  93. await page.getByLabel(/订单名称|名称/).fill(orderName);
  94. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  95. // 选择残疾人(必填)
  96. const hasDisabledPerson = await selectDisabledPersonForOrder(page);
  97. if (hasDisabledPerson) {
  98. // 有残疾人数据时,提交表单并验证成功消息
  99. const result = await orderManagementPage.submitForm();
  100. // 验证成功消息
  101. expect(result.successMessage).toBeDefined();
  102. expect(result.successMessage?.length).toBeGreaterThan(0);
  103. console.debug('创建订单成功消息:', result.successMessage);
  104. } else {
  105. // 没有数据时跳过验证
  106. console.debug('没有残疾人数据,跳过成功消息验证');
  107. await orderManagementPage.cancelDialog();
  108. }
  109. });
  110. });
  111. test.describe('创建订单并选择平台', () => {
  112. test('应该能创建订单时选择平台', async ({ orderManagementPage, page }) => {
  113. const orderName = `测试订单_平台_${Date.now()}`;
  114. // 打开创建对话框
  115. await orderManagementPage.openCreateDialog();
  116. // 填写必填字段
  117. await page.getByLabel(/订单名称|名称/).fill(orderName);
  118. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  119. // 选择平台(使用 selectRadixOption 工具)
  120. try {
  121. // 点击平台下拉框(通过文本定位)
  122. const platformTrigger = page.locator('[data-testid="platform-search-select"]');
  123. if (await platformTrigger.count() > 0) {
  124. await platformTrigger.click({ force: true });
  125. // 等待平台选项列表出现
  126. const platformOption = page.getByRole('option').first();
  127. await platformOption.waitFor({ state: 'visible', timeout: 3000 });
  128. // 选择第一个可用平台
  129. await platformOption.click();
  130. } else {
  131. console.debug('平台选择器未找到,跳过平台选择');
  132. }
  133. } catch (error) {
  134. console.debug('平台选择器无可用选项,跳过平台选择:', error);
  135. }
  136. // 选择残疾人(必填)
  137. const hasDisabledPerson = await selectDisabledPersonForOrder(page);
  138. if (hasDisabledPerson) {
  139. // 提交表单
  140. const result = await orderManagementPage.submitForm();
  141. // 验证创建成功
  142. expect(result.hasSuccess).toBe(true);
  143. expect(result.hasError).toBe(false);
  144. // 等待对话框关闭
  145. await orderManagementPage.waitForDialogClosed();
  146. // 验证订单出现在列表中
  147. await expect(async () => {
  148. const exists = await orderManagementPage.orderExists(orderName);
  149. expect(exists).toBe(true);
  150. }).toPass({ timeout: 5000 });
  151. } else {
  152. console.debug('没有残疾人数据,跳过平台选择测试验证');
  153. }
  154. });
  155. });
  156. test.describe('创建订单并选择公司', () => {
  157. test('应该能创建订单时选择公司', async ({ orderManagementPage, page }) => {
  158. const orderName = `测试订单_公司_${Date.now()}`;
  159. // 打开创建对话框
  160. await orderManagementPage.openCreateDialog();
  161. // 填写必填字段
  162. await page.getByLabel(/订单名称|名称/).fill(orderName);
  163. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  164. // 选择公司(使用 data-testid 定位)
  165. try {
  166. const companyTrigger = page.locator('[data-testid="company-search-select"]');
  167. if (await companyTrigger.count() > 0) {
  168. await companyTrigger.click({ force: true });
  169. // 等待公司选项列表出现
  170. const companyOption = page.getByRole('option').first();
  171. await companyOption.waitFor({ state: 'visible', timeout: 3000 });
  172. // 选择第一个可用公司
  173. await companyOption.click();
  174. } else {
  175. console.debug('公司选择器未找到,跳过公司选择');
  176. }
  177. } catch (error) {
  178. console.debug('公司选择器无可用选项,跳过公司选择:', error);
  179. }
  180. // 选择残疾人(必填)
  181. const hasDisabledPerson = await selectDisabledPersonForOrder(page);
  182. if (hasDisabledPerson) {
  183. // 提交表单
  184. const result = await orderManagementPage.submitForm();
  185. // 验证创建成功
  186. expect(result.hasSuccess).toBe(true);
  187. expect(result.hasError).toBe(false);
  188. // 等待对话框关闭
  189. await orderManagementPage.waitForDialogClosed();
  190. // 验证订单出现在列表中
  191. await expect(async () => {
  192. const exists = await orderManagementPage.orderExists(orderName);
  193. expect(exists).toBe(true);
  194. }).toPass({ timeout: 5000 });
  195. } else {
  196. console.debug('没有残疾人数据,跳过公司选择测试验证');
  197. }
  198. });
  199. });
  200. test.describe('创建订单并选择渠道', () => {
  201. test('应该能创建订单时选择渠道', async ({ orderManagementPage, page }) => {
  202. const orderName = `测试订单_渠道_${Date.now()}`;
  203. // 打开创建对话框
  204. await orderManagementPage.openCreateDialog();
  205. // 填写必填字段
  206. await page.getByLabel(/订单名称|名称/).fill(orderName);
  207. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  208. // 选择渠道(使用 data-testid 定位)
  209. try {
  210. const channelTrigger = page.locator('[data-testid="channel-search-select"]');
  211. if (await channelTrigger.count() > 0) {
  212. await channelTrigger.click({ force: true });
  213. // 等待渠道选项列表出现
  214. const channelOption = page.getByRole('option').first();
  215. await channelOption.waitFor({ state: 'visible', timeout: 3000 });
  216. // 选择第一个可用渠道
  217. await channelOption.click();
  218. } else {
  219. console.debug('渠道选择器未找到,跳过渠道选择');
  220. }
  221. } catch (error) {
  222. console.debug('渠道选择器无可用选项,跳过渠道选择:', error);
  223. }
  224. // 选择残疾人(必填)
  225. const hasDisabledPerson = await selectDisabledPersonForOrder(page);
  226. if (hasDisabledPerson) {
  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(orderName);
  237. expect(exists).toBe(true);
  238. }).toPass({ timeout: 5000 });
  239. } else {
  240. console.debug('没有残疾人数据,跳过渠道选择测试验证');
  241. }
  242. });
  243. });
  244. test.describe('创建完整订单(所有字段)', () => {
  245. test('应该能填写所有字段创建订单', async ({ orderManagementPage, page }) => {
  246. const orderName = `测试订单_完整_${Date.now()}`;
  247. // 打开创建对话框
  248. await orderManagementPage.openCreateDialog();
  249. // 填写所有字段
  250. await page.getByLabel(/订单名称|名称/).fill(orderName);
  251. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  252. // 尝试选择平台
  253. try {
  254. const platformTrigger = page.locator('[data-testid="platform-search-select"]');
  255. if (await platformTrigger.count() > 0) {
  256. await platformTrigger.click({ force: true });
  257. const platformOption = page.getByRole('option').first();
  258. await platformOption.waitFor({ state: 'visible', timeout: 3000 });
  259. await platformOption.click();
  260. } else {
  261. console.debug('平台选择器未找到');
  262. }
  263. } catch {
  264. console.debug('平台选择器无可用选项');
  265. }
  266. // 尝试选择公司
  267. try {
  268. const companyTrigger = page.locator('[data-testid="company-search-select"]');
  269. if (await companyTrigger.count() > 0) {
  270. await companyTrigger.click({ force: true });
  271. const companyOption = page.getByRole('option').first();
  272. await companyOption.waitFor({ state: 'visible', timeout: 3000 });
  273. await companyOption.click();
  274. } else {
  275. console.debug('公司选择器未找到');
  276. }
  277. } catch {
  278. console.debug('公司选择器无可用选项');
  279. }
  280. // 尝试选择渠道
  281. try {
  282. const channelTrigger = page.locator('[data-testid="channel-search-select"]');
  283. if (await channelTrigger.count() > 0) {
  284. await channelTrigger.click({ force: true });
  285. const channelOption = page.getByRole('option').first();
  286. await channelOption.waitFor({ state: 'visible', timeout: 3000 });
  287. await channelOption.click();
  288. } else {
  289. console.debug('渠道选择器未找到');
  290. }
  291. } catch {
  292. console.debug('渠道选择器无可用选项');
  293. }
  294. // 选择残疾人(必填)
  295. const hasDisabledPerson = await selectDisabledPersonForOrder(page);
  296. if (hasDisabledPerson) {
  297. // 提交表单
  298. const result = await orderManagementPage.submitForm();
  299. // 验证创建成功
  300. expect(result.hasSuccess).toBe(true);
  301. expect(result.hasError).toBe(false);
  302. // 等待对话框关闭
  303. await orderManagementPage.waitForDialogClosed();
  304. // 验证订单出现在列表中
  305. await expect(async () => {
  306. const exists = await orderManagementPage.orderExists(orderName);
  307. expect(exists).toBe(true);
  308. }).toPass({ timeout: 5000 });
  309. } else {
  310. console.debug('没有残疾人数据,跳过完整订单测试验证');
  311. }
  312. });
  313. });
  314. test.describe('表单验证测试', () => {
  315. test('应该能尝试提交表单', async ({ orderManagementPage, page }) => {
  316. // 注意:这个测试验证表单提交的基本流程
  317. // 完整的订单创建需要残疾人数据
  318. // 打开创建对话框
  319. await orderManagementPage.openCreateDialog();
  320. // 填写基本字段
  321. await page.getByLabel(/订单名称|名称/).fill('表单验证测试');
  322. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  323. // 验证对话框仍然打开(未提交)
  324. const dialog = page.locator('[role="dialog"]');
  325. await expect(dialog).toBeVisible();
  326. // 关闭对话框
  327. await orderManagementPage.cancelDialog();
  328. });
  329. test('应该能取消创建订单操作', async ({ orderManagementPage, page }) => {
  330. // 打开创建对话框
  331. await orderManagementPage.openCreateDialog();
  332. // 填写一些字段
  333. await page.getByLabel(/订单名称|名称/).fill('取消测试订单');
  334. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  335. // 验证对话框是打开的
  336. const dialog = page.locator('[role="dialog"]');
  337. await expect(dialog).toBeVisible();
  338. // 取消对话框
  339. await orderManagementPage.cancelDialog();
  340. // 验证对话框已关闭
  341. await expect(dialog).not.toBeVisible();
  342. // 验证订单没有出现在列表中
  343. const exists = await orderManagementPage.orderExists('取消测试订单');
  344. expect(exists).toBe(false);
  345. });
  346. test('应该能通过关闭对话框取消创建', async ({ orderManagementPage, page }) => {
  347. // 打开创建对话框
  348. await orderManagementPage.openCreateDialog();
  349. // 填写一些字段
  350. await page.getByLabel(/订单名称|名称/).fill('关闭测试订单');
  351. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  352. // 验证对话框是打开的
  353. const dialog = page.locator('[role="dialog"]');
  354. await expect(dialog).toBeVisible();
  355. // 点击对话框外的遮罩层或按 ESC 键关闭
  356. await page.keyboard.press('Escape');
  357. // 等待对话框关闭
  358. await orderManagementPage.waitForDialogClosed();
  359. // 验证订单没有出现在列表中
  360. const exists = await orderManagementPage.orderExists('关闭测试订单');
  361. expect(exists).toBe(false);
  362. });
  363. });
  364. test.describe('对话框元素验证', () => {
  365. test('应该显示创建订单对话框的所有字段', async ({ orderManagementPage, page }) => {
  366. // 打开创建对话框
  367. await orderManagementPage.openCreateDialog();
  368. // 验证对话框存在
  369. const dialog = page.locator('[role="dialog"]');
  370. await expect(dialog).toBeVisible();
  371. // 验证必填字段存在
  372. await expect(page.getByLabel(/订单名称|名称/)).toBeVisible();
  373. await expect(page.getByLabel(/预计开始日期|开始日期/)).toBeVisible();
  374. // 验证选择残疾人按钮存在
  375. await expect(page.getByRole('button', { name: '选择残疾人' })).toBeVisible();
  376. // 验证提示文本存在
  377. await expect(page.getByText(/创建订单时必须至少选择一名残疾人/)).toBeVisible();
  378. // 验证可选字段存在(在对话框内查找,使用 first() 避免匹配多个元素)
  379. await expect(dialog.getByText('平台').first()).toBeVisible();
  380. await expect(dialog.getByText('公司').first()).toBeVisible();
  381. await expect(dialog.getByText('渠道').first()).toBeVisible();
  382. // 验证按钮存在
  383. await expect(page.getByRole('button', { name: '创建' })).toBeVisible();
  384. await expect(page.getByRole('button', { name: '取消' })).toBeVisible();
  385. // 关闭对话框
  386. await orderManagementPage.cancelDialog();
  387. });
  388. });
  389. });