disability-person.page.ts 12 KB

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