/** * CourseDetailPage - 课程详情页 Page Object * 对应页面: pages/course-detail/course-detail */ class CourseDetailPage { constructor(miniProgram) { this.mp = miniProgram; } /** * 等待课程详情页加载完成 */ async waitForLoad() { const { waitForPage } = require('../helpers/automator'); return waitForPage(this.mp, 'pages/course-detail/course-detail'); } /** * 获取课程名称 * @returns {Promise} */ async getCourseName() { const page = await this.mp.currentPage(); try { const nameEl = await page.$('.course-name'); if (nameEl) { return await nameEl.text(); } } catch (e) { console.warn('[CourseDetailPage] 获取课程名称异常:', e.message); } return ''; } /** * 获取教练名称 * @returns {Promise} */ async getCoachName() { const page = await this.mp.currentPage(); try { const coachEl = await page.$('.coach-name'); if (coachEl) { return await coachEl.text(); } } catch (e) { console.warn('[CourseDetailPage] 获取教练名称异常:', e.message); } return ''; } /** * 获取课程详情信息 * @returns {Promise} */ async getCourseInfo() { const page = await this.mp.currentPage(); const info = { name: '', coach: '', time: '', location: '' }; try { info.name = await this.getCourseName(); info.coach = await this.getCoachName(); const timeEl = await page.$('.course-time'); if (timeEl) { info.time = await timeEl.text(); } const locationEl = await page.$('.course-location'); if (locationEl) { info.location = await locationEl.text(); } } catch (e) { console.warn('[CourseDetailPage] 获取课程信息异常:', e.message); } return info; } /** * 点击预约按钮 * @returns {Promise} 是否点击成功 */ async bookCourse() { const page = await this.mp.currentPage(); console.log('[CourseDetailPage] 点击预约按钮'); try { const bookBtn = await page.$('.book-btn'); if (bookBtn) { await bookBtn.tap(); await this.mp.waitFor(2000); return true; } // 尝试其他可能的按钮选择器 const confirmBtn = await page.$('.confirm-btn'); if (confirmBtn) { await confirmBtn.tap(); await this.mp.waitFor(2000); return true; } } catch (e) { console.warn('[CourseDetailPage] 预约按钮点击异常:', e.message); } return false; } /** * 验证预约成功提示 * @returns {Promise} */ async verifyBookingSuccess() { console.log('[CourseDetailPage] 验证预约成功...'); await this.mp.waitFor(1000); const page = await this.mp.currentPage(); try { // 检查 toast 提示 const toast = await page.$('.uni-toast'); if (toast) { const toastText = await toast.text(); console.log(`[CourseDetailPage] Toast 内容: "${toastText}"`); return toastText.includes('成功') || toastText.includes('预约'); } } catch (e) { console.warn('[CourseDetailPage] 验证 toast 异常:', e.message); } // 如果页面跳转到"我的课程",也视为成功 try { const currentPage = await this.mp.currentPage(); if (currentPage.path && currentPage.path.includes('my-courses')) { return true; } } catch (e) { // ignore } return false; } /** * 返回上一页 */ async goBack() { try { await this.mp.navigateBack(); await this.mp.waitFor(1000); } catch (e) { console.warn('[CourseDetailPage] 返回异常:', e.message); } } } module.exports = CourseDetailPage;