优化分批执行点击菜单

This commit is contained in:
dengqichen 2025-03-07 17:47:18 +08:00
parent 970d194ab3
commit 2f6456f027
4 changed files with 44 additions and 39 deletions

BIN
login-error.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 228 KiB

View File

@ -213,7 +213,7 @@ class BasePage {
if (!isLoading) { if (!isLoading) {
await this.waitForTimeout(stabilityDelay); await this.waitForTimeout(stabilityDelay);
console.log(`✅ 页面 ${pageName} 加载完成`); console.log(`✅ 页面 ${pageName} 加载完成`);
// 记录成功的性能数据 // 记录成功的性能数据
await performanceService.recordSuccess(pageName, startTime, Date.now()); await performanceService.recordSuccess(pageName, startTime, Date.now());
return true; return true;
@ -292,6 +292,14 @@ class BasePage {
await this.page.waitForTimeout(ms); await this.page.waitForTimeout(ms);
} }
async waitForVisible(selectors) {
await this.page.locator(selectors).waitFor({
state: 'visible',
timeout: 10000
});
}
/** /**
* 检查元素是否存在 * 检查元素是否存在
* @param {string} selector 元素选择器 * @param {string} selector 元素选择器

View File

@ -1,8 +1,4 @@
const BasePage = require('./BasePage'); const BasePage = require('./BasePage');
const dotenv = require('dotenv');
// 确保环境变量已加载
dotenv.config({ path: '.env.dev' });
class LongiLoginPage extends BasePage { class LongiLoginPage extends BasePage {
/** /**
@ -100,7 +96,10 @@ class LongiLoginPage extends BasePage {
* @param {string} username 用户名 * @param {string} username 用户名
*/ */
async enterUsername(username) { async enterUsername(username) {
await this.page.locator(this.selectors.usernameInput).fill('');
await this.fill(this.selectors.usernameInput, username); await this.fill(this.selectors.usernameInput, username);
// 等待输入稳定
await this.page.waitForTimeout(1000);
} }
/** /**
@ -108,50 +107,51 @@ class LongiLoginPage extends BasePage {
* @param {string} password 密码 * @param {string} password 密码
*/ */
async enterPassword(password) { async enterPassword(password) {
await this.page.locator(this.selectors.passwordInput).fill('');
await this.fill(this.selectors.passwordInput, password); await this.fill(this.selectors.passwordInput, password);
// 等待输入稳定
await this.page.waitForTimeout(1000);
} }
/**
* 输入验证码
* @param {string} captcha 验证码
*/
async enterCaptcha(captcha) {
await this.fill(this.selectors.captchaInput, captcha);
}
/**
* 获取验证码图片元素
* @returns {Promise<import('@playwright/test').Locator>} 验证码图片元素
*/
async getCaptchaImage() {
return this.waitForElement(this.selectors.captchaImage);
}
/** /**
* 执行登录操作 * 执行登录操作
* @param {string} [username] - 用户名默认使用配置的用户名
* @param {string} [password] - 密码默认使用配置的密码
* @returns {Promise<boolean>} 登录是否成功 * @returns {Promise<boolean>} 登录是否成功
*/ */
async login() { async login() {
try { try {
// 清空用户名输入框 console.log('开始登录流程...');
await this.page.locator(this.selectors.usernameInput).fill(''); console.log('使用的登录信息:', {
// 清空密码输入框 username: process.env.IBP_USERNAME,
await this.page.locator(this.selectors.passwordInput).fill(''); baseUrl: process.env.BASE_URL
});
// 输入用户名和密码
// 确保页面已加载
await this.waitForPageLoad();
await this.waitForVisible(this.selectors.usernameInput);
console.log('用户名输入框已就绪');
// 清空并输入用户名
await this.enterUsername(process.env.IBP_USERNAME); await this.enterUsername(process.env.IBP_USERNAME);
console.log('用户名已输入');
await this.waitForVisible(this.selectors.passwordInput);
console.log('密码输入框已就绪');
await this.enterPassword(process.env.IBP_PASSWORD); await this.enterPassword(process.env.IBP_PASSWORD);
console.log('密码已输入');
// 点击登录按钮并等待登录完成
if (await this.clickLoginButton()) { // 点击登录按钮
return await this.isLoginSuccessful(); console.log('准备点击登录按钮...');
const loginSuccess = await this.clickLoginButton();
if (!loginSuccess) {
throw new error('登录失败')
} }
return false;
} catch (error) { } catch (error) {
console.error('登录过程出错:', error.message); console.error('登录过程出错:', error);
return false; // 记录更多错误信息
console.error('错误堆栈:', error.stack);
} }
} }
} }

View File

@ -59,10 +59,7 @@ class LongiTestService {
async performLogin(page) { async performLogin(page) {
const loginPage = new LongiLoginPage(page); const loginPage = new LongiLoginPage(page);
await loginPage.navigateToLoginPage(); await loginPage.navigateToLoginPage();
const loginSuccess = await loginPage.login() await loginPage.login()
if (!loginSuccess) {
throw new Error('登录失败');
}
} }
/** /**