优化文件路径

This commit is contained in:
dengqichen 2025-03-06 10:14:41 +08:00
parent 25c56b2e14
commit 431551a9ae
2 changed files with 57 additions and 46 deletions

View File

@ -10,7 +10,23 @@ class BasePage {
*/ */
constructor(page) { constructor(page) {
this.page = page; this.page = page;
this.timeout = 30000; // 默认使用配置中的元素超时时间 this.initializeConfig();
}
/**
* 初始化配置
* @protected
*/
initializeConfig() {
this.config = {
timeout: 30000, // 默认超时时间
pageLoad: {
maxRetries: 30,
retryInterval: 500,
stabilityDelay: 1000
},
menuTimeout: parseInt(process.env.MENU_TIME_OUT || '30000', 10)
};
} }
/** /**
@ -40,7 +56,7 @@ class BasePage {
await element.waitFor({ await element.waitFor({
state: 'visible', state: 'visible',
timeout: options.timeout || this.timeout timeout: options.timeout || this.config.timeout
}); });
return options.returnBoolean ? true : element; return options.returnBoolean ? true : element;
@ -177,6 +193,39 @@ class BasePage {
return false; return false;
} }
} }
/**
* 等待指定时间
* @param {number} ms 等待时间毫秒
*/
async wait(ms) {
await this.page.waitForTimeout(ms);
}
/**
* 检查元素是否存在
* @param {string} selector 元素选择器
* @returns {Promise<boolean>} 是否存在
*/
async elementExists(selector) {
const count = await this.page.locator(selector).count();
return count > 0;
}
/**
* 获取元素文本
* @param {Object} element Playwright元素对象
* @returns {Promise<string>} 元素文本
*/
async getElementText(element) {
try {
const text = await element.textContent();
return text.trim();
} catch (error) {
console.error('获取元素文本失败:', error.message);
return '';
}
}
} }
module.exports = BasePage; module.exports = BasePage;

View File

@ -46,51 +46,13 @@ class LongiMainPage extends BasePage {
* @private * @private
*/ */
initializeConfig() { initializeConfig() {
this.config = { // 调用父类的配置初始化
timeout: 10000, super.initializeConfig();
pageLoad: {
maxRetries: 30, // 添加或覆盖特定于 LongiMainPage 的配置
retryInterval: 500, Object.assign(this.config, {
stabilityDelay: 1000
},
menuTimeout: parseInt(process.env.MENU_TIME_OUT || '30000', 10) menuTimeout: parseInt(process.env.MENU_TIME_OUT || '30000', 10)
}; });
}
/**
* 等待指定时间
* @param {number} ms 等待时间毫秒
* @private
*/
async wait(ms) {
await this.page.waitForTimeout(ms);
}
/**
* 检查元素是否存在
* @param {string} selector 元素选择器
* @returns {Promise<boolean>} 是否存在
* @private
*/
async elementExists(selector) {
const count = await this.page.locator(selector).count();
return count > 0;
}
/**
* 获取元素文本
* @param {Object} element Playwright元素对象
* @returns {Promise<string>} 元素文本
* @private
*/
async getElementText(element) {
try {
const text = await element.textContent();
return text.trim();
} catch (error) {
console.error('获取元素文本失败:', error.message);
return '';
}
} }
/** /**