disability-person.page.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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.log(` ✓ 上传照片: ${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.log(` ✓ 添加银行卡: ${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. }