disability-person.page.ts 12 KB

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