新增e2e测试脚本,修复部分问题

This commit was merged in pull request #51.
This commit is contained in:
2026-07-22 20:00:13 +08:00
parent 53d1ce6fb2
commit 4c07ec5455
105 changed files with 21700 additions and 318 deletions
@@ -0,0 +1,199 @@
const { sleep } = require('../helpers/automator');
class CourseDetailPage {
/**
* @param {object} miniProgram - miniprogram-automator 实例
*/
constructor(miniProgram) {
this.mp = miniProgram;
}
/**
* 等待课程详情页加载完成
*/
async waitForLoad() {
const page = await this.mp.currentPage();
await page.waitFor('.detail-page', { timeout: 10000 });
await sleep(1500); // 等待异步数据加载
return page;
}
/**
* 获取课程基本信息
* @returns {Promise<{courseName: string, status: string, time: string, location: string, memberCount: string}>}
*/
async getCourseInfo() {
const page = await this.mp.currentPage();
const info = {};
try {
// 课程名
const titleEl = await page.$('.detail-title');
info.courseName = titleEl ? (await titleEl.text()).trim() : '';
// 状态标签
const statusEl = await page.$('.cover-status text');
info.status = statusEl ? (await statusEl.text()).trim() : '';
// 信息项
const infoItems = await page.$$('.info-item');
for (const item of infoItems) {
try {
const labelEl = await item.$('.info-label');
const valueEl = await item.$('.info-value');
if (labelEl && valueEl) {
const label = (await labelEl.text()).trim();
const value = (await valueEl.text()).trim();
if (label === '时间') info.time = value;
if (label === '地点') info.location = value;
if (label === '人数') info.memberCount = value;
}
} catch (e) {
// 跳过
}
}
} catch (e) {
// 忽略
}
return info;
}
/**
* 点击开课按钮
* 仅当 course.status == 0 (待开课) 时可用
*/
async startCourse() {
const page = await this.mp.currentPage();
const startBtn = await page.$('.start-btn');
if (!startBtn) {
throw new Error('未找到开课按钮(课程可能不是"待开课"状态)');
}
// 检查按钮是否被禁用
const isDisabled = await this._isButtonDisabled(startBtn);
if (isDisabled) {
throw new Error('开课按钮被禁用(尚未到开课时间)');
}
await startBtn.tap();
await sleep(3000); // 等待开课请求完成
}
/**
* 点击结课按钮
* 仅当 course.status == 3 || 7 (进行中) 时可用
*/
async endCourse() {
const page = await this.mp.currentPage();
const endBtn = await page.$('.end-btn');
if (!endBtn) {
throw new Error('未找到结课按钮(课程可能不是"进行中"状态)');
}
const isDisabled = await this._isButtonDisabled(endBtn);
if (isDisabled) {
throw new Error('结课按钮被禁用(尚未到结课时间)');
}
await endBtn.tap();
await sleep(3000); // 等待结课请求完成
}
/**
* 获取学员列表
* 注意:当前课程详情页设计中没有独立的学员列表区域,
* 学员人数信息在 info-card 中展示。
* @returns {Promise<{count: number, max: number}>}
*/
async getStudentList() {
const info = await this.getCourseInfo();
if (info.memberCount) {
const match = info.memberCount.match(/(\d+)\s*\/\s*(\d+)/);
if (match) {
return {
count: parseInt(match[1], 10),
max: parseInt(match[2], 10)
};
}
}
return { count: 0, max: 0 };
}
/**
* 生成签到二维码
* 注意:当前课程详情页没有直接的生成二维码按钮。
* 二维码通常通过后端 API 生成,MiniTest 中可通过 evaluate 调用 API。
*/
async generateQRCode() {
const page = await this.mp.currentPage();
// 通过 evaluate 在小程序环境中生成二维码
// 实际二维码生成逻辑取决于后端 API
try {
const qrData = await this.mp.evaluate(() => {
// 尝试获取当前课程的签到信息
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
return {
courseId: currentPage.courseId || '',
timestamp: Date.now()
};
});
return qrData;
} catch (e) {
console.warn('[CourseDetailPage] 生成二维码失败:', e.message);
return null;
}
}
/**
* 获取操作按钮状态
*/
async getActionButtonStatus() {
const page = await this.mp.currentPage();
try {
const startBtn = await page.$('.start-btn');
if (startBtn) {
const text = (await startBtn.text()).trim();
const isDisabled = await this._isButtonDisabled(startBtn);
return { type: 'start', text, disabled: isDisabled };
}
const endBtn = await page.$('.end-btn');
if (endBtn) {
const text = (await endBtn.text()).trim();
const isDisabled = await this._isButtonDisabled(endBtn);
return { type: 'end', text, disabled: isDisabled };
}
const disabledBtn = await page.$('.disabled-btn');
if (disabledBtn) {
const text = (await disabledBtn.text()).trim();
return { type: 'disabled', text, disabled: true };
}
} catch (e) {
// 忽略
}
return null;
}
/**
* 内部方法:检查按钮是否被禁用
*/
async _isButtonDisabled(btnElement) {
try {
const disabled = await btnElement.attribute('disabled');
if (disabled === 'true' || disabled === true || disabled === '') {
return true;
}
// 也检查 CSS class
const className = await btnElement.attribute('class');
return (className || '').includes('disabled');
} catch (e) {
return false;
}
}
}
module.exports = CourseDetailPage;
@@ -0,0 +1,158 @@
const { sleep } = require('../helpers/automator');
class CourseListPage {
/**
* @param {object} miniProgram - miniprogram-automator 实例
*/
constructor(miniProgram) {
this.mp = miniProgram;
}
/**
* 等待课程列表页加载完成
*/
async waitForLoad() {
const page = await this.mp.currentPage();
await page.waitFor('.course-list-page', { timeout: 10000 });
await sleep(1500); // 等待数据加载
return page;
}
/**
* 获取课程列表
* @returns {Promise<Array<{name: string, status: string, location: string}>>}
*/
async getCourseList() {
const page = await this.mp.currentPage();
const cards = await page.$$('.course-card');
const courses = [];
for (const card of cards) {
try {
const nameEl = await card.$('.course-name');
const statusEl = await card.$('.status-tag text');
const name = nameEl ? (await nameEl.text()).trim() : '';
const status = statusEl ? (await statusEl.text()).trim() : '';
courses.push({ name, status });
} catch (e) {
// 跳过无法解析的卡片
}
}
return courses;
}
/**
* 获取课程卡片数量
*/
async getCourseCount() {
const page = await this.mp.currentPage();
try {
const cards = await page.$$('.course-card');
return cards.length;
} catch (e) {
return 0;
}
}
/**
* 按状态筛选课程
* @param {string} status - 'all' | 'pending' | 'in_progress' | 'ended'
*/
async filterByStatus(status) {
const page = await this.mp.currentPage();
const tabValueMap = {
all: 0,
pending: 1,
in_progress: 2,
ended: 3
};
const tabIndex = tabValueMap[status];
if (tabIndex === undefined) {
throw new Error(`未知的筛选状态: ${status}`);
}
const tabs = await page.$$('.tab-item');
if (tabs.length > tabIndex) {
await tabs[tabIndex].tap();
await sleep(1500); // 等待筛选结果渲染
}
}
/**
* 点击指定索引的课程卡片
* @param {number} index - 课程卡片索引(从0开始)
*/
async clickCourse(index = 0) {
const page = await this.mp.currentPage();
const cards = await page.$$('.course-card');
if (cards.length <= index) {
throw new Error(`课程索引 ${index} 超出范围,当前共 ${cards.length} 个课程`);
}
await cards[index].tap();
await sleep(2000); // 等待课程详情页加载
return this.mp.currentPage();
}
/**
* 验证某个课程是否有学员(人数 > 0)
* @param {number} index - 课程卡片索引
*/
async verifyCourseHasStudents(index = 0) {
const page = await this.mp.currentPage();
const cards = await page.$$('.course-card');
if (cards.length <= index) {
return false;
}
try {
const infoRows = await cards[index].$$('.info-row');
// 最后一行为 "人数 X / Y" 信息
if (infoRows.length > 0) {
const lastRow = infoRows[infoRows.length - 1];
const valueEl = await lastRow.$('.info-value');
if (valueEl) {
const text = (await valueEl.text()).trim();
const match = text.match(/^(\d+)\s*\/\s*\d+$/);
if (match) {
return parseInt(match[1], 10) > 0;
}
}
}
} catch (e) {
// 忽略
}
return false;
}
/**
* 检查是否显示"暂无课程"的空状态
*/
async isEmpty() {
const page = await this.mp.currentPage();
try {
const emptyEl = await page.$('.empty-wrap');
return !!emptyEl;
} catch (e) {
return false;
}
}
/**
* 检查是否仍在加载中
*/
async isLoading() {
const page = await this.mp.currentPage();
try {
const loadingEl = await page.$('.loading-wrap');
return !!loadingEl;
} catch (e) {
return false;
}
}
}
module.exports = CourseListPage;
@@ -0,0 +1,114 @@
const { sleep } = require('../helpers/automator');
class HomePage {
/**
* @param {object} miniProgram - miniprogram-automator 实例
*/
constructor(miniProgram) {
this.mp = miniProgram;
}
/**
* 等待首页加载完成
*/
async waitForLoad() {
const page = await this.mp.currentPage();
await page.waitFor('.greeting-card', { timeout: 10000 });
await page.waitFor('.stats-card', { timeout: 5000 });
return page;
}
/**
* 获取当前登录的教练名字
* @returns {Promise<string>}
*/
async getCoachName() {
const page = await this.mp.currentPage();
try {
const nameEl = await page.$('.coach-name');
if (nameEl) {
const text = await nameEl.text();
return (text || '').replace(' 教练', '').trim();
}
} catch (e) {
// 忽略
}
return null;
}
/**
* 获取今日统计信息(待开课、进行中、已结束)
* @returns {Promise<{pending: string, inProgress: string, ended: string}>}
*/
async getTodayStats() {
const page = await this.mp.currentPage();
try {
const statNumbers = await page.$$('.stat-number');
const stats = {};
if (statNumbers.length >= 3) {
stats.pending = (await statNumbers[0].text()).trim();
stats.inProgress = (await statNumbers[1].text()).trim();
stats.ended = (await statNumbers[2].text()).trim();
}
return stats;
} catch (e) {
return { pending: '0', inProgress: '0', ended: '0' };
}
}
/**
* 导航到课程列表页
*/
async navigateToCourseList() {
const page = await this.mp.currentPage();
// 点击"我的团课"入口卡片
const entryCards = await page.$$('.entry-card');
if (entryCards.length > 0) {
await entryCards[0].tap();
} else {
// 备用:通过 URL 导航
await this.mp.navigateTo('/pages/course-list/course-list');
}
await sleep(2000);
return this.mp.currentPage();
}
/**
* 导航到个人中心
*/
async navigateToProfile() {
const page = await this.mp.currentPage();
const entryCards = await page.$$('.entry-card');
if (entryCards.length >= 2) {
await entryCards[1].tap();
} else {
await this.mp.navigateTo('/pages/profile/profile');
}
await sleep(2000);
return this.mp.currentPage();
}
/**
* 验证首页关键元素存在
*/
async verifyPageElements() {
const page = await this.mp.currentPage();
const checks = {};
try {
checks.greetingCard = !!(await page.$('.greeting-card'));
} catch (e) { checks.greetingCard = false; }
try {
checks.statsCard = !!(await page.$('.stats-card'));
} catch (e) { checks.statsCard = false; }
try {
checks.navTitle = !!(await page.$('.nav-title'));
} catch (e) { checks.navTitle = false; }
return checks;
}
}
module.exports = HomePage;
@@ -0,0 +1,101 @@
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;
@@ -0,0 +1,158 @@
const { sleep } = require('../helpers/automator');
class ProfilePage {
/**
* @param {object} miniProgram - miniprogram-automator 实例
*/
constructor(miniProgram) {
this.mp = miniProgram;
}
/**
* 等待个人中心页加载完成
*/
async waitForLoad() {
const page = await this.mp.currentPage();
await page.waitFor('.profile-page', { timeout: 10000 });
await sleep(1500); // 等待异步数据加载
return page;
}
/**
* 获取教练基本信息
* @returns {Promise<{username: string, coachId: string}>}
*/
async getCoachInfo() {
const page = await this.mp.currentPage();
const info = {};
try {
const nameEl = await page.$('.user-name');
info.username = nameEl ? (await nameEl.text()).trim() : '';
const idEl = await page.$('.detail-value');
info.coachId = idEl ? (await idEl.text()).trim() : '';
} catch (e) {
// 忽略
}
return info;
}
/**
* 获取违规记录
* @returns {Promise<Array<{courseName: string, type: string, time: string}>>}
*/
async getViolationRecords() {
const page = await this.mp.currentPage();
const records = [];
try {
// 先检查是否有违规记录
const statNumber = await page.$('.stats-card .stat-number');
if (statNumber) {
const countText = (await statNumber.text()).trim();
const count = parseInt(countText, 10);
if (count > 0) {
const cards = await page.$$('.violation-card');
for (const card of cards) {
try {
const courseEl = await card.$('.violation-course');
const tagEl = await card.$('.violation-tag text');
const timeEl = await card.$('.violation-time');
records.push({
courseName: courseEl ? (await courseEl.text()).trim() : '',
type: tagEl ? (await tagEl.text()).trim() : '',
time: timeEl ? (await timeEl.text()).trim() : ''
});
} catch (e) {
// 跳过无法解析的记录
}
}
}
}
} catch (e) {
// 忽略
}
return records;
}
/**
* 获取违规次数统计
*/
async getViolationCount() {
const page = await this.mp.currentPage();
try {
const statNumber = await page.$('.stats-card .stat-number');
if (statNumber) {
const text = (await statNumber.text()).trim();
return parseInt(text, 10);
}
} catch (e) {
// 忽略
}
return 0;
}
/**
* 获取业绩数据
* @returns {Promise<{totalLessons: string, attendanceRate: string, compositeScore: string}>}
*/
async getPerformance() {
const page = await this.mp.currentPage();
const perf = {};
try {
const perfNumbers = await page.$$('.perf-number');
if (perfNumbers.length >= 4) {
perf.totalLessons = (await perfNumbers[0].text()).trim();
// perfNumbers[1] = totalAttendees
perf.attendanceRate = (await perfNumbers[2].text()).trim();
perf.fullnessRate = (await perfNumbers[3].text()).trim();
}
const scoreEl = await page.$('.score-badge');
perf.compositeScore = scoreEl ? (await scoreEl.text()).trim() : '';
} catch (e) {
// 忽略
}
return perf;
}
/**
* 点击退出登录
*/
async logout() {
const page = await this.mp.currentPage();
const logoutBtn = await page.$('.logout-btn');
if (logoutBtn) {
await logoutBtn.tap();
await sleep(1000);
// 确认弹窗需要额外处理
// MiniTest 中弹窗按钮难以直接操作,这里只触发点击
}
}
/**
* 返回上一页
*/
async goBack() {
const page = await this.mp.currentPage();
try {
const backBtn = await page.$('.back-btn');
if (backBtn) {
await backBtn.tap();
await sleep(1000);
}
} catch (e) {
// 备用:使用系统返回
await this.mp.navigateBack();
}
}
}
module.exports = ProfilePage;