From b7f00b5a9f12f9ff7dc8e67b9a7b0409939bb2b8 Mon Sep 17 00:00:00 2001 From: dengqichen Date: Thu, 6 Mar 2025 10:29:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6=E8=B7=AF?= =?UTF-8?q?=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/pages/LongiLoginPage.js | 118 ++++++++++++++++++++++++++++------ 1 file changed, 98 insertions(+), 20 deletions(-) diff --git a/tests/pages/LongiLoginPage.js b/tests/pages/LongiLoginPage.js index c8ee6a6..a02685f 100644 --- a/tests/pages/LongiLoginPage.js +++ b/tests/pages/LongiLoginPage.js @@ -1,25 +1,50 @@ const BasePage = require('./BasePage'); class LongiLoginPage extends BasePage { - + /** + * 创建登录页面对象 + * @param {import('@playwright/test').Page} page Playwright页面对象 + */ constructor(page) { super(page); + this.initializeSelectors(); + this.initializeConfig(); + } - // 页面元素选择器 - this.selectors = { - usernameInput: 'input[name="username"]', // 用户名输入框 - passwordInput: 'input[name="passWord"]', // 密码输入框 - captchaInput: 'input[placeholder*="验证码"]', // 验证码输入框 - captchaImage: 'img[style*="width: 78px"]', // 验证码图片 - loginButton: 'button.el-button--large.container-button', // 登录按钮 - loginButtonByText: 'button:text("登录"):not(:text("SSO登录"))', // 通过文本定位登录按钮 - loginButtonFirst: 'button:has-text("登录")', // 通过文本定位登录按钮(使用first()方法) - errorMessage: '.el-form-item__error, .el-form-item.is-error', // 错误消息 - ssoLoginButton: 'button:has-text("SSO登录")' // SSO登录按钮 - }; + /** + * 初始化选择器 + * @private + */ + initializeSelectors() { + // 调用父类的选择器初始化 + super.initializeSelectors(); + + // 添加或覆盖特定于 LongiLoginPage 的选择器 + Object.assign(this.selectors, { + // 登录表单元素 + usernameInput: 'input[name="username"]', + passwordInput: 'input[name="passWord"]', + captchaInput: 'input[placeholder*="验证码"]', + captchaImage: 'img[style*="width: 78px"]', + loginButton: 'button.el-button--large.container-button', + loginButtonByText: 'button:text("登录"):not(:text("SSO登录"))', + loginButtonFirst: 'button:has-text("登录")', + ssoLoginButton: 'button:has-text("SSO登录")' + }); + } - // 设置超时时间 - this.timeout = 10000; + /** + * 初始化配置 + * @private + */ + initializeConfig() { + // 调用父类的配置初始化 + super.initializeConfig(); + + // 添加或覆盖特定于 LongiLoginPage 的配置 + Object.assign(this.config, { + loginTimeout: 10000 + }); } /** @@ -30,7 +55,6 @@ class LongiLoginPage extends BasePage { await this.navigate(process.env.BASE_URL); } - /** * 点击登录按钮 * @returns {Promise} 是否成功点击登录按钮 @@ -46,16 +70,16 @@ class LongiLoginPage extends BasePage { console.log('第一种方法失败,尝试备用方法...'); try { // 方法2: 使用精确文本匹配 - await this.page.locator(this.selectors.loginButtonByText).click(); - console.log('使用精确文本匹配成功点击登录按钮'); + await this.safeClick(await this.page.locator(this.selectors.loginButtonByText), '登录按钮(精确文本匹配)'); + // console.log('使用精确文本匹配成功点击登录按钮'); await this.waitForPageLoad(); return true; } catch (secondError) { console.log('第二种方法也失败,尝试第三种方法...'); try { // 方法3: 使用first()选择第一个匹配的按钮 - await this.page.locator(this.selectors.loginButtonFirst).first().click(); - console.log('使用first()方法成功点击登录按钮'); + await this.safeClick(await this.page.locator(this.selectors.loginButtonFirst).first(), '登录按钮(first方法)'); + // console.log('使用first()方法成功点击登录按钮'); await this.waitForPageLoad(); return true; } catch (thirdError) { @@ -76,6 +100,60 @@ class LongiLoginPage extends BasePage { return currentUrl.includes('/dashboard'); } + /** + * 输入用户名 + * @param {string} username 用户名 + */ + async enterUsername(username) { + await this.fill(this.selectors.usernameInput, username); + } + + /** + * 输入密码 + * @param {string} password 密码 + */ + async enterPassword(password) { + await this.fill(this.selectors.passwordInput, password); + } + + /** + * 输入验证码 + * @param {string} captcha 验证码 + */ + async enterCaptcha(captcha) { + await this.fill(this.selectors.captchaInput, captcha); + } + + /** + * 获取验证码图片元素 + * @returns {Promise} 验证码图片元素 + */ + async getCaptchaImage() { + return this.waitForElement(this.selectors.captchaImage); + } + + /** + * 执行登录操作 + * @param {string} username 用户名 + * @param {string} password 密码 + * @param {string} captcha 验证码 + * @returns {Promise} 登录是否成功 + */ + async login(username, password, captcha) { + try { + await this.enterUsername(username); + await this.enterPassword(password); + await this.enterCaptcha(captcha); + + if (await this.clickLoginButton()) { + return await this.isLoginSuccessful(); + } + return false; + } catch (error) { + console.error('登录过程出错:', error.message); + return false; + } + } } module.exports = LongiLoginPage; \ No newline at end of file