disability-person.page.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. import { Page, Locator } from '@playwright/test';
  2. import { selectRadixOption, selectProvinceCity } from '@d8d/e2e-test-utils';
  3. // 注意:@d8d/e2e-test-utils 包已安装,将在后续 story (2.2, 2.3) 中实际使用
  4. export class DisabilityPersonManagementPage {
  5. readonly page: Page;
  6. readonly pageTitle: Locator;
  7. readonly addPersonButton: Locator;
  8. readonly keywordSearchInput: Locator;
  9. readonly searchButton: Locator;
  10. readonly personTable: Locator;
  11. constructor(page: Page) {
  12. this.page = page;
  13. this.pageTitle = page.getByText('残疾人个人管理');
  14. this.addPersonButton = page.getByRole('button', { name: '新增残疾人' });
  15. this.keywordSearchInput = page.getByPlaceholder('搜索姓名或身份证号');
  16. this.searchButton = page.getByRole('button', { name: '搜索' });
  17. this.personTable = page.locator('table');
  18. }
  19. async goto() {
  20. await this.page.goto('/admin/disabilities');
  21. await this.page.waitForLoadState('domcontentloaded');
  22. // 等待页面标题出现
  23. await this.pageTitle.waitFor({ state: 'visible', timeout: 15000 });
  24. // 等待表格数据加载
  25. await this.page.waitForSelector('table tbody tr', { state: 'visible', timeout: 20000 });
  26. await this.expectToBeVisible();
  27. }
  28. async expectToBeVisible() {
  29. await this.pageTitle.waitFor({ state: 'visible', timeout: 15000 });
  30. await this.addPersonButton.waitFor({ state: 'visible', timeout: 10000 });
  31. }
  32. async openCreateDialog() {
  33. // 监听网络请求
  34. const responses: any[] = [];
  35. this.page.on('response', async (response) => {
  36. if (response.url().includes('disabled-person') || response.url().includes('aggregated')) {
  37. try {
  38. const responseData = await response.json().catch(() => ({ status: response.status() }));
  39. responses.push({
  40. url: response.url(),
  41. status: response.status(),
  42. data: responseData
  43. });
  44. } catch (e) {
  45. responses.push({
  46. url: response.url(),
  47. status: response.status(),
  48. error: e
  49. });
  50. }
  51. }
  52. });
  53. await this.addPersonButton.click();
  54. await this.page.waitForSelector('[data-testid="create-disabled-person-dialog-title"]', { state: 'visible', timeout: 5000 });
  55. return responses;
  56. }
  57. async fillBasicForm(data: {
  58. name: string;
  59. gender: string;
  60. idCard: string;
  61. disabilityId: string;
  62. disabilityType: string;
  63. disabilityLevel: string;
  64. phone: string;
  65. idAddress: string;
  66. province: string;
  67. city: string;
  68. }) {
  69. // 等待表单出现
  70. const form = this.page.locator('form#create-form');
  71. await form.waitFor({ state: 'visible', timeout: 5000 });
  72. console.debug('开始填写表单...');
  73. // 填写基本信息 - 使用 form locator 确保只在对话框内查找
  74. await form.getByLabel('姓名 *').fill(data.name);
  75. console.debug('✓ 姓名已填写:', data.name);
  76. // 性别 - 使用 data-testid
  77. const genderTrigger = this.page.locator('[data-testid="gender-select"]');
  78. await genderTrigger.click();
  79. await this.page.getByRole('option', { name: data.gender }).click();
  80. console.debug('✓ 性别已选择:', data.gender);
  81. await form.getByLabel('身份证号 *').fill(data.idCard);
  82. console.debug('✓ 身份证号已填写');
  83. await form.getByLabel('残疾证号 *').fill(data.disabilityId);
  84. console.debug('✓ 残疾证号已填写');
  85. // 残疾类型 - 使用 data-testid
  86. const disabilityTypeTrigger = this.page.locator('[data-testid="disability-type-select"]');
  87. await disabilityTypeTrigger.scrollIntoViewIfNeeded();
  88. await this.page.waitForTimeout(200);
  89. await disabilityTypeTrigger.click();
  90. await this.page.getByRole('option', { name: data.disabilityType }).click();
  91. console.debug('✓ 残疾类型已选择:', data.disabilityType);
  92. // 残疾等级 - 使用 data-testid
  93. const disabilityLevelTrigger = this.page.locator('[data-testid="disability-level-select"]');
  94. await disabilityLevelTrigger.scrollIntoViewIfNeeded();
  95. await this.page.waitForTimeout(200);
  96. await disabilityLevelTrigger.click();
  97. await this.page.getByRole('option', { name: data.disabilityLevel }).click();
  98. console.debug('✓ 残疾等级已选择:', data.disabilityLevel);
  99. await form.getByLabel('联系电话 *').fill(data.phone);
  100. console.debug('✓ 联系电话已填写');
  101. await form.getByLabel('身份证地址 *').fill(data.idAddress);
  102. console.debug('✓ 身份证地址已填写');
  103. // 居住地址 - 省市区级联选择
  104. // AreaSelectForm 使用 data-testid="area-select-province" 等属性
  105. const provinceTrigger = this.page.locator('[data-testid="area-select-province"]');
  106. await provinceTrigger.scrollIntoViewIfNeeded();
  107. await provinceTrigger.click();
  108. await this.page.getByRole('option', { name: data.province }).click();
  109. console.debug('✓ 省份已选择:', data.province);
  110. const cityTrigger = this.page.locator('[data-testid="area-select-city"]');
  111. await cityTrigger.click();
  112. await this.page.getByRole('option', { name: data.city }).click();
  113. console.debug('✓ 城市已选择:', data.city);
  114. console.debug('表单填写完成');
  115. }
  116. async submitForm() {
  117. // 收集网络响应
  118. const responses: any[] = [];
  119. // 监听所有网络请求
  120. this.page.on('response', async (response) => {
  121. const url = response.url();
  122. if (url.includes('disabled-person') || url.includes('aggregated')) {
  123. const requestBody = response.request()?.postData();
  124. const responseBody = await response.text().catch(() => '');
  125. let jsonBody = null;
  126. try {
  127. jsonBody = JSON.parse(responseBody);
  128. } catch (e) {
  129. // 不是 JSON
  130. }
  131. responses.push({
  132. url,
  133. method: response.request()?.method(),
  134. status: response.status(),
  135. ok: response.ok(),
  136. requestHeaders: await response.allHeaders().catch(() => ({})),
  137. responseHeaders: await response.allHeaders().catch(() => ({})),
  138. requestBody: requestBody ? JSON.parse(requestBody) : null,
  139. responseBody: jsonBody || responseBody,
  140. statusText: response.statusText()
  141. });
  142. }
  143. });
  144. // 点击创建按钮
  145. const submitButton = this.page.getByRole('button', { name: '创建' });
  146. await submitButton.click();
  147. // 等待网络请求完成
  148. await this.page.waitForLoadState('networkidle', { timeout: 10000 });
  149. // 等待一段时间让 Toast 消息显示
  150. await this.page.waitForTimeout(2000);
  151. // 检查是否有错误提示
  152. const errorToast = this.page.locator('[data-sonner-toast][data-type="error"]');
  153. const successToast = this.page.locator('[data-sonner-toast][data-type="success"]');
  154. const hasError = await errorToast.count() > 0;
  155. const hasSuccess = await successToast.count() > 0;
  156. let errorMessage = null;
  157. let successMessage = null;
  158. if (hasError) {
  159. errorMessage = await errorToast.first().textContent();
  160. }
  161. if (hasSuccess) {
  162. successMessage = await successToast.first().textContent();
  163. }
  164. return {
  165. responses,
  166. hasError,
  167. hasSuccess,
  168. errorMessage,
  169. successMessage
  170. };
  171. }
  172. async searchByName(name: string) {
  173. await this.keywordSearchInput.fill(name);
  174. await this.searchButton.click();
  175. await this.page.waitForLoadState('networkidle');
  176. await this.page.waitForTimeout(1000);
  177. }
  178. async personExists(name: string): Promise<boolean> {
  179. const personRow = this.personTable.locator('tbody tr').filter({ hasText: name }).first();
  180. return (await personRow.count()) > 0;
  181. }
  182. /**
  183. * 上传照片
  184. * @param photoType 照片类型(身份证照片、残疾证照片、个人照片、其他照片)
  185. * @param fileName 文件名
  186. */
  187. async uploadPhoto(photoType: string, fileName: string) {
  188. // 找到对应照片类型的上传按钮区域
  189. const photoSection = this.page.locator('text=' + photoType).first();
  190. await photoSection.scrollIntoViewIfNeeded();
  191. // 查找该类型照片区域的"上传"按钮
  192. const uploadButton = photoSection.locator('xpath=ancestor::div[contains(@class, "space-y-")]').first()
  193. .getByRole('button', { name: /上传/ }).first();
  194. // 创建测试文件
  195. const fileInput = await uploadButton.evaluateHandle((el: any) => {
  196. const input = el.querySelector('input[type="file"]');
  197. return input;
  198. });
  199. // 使用临时文件上传
  200. const file = {
  201. name: fileName,
  202. mimeType: 'image/jpeg',
  203. buffer: Buffer.from('fake image content')
  204. };
  205. await fileInput.uploadFile(file as any);
  206. await this.page.waitForTimeout(500); // 等待上传处理
  207. console.log(` ✓ 上传照片: ${photoType} - ${fileName}`);
  208. }
  209. /**
  210. * 添加银行卡
  211. * @param bankCard 银行卡信息
  212. */
  213. async addBankCard(bankCard: {
  214. bankName: string;
  215. subBankName: string;
  216. cardNumber: string;
  217. cardholderName: string;
  218. cardType?: string;
  219. photoFileName?: string;
  220. }) {
  221. // 点击"添加银行卡"按钮
  222. const addCardButton = this.page.getByRole('button', { name: /添加银行卡/ });
  223. await addCardButton.click();
  224. await this.page.waitForTimeout(300);
  225. // 填写银行卡信息
  226. await selectRadixOption(this.page, '银行名称', bankCard.bankName);
  227. await this.page.getByLabel(/发卡支行/).fill(bankCard.subBankName);
  228. await this.page.getByLabel(/银行卡号/).fill(bankCard.cardNumber);
  229. await this.page.getByLabel(/持卡人姓名/).fill(bankCard.cardholderName);
  230. // 选择银行卡类型(可选)
  231. if (bankCard.cardType) {
  232. await selectRadixOption(this.page, '银行卡类型', bankCard.cardType);
  233. }
  234. // 上传银行卡照片
  235. if (bankCard.photoFileName) {
  236. const photoInput = this.page.locator('input[type="file"]').last();
  237. await photoInput.setInputFiles({
  238. name: bankCard.photoFileName,
  239. mimeType: 'image/jpeg',
  240. buffer: Buffer.from('fake bank card image')
  241. });
  242. await this.page.waitForTimeout(500);
  243. }
  244. console.log(` ✓ 添加银行卡: ${bankCard.bankName} - ${bankCard.cardNumber}`);
  245. }
  246. /**
  247. * 添加备注
  248. * @param remark 备注信息
  249. */
  250. async addRemark(remark: {
  251. content: string;
  252. isSpecialNeeds?: boolean;
  253. }) {
  254. // 点击"添加备注"按钮
  255. const addRemarkButton = this.page.getByRole('button', { name: /添加备注/ });
  256. await addRemarkButton.click();
  257. await this.page.waitForTimeout(300);
  258. // 填写备注内容
  259. const remarkTextarea = this.page.getByPlaceholder(/请输入备注内容/).last();
  260. await remarkTextarea.fill(remark.content);
  261. // 标记特殊需求(如果需要)
  262. if (remark.isSpecialNeeds) {
  263. const specialNeedsCheckbox = this.page.getByRole('checkbox', { name: /特殊需求/ });
  264. const isChecked = await specialNeedsCheckbox.isChecked();
  265. if (!isChecked) {
  266. await specialNeedsCheckbox.click();
  267. }
  268. }
  269. console.log(` ✓ 添加备注: ${remark.content.substring(0, 20)}...`);
  270. }
  271. /**
  272. * 添加回访记录
  273. * @param visit 回访信息
  274. */
  275. async addVisit(visit: {
  276. visitDate: string;
  277. visitType: string;
  278. visitContent: string;
  279. visitResult?: string;
  280. nextVisitDate?: string;
  281. }) {
  282. // 点击"添加回访"按钮
  283. const addVisitButton = this.page.getByRole('button', { name: /添加回访/ });
  284. await addVisitButton.click();
  285. await this.page.waitForTimeout(300);
  286. // 填写回访信息
  287. await this.page.getByLabel(/回访日期/).fill(visit.visitDate);
  288. await selectRadixOption(this.page, '回访类型', visit.visitType);
  289. // 查找回访内容输入框(可能有多个,使用最后一个)
  290. const visitContentTextarea = this.page.locator('textarea').filter({ hasText: '' }).last();
  291. await visitContentTextarea.fill(visit.visitContent);
  292. // 填写回访结果(可选)
  293. if (visit.visitResult) {
  294. const resultTextareas = this.page.locator('textarea');
  295. const count = await resultTextareas.count();
  296. if (count > 0) {
  297. await resultTextareas.nth(count - 1).fill(visit.visitResult);
  298. }
  299. }
  300. // 填写下一次回访日期(可选)
  301. if (visit.nextVisitDate) {
  302. const nextDateInput = this.page.getByLabel(/下次回访日期/);
  303. await nextDateInput.fill(visit.nextVisitDate);
  304. }
  305. console.log(` ✓ 添加回访: ${visit.visitType} - ${visit.visitDate}`);
  306. }
  307. /**
  308. * 滚动表单到指定区域
  309. * @param sectionName 区域名称
  310. */
  311. async scrollToSection(sectionName: string) {
  312. const section = this.page.locator(`text=${sectionName}`).first();
  313. await section.scrollIntoViewIfNeeded();
  314. await this.page.waitForTimeout(300);
  315. }
  316. /**
  317. * 等待对话框关闭
  318. */
  319. async waitForDialogClosed() {
  320. const dialog = this.page.locator('[role="dialog"]');
  321. await dialog.waitFor({ state: 'hidden', timeout: 5000 }).catch(() => {});
  322. await this.page.waitForTimeout(500);
  323. }
  324. /**
  325. * 取消对话框
  326. */
  327. async cancelDialog() {
  328. const cancelButton = this.page.getByRole('button', { name: '取消' });
  329. await cancelButton.click();
  330. await this.waitForDialogClosed();
  331. }
  332. }