disability-person.page.ts 35 KB

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