disability-person.page.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. // 等待表单出现 - 使用正确的 form ID
  68. await this.page.waitForSelector('form#create-form', { state: 'visible', timeout: 5000 });
  69. // 填写基本信息
  70. await this.page.getByLabel('姓名 *').fill(data.name);
  71. await this.page.getByLabel('性别 *').selectOption(data.gender);
  72. await this.page.getByLabel('身份证号 *').fill(data.idCard);
  73. await this.page.getByLabel('残疾证号 *').fill(data.disabilityId);
  74. await this.page.getByLabel('残疾类型 *').selectOption(data.disabilityType);
  75. await this.page.getByLabel('残疾等级 *').selectOption(data.disabilityLevel);
  76. await this.page.getByLabel('联系电话 *').fill(data.phone);
  77. await this.page.getByLabel('身份证地址 *').fill(data.idAddress);
  78. // 居住地址 - 使用省份选择
  79. await this.page.locator('select[name="province"]').selectOption(data.province);
  80. await this.page.waitForTimeout(500); // 等待城市加载
  81. await this.page.locator('select[name="city"]').selectOption(data.city);
  82. }
  83. async submitForm() {
  84. // 收集网络响应
  85. const responses: any[] = [];
  86. // 监听所有网络请求
  87. this.page.on('response', async (response) => {
  88. const url = response.url();
  89. if (url.includes('disabled-person') || url.includes('aggregated')) {
  90. const requestBody = response.request()?.postData();
  91. const responseBody = await response.text().catch(() => '');
  92. let jsonBody = null;
  93. try {
  94. jsonBody = JSON.parse(responseBody);
  95. } catch (e) {
  96. // 不是 JSON
  97. }
  98. responses.push({
  99. url,
  100. method: response.request()?.method(),
  101. status: response.status(),
  102. ok: response.ok(),
  103. requestHeaders: await response.allHeaders().catch(() => ({})),
  104. responseHeaders: await response.allHeaders().catch(() => ({})),
  105. requestBody: requestBody ? JSON.parse(requestBody) : null,
  106. responseBody: jsonBody || responseBody,
  107. statusText: response.statusText()
  108. });
  109. }
  110. });
  111. // 点击创建按钮
  112. const submitButton = this.page.locator('form#create-form button[type="submit"]');
  113. await submitButton.click();
  114. // 等待网络请求完成
  115. await this.page.waitForLoadState('networkidle', { timeout: 10000 });
  116. // 等待一段时间让 Toast 消息显示
  117. await this.page.waitForTimeout(2000);
  118. // 检查是否有错误提示
  119. const errorToast = this.page.locator('[data-sonner-toast][data-type="error"]');
  120. const successToast = this.page.locator('[data-sonner-toast][data-type="success"]');
  121. const hasError = await errorToast.count() > 0;
  122. const hasSuccess = await successToast.count() > 0;
  123. let errorMessage = null;
  124. let successMessage = null;
  125. if (hasError) {
  126. errorMessage = await errorToast.textContent();
  127. }
  128. if (hasSuccess) {
  129. successMessage = await successToast.textContent();
  130. }
  131. return {
  132. responses,
  133. hasError,
  134. hasSuccess,
  135. errorMessage,
  136. successMessage
  137. };
  138. }
  139. async searchByName(name: string) {
  140. await this.keywordSearchInput.fill(name);
  141. await this.searchButton.click();
  142. await this.page.waitForLoadState('networkidle');
  143. await this.page.waitForTimeout(1000);
  144. }
  145. async personExists(name: string): Promise<boolean> {
  146. const personRow = this.personTable.locator('tbody tr').filter({ hasText: name }).first();
  147. return (await personRow.count()) > 0;
  148. }
  149. }