disability-person.page.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. await this.page.waitForSelector('form#create-form', { state: 'visible', timeout: 5000 });
  71. // 填写基本信息
  72. await this.page.getByLabel('姓名 *').fill(data.name);
  73. // 性别使用 Radix UI Select
  74. await selectRadixOption(this.page, '性别 *', data.gender);
  75. await this.page.getByLabel('身份证号 *').fill(data.idCard);
  76. await this.page.getByLabel('残疾证号 *').fill(data.disabilityId);
  77. await selectRadixOption(this.page, '残疾类型 *', data.disabilityType);
  78. await selectRadixOption(this.page, '残疾等级 *', data.disabilityLevel);
  79. await this.page.getByLabel('联系电话 *').fill(data.phone);
  80. await this.page.getByLabel('身份证地址 *').fill(data.idAddress);
  81. // 居住地址 - 使用省市区级联选择工具
  82. await selectProvinceCity(this.page, data.province, data.city);
  83. }
  84. async submitForm() {
  85. // 收集网络响应
  86. const responses: any[] = [];
  87. // 监听所有网络请求
  88. this.page.on('response', async (response) => {
  89. const url = response.url();
  90. if (url.includes('disabled-person') || url.includes('aggregated')) {
  91. const requestBody = response.request()?.postData();
  92. const responseBody = await response.text().catch(() => '');
  93. let jsonBody = null;
  94. try {
  95. jsonBody = JSON.parse(responseBody);
  96. } catch (e) {
  97. // 不是 JSON
  98. }
  99. responses.push({
  100. url,
  101. method: response.request()?.method(),
  102. status: response.status(),
  103. ok: response.ok(),
  104. requestHeaders: await response.allHeaders().catch(() => ({})),
  105. responseHeaders: await response.allHeaders().catch(() => ({})),
  106. requestBody: requestBody ? JSON.parse(requestBody) : null,
  107. responseBody: jsonBody || responseBody,
  108. statusText: response.statusText()
  109. });
  110. }
  111. });
  112. // 点击创建按钮
  113. const submitButton = this.page.getByRole('button', { name: '创建' });
  114. await submitButton.click();
  115. // 等待网络请求完成
  116. await this.page.waitForLoadState('networkidle', { timeout: 10000 });
  117. // 等待一段时间让 Toast 消息显示
  118. await this.page.waitForTimeout(2000);
  119. // 检查是否有错误提示
  120. const errorToast = this.page.locator('[data-sonner-toast][data-type="error"]');
  121. const successToast = this.page.locator('[data-sonner-toast][data-type="success"]');
  122. const hasError = await errorToast.count() > 0;
  123. const hasSuccess = await successToast.count() > 0;
  124. let errorMessage = null;
  125. let successMessage = null;
  126. if (hasError) {
  127. errorMessage = await errorToast.first().textContent();
  128. }
  129. if (hasSuccess) {
  130. successMessage = await successToast.first().textContent();
  131. }
  132. return {
  133. responses,
  134. hasError,
  135. hasSuccess,
  136. errorMessage,
  137. successMessage
  138. };
  139. }
  140. async searchByName(name: string) {
  141. await this.keywordSearchInput.fill(name);
  142. await this.searchButton.click();
  143. await this.page.waitForLoadState('networkidle');
  144. await this.page.waitForTimeout(1000);
  145. }
  146. async personExists(name: string): Promise<boolean> {
  147. const personRow = this.personTable.locator('tbody tr').filter({ hasText: name }).first();
  148. return (await personRow.count()) > 0;
  149. }
  150. /**
  151. * 上传照片
  152. * @param photoType 照片类型(身份证照片、残疾证照片、个人照片、其他照片)
  153. * @param fileName 文件名
  154. */
  155. async uploadPhoto(photoType: string, fileName: string) {
  156. // 找到对应照片类型的上传按钮区域
  157. const photoSection = this.page.locator('text=' + photoType).first();
  158. await photoSection.scrollIntoViewIfNeeded();
  159. // 查找该类型照片区域的"上传"按钮
  160. const uploadButton = photoSection.locator('xpath=ancestor::div[contains(@class, "space-y-")]').first()
  161. .getByRole('button', { name: /上传/ }).first();
  162. // 创建测试文件
  163. const fileInput = await uploadButton.evaluateHandle((el: any) => {
  164. const input = el.querySelector('input[type="file"]');
  165. return input;
  166. });
  167. // 使用临时文件上传
  168. const file = {
  169. name: fileName,
  170. mimeType: 'image/jpeg',
  171. buffer: Buffer.from('fake image content')
  172. };
  173. await fileInput.uploadFile(file as any);
  174. await this.page.waitForTimeout(500); // 等待上传处理
  175. console.log(` ✓ 上传照片: ${photoType} - ${fileName}`);
  176. }
  177. /**
  178. * 添加银行卡
  179. * @param bankCard 银行卡信息
  180. */
  181. async addBankCard(bankCard: {
  182. bankName: string;
  183. subBankName: string;
  184. cardNumber: string;
  185. cardholderName: string;
  186. cardType?: string;
  187. photoFileName?: string;
  188. }) {
  189. // 点击"添加银行卡"按钮
  190. const addCardButton = this.page.getByRole('button', { name: /添加银行卡/ });
  191. await addCardButton.click();
  192. await this.page.waitForTimeout(300);
  193. // 填写银行卡信息
  194. await selectRadixOption(this.page, '银行名称', bankCard.bankName);
  195. await this.page.getByLabel(/发卡支行/).fill(bankCard.subBankName);
  196. await this.page.getByLabel(/银行卡号/).fill(bankCard.cardNumber);
  197. await this.page.getByLabel(/持卡人姓名/).fill(bankCard.cardholderName);
  198. // 选择银行卡类型(可选)
  199. if (bankCard.cardType) {
  200. await selectRadixOption(this.page, '银行卡类型', bankCard.cardType);
  201. }
  202. // 上传银行卡照片
  203. if (bankCard.photoFileName) {
  204. const photoInput = this.page.locator('input[type="file"]').last();
  205. await photoInput.setInputFiles({
  206. name: bankCard.photoFileName,
  207. mimeType: 'image/jpeg',
  208. buffer: Buffer.from('fake bank card image')
  209. });
  210. await this.page.waitForTimeout(500);
  211. }
  212. console.log(` ✓ 添加银行卡: ${bankCard.bankName} - ${bankCard.cardNumber}`);
  213. }
  214. /**
  215. * 添加备注
  216. * @param remark 备注信息
  217. */
  218. async addRemark(remark: {
  219. content: string;
  220. isSpecialNeeds?: boolean;
  221. }) {
  222. // 点击"添加备注"按钮
  223. const addRemarkButton = this.page.getByRole('button', { name: /添加备注/ });
  224. await addRemarkButton.click();
  225. await this.page.waitForTimeout(300);
  226. // 填写备注内容
  227. const remarkTextarea = this.page.getByPlaceholder(/请输入备注内容/).last();
  228. await remarkTextarea.fill(remark.content);
  229. // 标记特殊需求(如果需要)
  230. if (remark.isSpecialNeeds) {
  231. const specialNeedsCheckbox = this.page.getByRole('checkbox', { name: /特殊需求/ });
  232. const isChecked = await specialNeedsCheckbox.isChecked();
  233. if (!isChecked) {
  234. await specialNeedsCheckbox.click();
  235. }
  236. }
  237. console.log(` ✓ 添加备注: ${remark.content.substring(0, 20)}...`);
  238. }
  239. /**
  240. * 添加回访记录
  241. * @param visit 回访信息
  242. */
  243. async addVisit(visit: {
  244. visitDate: string;
  245. visitType: string;
  246. visitContent: string;
  247. visitResult?: string;
  248. nextVisitDate?: string;
  249. }) {
  250. // 点击"添加回访"按钮
  251. const addVisitButton = this.page.getByRole('button', { name: /添加回访/ });
  252. await addVisitButton.click();
  253. await this.page.waitForTimeout(300);
  254. // 填写回访信息
  255. await this.page.getByLabel(/回访日期/).fill(visit.visitDate);
  256. await selectRadixOption(this.page, '回访类型', visit.visitType);
  257. // 查找回访内容输入框(可能有多个,使用最后一个)
  258. const visitContentTextarea = this.page.locator('textarea').filter({ hasText: '' }).last();
  259. await visitContentTextarea.fill(visit.visitContent);
  260. // 填写回访结果(可选)
  261. if (visit.visitResult) {
  262. const resultTextareas = this.page.locator('textarea');
  263. const count = await resultTextareas.count();
  264. if (count > 0) {
  265. await resultTextareas.nth(count - 1).fill(visit.visitResult);
  266. }
  267. }
  268. // 填写下一次回访日期(可选)
  269. if (visit.nextVisitDate) {
  270. const nextDateInput = this.page.getByLabel(/下次回访日期/);
  271. await nextDateInput.fill(visit.nextVisitDate);
  272. }
  273. console.log(` ✓ 添加回访: ${visit.visitType} - ${visit.visitDate}`);
  274. }
  275. /**
  276. * 滚动表单到指定区域
  277. * @param sectionName 区域名称
  278. */
  279. async scrollToSection(sectionName: string) {
  280. const section = this.page.locator(`text=${sectionName}`).first();
  281. await section.scrollIntoViewIfNeeded();
  282. await this.page.waitForTimeout(300);
  283. }
  284. /**
  285. * 等待对话框关闭
  286. */
  287. async waitForDialogClosed() {
  288. const dialog = this.page.locator('[role="dialog"]');
  289. await dialog.waitFor({ state: 'hidden', timeout: 5000 }).catch(() => {});
  290. await this.page.waitForTimeout(500);
  291. }
  292. /**
  293. * 取消对话框
  294. */
  295. async cancelDialog() {
  296. const cancelButton = this.page.getByRole('button', { name: '取消' });
  297. await cancelButton.click();
  298. await this.waitForDialogClosed();
  299. }
  300. }