disability-person.page.ts 11 KB

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