a96ef304f3
- 优化按钮选择器使用locator API - 增加页面加载等待时间 - 添加错误处理和日志 任务 1/4
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
import { Page, expect } from '@playwright/test';
|
|
|
|
export interface UserData {
|
|
email: string;
|
|
password: string;
|
|
name?: string;
|
|
role?: 'admin' | 'editor' | 'viewer';
|
|
}
|
|
|
|
export class AdminUserPage {
|
|
constructor(private page: Page) { }
|
|
|
|
async goto() {
|
|
await this.page.goto('/admin/users');
|
|
await this.page.waitForLoadState('domcontentloaded');
|
|
await this.page.waitForSelector('table', { timeout: 10000, state: 'visible' });
|
|
}
|
|
|
|
async createUser(data: UserData) {
|
|
await this.goto();
|
|
|
|
await this.page.waitForLoadState('domcontentloaded');
|
|
await this.page.waitForTimeout(1000);
|
|
|
|
const addButton = this.page.locator('button:has-text("添加用户")');
|
|
await addButton.waitFor({ timeout: 10000, state: 'visible' });
|
|
await addButton.click();
|
|
|
|
await this.page.waitForSelector('input[name="email"]', { timeout: 5000, state: 'visible' });
|
|
await this.page.fill('input[name="email"]', data.email);
|
|
await this.page.fill('input[name="password"]', data.password);
|
|
|
|
if (data.name) {
|
|
await this.page.fill('input[name="name"]', data.name);
|
|
}
|
|
|
|
if (data.role) {
|
|
await this.page.selectOption('select[name="role"]', data.role);
|
|
}
|
|
|
|
await this.page.click('button:has-text("创建")');
|
|
}
|
|
|
|
async expectUserInList(email: string) {
|
|
await this.goto();
|
|
const row = this.page.locator(`tr:has-text("${email}")`);
|
|
await expect(row).toBeVisible();
|
|
}
|
|
}
|