feat: 增强测试稳定性和可靠性

- 增加登录重试机制(最多3次),提高登录成功率
- 添加后端健康监控,每30秒检查一次后端状态
- 改进测试隔离,每个测试后清理localStorage和sessionStorage
- 优化错误处理和日志输出
- 添加globalTeardown函数,确保测试后正确清理资源
This commit is contained in:
张翔
2026-04-04 13:29:06 +08:00
parent be1c587dbf
commit aedca1cf85
3 changed files with 123 additions and 28 deletions
+44 -28
View File
@@ -22,38 +22,54 @@ export class LoginPage {
await this.page.waitForLoadState('networkidle');
}
async login(username: string, password: string) {
console.log('Starting login process...');
await this.usernameInput.fill(username);
await this.passwordInput.fill(password);
console.log('Filled username and password');
async login(username: string, password: string, maxRetries: number = 3) {
let lastError: Error | null = null;
await this.loginButton.click();
console.log('Clicked login button');
for (let attempt = 1; attempt <= maxRetries; attempt++) {
console.log(`Login attempt ${attempt}/${maxRetries}`);
try {
await this.usernameInput.fill(username);
await this.passwordInput.fill(password);
console.log('Filled username and password');
await this.loginButton.click();
console.log('Clicked login button');
try {
await this.page.waitForURL(/\/(dashboard|\/)$/, { timeout: 30000 });
console.log('Successfully navigated to dashboard or home');
await this.page.waitForLoadState('networkidle');
console.log('Network idle achieved');
await this.page.waitForTimeout(2000);
console.log('Wait completed');
} catch (error) {
console.log('Login failed or timeout:', error);
const currentUrl = this.page.url();
console.log('Current URL:', currentUrl);
const errorMessage = await this.getErrorMessage();
if (errorMessage) {
console.log('Login error message:', errorMessage);
await this.page.waitForURL(/\/(dashboard|\/)$/, { timeout: 30000 });
console.log('Successfully navigated to dashboard or home');
await this.page.waitForLoadState('networkidle');
console.log('Network idle achieved');
await this.page.waitForTimeout(2000);
console.log('Login completed successfully');
return;
} catch (error) {
lastError = error as Error;
console.log(`Login attempt ${attempt} failed:`, error);
const currentUrl = this.page.url();
console.log('Current URL:', currentUrl);
const errorMessage = await this.getErrorMessage();
if (errorMessage) {
console.log('Login error message:', errorMessage);
}
const token = await this.page.evaluate(() => localStorage.getItem('token'));
console.log('Token in localStorage:', token ? 'exists' : 'not found');
if (attempt < maxRetries) {
console.log(`Waiting 2 seconds before retry...`);
await this.page.waitForTimeout(2000);
await this.goto();
console.log('Navigated back to login page for retry');
}
}
const token = await this.page.evaluate(() => localStorage.getItem('token'));
console.log('Token in localStorage:', token ? 'exists' : 'not found');
await this.page.waitForTimeout(1000);
throw error;
}
console.log(`All ${maxRetries} login attempts failed`);
throw lastError || new Error('Login failed after all retries');
}
async getErrorMessage(): Promise<string | null> {