Files
gym-manage/gym-manage-coach-uniapp/e2e/pages/LoginPage.js
T

102 lines
2.5 KiB
JavaScript

const { sleep } = require('../helpers/automator');
class LoginPage {
/**
* @param {object} miniProgram - miniprogram-automator 实例
*/
constructor(miniProgram) {
this.mp = miniProgram;
}
/**
* 等待登录页加载完成
*/
async waitForLoad() {
const page = await this.mp.currentPage();
await page.waitFor('.login-form', { timeout: 10000 });
await page.waitFor('.login-btn', { timeout: 10000 });
return page;
}
/**
* 填写用户名和密码并提交登录
* @param {string} username
* @param {string} password
*/
async login(username, password) {
const page = await this.mp.currentPage();
await page.waitFor('input', { timeout: 10000 });
const inputs = await page.$$('input');
if (inputs.length < 2) {
throw new Error('登录页未找到用户名/密码输入框');
}
// 清空并填写用户名
await inputs[0].input(username);
await sleep(300);
// 清空并填写密码
await inputs[1].input(password);
await sleep(300);
// 点击登录按钮
const loginBtn = await page.$('.login-btn');
if (!loginBtn) {
throw new Error('未找到登录按钮');
}
await loginBtn.tap();
}
/**
* 获取页面上的错误提示(Toast)
* 注意:小程序 Toast 通过 wx.showToast 展示,MiniTest 中难以直接捕获。
* 此方法返回页面上的错误元素文本。
*/
async getErrorMessage() {
try {
const page = await this.mp.currentPage();
// 检查是否有 toast 相关的错误元素
const toastEl = await page.$('.uni-toast__content');
if (toastEl) {
const text = await toastEl.text();
return text;
}
} catch (e) {
// 忽略
}
return null;
}
/**
* 等待登录成功后跳转到首页
* @param {number} timeoutMs
*/
async waitForLoginSuccess(timeoutMs = 15000) {
const startTime = Date.now();
while (Date.now() - startTime < timeoutMs) {
try {
const page = await this.mp.currentPage();
if (page.path.includes('pages/index/index')) {
await sleep(1000); // 等待首页渲染
return page;
}
} catch (e) {
// 页面跳转中
}
await sleep(500);
}
throw new Error(`等待登录成功跳转超时 (${timeoutMs}ms)`);
}
/**
* 验证是否在登录页
*/
async isOnLoginPage() {
const page = await this.mp.currentPage();
return page.path.includes('pages/login/login');
}
}
module.exports = LoginPage;