203 lines
7.0 KiB
TypeScript
203 lines
7.0 KiB
TypeScript
import { Page, Locator, expect } from '@playwright/test';
|
|
|
|
export class CoachManagementPage {
|
|
readonly page: Page;
|
|
readonly table: Locator;
|
|
readonly createButton: Locator;
|
|
readonly searchInput: Locator;
|
|
readonly searchButton: Locator;
|
|
readonly successMessage: Locator;
|
|
readonly nicknameInput: Locator;
|
|
|
|
constructor(page: Page) {
|
|
this.page = page;
|
|
this.table = page.locator('.el-table').first();
|
|
this.createButton = page.getByRole('button', { name: '新增教练' }).or(page.locator('button:has-text("新增教练")'));
|
|
this.searchInput = page.locator('input[placeholder*="搜索教练"]').or(page.locator('input[placeholder*="搜索"]'));
|
|
this.searchButton = page.getByRole('button', { name: '搜索' }).or(page.locator('button:has-text("搜索")'));
|
|
this.successMessage = page.locator('.el-message--success').or(page.locator('.success-message'));
|
|
this.nicknameInput = page.locator('.el-dialog .el-form-item').filter({ hasText: '昵称' }).or(page.locator('.el-dialog .el-form-item').filter({ hasText: '姓名' })).locator('input');
|
|
}
|
|
|
|
async goto() {
|
|
try {
|
|
console.log('导航到教练管理页面...');
|
|
await this.page.goto('/coach');
|
|
|
|
await this.page.waitForURL('**/coach', { timeout: 30000 });
|
|
await this.page.waitForLoadState('networkidle');
|
|
|
|
await this.table.waitFor({ state: 'visible', timeout: 15000 });
|
|
|
|
await expect(this.page).toHaveURL(/.*coach/);
|
|
|
|
console.log('教练管理页面加载完成');
|
|
} catch (error) {
|
|
await this.page.screenshot({ path: `test-results/coach-management-error-${Date.now()}.png` });
|
|
|
|
console.error('导航到教练管理页面失败:', error);
|
|
|
|
throw new Error(`导航到教练管理页面失败: ${error instanceof Error ? error.message : String(error)}`);
|
|
}
|
|
}
|
|
|
|
async waitForTableReady() {
|
|
await this.table.waitFor({ state: 'visible', timeout: 10000 });
|
|
|
|
await this.page.waitForFunction(
|
|
() => {
|
|
const rows = document.querySelectorAll('.el-table__body-wrapper tbody tr');
|
|
return rows.length > 0;
|
|
},
|
|
{ timeout: 5000 }
|
|
).catch(() => {
|
|
console.log('表格没有数据,继续执行');
|
|
});
|
|
}
|
|
|
|
async clickCreate() {
|
|
await this.createButton.click();
|
|
await this.page.waitForTimeout(500);
|
|
}
|
|
|
|
async fillCoachForm(coachData: {
|
|
name?: string;
|
|
nickname?: string;
|
|
phone?: string;
|
|
email?: string;
|
|
username?: string;
|
|
password?: string;
|
|
}) {
|
|
// Normalize property names from spec
|
|
const name = coachData.name || coachData.nickname || '';
|
|
const phone = coachData.phone || '';
|
|
const email = coachData.email || '';
|
|
const username = coachData.username || '';
|
|
const password = coachData.password || '';
|
|
|
|
const dialog = this.page.locator('.el-dialog');
|
|
|
|
// 姓名 / 昵称
|
|
const nameInput = dialog.locator('.el-form-item').filter({ hasText: '姓名' }).or(dialog.locator('.el-form-item').filter({ hasText: '昵称' })).locator('input');
|
|
if (await nameInput.count() > 0) {
|
|
await nameInput.fill(name);
|
|
} else if (await dialog.locator('input').count() > 0) {
|
|
await dialog.locator('input').first().fill(name);
|
|
}
|
|
|
|
// 手机号
|
|
const phoneInput = dialog.locator('.el-form-item').filter({ hasText: '手机' }).locator('input');
|
|
if (await phoneInput.count() > 0) {
|
|
await phoneInput.fill(phone);
|
|
} else if (await dialog.locator('input').count() > 1) {
|
|
await dialog.locator('input').nth(1).fill(phone);
|
|
}
|
|
|
|
// 邮箱
|
|
const emailInput = dialog.locator('.el-form-item').filter({ hasText: '邮箱' }).locator('input');
|
|
if (await emailInput.count() > 0) {
|
|
await emailInput.fill(email);
|
|
} else if (await dialog.locator('input').count() > 2) {
|
|
await dialog.locator('input').nth(2).fill(email);
|
|
}
|
|
|
|
// 用户名
|
|
const usernameInput = dialog.locator('.el-form-item').filter({ hasText: '用户名' }).locator('input');
|
|
if (await usernameInput.count() > 0) {
|
|
await usernameInput.fill(username);
|
|
} else if (await dialog.locator('input').count() > 3) {
|
|
await dialog.locator('input').nth(3).fill(username);
|
|
}
|
|
|
|
// 密码
|
|
const passwordInput = dialog.locator('input[type="password"]');
|
|
if (await passwordInput.count() > 0) {
|
|
await passwordInput.fill(password);
|
|
}
|
|
}
|
|
|
|
async submitForm() {
|
|
const dialog = this.page.locator('.el-dialog');
|
|
const submitButton = dialog.getByRole('button', { name: '确定' }).or(dialog.locator('button:has-text("确定")'));
|
|
|
|
await submitButton.click();
|
|
|
|
await this.page.waitForTimeout(1000);
|
|
}
|
|
|
|
async waitForSuccessMessage(timeout: number = 10000): Promise<boolean> {
|
|
try {
|
|
const message = this.page.locator('.el-message--success').or(this.page.locator('.el-message'));
|
|
await message.waitFor({ state: 'visible', timeout });
|
|
return true;
|
|
} catch (error) {
|
|
console.log('等待成功消息超时,检查是否有错误消息');
|
|
|
|
try {
|
|
const errorMessage = this.page.locator('.el-message--error').or(this.page.locator('.el-message--warning'));
|
|
if (await errorMessage.count() > 0) {
|
|
const errorText = await errorMessage.first().textContent();
|
|
console.log('发现错误消息:', errorText);
|
|
}
|
|
} catch (e) {
|
|
console.log('没有发现错误消息');
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async editCoach(rowNumber: number) {
|
|
await this.table.locator(`tbody tr:nth-child(${rowNumber})`).getByRole('button', { name: '编辑' }).or(this.page.locator(`tbody tr:nth-child(${rowNumber}) .edit-button`)).click();
|
|
}
|
|
|
|
async deleteCoach(rowNumber: number) {
|
|
await this.table.locator(`tbody tr:nth-child(${rowNumber})`).getByRole('button', { name: '删除' }).or(this.page.locator(`tbody tr:nth-child(${rowNumber}) .delete-button`)).click();
|
|
}
|
|
|
|
async confirmDelete() {
|
|
const confirmDialog = this.page.locator('.el-message-box');
|
|
await confirmDialog.getByRole('button', { name: '确定' }).or(confirmDialog.locator('button:has-text("确定")')).click();
|
|
}
|
|
|
|
async search(keyword: string) {
|
|
await this.searchInput.fill(keyword);
|
|
await this.searchButton.click();
|
|
}
|
|
|
|
async isSuccessMessageVisible(): Promise<boolean> {
|
|
try {
|
|
return await this.successMessage.isVisible({ timeout: 3000 });
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async clickAdd(): Promise<void> {
|
|
await this.clickCreate();
|
|
}
|
|
|
|
async waitForDialogClose(): Promise<void> {
|
|
await this.page.waitForTimeout(1000);
|
|
await this.page.locator('.el-dialog').waitFor({ state: 'hidden', timeout: 10000 }).catch(() => {
|
|
console.log('对话框可能已关闭或不存在');
|
|
});
|
|
}
|
|
|
|
async clickEditOnFirstRow(): Promise<void> {
|
|
await this.editCoach(1);
|
|
}
|
|
|
|
async getCoachCount(): Promise<number> {
|
|
return await this.table.locator('tbody tr').count();
|
|
}
|
|
|
|
async getTableRowCount(): Promise<number> {
|
|
return await this.table.locator('tbody tr').count();
|
|
}
|
|
|
|
async containsText(text: string): Promise<boolean> {
|
|
return await this.table.getByText(text).count() > 0;
|
|
}
|
|
}
|