From 6e36aa1cd395bdf7c3aa5c61c28e3b3898f43f32 Mon Sep 17 00:00:00 2001 From: dengqichen Date: Thu, 6 Mar 2025 13:19:29 +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/longi-ibp/check-page-normal.test.js | 6 ----- tests/pages/BasePage.js | 33 ++++++++++++----------- tests/pages/LongiLoginPage.js | 10 +++---- tests/pages/LongiMainPage.js | 14 +++++----- 4 files changed, 30 insertions(+), 33 deletions(-) diff --git a/tests/longi-ibp/check-page-normal.test.js b/tests/longi-ibp/check-page-normal.test.js index e34b1e5..5dc37b0 100644 --- a/tests/longi-ibp/check-page-normal.test.js +++ b/tests/longi-ibp/check-page-normal.test.js @@ -14,18 +14,12 @@ test('隆基登录', async ({page}) => { // 2. 导航到登录页面 await loginPage.navigateToLoginPage(); - // 3. 等待页面加载完成 - await page.waitForLoadState('networkidle'); - // 4. 点击登录按钮 - 使用页面对象模型 const clickSuccess = await loginPage.clickLoginButton(); // 5. 验证点击是否成功 expect(clickSuccess, '验证是否登录成功').toBeTruthy(); - let loginSuccess = await loginPage.isLoginSuccessful(); - console.log(`登录状态: ${loginSuccess ? '成功' : '失败'}`); - // 10. 检查菜单数据文件是否存在 let menuItems = await mainPage.checkAndLoadMenuItems(); diff --git a/tests/pages/BasePage.js b/tests/pages/BasePage.js index 1ed3140..8f771aa 100644 --- a/tests/pages/BasePage.js +++ b/tests/pages/BasePage.js @@ -56,13 +56,16 @@ class BasePage { */ initializeConfig() { this.config = { - timeout: 30000, // 默认超时时间 + timeout: { + page: 30000, + element: 30000, + navigate: 30000 + }, pageLoad: { maxRetries: 30, retryInterval: 500, stabilityDelay: 1000 - }, - menuTimeout: parseInt(process.env.MENU_TIME_OUT || '30000', 10) + } }; } @@ -73,7 +76,7 @@ class BasePage { async navigate(url) { await this.page.goto(url, { waitUntil: 'networkidle', - timeout: 30000 + timeout: this.config.timeout.navigate }); } @@ -87,13 +90,11 @@ class BasePage { */ async waitForElement(selector, options = {}) { try { - const element = options.firstOnly ? - this.page.locator(selector).first() : - this.page.locator(selector); + const element = options.firstOnly ? this.page.locator(selector).first() : this.page.locator(selector); await element.waitFor({ state: 'visible', - timeout: options.timeout || this.config.timeout + timeout: this.config.timeout.element }); return options.returnBoolean ? true : element; @@ -110,7 +111,7 @@ class BasePage { * @param {string} selector 元素选择器 * @param {Object} options 选项 */ - async click(selector, options = {}) { + async clickBySelector(selector, options = {}) { const element = await this.waitForElement(selector, options); await element.click(options); } @@ -120,7 +121,7 @@ class BasePage { * @param {Object} element Playwright元素对象 * @returns {Promise} 是否点击成功 */ - async safeClick(element) { + async clickByElement(element) { try { await element.click(); return true; @@ -135,7 +136,7 @@ class BasePage { * @param {string} selector 要点击的元素选择器 * @returns {Promise} 操作是否成功 */ - async clickAndWaitForLoad(selector) { + async clickBySelectorAndWaitForLoad(selector) { try { const element = await this.waitForElement(selector); await element.click(); @@ -147,9 +148,9 @@ class BasePage { } } - async safeClickAndWaitForLoad(element) { + async clickByElementAndWaitForLoad(element) { try { - await this.click(element); + await this.clickBySelector(element); await this.waitForPageLoad(); return true; } catch (error) { @@ -174,7 +175,7 @@ class BasePage { */ async waitForPageLoad() { await this.page.waitForLoadState('networkidle', { - timeout: 30000 + timeout: this.config.timeout.page }); } @@ -232,7 +233,7 @@ class BasePage { for (const selector of errorSelectors) { const elements = await this.page.locator(selector).all(); if (elements.length > 0) { - const errorText = await this.getElementText(elements[0]); + const errorText = await this.getTextByElement(elements[0]); console.error(`页面加载出现错误: ${pageName}, 错误信息: ${errorText}`); return true; } @@ -274,7 +275,7 @@ class BasePage { * @param {Object} element Playwright元素对象 * @returns {Promise} 元素文本 */ - async getElementText(element) { + async getTextByElement(element) { try { const text = await element.textContent(); return text.trim(); diff --git a/tests/pages/LongiLoginPage.js b/tests/pages/LongiLoginPage.js index e05bbff..99a9b0f 100644 --- a/tests/pages/LongiLoginPage.js +++ b/tests/pages/LongiLoginPage.js @@ -53,6 +53,7 @@ class LongiLoginPage extends BasePage { async navigateToLoginPage() { console.log('当前使用的 BASE_URL:', process.env.BASE_URL); await this.navigate(process.env.BASE_URL); + await this.waitForPageLoad(); } /** @@ -61,17 +62,17 @@ class LongiLoginPage extends BasePage { */ async clickLoginButton() { try { - await this.clickAndWaitForLoad(this.selectors.loginButton) + await this.clickBySelectorAndWaitForLoad(this.selectors.loginButton) return true; } catch (error) { console.log('第一种方法失败,尝试备用方法...'); try { - await this.clickAndWaitForLoad(this.selectors.loginButtonByText) + await this.clickBySelectorAndWaitForLoad(this.selectors.loginButtonByText) return true; } catch (secondError) { console.log('第二种方法也失败,尝试第三种方法...'); try { - await this.safeClickAndWaitForLoad(await this.page.locator(this.selectors.loginButtonFirst).first()); + await this.clickByElementAndWaitForLoad(await this.page.locator(this.selectors.loginButtonFirst).first()); return true; } catch (thirdError) { console.error('所有方法都失败,无法点击登录按钮', thirdError); @@ -87,8 +88,7 @@ class LongiLoginPage extends BasePage { */ async isLoginSuccessful() { // 登录成功后通常会重定向到其他页面 - const currentUrl = await this.getCurrentUrl(); - return currentUrl.includes('/dashboard'); + return (await this.getCurrentUrl()).includes('/dashboard'); } /** diff --git a/tests/pages/LongiMainPage.js b/tests/pages/LongiMainPage.js index 5a1de25..408e59c 100644 --- a/tests/pages/LongiMainPage.js +++ b/tests/pages/LongiMainPage.js @@ -49,7 +49,9 @@ class LongiMainPage extends BasePage { // 添加或覆盖特定于 LongiMainPage 的配置 Object.assign(this.config, { - menuTimeout: parseInt(process.env.MENU_TIME_OUT || '30000', 10) + timeout: { + menuTimeout: parseInt(process.env.MENU_TIME_OUT || '30000', 10) + } }); } @@ -234,7 +236,7 @@ class LongiMainPage extends BasePage { * @returns {string} 菜单路径 * @private */ - async getMenuPath(menuInfo, parentMenu = null) { + getMenuPath(menuInfo, parentMenu = null) { return parentMenu ? `${parentMenu.text} > ${menuInfo.text}` : menuInfo.text; } @@ -404,10 +406,10 @@ class LongiMainPage extends BasePage { * @param {Object} parentMenu 父级菜单(可选) */ async handleMenuClick(menuInfo, parentMenu = null) { - const menuPath = this.getMenuPath(menuInfo, parentMenu); + const menuPath = await this.getMenuPath(menuInfo, parentMenu); console.log(`点击菜单: ${menuPath}`); - if (!await this.safeClick(menuInfo.element)) { + if (!await this.clickByElement(menuInfo.element)) { return; } @@ -476,7 +478,7 @@ class LongiMainPage extends BasePage { async getTabInfos(tabs) { return Promise.all( tabs.map(async element => ({ - text: await this.getElementText(element), + text: await this.getTextByElement(element), isActive: await element.evaluate(el => el.classList.contains('is-active')), element: element })) @@ -495,7 +497,7 @@ class LongiMainPage extends BasePage { if (await this.canCloseTab(activeTab, closeButton)) { await closeButton.waitFor({state: 'visible', timeout: 5000}); - await this.safeClick(closeButton); + await this.clickByElement(closeButton); await this.waitForTimeout(500); } else { console.log(`⚠️ [${parentMenu.text}] 没有找到可关闭的tab,继续执行...`);