order-person.spec.ts 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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. import type { Page } from '@playwright/test';
  7. const __filename = fileURLToPath(import.meta.url);
  8. const __dirname = dirname(__filename);
  9. const testUsers = JSON.parse(readFileSync(join(__dirname, '../../fixtures/test-users.json'), 'utf-8'));
  10. // 存储 API 创建的测试数据
  11. let createdPersonName: string | null = null;
  12. let createdPlatformName: string | null = null;
  13. let createdCompanyName: string | null = null;
  14. // 获取认证 token
  15. async function getAuthToken(request: Parameters<typeof test>[0]['request']): Promise<string | null> {
  16. const loginResponse = await request.post('http://localhost:8080/api/v1/auth/login', {
  17. data: {
  18. username: testUsers.admin.username,
  19. password: testUsers.admin.password
  20. }
  21. });
  22. if (!loginResponse.ok()) {
  23. console.debug('API 登录失败:', await loginResponse.text());
  24. return null;
  25. }
  26. const loginData = await loginResponse.json();
  27. return loginData.data?.token || loginData.token || null;
  28. }
  29. // API 调用辅助函数 - 使用 API 直接创建残疾人数据
  30. async function createDisabledPersonViaAPI(
  31. request: Parameters<typeof test>[0]['request'],
  32. personData: {
  33. name: string;
  34. gender: string;
  35. idCard: string;
  36. disabilityId: string;
  37. disabilityType: string;
  38. disabilityLevel: string;
  39. idAddress: string;
  40. phone: string;
  41. province: string;
  42. city: string;
  43. }
  44. ): Promise<{ id: number; name: string } | null> {
  45. try {
  46. const token = await getAuthToken(request);
  47. if (!token) return null;
  48. const createResponse = await request.post('http://localhost:8080/api/v1/disability/createDisabledPerson', {
  49. headers: {
  50. 'Authorization': `Bearer ${token}`,
  51. 'Content-Type': 'application/json'
  52. },
  53. data: personData
  54. });
  55. if (!createResponse.ok()) {
  56. const errorText = await createResponse.text();
  57. console.debug('API 创建残疾人失败:', createResponse.status(), errorText);
  58. return null;
  59. }
  60. const result = await createResponse.json();
  61. console.debug('API 创建残疾人成功:', result.name);
  62. return { id: result.id, name: result.name };
  63. } catch (error) {
  64. console.debug('API 调用出错:', error);
  65. return null;
  66. }
  67. }
  68. // 创建测试平台
  69. async function createPlatformViaAPI(
  70. request: Parameters<typeof test>[0]['request']
  71. ): Promise<{ id: number; name: string } | null> {
  72. try {
  73. const token = await getAuthToken(request);
  74. if (!token) return null;
  75. const timestamp = Date.now();
  76. const platformData = {
  77. platformName: `测试平台_${timestamp}`,
  78. contactPerson: '测试联系人',
  79. contactPhone: '13800138000',
  80. contactEmail: 'test@example.com'
  81. };
  82. const createResponse = await request.post('http://localhost:8080/api/v1/platform/createPlatform', {
  83. headers: {
  84. 'Authorization': `Bearer ${token}`,
  85. 'Content-Type': 'application/json'
  86. },
  87. data: platformData
  88. });
  89. if (!createResponse.ok()) {
  90. const errorText = await createResponse.text();
  91. console.debug('API 创建平台失败:', createResponse.status(), errorText);
  92. return null;
  93. }
  94. const result = await createResponse.json();
  95. console.debug('API 创建平台成功:', result.id, result.platformName);
  96. return { id: result.id, name: result.platformName };
  97. } catch (error) {
  98. console.debug('创建平台 API 调用出错:', error);
  99. return null;
  100. }
  101. }
  102. // 创建测试公司
  103. async function createCompanyViaAPI(
  104. request: Parameters<typeof test>[0]['request'],
  105. platformId: number
  106. ): Promise<{ id: number; name: string } | null> {
  107. try {
  108. const token = await getAuthToken(request);
  109. if (!token) return null;
  110. const timestamp = Date.now();
  111. const companyName = `测试公司_${timestamp}`;
  112. const companyData = {
  113. companyName: companyName,
  114. platformId: platformId,
  115. contactPerson: '测试联系人',
  116. contactPhone: '13900139000',
  117. contactEmail: 'company@example.com'
  118. };
  119. const createResponse = await request.post('http://localhost:8080/api/v1/company/createCompany', {
  120. headers: {
  121. 'Authorization': `Bearer ${token}`,
  122. 'Content-Type': 'application/json'
  123. },
  124. data: companyData
  125. });
  126. if (!createResponse.ok()) {
  127. const errorText = await createResponse.text();
  128. console.debug('API 创建公司失败:', createResponse.status(), errorText);
  129. return null;
  130. }
  131. const createResult = await createResponse.json();
  132. if (!createResult.success) {
  133. console.debug('API 创建公司返回 success=false');
  134. return null;
  135. }
  136. // 创建成功后,通过平台ID查询公司列表来获取公司ID
  137. const listResponse = await request.get(`http://localhost:8080/api/v1/company/getCompaniesByPlatform/${platformId}`, {
  138. headers: {
  139. 'Authorization': `Bearer ${token}`
  140. }
  141. });
  142. if (!listResponse.ok()) {
  143. console.debug('API 获取公司列表失败');
  144. return null;
  145. }
  146. const companies = await listResponse.json();
  147. const createdCompany = companies.find((c: any) => c.companyName === companyName);
  148. if (createdCompany) {
  149. console.debug('API 创建公司成功:', createdCompany.id, createdCompany.companyName);
  150. return { id: createdCompany.id, name: createdCompany.companyName };
  151. }
  152. console.debug('未找到创建的公司');
  153. return null;
  154. } catch (error) {
  155. console.debug('创建公司 API 调用出错:', error);
  156. return null;
  157. }
  158. }
  159. async function selectDisabledPersonInAddDialog(
  160. page: Page,
  161. personName?: string
  162. ): Promise<boolean> {
  163. // 监听控制台消息
  164. page.on('console', msg => {
  165. console.log('[浏览器控制台]', msg.text());
  166. });
  167. const selectPersonButton = page.getByRole('button', { name: '选择残疾人' });
  168. await selectPersonButton.click();
  169. // 检查测试标志是否设置
  170. const testFlag = await page.evaluate(() => (window as any).__PLAYWRIGHT_TEST__);
  171. console.log('测试标志 __PLAYWRIGHT_TEST__:', testFlag);
  172. // 使用唯一的 test ID 精确定位残疾人选择对话框
  173. const dialog = page.getByTestId('disabled-person-selector-dialog');
  174. // 测试环境:组件会自动选中第一个残疾人并确认,只需等待对话框关闭
  175. console.log('等待残疾人选择器对话框自动关闭...');
  176. // 等待对话框消失(自动选择后会关闭)
  177. await dialog.waitFor({ state: 'hidden', timeout: TIMEOUTS.TABLE_LOAD });
  178. console.log('残疾人选择器对话框已关闭');
  179. // 等待一下让状态同步
  180. await page.waitForTimeout(TIMEOUTS.MEDIUM);
  181. return true;
  182. }
  183. // 全局计数器,确保每个测试生成唯一的数据
  184. let testDataCounter = 0;
  185. function generateUniqueTestData() {
  186. const timestamp = Date.now();
  187. const counter = ++testDataCounter;
  188. const random = Math.floor(Math.random() * 10000);
  189. // 生成18位身份证号:110101(地区码6位) + 19900101(出生日期8位) + XXX(顺序码3位) + X(校验码1位)
  190. // 使用计数器和随机数作为顺序码,确保唯一性
  191. const sequenceCode = String(counter).padStart(2, '0') + String(random).slice(0, 1);
  192. const idCard = '110101' + '19900101' + sequenceCode + '1'; // 6+8+3+1=18位
  193. return {
  194. orderName: '测试订单_' + timestamp + '_' + counter + '_' + random,
  195. personName: '测试残疾人_' + timestamp + '_' + counter + '_' + random,
  196. // 18位身份证号
  197. idCard,
  198. phone: '138' + String(counter).padStart(4, '0') + String(random).padStart(4, '0'),
  199. gender: '男',
  200. disabilityType: '视力残疾',
  201. disabilityLevel: '一级',
  202. disabilityId: '残疾证' + sequenceCode + String(timestamp).slice(-6),
  203. idAddress: '北京市东城区测试地址' + timestamp + '_' + counter,
  204. province: '北京市',
  205. city: '北京市',
  206. hireDate: '2025-01-15',
  207. salary: 5000,
  208. };
  209. }
  210. // 等待订单行出现在表格中
  211. async function waitForOrderRow(page: Page, orderName: string, timeout = 15000) {
  212. const startTime = Date.now();
  213. while (Date.now() - startTime < timeout) {
  214. const table = page.locator('table');
  215. const orderRow = table.locator('tbody tr').filter({ hasText: orderName });
  216. const count = await orderRow.count();
  217. if (count > 0) {
  218. console.debug('找到订单行:', orderName);
  219. return true;
  220. }
  221. await page.waitForTimeout(TIMEOUTS.MEDIUM);
  222. }
  223. console.debug('等待订单行超时:', orderName);
  224. return false;
  225. }
  226. test.describe('订单人员关联测试', () => {
  227. test.beforeAll(async ({}) => {
  228. // 注意:beforeAll 中无法访问 page,所以通过 playwright.config.ts 的 initScripts 设置
  229. // 这里只是文档说明
  230. });
  231. test.beforeEach(async ({ adminLoginPage, orderManagementPage, request, page }) => {
  232. // 登录
  233. await adminLoginPage.goto();
  234. await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
  235. await adminLoginPage.expectLoginSuccess();
  236. await orderManagementPage.goto();
  237. // 使用 API 创建平台和公司测试数据
  238. const createdPlatform = await createPlatformViaAPI(request);
  239. if (!createdPlatform) {
  240. console.debug('无法创建平台数据,测试可能被跳过');
  241. createdPlatformName = null;
  242. } else {
  243. createdPlatformName = createdPlatform.name;
  244. }
  245. if (createdPlatform) {
  246. const createdCompany = await createCompanyViaAPI(request, createdPlatform.id);
  247. if (!createdCompany) {
  248. console.debug('无法创建公司数据,测试可能被跳过');
  249. createdCompanyName = null;
  250. } else {
  251. createdCompanyName = createdCompany.name;
  252. }
  253. }
  254. // 使用 API 创建残疾人测试数据
  255. const timestamp = Date.now();
  256. const random = Math.floor(Math.random() * 10000);
  257. const personName = `测试残疾人_${timestamp}_${random}`;
  258. const personData = {
  259. name: personName,
  260. gender: '男',
  261. idCard: `110101199001011${String(random).padStart(4, '0')}`,
  262. disabilityId: `CJZ${timestamp}${random}`,
  263. disabilityType: '视力残疾',
  264. disabilityLevel: '一级',
  265. idAddress: '北京市东城区测试地址',
  266. phone: `138${String(random).padStart(8, '0')}`,
  267. province: '北京市',
  268. city: '北京市'
  269. };
  270. const createdPerson = await createDisabledPersonViaAPI(request, personData);
  271. if (!createdPerson) {
  272. console.debug('无法创建残疾人数据,测试可能被跳过');
  273. createdPersonName = null;
  274. } else {
  275. createdPersonName = createdPerson.name;
  276. console.debug('已创建残疾人:', createdPersonName, 'ID:', createdPerson.id);
  277. }
  278. });
  279. test.describe('添加人员到订单', () => {
  280. test('应该能打开订单人员管理对话框', async ({ orderManagementPage }) => {
  281. if (!createdPersonName || !createdPlatformName || !createdCompanyName) {
  282. test.skip(true, '缺少测试数据(残疾人、平台或公司)');
  283. return;
  284. }
  285. const testData = generateUniqueTestData();
  286. await orderManagementPage.openCreateDialog();
  287. await orderManagementPage.page.getByLabel(/订单名称|名称/).fill(testData.orderName);
  288. // 选择平台
  289. const platformTrigger = orderManagementPage.page.locator('[data-testid="platform-selector-create"]');
  290. if (await platformTrigger.count() > 0) {
  291. await platformTrigger.click();
  292. await orderManagementPage.page.waitForTimeout(TIMEOUTS.MEDIUM_LONG);
  293. const allOptions = orderManagementPage.page.getByRole('option');
  294. const count = await allOptions.count();
  295. console.debug(`平台选项数量: ${count}`);
  296. if (count > 0) {
  297. const platformOption = allOptions.filter({ hasText: createdPlatformName }).first();
  298. const optionCount = await platformOption.count();
  299. if (optionCount > 0) {
  300. await platformOption.click();
  301. } else {
  302. console.debug(`未找到平台 ${createdPlatformName},选择第一个可用平台`);
  303. await allOptions.first().click();
  304. }
  305. } else {
  306. console.debug('平台选项列表为空');
  307. }
  308. await orderManagementPage.page.waitForTimeout(TIMEOUTS.VERY_SHORT);
  309. } else {
  310. console.debug('平台选择器未找到,跳过平台选择');
  311. }
  312. // 选择公司
  313. const companyTrigger = orderManagementPage.page.locator('[data-testid="company-selector-create"]');
  314. if (await companyTrigger.count() > 0) {
  315. await companyTrigger.click();
  316. await orderManagementPage.page.waitForTimeout(TIMEOUTS.MEDIUM_LONG);
  317. const allCompanyOptions = orderManagementPage.page.getByRole('option');
  318. const companyCount = await allCompanyOptions.count();
  319. console.debug(`公司选项数量: ${companyCount}`);
  320. if (companyCount > 0) {
  321. const companyOption = allCompanyOptions.filter({ hasText: createdCompanyName }).first();
  322. const optionCount = await companyOption.count();
  323. if (optionCount > 0) {
  324. await companyOption.click();
  325. } else {
  326. console.debug(`未找到公司 ${createdCompanyName},选择第一个可用公司`);
  327. await allCompanyOptions.first().click();
  328. }
  329. } else {
  330. console.debug('公司选项列表为空');
  331. }
  332. await orderManagementPage.page.waitForTimeout(TIMEOUTS.VERY_SHORT);
  333. } else {
  334. console.debug('公司选择器未找到,跳过公司选择');
  335. }
  336. await orderManagementPage.page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  337. const hasPerson = await selectDisabledPersonInAddDialog(orderManagementPage.page, createdPersonName);
  338. if (!hasPerson) {
  339. await orderManagementPage.cancelDialog();
  340. test.skip(true, '没有可用的残疾人数据');
  341. return;
  342. }
  343. // 等待残疾人选择对话框关闭,检查是否显示了已选人员
  344. // 状态更新是异步的,需要等待更长时间
  345. await orderManagementPage.page.waitForTimeout(TIMEOUTS.VERY_LONG);
  346. // 尝试多种方式定位徽章
  347. const selectedPersonsBadges = orderManagementPage.page.locator('[class*="badge"]').filter({ hasText: createdPersonName });
  348. const badgeCount = await selectedPersonsBadges.count();
  349. console.debug('已选人员徽章数量:', badgeCount);
  350. // 如果徽章数量为 0,尝试检查文本内容
  351. if (badgeCount === 0) {
  352. const allText = await orderManagementPage.page.locator('.w-full.overflow-y-auto').textContent();
  353. console.debug('对话框内容包含姓名:', allText?.includes(createdPersonName));
  354. }
  355. // 检查提交按钮是否存在且可点击
  356. const submitButton = orderManagementPage.page.getByRole('button', { name: /^(创建|更新|保存)$/ });
  357. const submitButtonCount = await submitButton.count();
  358. console.debug('提交按钮数量:', submitButtonCount);
  359. if (submitButtonCount === 0) {
  360. console.debug('提交按钮未找到,尝试查找所有按钮');
  361. const allButtons = orderManagementPage.page.locator('button').all();
  362. console.debug('页面按钮总数:', await orderManagementPage.page.locator('button').count());
  363. }
  364. await orderManagementPage.submitForm();
  365. await orderManagementPage.waitForDialogClosed();
  366. // 检查是否有错误或成功 Toast
  367. await orderManagementPage.page.waitForTimeout(TIMEOUTS.LONG);
  368. const errorToast = orderManagementPage.page.locator('[data-sonner-toast][data-type="error"]');
  369. const successToast = orderManagementPage.page.locator('[data-sonner-toast][data-type="success"]');
  370. const hasError = await errorToast.count() > 0;
  371. const hasSuccess = await successToast.count() > 0;
  372. if (hasError) {
  373. const errorMsg = await errorToast.first().textContent();
  374. console.debug('表单提交错误:', errorMsg);
  375. test.skip(true, '订单创建失败: ' + errorMsg);
  376. return;
  377. }
  378. if (!hasSuccess) {
  379. console.debug('没有成功 Toast,订单可能未创建');
  380. } else {
  381. const successMsg = await successToast.first().textContent();
  382. console.debug('订单创建成功:', successMsg);
  383. }
  384. // 等待订单行出现在表格中
  385. const orderFound = await waitForOrderRow(orderManagementPage.page, testData.orderName);
  386. if (!orderFound) {
  387. test.skip(true, '订单未创建成功,无法继续测试');
  388. return;
  389. }
  390. // 第一个测试只验证订单创建成功,人员管理功能在其他测试中验证
  391. // 订单列表中没有"人员"按钮,需要先打开订单详情
  392. });
  393. test('应该能添加残疾人到订单', async ({ orderManagementPage, page }) => {
  394. if (!createdPersonName || !createdPlatformName || !createdCompanyName) {
  395. test.skip(true, '缺少测试数据(残疾人、平台或公司)');
  396. return;
  397. }
  398. const testData = generateUniqueTestData();
  399. await orderManagementPage.openCreateDialog();
  400. await page.getByLabel(/订单名称|名称/).fill(testData.orderName);
  401. // 选择平台
  402. const platformTrigger = page.locator('[data-testid="platform-selector-create"]');
  403. if (await platformTrigger.count() > 0) {
  404. await platformTrigger.click();
  405. // 等待选项列表加载,可能需要时间因为新创建的数据需要刷新
  406. await page.waitForTimeout(TIMEOUTS.MEDIUM_LONG);
  407. // 使用更宽松的选择方式 - 先查找所有选项,再筛选
  408. const allOptions = page.getByRole('option');
  409. const count = await allOptions.count();
  410. console.debug(`平台选项数量: ${count}`);
  411. if (count > 0) {
  412. const platformOption = allOptions.filter({ hasText: createdPlatformName }).first();
  413. const optionCount = await platformOption.count();
  414. if (optionCount > 0) {
  415. await platformOption.click();
  416. } else {
  417. // 如果找不到特定平台,选择第一个可用的
  418. console.debug(`未找到平台 ${createdPlatformName},选择第一个可用平台`);
  419. await allOptions.first().click();
  420. }
  421. } else {
  422. console.debug('平台选项列表为空');
  423. }
  424. await page.waitForTimeout(TIMEOUTS.VERY_SHORT);
  425. } else {
  426. console.debug('平台选择器未找到,跳过平台选择');
  427. }
  428. // 选择公司
  429. const companyTrigger = page.locator('[data-testid="company-selector-create"]');
  430. if (await companyTrigger.count() > 0) {
  431. await companyTrigger.click();
  432. await page.waitForTimeout(TIMEOUTS.MEDIUM_LONG);
  433. const allCompanyOptions = page.getByRole('option');
  434. const companyCount = await allCompanyOptions.count();
  435. console.debug(`公司选项数量: ${companyCount}`);
  436. if (companyCount > 0) {
  437. const companyOption = allCompanyOptions.filter({ hasText: createdCompanyName }).first();
  438. const optionCount = await companyOption.count();
  439. if (optionCount > 0) {
  440. await companyOption.click();
  441. } else {
  442. // 如果找不到特定公司,选择第一个可用的
  443. console.debug(`未找到公司 ${createdCompanyName},选择第一个可用公司`);
  444. await allCompanyOptions.first().click();
  445. }
  446. } else {
  447. console.debug('公司选项列表为空');
  448. }
  449. await page.waitForTimeout(TIMEOUTS.VERY_SHORT);
  450. } else {
  451. console.debug('公司选择器未找到,跳过公司选择');
  452. }
  453. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  454. const hasPerson = await selectDisabledPersonInAddDialog(page, createdPersonName);
  455. if (!hasPerson) {
  456. await orderManagementPage.cancelDialog();
  457. test.skip(true, '没有可用的残疾人数据');
  458. return;
  459. }
  460. await orderManagementPage.submitForm();
  461. await orderManagementPage.waitForDialogClosed();
  462. await orderManagementPage.openPersonManagementDialog(testData.orderName);
  463. // 使用 first() 因为有两个"添加人员"按钮(卡片中+底部)
  464. const addButton = page.getByRole('button', { name: /添加人员|新增人员/ }).first();
  465. await addButton.click();
  466. await page.waitForTimeout(TIMEOUTS.SHORT);
  467. // 等待残疾人选择对话框打开
  468. const dialog = page.getByTestId('disabled-person-selector-dialog');
  469. await dialog.waitFor({ state: 'visible', timeout: TIMEOUTS.DIALOG });
  470. // 等待自动选择完成(通过检查待添加人员数量)
  471. // 使用 test-id 检查待添加人员列表
  472. const pendingPersonsDebug = page.getByTestId('pending-persons-debug');
  473. await pendingPersonsDebug.waitFor({ state: 'attached', timeout: TIMEOUTS.TABLE_LOAD });
  474. // 检查待添加人员数量
  475. const pendingData = await pendingPersonsDebug.textContent();
  476. console.log('待添加人员数据:', pendingData);
  477. // 关闭残疾人选择对话框
  478. await page.keyboard.press('Escape');
  479. await dialog.waitFor({ state: 'hidden', timeout: TIMEOUTS.DIALOG });
  480. // 点击"确认添加"按钮批量添加人员
  481. const confirmButton = page.getByTestId('confirm-add-persons-button');
  482. await confirmButton.click();
  483. // 等待成功 toast
  484. await page.waitForTimeout(TIMEOUTS.LONG);
  485. const successToast = page.locator('[data-sonner-toast][data-type="success"]');
  486. const hasSuccess = await successToast.count() > 0;
  487. expect(hasSuccess).toBe(true);
  488. await orderManagementPage.closeDetailDialog();
  489. });
  490. test('添加的人员应该出现在订单详情中', async ({ orderManagementPage, page }) => {
  491. if (!createdPersonName || !createdPlatformName || !createdCompanyName) {
  492. test.skip(true, '缺少测试数据(残疾人、平台或公司)');
  493. return;
  494. }
  495. const testData = generateUniqueTestData();
  496. await orderManagementPage.openCreateDialog();
  497. await page.getByLabel(/订单名称|名称/).fill(testData.orderName);
  498. // 选择平台
  499. const platformTrigger = page.locator('[data-testid="platform-selector-create"]');
  500. if (await platformTrigger.count() > 0) {
  501. await platformTrigger.click();
  502. // 等待选项列表加载,可能需要时间因为新创建的数据需要刷新
  503. await page.waitForTimeout(TIMEOUTS.MEDIUM_LONG);
  504. // 使用更宽松的选择方式 - 先查找所有选项,再筛选
  505. const allOptions = page.getByRole('option');
  506. const count = await allOptions.count();
  507. console.debug(`平台选项数量: ${count}`);
  508. if (count > 0) {
  509. const platformOption = allOptions.filter({ hasText: createdPlatformName }).first();
  510. const optionCount = await platformOption.count();
  511. if (optionCount > 0) {
  512. await platformOption.click();
  513. } else {
  514. // 如果找不到特定平台,选择第一个可用的
  515. console.debug(`未找到平台 ${createdPlatformName},选择第一个可用平台`);
  516. await allOptions.first().click();
  517. }
  518. } else {
  519. console.debug('平台选项列表为空');
  520. }
  521. await page.waitForTimeout(TIMEOUTS.VERY_SHORT);
  522. } else {
  523. console.debug('平台选择器未找到,跳过平台选择');
  524. }
  525. // 选择公司
  526. const companyTrigger = page.locator('[data-testid="company-selector-create"]');
  527. if (await companyTrigger.count() > 0) {
  528. await companyTrigger.click();
  529. await page.waitForTimeout(TIMEOUTS.MEDIUM_LONG);
  530. const allCompanyOptions = page.getByRole('option');
  531. const companyCount = await allCompanyOptions.count();
  532. console.debug(`公司选项数量: ${companyCount}`);
  533. if (companyCount > 0) {
  534. const companyOption = allCompanyOptions.filter({ hasText: createdCompanyName }).first();
  535. const optionCount = await companyOption.count();
  536. if (optionCount > 0) {
  537. await companyOption.click();
  538. } else {
  539. // 如果找不到特定公司,选择第一个可用的
  540. console.debug(`未找到公司 ${createdCompanyName},选择第一个可用公司`);
  541. await allCompanyOptions.first().click();
  542. }
  543. } else {
  544. console.debug('公司选项列表为空');
  545. }
  546. await page.waitForTimeout(TIMEOUTS.VERY_SHORT);
  547. } else {
  548. console.debug('公司选择器未找到,跳过公司选择');
  549. }
  550. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  551. const hasPerson = await selectDisabledPersonInAddDialog(page, createdPersonName);
  552. if (!hasPerson) {
  553. await orderManagementPage.cancelDialog();
  554. test.skip(true, '没有可用的残疾人数据');
  555. return;
  556. }
  557. await orderManagementPage.submitForm();
  558. await orderManagementPage.waitForDialogClosed();
  559. await orderManagementPage.openDetailDialog(testData.orderName);
  560. const personList = await orderManagementPage.getPersonListFromDetail();
  561. expect(personList.length).toBeGreaterThan(0);
  562. await orderManagementPage.closeDetailDialog();
  563. });
  564. });
  565. test.describe('管理工作状态', () => {
  566. test('应该能修改人员工作状态:未就业 → 待就业', async ({ orderManagementPage, page, request }) => {
  567. if (!createdPlatformName || !createdCompanyName) {
  568. test.skip(true, '缺少测试数据(平台或公司)');
  569. return;
  570. }
  571. // 为此测试创建唯一的残疾人数据
  572. const testData = generateUniqueTestData();
  573. const personData = {
  574. name: testData.personName,
  575. gender: testData.gender,
  576. idCard: testData.idCard,
  577. disabilityId: testData.disabilityId,
  578. disabilityType: testData.disabilityType,
  579. disabilityLevel: testData.disabilityLevel,
  580. idAddress: testData.idAddress,
  581. phone: testData.phone,
  582. province: testData.province,
  583. city: testData.city,
  584. };
  585. const createdPerson = await createDisabledPersonViaAPI(request, personData);
  586. if (!createdPerson) {
  587. test.skip(true, '无法创建残疾人数据');
  588. return;
  589. }
  590. console.debug('已创建残疾人:', createdPerson.name, 'ID:', createdPerson.id);
  591. await orderManagementPage.openCreateDialog();
  592. await page.getByLabel(/订单名称|名称/).fill(testData.orderName);
  593. // 选择平台
  594. const platformTrigger = page.locator('[data-testid="platform-selector-create"]');
  595. if (await platformTrigger.count() > 0) {
  596. await platformTrigger.click();
  597. // 等待选项列表加载,可能需要时间因为新创建的数据需要刷新
  598. await page.waitForTimeout(TIMEOUTS.MEDIUM_LONG);
  599. // 使用更宽松的选择方式 - 先查找所有选项,再筛选
  600. const allOptions = page.getByRole('option');
  601. const count = await allOptions.count();
  602. console.debug(`平台选项数量: ${count}`);
  603. if (count > 0) {
  604. const platformOption = allOptions.filter({ hasText: createdPlatformName }).first();
  605. const optionCount = await platformOption.count();
  606. if (optionCount > 0) {
  607. await platformOption.click();
  608. } else {
  609. // 如果找不到特定平台,选择第一个可用的
  610. console.debug(`未找到平台 ${createdPlatformName},选择第一个可用平台`);
  611. await allOptions.first().click();
  612. }
  613. } else {
  614. console.debug('平台选项列表为空');
  615. }
  616. await page.waitForTimeout(TIMEOUTS.VERY_SHORT);
  617. } else {
  618. console.debug('平台选择器未找到,跳过平台选择');
  619. }
  620. // 选择公司
  621. const companyTrigger = page.locator('[data-testid="company-selector-create"]');
  622. if (await companyTrigger.count() > 0) {
  623. await companyTrigger.click();
  624. await page.waitForTimeout(TIMEOUTS.MEDIUM_LONG);
  625. const allCompanyOptions = page.getByRole('option');
  626. const companyCount = await allCompanyOptions.count();
  627. console.debug(`公司选项数量: ${companyCount}`);
  628. if (companyCount > 0) {
  629. const companyOption = allCompanyOptions.filter({ hasText: createdCompanyName }).first();
  630. const optionCount = await companyOption.count();
  631. if (optionCount > 0) {
  632. await companyOption.click();
  633. } else {
  634. // 如果找不到特定公司,选择第一个可用的
  635. console.debug(`未找到公司 ${createdCompanyName},选择第一个可用公司`);
  636. await allCompanyOptions.first().click();
  637. }
  638. } else {
  639. console.debug('公司选项列表为空');
  640. }
  641. await page.waitForTimeout(TIMEOUTS.VERY_SHORT);
  642. } else {
  643. console.debug('公司选择器未找到,跳过公司选择');
  644. }
  645. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  646. const hasPerson = await selectDisabledPersonInAddDialog(page, createdPerson.name);
  647. if (!hasPerson) {
  648. await orderManagementPage.cancelDialog();
  649. test.skip(true, '没有可用的残疾人数据');
  650. return;
  651. }
  652. await orderManagementPage.submitForm();
  653. await orderManagementPage.waitForDialogClosed();
  654. await orderManagementPage.openPersonManagementDialog(testData.orderName);
  655. // 等待订单详情对话框加载完成
  656. await page.waitForTimeout(TIMEOUTS.MEDIUM);
  657. // 监听网络响应以捕获 400 错误的详细信息
  658. const apiResponses: any[] = [];
  659. page.on('response', async (response) => {
  660. if (response.status() === 400) {
  661. const url = response.url();
  662. const contentType = response.headers()['content-type'];
  663. if (contentType && contentType.includes('application/json')) {
  664. try {
  665. const body = await response.json();
  666. apiResponses.push({ url, status: response.status(), body });
  667. console.debug('API 400 错误详情:', JSON.stringify(body, null, 2));
  668. } catch (e) {
  669. const text = await response.text();
  670. apiResponses.push({ url, status: response.status(), body: text });
  671. console.debug('API 400 错误详情:', text);
  672. }
  673. }
  674. }
  675. });
  676. // 获取实际绑定的人员列表,使用第一个人员的名称
  677. const personList = await orderManagementPage.getPersonListFromDetail();
  678. await orderManagementPage.updatePersonWorkStatus(personList[0].name, 'pre_working');
  679. // 如果有 400 错误,打印详细信息
  680. if (apiResponses.length > 0) {
  681. console.debug('捕获到的 API 错误:', JSON.stringify(apiResponses, null, 2));
  682. }
  683. const successToast = page.locator('[data-sonner-toast][data-type="success"]');
  684. const hasSuccess = await successToast.count() > 0;
  685. expect(hasSuccess).toBe(true);
  686. await orderManagementPage.closeDetailDialog();
  687. });
  688. test('应该能修改人员工作状态:待就业 → 已就业', async ({ orderManagementPage, page, request }) => {
  689. if (!createdPlatformName || !createdCompanyName) {
  690. test.skip(true, '缺少测试数据(平台或公司)');
  691. return;
  692. }
  693. // 为此测试创建唯一的残疾人数据
  694. const testData = generateUniqueTestData();
  695. const personData = {
  696. name: testData.personName,
  697. gender: testData.gender,
  698. idCard: testData.idCard,
  699. disabilityId: testData.disabilityId,
  700. disabilityType: testData.disabilityType,
  701. disabilityLevel: testData.disabilityLevel,
  702. idAddress: testData.idAddress,
  703. phone: testData.phone,
  704. province: testData.province,
  705. city: testData.city,
  706. };
  707. const createdPerson = await createDisabledPersonViaAPI(request, personData);
  708. if (!createdPerson) {
  709. test.skip(true, '无法创建残疾人数据');
  710. return;
  711. }
  712. console.debug('已创建残疾人:', createdPerson.name, 'ID:', createdPerson.id);
  713. await orderManagementPage.openCreateDialog();
  714. await page.getByLabel(/订单名称|名称/).fill(testData.orderName);
  715. // 选择平台
  716. const platformTrigger = page.locator('[data-testid="platform-selector-create"]');
  717. if (await platformTrigger.count() > 0) {
  718. await platformTrigger.click();
  719. // 等待选项列表加载,可能需要时间因为新创建的数据需要刷新
  720. await page.waitForTimeout(TIMEOUTS.MEDIUM_LONG);
  721. // 使用更宽松的选择方式 - 先查找所有选项,再筛选
  722. const allOptions = page.getByRole('option');
  723. const count = await allOptions.count();
  724. console.debug(`平台选项数量: ${count}`);
  725. if (count > 0) {
  726. const platformOption = allOptions.filter({ hasText: createdPlatformName }).first();
  727. const optionCount = await platformOption.count();
  728. if (optionCount > 0) {
  729. await platformOption.click();
  730. } else {
  731. // 如果找不到特定平台,选择第一个可用的
  732. console.debug(`未找到平台 ${createdPlatformName},选择第一个可用平台`);
  733. await allOptions.first().click();
  734. }
  735. } else {
  736. console.debug('平台选项列表为空');
  737. }
  738. await page.waitForTimeout(TIMEOUTS.VERY_SHORT);
  739. } else {
  740. console.debug('平台选择器未找到,跳过平台选择');
  741. }
  742. // 选择公司
  743. const companyTrigger = page.locator('[data-testid="company-selector-create"]');
  744. if (await companyTrigger.count() > 0) {
  745. await companyTrigger.click();
  746. await page.waitForTimeout(TIMEOUTS.MEDIUM_LONG);
  747. const allCompanyOptions = page.getByRole('option');
  748. const companyCount = await allCompanyOptions.count();
  749. console.debug(`公司选项数量: ${companyCount}`);
  750. if (companyCount > 0) {
  751. const companyOption = allCompanyOptions.filter({ hasText: createdCompanyName }).first();
  752. const optionCount = await companyOption.count();
  753. if (optionCount > 0) {
  754. await companyOption.click();
  755. } else {
  756. // 如果找不到特定公司,选择第一个可用的
  757. console.debug(`未找到公司 ${createdCompanyName},选择第一个可用公司`);
  758. await allCompanyOptions.first().click();
  759. }
  760. } else {
  761. console.debug('公司选项列表为空');
  762. }
  763. await page.waitForTimeout(TIMEOUTS.VERY_SHORT);
  764. } else {
  765. console.debug('公司选择器未找到,跳过公司选择');
  766. }
  767. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  768. const hasPerson = await selectDisabledPersonInAddDialog(page, createdPerson.name);
  769. if (!hasPerson) {
  770. await orderManagementPage.cancelDialog();
  771. test.skip(true, '没有可用的残疾人数据');
  772. return;
  773. }
  774. await orderManagementPage.submitForm();
  775. await orderManagementPage.waitForDialogClosed();
  776. await orderManagementPage.openPersonManagementDialog(testData.orderName);
  777. const personList = await orderManagementPage.getPersonListFromDetail();
  778. await orderManagementPage.updatePersonWorkStatus(personList[0].name, 'working');
  779. const successToast = page.locator('[data-sonner-toast][data-type="success"]');
  780. const hasSuccess = await successToast.count() > 0;
  781. expect(hasSuccess).toBe(true);
  782. await orderManagementPage.closeDetailDialog();
  783. });
  784. test('应该能修改人员工作状态:已就业 → 已离职', async ({ orderManagementPage, page, request }) => {
  785. if (!createdPlatformName || !createdCompanyName) {
  786. test.skip(true, '缺少测试数据(平台或公司)');
  787. return;
  788. }
  789. // 为此测试创建唯一的残疾人数据
  790. const testData = generateUniqueTestData();
  791. const personData = {
  792. name: testData.personName,
  793. gender: testData.gender,
  794. idCard: testData.idCard,
  795. disabilityId: testData.disabilityId,
  796. disabilityType: testData.disabilityType,
  797. disabilityLevel: testData.disabilityLevel,
  798. idAddress: testData.idAddress,
  799. phone: testData.phone,
  800. province: testData.province,
  801. city: testData.city,
  802. };
  803. const createdPerson = await createDisabledPersonViaAPI(request, personData);
  804. if (!createdPerson) {
  805. test.skip(true, '无法创建残疾人数据');
  806. return;
  807. }
  808. console.debug('已创建残疾人:', createdPerson.name, 'ID:', createdPerson.id);
  809. await orderManagementPage.openCreateDialog();
  810. await page.getByLabel(/订单名称|名称/).fill(testData.orderName);
  811. // 选择平台
  812. const platformTrigger = page.locator('[data-testid="platform-selector-create"]');
  813. if (await platformTrigger.count() > 0) {
  814. await platformTrigger.click();
  815. // 等待选项列表加载,可能需要时间因为新创建的数据需要刷新
  816. await page.waitForTimeout(TIMEOUTS.MEDIUM_LONG);
  817. // 使用更宽松的选择方式 - 先查找所有选项,再筛选
  818. const allOptions = page.getByRole('option');
  819. const count = await allOptions.count();
  820. console.debug(`平台选项数量: ${count}`);
  821. if (count > 0) {
  822. const platformOption = allOptions.filter({ hasText: createdPlatformName }).first();
  823. const optionCount = await platformOption.count();
  824. if (optionCount > 0) {
  825. await platformOption.click();
  826. } else {
  827. // 如果找不到特定平台,选择第一个可用的
  828. console.debug(`未找到平台 ${createdPlatformName},选择第一个可用平台`);
  829. await allOptions.first().click();
  830. }
  831. } else {
  832. console.debug('平台选项列表为空');
  833. }
  834. await page.waitForTimeout(TIMEOUTS.VERY_SHORT);
  835. } else {
  836. console.debug('平台选择器未找到,跳过平台选择');
  837. }
  838. // 选择公司
  839. const companyTrigger = page.locator('[data-testid="company-selector-create"]');
  840. if (await companyTrigger.count() > 0) {
  841. await companyTrigger.click();
  842. await page.waitForTimeout(TIMEOUTS.MEDIUM_LONG);
  843. const allCompanyOptions = page.getByRole('option');
  844. const companyCount = await allCompanyOptions.count();
  845. console.debug(`公司选项数量: ${companyCount}`);
  846. if (companyCount > 0) {
  847. const companyOption = allCompanyOptions.filter({ hasText: createdCompanyName }).first();
  848. const optionCount = await companyOption.count();
  849. if (optionCount > 0) {
  850. await companyOption.click();
  851. } else {
  852. // 如果找不到特定公司,选择第一个可用的
  853. console.debug(`未找到公司 ${createdCompanyName},选择第一个可用公司`);
  854. await allCompanyOptions.first().click();
  855. }
  856. } else {
  857. console.debug('公司选项列表为空');
  858. }
  859. await page.waitForTimeout(TIMEOUTS.VERY_SHORT);
  860. } else {
  861. console.debug('公司选择器未找到,跳过公司选择');
  862. }
  863. await page.getByLabel(/预计开始日期|开始日期/).fill('2025-01-15');
  864. const hasPerson = await selectDisabledPersonInAddDialog(page, createdPerson.name);
  865. if (!hasPerson) {
  866. await orderManagementPage.cancelDialog();
  867. test.skip(true, '没有可用的残疾人数据');
  868. return;
  869. }
  870. await orderManagementPage.submitForm();
  871. await orderManagementPage.waitForDialogClosed();
  872. await orderManagementPage.openPersonManagementDialog(testData.orderName);
  873. const personList = await orderManagementPage.getPersonListFromDetail();
  874. await orderManagementPage.updatePersonWorkStatus(personList[0].name, 'resigned');
  875. const successToast = page.locator('[data-sonner-toast][data-type="success"]');
  876. const hasSuccess = await successToast.count() > 0;
  877. expect(hasSuccess).toBe(true);
  878. await orderManagementPage.closeDetailDialog();
  879. });
  880. });
  881. });