| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- import { Page, Locator } from '@playwright/test';
- export class DisabilityPersonManagementPage {
- readonly page: Page;
- readonly pageTitle: Locator;
- readonly addPersonButton: Locator;
- readonly keywordSearchInput: Locator;
- readonly searchButton: Locator;
- readonly personTable: Locator;
- constructor(page: Page) {
- this.page = page;
- this.pageTitle = page.getByText('残疾人个人管理');
- this.addPersonButton = page.getByRole('button', { name: '新增残疾人' });
- this.keywordSearchInput = page.getByPlaceholder('搜索姓名或身份证号');
- this.searchButton = page.getByRole('button', { name: '搜索' });
- this.personTable = page.locator('table');
- }
- async goto() {
- await this.page.goto('/admin/disabilities');
- await this.page.waitForLoadState('domcontentloaded');
- // 等待页面标题出现
- await this.pageTitle.waitFor({ state: 'visible', timeout: 15000 });
- // 等待表格数据加载
- await this.page.waitForSelector('table tbody tr', { state: 'visible', timeout: 20000 });
- await this.expectToBeVisible();
- }
- async expectToBeVisible() {
- await this.pageTitle.waitFor({ state: 'visible', timeout: 15000 });
- await this.addPersonButton.waitFor({ state: 'visible', timeout: 10000 });
- }
- async openCreateDialog() {
- // 监听网络请求
- const responses: any[] = [];
- this.page.on('response', async (response) => {
- if (response.url().includes('disabled-person') || response.url().includes('aggregated')) {
- try {
- const responseData = await response.json().catch(() => ({ status: response.status() }));
- responses.push({
- url: response.url(),
- status: response.status(),
- data: responseData
- });
- } catch (e) {
- responses.push({
- url: response.url(),
- status: response.status(),
- error: e
- });
- }
- }
- });
- await this.addPersonButton.click();
- await this.page.waitForSelector('[data-testid="create-disabled-person-dialog-title"]', { state: 'visible', timeout: 5000 });
- return responses;
- }
- async fillBasicForm(data: {
- name: string;
- gender: string;
- idCard: string;
- disabilityId: string;
- disabilityType: string;
- disabilityLevel: string;
- phone: string;
- idAddress: string;
- province: string;
- city: string;
- }) {
- // 等待表单出现 - 使用正确的 form ID
- await this.page.waitForSelector('form#create-form', { state: 'visible', timeout: 5000 });
- // 填写基本信息
- await this.page.getByLabel('姓名 *').fill(data.name);
- await this.page.getByLabel('性别 *').selectOption(data.gender);
- await this.page.getByLabel('身份证号 *').fill(data.idCard);
- await this.page.getByLabel('残疾证号 *').fill(data.disabilityId);
- await this.page.getByLabel('残疾类型 *').selectOption(data.disabilityType);
- await this.page.getByLabel('残疾等级 *').selectOption(data.disabilityLevel);
- await this.page.getByLabel('联系电话 *').fill(data.phone);
- await this.page.getByLabel('身份证地址 *').fill(data.idAddress);
- // 居住地址 - 使用省份选择
- await this.page.locator('select[name="province"]').selectOption(data.province);
- await this.page.waitForTimeout(500); // 等待城市加载
- await this.page.locator('select[name="city"]').selectOption(data.city);
- }
- async submitForm() {
- // 收集网络响应
- const responses: any[] = [];
- // 监听所有网络请求
- this.page.on('response', async (response) => {
- const url = response.url();
- if (url.includes('disabled-person') || url.includes('aggregated')) {
- const requestBody = response.request()?.postData();
- const responseBody = await response.text().catch(() => '');
- let jsonBody = null;
- try {
- jsonBody = JSON.parse(responseBody);
- } catch (e) {
- // 不是 JSON
- }
- responses.push({
- url,
- method: response.request()?.method(),
- status: response.status(),
- ok: response.ok(),
- requestHeaders: await response.allHeaders().catch(() => ({})),
- responseHeaders: await response.allHeaders().catch(() => ({})),
- requestBody: requestBody ? JSON.parse(requestBody) : null,
- responseBody: jsonBody || responseBody,
- statusText: response.statusText()
- });
- }
- });
- // 点击创建按钮
- const submitButton = this.page.locator('form#create-form button[type="submit"]');
- await submitButton.click();
- // 等待网络请求完成
- await this.page.waitForLoadState('networkidle', { timeout: 10000 });
- // 等待一段时间让 Toast 消息显示
- await this.page.waitForTimeout(2000);
- // 检查是否有错误提示
- const errorToast = this.page.locator('[data-sonner-toast][data-type="error"]');
- const successToast = this.page.locator('[data-sonner-toast][data-type="success"]');
- const hasError = await errorToast.count() > 0;
- const hasSuccess = await successToast.count() > 0;
- let errorMessage = null;
- let successMessage = null;
- if (hasError) {
- errorMessage = await errorToast.textContent();
- }
- if (hasSuccess) {
- successMessage = await successToast.textContent();
- }
- return {
- responses,
- hasError,
- hasSuccess,
- errorMessage,
- successMessage
- };
- }
- async searchByName(name: string) {
- await this.keywordSearchInput.fill(name);
- await this.searchButton.click();
- await this.page.waitForLoadState('networkidle');
- await this.page.waitForTimeout(1000);
- }
- async personExists(name: string): Promise<boolean> {
- const personRow = this.personTable.locator('tbody tr').filter({ hasText: name }).first();
- return (await personRow.count()) > 0;
- }
- }
|