disability-person.page.ts 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. import { Page, Locator } from '@playwright/test';
  2. import { selectRadixOption, selectProvinceCity } from '@d8d/e2e-test-utils';
  3. // 超时配置常量
  4. const TIMEOUTS = {
  5. SHORT: 300,
  6. MEDIUM: 500,
  7. LONG: 1000,
  8. VERY_SHORT: 200,
  9. } as const;
  10. // 注意:@d8d/e2e-test-utils 包已安装,将在后续 story (2.2, 2.3) 中实际使用
  11. export class DisabilityPersonManagementPage {
  12. readonly page: Page;
  13. readonly pageTitle: Locator;
  14. readonly addPersonButton: Locator;
  15. readonly keywordSearchInput: Locator;
  16. readonly searchButton: Locator;
  17. readonly personTable: Locator;
  18. constructor(page: Page) {
  19. this.page = page;
  20. this.pageTitle = page.getByText('残疾人个人管理');
  21. this.addPersonButton = page.getByRole('button', { name: '新增残疾人' });
  22. this.keywordSearchInput = page.getByPlaceholder('搜索姓名或身份证号');
  23. this.searchButton = page.getByRole('button', { name: '搜索' });
  24. this.personTable = page.locator('table');
  25. }
  26. async goto() {
  27. await this.page.goto('/admin/disabilities');
  28. await this.page.waitForLoadState('domcontentloaded');
  29. // 等待页面标题出现
  30. await this.pageTitle.waitFor({ state: 'visible', timeout: 15000 });
  31. // 等待表格数据加载
  32. await this.page.waitForSelector('table tbody tr', { state: 'visible', timeout: 20000 });
  33. await this.expectToBeVisible();
  34. }
  35. async expectToBeVisible() {
  36. await this.pageTitle.waitFor({ state: 'visible', timeout: 15000 });
  37. await this.addPersonButton.waitFor({ state: 'visible', timeout: 10000 });
  38. }
  39. async openCreateDialog() {
  40. // 监听网络请求
  41. const responses: any[] = [];
  42. this.page.on('response', async (response) => {
  43. if (response.url().includes('disabled-person') || response.url().includes('aggregated')) {
  44. try {
  45. const responseData = await response.json().catch(() => ({ status: response.status() }));
  46. responses.push({
  47. url: response.url(),
  48. status: response.status(),
  49. data: responseData
  50. });
  51. } catch (e) {
  52. responses.push({
  53. url: response.url(),
  54. status: response.status(),
  55. error: e
  56. });
  57. }
  58. }
  59. });
  60. await this.addPersonButton.click();
  61. await this.page.waitForSelector('[data-testid="create-disabled-person-dialog-title"]', { state: 'visible', timeout: 5000 });
  62. return responses;
  63. }
  64. async fillBasicForm(data: {
  65. name: string;
  66. gender: string;
  67. idCard: string;
  68. disabilityId: string;
  69. disabilityType: string;
  70. disabilityLevel: string;
  71. phone: string;
  72. idAddress: string;
  73. province: string;
  74. city: string;
  75. }) {
  76. // 等待表单出现
  77. const form = this.page.locator('form#create-form');
  78. await form.waitFor({ state: 'visible', timeout: 5000 });
  79. console.debug('开始填写表单...');
  80. // 填写基本信息 - 使用 form locator 确保只在对话框内查找
  81. await form.getByLabel('姓名 *').fill(data.name);
  82. console.debug('✓ 姓名已填写:', data.name);
  83. // 性别 - 使用 data-testid
  84. const genderTrigger = this.page.locator('[data-testid="gender-select"]');
  85. await genderTrigger.click();
  86. await this.page.getByRole('option', { name: data.gender }).click();
  87. console.debug('✓ 性别已选择:', data.gender);
  88. await form.getByLabel('身份证号 *').fill(data.idCard);
  89. console.debug('✓ 身份证号已填写');
  90. await form.getByLabel('残疾证号 *').fill(data.disabilityId);
  91. console.debug('✓ 残疾证号已填写');
  92. // 残疾类型 - 使用 data-testid
  93. const disabilityTypeTrigger = this.page.locator('[data-testid="disability-type-select"]');
  94. await disabilityTypeTrigger.scrollIntoViewIfNeeded();
  95. await this.page.waitForTimeout(200);
  96. await disabilityTypeTrigger.click();
  97. await this.page.getByRole('option', { name: data.disabilityType }).click();
  98. console.debug('✓ 残疾类型已选择:', data.disabilityType);
  99. // 残疾等级 - 使用 data-testid
  100. const disabilityLevelTrigger = this.page.locator('[data-testid="disability-level-select"]');
  101. await disabilityLevelTrigger.scrollIntoViewIfNeeded();
  102. await this.page.waitForTimeout(200);
  103. await disabilityLevelTrigger.click();
  104. await this.page.getByRole('option', { name: data.disabilityLevel }).click();
  105. console.debug('✓ 残疾等级已选择:', data.disabilityLevel);
  106. await form.getByLabel('联系电话 *').fill(data.phone);
  107. console.debug('✓ 联系电话已填写');
  108. await form.getByLabel('身份证地址 *').fill(data.idAddress);
  109. console.debug('✓ 身份证地址已填写');
  110. // 居住地址 - 省市区级联选择
  111. // AreaSelectForm 使用 data-testid="area-select-province" 等属性
  112. const provinceTrigger = this.page.locator('[data-testid="area-select-province"]');
  113. await provinceTrigger.scrollIntoViewIfNeeded();
  114. await provinceTrigger.click();
  115. await this.page.getByRole('option', { name: data.province }).click();
  116. console.debug('✓ 省份已选择:', data.province);
  117. const cityTrigger = this.page.locator('[data-testid="area-select-city"]');
  118. await cityTrigger.click();
  119. await this.page.getByRole('option', { name: data.city }).click();
  120. console.debug('✓ 城市已选择:', data.city);
  121. console.debug('表单填写完成');
  122. }
  123. async submitForm() {
  124. // 收集网络响应
  125. const responses: any[] = [];
  126. // 监听所有网络请求
  127. this.page.on('response', async (response) => {
  128. const url = response.url();
  129. if (url.includes('disabled-person') || url.includes('aggregated')) {
  130. const requestBody = response.request()?.postData();
  131. const responseBody = await response.text().catch(() => '');
  132. let jsonBody = null;
  133. try {
  134. jsonBody = JSON.parse(responseBody);
  135. } catch (e) {
  136. // 不是 JSON
  137. }
  138. responses.push({
  139. url,
  140. method: response.request()?.method(),
  141. status: response.status(),
  142. ok: response.ok(),
  143. requestHeaders: await response.allHeaders().catch(() => ({})),
  144. responseHeaders: await response.allHeaders().catch(() => ({})),
  145. requestBody: requestBody ? JSON.parse(requestBody) : null,
  146. responseBody: jsonBody || responseBody,
  147. statusText: response.statusText()
  148. });
  149. }
  150. });
  151. // 点击创建按钮
  152. const submitButton = this.page.getByRole('button', { name: '创建' });
  153. await submitButton.click();
  154. // 等待网络请求完成
  155. await this.page.waitForLoadState('networkidle', { timeout: 10000 });
  156. // 等待一段时间让 Toast 消息显示
  157. await this.page.waitForTimeout(2000);
  158. // 检查是否有错误提示
  159. const errorToast = this.page.locator('[data-sonner-toast][data-type="error"]');
  160. const successToast = this.page.locator('[data-sonner-toast][data-type="success"]');
  161. const hasError = await errorToast.count() > 0;
  162. const hasSuccess = await successToast.count() > 0;
  163. let errorMessage = null;
  164. let successMessage = null;
  165. if (hasError) {
  166. errorMessage = await errorToast.first().textContent();
  167. }
  168. if (hasSuccess) {
  169. successMessage = await successToast.first().textContent();
  170. }
  171. return {
  172. responses,
  173. hasError,
  174. hasSuccess,
  175. errorMessage,
  176. successMessage
  177. };
  178. }
  179. async searchByName(name: string) {
  180. await this.keywordSearchInput.fill(name);
  181. await this.searchButton.click();
  182. await this.page.waitForLoadState('networkidle');
  183. await this.page.waitForTimeout(1000);
  184. }
  185. async personExists(name: string): Promise<boolean> {
  186. const personRow = this.personTable.locator('tbody tr').filter({ hasText: name }).first();
  187. return (await personRow.count()) > 0;
  188. }
  189. /**
  190. * 上传照片
  191. * @param photoType 照片类型(身份证照片、残疾证照片、个人照片、其他照片)
  192. * @param fileName 文件名
  193. */
  194. async uploadPhoto(photoType: string, fileName: string) {
  195. // 找到对应照片类型的上传按钮区域
  196. const photoSection = this.page.locator('text=' + photoType).first();
  197. await photoSection.scrollIntoViewIfNeeded();
  198. // 查找该类型照片区域的"上传"按钮
  199. const uploadButton = photoSection.locator('xpath=ancestor::div[contains(@class, "space-y-")]').first()
  200. .getByRole('button', { name: /上传/ }).first();
  201. // 创建测试文件
  202. const fileInput = await uploadButton.evaluateHandle((el: any) => {
  203. const input = el.querySelector('input[type="file"]');
  204. return input;
  205. });
  206. // 使用临时文件上传
  207. const file = {
  208. name: fileName,
  209. mimeType: 'image/jpeg',
  210. buffer: Buffer.from('fake image content')
  211. };
  212. await fileInput.uploadFile(file as any);
  213. await this.page.waitForTimeout(500); // 等待上传处理
  214. console.debug(` ✓ 上传照片: ${photoType} - ${fileName}`);
  215. }
  216. /**
  217. * 添加银行卡(内联表单模式)
  218. * @param bankCard 银行卡信息
  219. */
  220. async addBankCard(bankCard: {
  221. bankName: string;
  222. subBankName: string;
  223. cardNumber: string;
  224. cardholderName: string;
  225. cardType?: string;
  226. isDefault?: boolean;
  227. }) {
  228. // 滚动到银行卡管理区域
  229. const bankCardLabel = this.page.getByText('银行卡管理');
  230. await bankCardLabel.scrollIntoViewIfNeeded();
  231. await this.page.waitForTimeout(TIMEOUTS.SHORT);
  232. // 获取当前银行卡数量
  233. const currentCardCount = await this.page.locator('[data-testid^="remove-bank-card-"]').count();
  234. // 点击"添加银行卡"按钮
  235. const addButton = this.page.locator('[data-testid="add-bank-card-button"]');
  236. await addButton.click();
  237. await this.page.waitForTimeout(500);
  238. // 等待新的银行卡卡片出现
  239. const newCardCount = await this.page.locator('[data-testid^="remove-bank-card-"]').count();
  240. if (newCardCount !== currentCardCount + 1) {
  241. throw new Error(`添加银行卡失败:预期 ${currentCardCount + 1} 张,实际 ${newCardCount} 张`);
  242. }
  243. // 新添加的银行卡索引
  244. const cardIndex = currentCardCount;
  245. // 1. 选择银行名称
  246. const bankSelectTrigger = this.page.locator(`[data-testid="bank-select-${cardIndex}"]`);
  247. await bankSelectTrigger.click();
  248. await this.page.waitForTimeout(TIMEOUTS.SHORT);
  249. await this.page.getByRole('option', { name: bankCard.bankName }).click();
  250. // 2. 填写发卡支行
  251. const subBankInput = this.page.locator(`[data-testid="sub-bank-name-input-${cardIndex}"]`);
  252. await subBankInput.fill(bankCard.subBankName);
  253. // 3. 填写银行卡号
  254. const cardNumberInput = this.page.locator(`[data-testid="card-number-input-${cardIndex}"]`);
  255. await cardNumberInput.fill(bankCard.cardNumber);
  256. // 4. 填写持卡人姓名
  257. const cardholderInput = this.page.locator(`[data-testid="cardholder-name-input-${cardIndex}"]`);
  258. await cardholderInput.fill(bankCard.cardholderName);
  259. // 5. 选择银行卡类型(可选)
  260. if (bankCard.cardType) {
  261. const cardTypeTrigger = this.page.locator(`[data-testid="card-type-select-${cardIndex}"]`);
  262. await cardTypeTrigger.click();
  263. await this.page.waitForTimeout(TIMEOUTS.SHORT);
  264. await this.page.getByRole('option', { name: bankCard.cardType }).click();
  265. }
  266. // 6. 设置默认银行卡(可选)
  267. if (bankCard.isDefault) {
  268. const defaultSwitch = this.page.locator(`[data-testid="default-card-switch-${cardIndex}"]`);
  269. const isChecked = await defaultSwitch.isChecked();
  270. if (!isChecked) {
  271. await defaultSwitch.click();
  272. }
  273. }
  274. console.debug(` ✓ 添加银行卡: ${bankCard.bankName} - ${bankCard.cardNumber}`);
  275. return cardIndex;
  276. }
  277. /**
  278. * 编辑指定索引的银行卡
  279. * @param cardIndex 银行卡索引(从0开始)
  280. * @param updatedData 更新的银行卡数据
  281. */
  282. async editBankCard(cardIndex: number, updatedData: {
  283. bankName?: string;
  284. subBankName?: string;
  285. cardNumber?: string;
  286. cardholderName?: string;
  287. cardType?: string;
  288. isDefault?: boolean;
  289. }) {
  290. console.debug(` 编辑银行卡 ${cardIndex}`);
  291. // 编辑银行名称
  292. if (updatedData.bankName !== undefined) {
  293. const bankSelectTrigger = this.page.locator(`[data-testid="bank-select-${cardIndex}"]`);
  294. await bankSelectTrigger.click();
  295. await this.page.waitForTimeout(TIMEOUTS.SHORT);
  296. await this.page.getByRole('option', { name: updatedData.bankName }).click();
  297. console.debug(` ✓ 更新银行名称: ${updatedData.bankName}`);
  298. }
  299. // 编辑发卡支行
  300. if (updatedData.subBankName !== undefined) {
  301. const subBankInput = this.page.locator(`[data-testid="sub-bank-name-input-${cardIndex}"]`);
  302. await subBankInput.clear();
  303. await subBankInput.fill(updatedData.subBankName);
  304. console.debug(` ✓ 更新发卡支行: ${updatedData.subBankName}`);
  305. }
  306. // 编辑银行卡号
  307. if (updatedData.cardNumber !== undefined) {
  308. const cardNumberInput = this.page.locator(`[data-testid="card-number-input-${cardIndex}"]`);
  309. await cardNumberInput.clear();
  310. await cardNumberInput.fill(updatedData.cardNumber);
  311. console.debug(` ✓ 更新银行卡号: ${updatedData.cardNumber}`);
  312. }
  313. // 编辑持卡人姓名
  314. if (updatedData.cardholderName !== undefined) {
  315. const cardholderInput = this.page.locator(`[data-testid="cardholder-name-input-${cardIndex}"]`);
  316. await cardholderInput.clear();
  317. await cardholderInput.fill(updatedData.cardholderName);
  318. console.debug(` ✓ 更新持卡人姓名: ${updatedData.cardholderName}`);
  319. }
  320. // 编辑银行卡类型
  321. if (updatedData.cardType !== undefined) {
  322. const cardTypeTrigger = this.page.locator(`[data-testid="card-type-select-${cardIndex}"]`);
  323. await cardTypeTrigger.click();
  324. await this.page.waitForTimeout(TIMEOUTS.SHORT);
  325. await this.page.getByRole('option', { name: updatedData.cardType }).click();
  326. console.debug(` ✓ 更新银行卡类型: ${updatedData.cardType}`);
  327. }
  328. // 设置默认银行卡
  329. if (updatedData.isDefault !== undefined) {
  330. const defaultSwitch = this.page.locator(`[data-testid="default-card-switch-${cardIndex}"]`);
  331. const isChecked = await defaultSwitch.isChecked();
  332. if (isChecked !== updatedData.isDefault) {
  333. await defaultSwitch.click();
  334. console.debug(` ✓ 设置默认银行卡: ${updatedData.isDefault}`);
  335. }
  336. }
  337. console.debug(` ✓ 银行卡 ${cardIndex} 编辑完成`);
  338. }
  339. /**
  340. * 删除指定索引的银行卡
  341. * @param cardIndex 银行卡索引(从0开始)
  342. */
  343. async deleteBankCard(cardIndex: number) {
  344. console.debug(` 删除银行卡 ${cardIndex}`);
  345. const removeButton = this.page.locator(`[data-testid="remove-bank-card-${cardIndex}"]`);
  346. await removeButton.click();
  347. await this.page.waitForTimeout(500);
  348. console.debug(` ✓ 银行卡 ${cardIndex} 已删除`);
  349. }
  350. /**
  351. * 获取银行卡列表
  352. * @returns 银行卡号数组
  353. */
  354. async getBankCardList(): Promise<string[]> {
  355. const cards: string[] = [];
  356. const cardCount = await this.page.locator('[data-testid^="remove-bank-card-"]').count();
  357. for (let i = 0; i < cardCount; i++) {
  358. const cardNumberInput = this.page.locator(`[data-testid="card-number-input-${i}"]`);
  359. const cardNumber = await cardNumberInput.inputValue();
  360. cards.push(cardNumber);
  361. }
  362. return cards;
  363. }
  364. /**
  365. * 获取默认银行卡的索引
  366. * @returns 默认银行卡的索引,如果没有则返回 null
  367. */
  368. async getDefaultBankCardIndex(): Promise<number | null> {
  369. const cardCount = await this.page.locator('[data-testid^="remove-bank-card-"]').count();
  370. for (let i = 0; i < cardCount; i++) {
  371. const defaultSwitch = this.page.locator(`[data-testid="default-card-switch-${i}"]`);
  372. const isChecked = await defaultSwitch.isChecked();
  373. if (isChecked) {
  374. return i;
  375. }
  376. }
  377. return null;
  378. }
  379. /**
  380. * 获取当前银行卡数量
  381. * @returns 银行卡数量
  382. */
  383. async getBankCardCount(): Promise<number> {
  384. return await this.page.locator('[data-testid^="remove-bank-card-"]').count();
  385. }
  386. /**
  387. * 检查添加银行卡按钮是否被禁用
  388. * @returns 是否禁用
  389. */
  390. async isAddBankCardButtonDisabled(): Promise<boolean> {
  391. const addButton = this.page.locator('[data-testid="add-bank-card-button"]');
  392. return await addButton.isDisabled();
  393. }
  394. /**
  395. * 添加回访记录(内联表单模式)
  396. * @param visit 回访信息
  397. * @returns 添加的回访记录索引
  398. */
  399. async addVisit(visit: {
  400. visitDate: string;
  401. visitType: string;
  402. visitContent: string;
  403. visitResult?: string;
  404. nextVisitDate?: string;
  405. }): Promise<number> {
  406. // 获取当前回访数量
  407. const currentVisitCount = await this.getVisitCount();
  408. // 点击"添加回访"按钮
  409. const addButton = this.page.locator('[data-testid="add-visit-button"]');
  410. await addButton.click();
  411. await this.page.waitForTimeout(TIMEOUTS.SHORT);
  412. // 等待新的回访卡片出现
  413. const newVisitCount = await this.getVisitCount();
  414. if (newVisitCount !== currentVisitCount + 1) {
  415. throw new Error(`添加回访失败:预期 ${currentVisitCount + 1} 条,实际 ${newVisitCount} 条`);
  416. }
  417. // 新添加的回访索引
  418. const visitIndex = currentVisitCount;
  419. // 1. 填写回访日期
  420. const dateInput = this.page.locator(`[data-testid="visit-date-input-${visitIndex}"]`);
  421. await dateInput.fill(visit.visitDate);
  422. // 2. 选择回访类型
  423. const typeSelectTrigger = this.page.locator(`[data-testid="visit-type-select-${visitIndex}"]`);
  424. await typeSelectTrigger.click();
  425. await this.page.waitForTimeout(TIMEOUTS.VERY_SHORT);
  426. await this.page.getByRole('option', { name: visit.visitType }).click();
  427. // 3. 填写回访内容
  428. const contentTextarea = this.page.locator(`[data-testid="visit-content-textarea-${visitIndex}"]`);
  429. await contentTextarea.fill(visit.visitContent);
  430. // 4. 填写回访结果(可选)
  431. if (visit.visitResult !== undefined) {
  432. const resultInput = this.page.locator(`[data-testid="visit-result-input-${visitIndex}"]`);
  433. await resultInput.fill(visit.visitResult);
  434. }
  435. // 5. 填写下一次回访日期(可选)
  436. if (visit.nextVisitDate !== undefined) {
  437. const nextDateInput = this.page.locator(`[data-testid="next-visit-date-input-${visitIndex}"]`);
  438. await nextDateInput.fill(visit.nextVisitDate);
  439. }
  440. console.debug(` ✓ 添加回访: ${visit.visitType} - ${visit.visitDate}`);
  441. return visitIndex;
  442. }
  443. /**
  444. * 编辑指定索引的回访记录
  445. * @param index 回访索引(从0开始)
  446. * @param updatedData 更新的回访数据
  447. */
  448. async editVisit(index: number, updatedData: {
  449. visitDate?: string;
  450. visitType?: string;
  451. visitContent?: string;
  452. visitResult?: string;
  453. nextVisitDate?: string;
  454. }): Promise<void> {
  455. console.debug(` 编辑回访 ${index}`);
  456. // 编辑回访日期
  457. if (updatedData.visitDate !== undefined) {
  458. const dateInput = this.page.locator(`[data-testid="visit-date-input-${index}"]`);
  459. await dateInput.clear();
  460. await dateInput.fill(updatedData.visitDate);
  461. console.debug(` ✓ 更新回访日期: ${updatedData.visitDate}`);
  462. }
  463. // 编辑回访类型
  464. if (updatedData.visitType !== undefined) {
  465. const typeSelectTrigger = this.page.locator(`[data-testid="visit-type-select-${index}"]`);
  466. await typeSelectTrigger.click();
  467. await this.page.waitForTimeout(TIMEOUTS.VERY_SHORT);
  468. await this.page.getByRole('option', { name: updatedData.visitType }).click();
  469. console.debug(` ✓ 更新回访类型: ${updatedData.visitType}`);
  470. }
  471. // 编辑回访内容
  472. if (updatedData.visitContent !== undefined) {
  473. const contentTextarea = this.page.locator(`[data-testid="visit-content-textarea-${index}"]`);
  474. await contentTextarea.clear();
  475. await contentTextarea.fill(updatedData.visitContent);
  476. console.debug(` ✓ 更新回访内容`);
  477. }
  478. // 编辑回访结果
  479. if (updatedData.visitResult !== undefined) {
  480. const resultInput = this.page.locator(`[data-testid="visit-result-input-${index}"]`);
  481. await resultInput.clear();
  482. await resultInput.fill(updatedData.visitResult);
  483. console.debug(` ✓ 更新回访结果`);
  484. }
  485. // 编辑下次回访日期
  486. if (updatedData.nextVisitDate !== undefined) {
  487. const nextDateInput = this.page.locator(`[data-testid="next-visit-date-input-${index}"]`);
  488. await nextDateInput.clear();
  489. await nextDateInput.fill(updatedData.nextVisitDate);
  490. console.debug(` ✓ 更新下次回访日期`);
  491. }
  492. console.debug(` ✓ 回访 ${index} 编辑完成`);
  493. }
  494. /**
  495. * 删除指定索引的回访记录
  496. * @param index 回访索引(从0开始)
  497. */
  498. async deleteVisit(index: number): Promise<void> {
  499. console.debug(` 删除回访 ${index}`);
  500. const removeButton = this.page.locator(`[data-testid="remove-visit-${index}"]`);
  501. await removeButton.click();
  502. await this.page.waitForTimeout(TIMEOUTS.SHORT);
  503. console.debug(` ✓ 回访 ${index} 已删除`);
  504. }
  505. /**
  506. * 获取回访记录列表
  507. * @returns 回访信息数组
  508. */
  509. async getVisitList(): Promise<Array<{
  510. visitDate: string;
  511. visitType: string;
  512. visitContent: string;
  513. visitResult?: string;
  514. nextVisitDate?: string;
  515. }>> {
  516. const visits: Array<{
  517. visitDate: string;
  518. visitType: string;
  519. visitContent: string;
  520. visitResult?: string;
  521. nextVisitDate?: string;
  522. }> = [];
  523. const visitCount = await this.getVisitCount();
  524. for (let i = 0; i < visitCount; i++) {
  525. // 获取回访日期
  526. const dateInput = this.page.locator(`[data-testid="visit-date-input-${i}"]`);
  527. const visitDate = await dateInput.inputValue();
  528. // 获取回访类型
  529. const typeSelectTrigger = this.page.locator(`[data-testid="visit-type-select-${i}"]`);
  530. const visitType = await typeSelectTrigger.textContent() || '';
  531. // 获取回访内容
  532. const contentTextarea = this.page.locator(`[data-testid="visit-content-textarea-${i}"]`);
  533. const visitContent = await contentTextarea.inputValue();
  534. // 获取回访结果
  535. const resultInput = this.page.locator(`[data-testid="visit-result-input-${i}"]`);
  536. const visitResult = await resultInput.inputValue().catch(() => '');
  537. // 获取下次回访日期
  538. const nextDateInput = this.page.locator(`[data-testid="next-visit-date-input-${i}"]`);
  539. const nextVisitDate = await nextDateInput.inputValue().catch(() => '');
  540. visits.push({
  541. visitDate,
  542. visitType: visitType.trim(),
  543. visitContent,
  544. visitResult: visitResult || undefined,
  545. nextVisitDate: nextVisitDate || undefined,
  546. });
  547. }
  548. return visits;
  549. }
  550. /**
  551. * 获取当前回访记录数量
  552. * @returns 回访数量
  553. */
  554. async getVisitCount(): Promise<number> {
  555. return await this.page.locator('[data-testid^="remove-visit-"]').count();
  556. }
  557. /**
  558. * 检查添加回访按钮是否被禁用
  559. * @returns 是否禁用
  560. */
  561. async isAddVisitButtonDisabled(): Promise<boolean> {
  562. const addButton = this.page.locator('[data-testid="add-visit-button"]');
  563. return await addButton.isDisabled();
  564. }
  565. /**
  566. * 滚动表单到指定区域
  567. * @param sectionName 区域名称
  568. */
  569. async scrollToSection(sectionName: string) {
  570. const section = this.page.locator(`text=${sectionName}`).first();
  571. await section.scrollIntoViewIfNeeded();
  572. await this.page.waitForTimeout(TIMEOUTS.SHORT);
  573. }
  574. /**
  575. * 等待对话框关闭
  576. */
  577. async waitForDialogClosed() {
  578. const dialog = this.page.locator('[role="dialog"]');
  579. await dialog.waitFor({ state: 'hidden', timeout: 5000 }).catch(() => {});
  580. await this.page.waitForTimeout(500);
  581. }
  582. /**
  583. * 取消对话框
  584. */
  585. async cancelDialog() {
  586. const cancelButton = this.page.getByRole('button', { name: '取消' });
  587. await cancelButton.click();
  588. await this.waitForDialogClosed();
  589. }
  590. /**
  591. * 添加备注(内联表单模式)
  592. * @param content 备注内容
  593. * @param options 选项
  594. */
  595. async addNote(content: string, options?: { isSpecialNeeds?: boolean }): Promise<number> {
  596. // 点击"添加备注"按钮
  597. const addButton = this.page.locator('[data-testid="add-remark-button"]');
  598. await addButton.click();
  599. await this.page.waitForTimeout(TIMEOUTS.SHORT);
  600. // 获取当前备注数量
  601. const noteCount = await this.getNoteCount();
  602. const noteIndex = noteCount > 0 ? noteCount - 1 : 0;
  603. // 填写备注内容
  604. const textarea = this.page.locator(`[data-testid="remark-content-textarea-${noteIndex}"]`);
  605. await textarea.fill(content);
  606. // 标记特殊需求(如果需要)
  607. if (options?.isSpecialNeeds) {
  608. const switchControl = this.page.locator(`[data-testid="special-needs-switch-${noteIndex}"]`);
  609. const isChecked = await switchControl.isChecked();
  610. if (!isChecked) {
  611. await switchControl.click();
  612. }
  613. }
  614. console.debug(` ✓ 添加备注: ${content.substring(0, 30)}...`);
  615. return noteIndex;
  616. }
  617. /**
  618. * 编辑指定索引的备注
  619. * @param index 备注索引(从0开始)
  620. * @param content 更新的备注内容
  621. */
  622. async editNote(index: number, content: string): Promise<void> {
  623. console.debug(` 编辑备注 ${index}`);
  624. const textarea = this.page.locator(`[data-testid="remark-content-textarea-${index}"]`);
  625. await textarea.clear();
  626. await textarea.fill(content);
  627. console.debug(` ✓ 备注 ${index} 编辑完成`);
  628. }
  629. /**
  630. * 删除指定索引的备注
  631. * @param index 备注索引(从0开始)
  632. */
  633. async deleteNote(index: number): Promise<void> {
  634. console.debug(` 删除备注 ${index}`);
  635. const removeButton = this.page.locator(`[data-testid="remove-remark-${index}"]`);
  636. await removeButton.click();
  637. await this.page.waitForTimeout(TIMEOUTS.SHORT);
  638. console.debug(` ✓ 备注 ${index} 已删除`);
  639. }
  640. /**
  641. * 获取备注列表
  642. * @returns 备注内容数组
  643. */
  644. async getNoteList(): Promise<string[]> {
  645. const notes: string[] = [];
  646. const noteCount = await this.page.locator('[data-testid^="remark-content-textarea-"]').count();
  647. for (let i = 0; i < noteCount; i++) {
  648. const textarea = this.page.locator(`[data-testid="remark-content-textarea-${i}"]`);
  649. const content = await textarea.inputValue();
  650. notes.push(content);
  651. }
  652. return notes;
  653. }
  654. /**
  655. * 获取备注数量
  656. * @returns 备注数量
  657. */
  658. async getNoteCount(): Promise<number> {
  659. return await this.page.locator('[data-testid^="remark-content-textarea-"]').count();
  660. }
  661. /**
  662. * 获取指定备注的特殊需求状态
  663. * @param index 备注索引(从0开始)
  664. * @returns 是否标记为特殊需求
  665. */
  666. async getNoteSpecialNeedsStatus(index: number): Promise<boolean> {
  667. const switchControl = this.page.locator(`[data-testid="special-needs-switch-${index}"]`);
  668. return await switchControl.isChecked();
  669. }
  670. /**
  671. * 检查添加备注按钮是否被禁用
  672. * @returns 是否禁用
  673. */
  674. async isAddNoteButtonDisabled(): Promise<boolean> {
  675. const addButton = this.page.locator('[data-testid="add-remark-button"]');
  676. return await addButton.isDisabled();
  677. }
  678. /**
  679. * 打开编辑对话框(通过姓名查找残疾人记录并点击编辑按钮)
  680. * @param name 残疾人姓名
  681. */
  682. async openEditDialog(name: string): Promise<void> {
  683. // 在列表中找到包含该姓名的行
  684. const row = this.personTable.locator('tbody tr').filter({ hasText: name }).first();
  685. // 等待行可见
  686. await row.waitFor({ state: 'visible', timeout: 5000 });
  687. // 点击该行的编辑按钮(使用 data-testid)
  688. // 需要先获取该行的 ID,因为 edit-person-{id} 按钮使用 ID
  689. const editButton = row.locator('[data-testid^="edit-person-"]').first();
  690. await editButton.click();
  691. // 等待编辑对话框出现
  692. await this.page.waitForSelector('[data-testid="edit-disabled-person-dialog-title"]', { state: 'visible', timeout: 5000 });
  693. console.debug(`✓ 打开编辑对话框: ${name}`);
  694. }
  695. /**
  696. * 打开详情对话框(通过姓名查找残疾人记录并点击查看按钮)
  697. * @param name 残疾人姓名
  698. */
  699. async openDetailDialog(name: string): Promise<void> {
  700. // 在列表中找到包含该姓名的行
  701. const row = this.personTable.locator('tbody tr').filter({ hasText: name }).first();
  702. // 等待行可见
  703. await row.waitFor({ state: 'visible', timeout: 5000 });
  704. // 点击该行的查看按钮(使用 data-testid)
  705. const viewButton = row.locator('[data-testid^="view-person-"]').first();
  706. await viewButton.click();
  707. // 等待详情对话框出现
  708. await this.page.waitForSelector('text=残疾人详情', { state: 'visible', timeout: 5000 });
  709. console.debug(`✓ 打开详情对话框: ${name}`);
  710. }
  711. /**
  712. * 删除残疾人记录(通过姓名查找并删除)
  713. * @param name 残疾人姓名
  714. */
  715. async deleteDisabilityPerson(name: string): Promise<void> {
  716. // 在列表中找到包含该姓名的行
  717. const row = this.personTable.locator('tbody tr').filter({ hasText: name }).first();
  718. // 等待行可见
  719. await row.waitFor({ state: 'visible', timeout: 5000 });
  720. // 点击该行的删除按钮
  721. const deleteButton = row.locator('[data-testid^="delete-person-"]').first();
  722. await deleteButton.click();
  723. // 等待删除确认对话框出现
  724. await this.page.waitForSelector('[data-testid="delete-confirmation-dialog-title"]', { state: 'visible', timeout: 3000 });
  725. // 点击确认删除按钮
  726. const confirmButton = this.page.getByRole('button', { name: '确认删除' });
  727. await confirmButton.click();
  728. // 等待网络请求完成
  729. await this.page.waitForLoadState('networkidle', { timeout: 10000 });
  730. // 等待 Toast 提示
  731. await this.page.waitForTimeout(2000);
  732. console.debug(`✓ 删除残疾人记录: ${name}`);
  733. }
  734. /**
  735. * 获取列表中所有残疾人记录的数据
  736. * @returns 残疾人信息数组
  737. */
  738. async getListData(): Promise<Array<{
  739. name: string;
  740. idCard: string;
  741. disabilityType: string;
  742. disabilityLevel: string;
  743. phone: string;
  744. address: string;
  745. }>> {
  746. const rows = await this.personTable.locator('tbody tr').all();
  747. const data: Array<{
  748. name: string;
  749. idCard: string;
  750. disabilityType: string;
  751. disabilityLevel: string;
  752. phone: string;
  753. address: string;
  754. }> = [];
  755. for (const row of rows) {
  756. const cells = await row.locator('td').all();
  757. if (cells.length >= 6) {
  758. data.push({
  759. name: await cells[0].textContent() || '',
  760. idCard: await cells[2].textContent() || '',
  761. disabilityType: await cells[4].textContent() || '',
  762. disabilityLevel: await cells[5].textContent() || '',
  763. phone: await cells[6].textContent() || '',
  764. address: '',
  765. });
  766. }
  767. }
  768. return data;
  769. }
  770. /**
  771. * 获取当前列表记录数量
  772. * @returns 记录数量
  773. */
  774. async getListCount(): Promise<number> {
  775. return await this.personTable.locator('tbody tr').count();
  776. }
  777. /**
  778. * 按残疾类型筛选
  779. * @param disabilityType 残疾类型
  780. */
  781. async filterByDisabilityType(disabilityType: string): Promise<void> {
  782. const filterSelect = this.page.locator('[data-testid="disability-type-filter"]');
  783. await filterSelect.click();
  784. await this.page.getByRole('option', { name: disabilityType }).click();
  785. await this.page.waitForLoadState('networkidle');
  786. await this.page.waitForTimeout(1000);
  787. console.debug(`✓ 按残疾类型筛选: ${disabilityType}`);
  788. }
  789. /**
  790. * 重置筛选条件
  791. */
  792. async resetFilters(): Promise<void> {
  793. const resetButton = this.page.getByRole('button', { name: '重置筛选' });
  794. await resetButton.click();
  795. await this.page.waitForLoadState('networkidle');
  796. await this.page.waitForTimeout(1000);
  797. console.debug('✓ 已重置筛选条件');
  798. }
  799. /**
  800. * 等待详情对话框关闭
  801. */
  802. async waitForDetailDialogClosed(): Promise<void> {
  803. const dialog = this.page.locator('[role="dialog"]').filter({ hasText: '残疾人详情' });
  804. await dialog.waitFor({ state: 'hidden', timeout: 5000 }).catch(() => {});
  805. await this.page.waitForTimeout(500);
  806. }
  807. /**
  808. * 提交表单并保存(编辑模式通用方法)
  809. * @returns 提交结果
  810. */
  811. async submitAndSave(): Promise<{
  812. hasSuccess: boolean;
  813. hasError: boolean;
  814. errorMessage?: string;
  815. successMessage?: string;
  816. }> {
  817. // 点击更新按钮(编辑模式)
  818. const submitButton = this.page.getByRole('button', { name: '更新' });
  819. await submitButton.click();
  820. // 等待网络请求完成
  821. await this.page.waitForLoadState('networkidle', { timeout: 10000 }).catch(() => {});
  822. await this.page.waitForTimeout(2000);
  823. // 检查 Toast 消息
  824. const errorToast = this.page.locator('[data-sonner-toast][data-type="error"]');
  825. const successToast = this.page.locator('[data-sonner-toast][data-type="success"]');
  826. const hasError = await errorToast.count() > 0;
  827. const hasSuccess = await successToast.count() > 0;
  828. let errorMessage: string | null = null;
  829. let successMessage: string | null = null;
  830. if (hasError) {
  831. errorMessage = await errorToast.first().textContent();
  832. }
  833. if (hasSuccess) {
  834. successMessage = await successToast.first().textContent();
  835. }
  836. return {
  837. hasSuccess: hasSuccess || (!hasError && !hasSuccess),
  838. hasError,
  839. errorMessage: errorMessage ?? undefined,
  840. successMessage: successMessage ?? undefined,
  841. };
  842. }
  843. /**
  844. * 导出残疾人列表数据
  845. * @returns 下载事件(可用于验证下载)
  846. */
  847. async exportData(): Promise<{
  848. success: boolean;
  849. fileName?: string;
  850. }> {
  851. // 监听下载事件
  852. const downloadPromise = this.page.waitForEvent('download', { timeout: 10000 }).catch(() => null);
  853. // 点击导出按钮
  854. const exportButton = this.page.getByRole('button', { name: /导出|下载|Export/i }).first();
  855. const buttonExists = await exportButton.count() > 0;
  856. if (!buttonExists) {
  857. console.debug('⚠️ exportData: 未找到导出按钮');
  858. return { success: false };
  859. }
  860. await exportButton.click();
  861. // 等待下载开始
  862. const download = await downloadPromise;
  863. if (download) {
  864. const fileName = download.suggestedFilename();
  865. console.debug(`✓ exportData: 文件下载开始: ${fileName}`);
  866. return { success: true, fileName };
  867. }
  868. return { success: false };
  869. }
  870. }