93 lines
2.6 KiB
JavaScript
93 lines
2.6 KiB
JavaScript
/**
|
||
* 我的课程页 Page Object
|
||
*/
|
||
class MyCoursesPage {
|
||
constructor(miniProgram) {
|
||
this.mp = miniProgram;
|
||
}
|
||
|
||
async waitForReady() {
|
||
const page = await this.mp.currentPage();
|
||
await page.waitFor(2000);
|
||
console.log('我的课程页加载完成');
|
||
}
|
||
|
||
async getPath() {
|
||
const page = await this.mp.currentPage();
|
||
return page.path;
|
||
}
|
||
|
||
/**
|
||
* 点击取消预约按钮
|
||
*/
|
||
async clickCancelBooking(index = 0) {
|
||
const page = await this.mp.currentPage();
|
||
const cancelBtns = await page.$$('.cancel-btn');
|
||
if (cancelBtns.length > index) {
|
||
await cancelBtns[index].tap();
|
||
console.log(`取消第${index + 1}个预约`);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 扫码签到:mock wx.scanCode 后点击签到按钮
|
||
* @param {string} courseName 课程名称(如"流瑜伽")- 用于匹配对应 booking card
|
||
*/
|
||
async scanSignIn(courseName) {
|
||
const page = await this.mp.currentPage();
|
||
|
||
// 先通过 page data 确认目标课程存在
|
||
const pageData = await page.data();
|
||
const bookings = pageData.bookings || [];
|
||
const targetBooking = bookings.find(b => b.courseName === courseName);
|
||
if (!targetBooking) {
|
||
console.log(`未在数据中找到「${courseName}」的预约记录`);
|
||
return false;
|
||
}
|
||
const targetIndex = bookings.indexOf(targetBooking);
|
||
console.log(`「${courseName}」在 bookings[${targetIndex}]`);
|
||
|
||
// 通过 DOM 元素匹配
|
||
const cards = await page.$$('.booking-card');
|
||
console.log(`找到 ${cards.length} 个预约卡片`);
|
||
|
||
if (cards.length > targetIndex) {
|
||
const signBtn = await cards[targetIndex].$('.signin-btn');
|
||
if (signBtn) {
|
||
await signBtn.tap();
|
||
console.log(`点击「${courseName}」扫码签到按钮`);
|
||
return true;
|
||
}
|
||
console.log('未找到 signin-btn,尝试 wxml 匹配');
|
||
|
||
// 回退:通过 wxml 匹配
|
||
for (let i = 0; i < cards.length; i++) {
|
||
const wxml = await cards[i].wxml();
|
||
if (wxml.includes(courseName)) {
|
||
const btn = await cards[i].$('.signin-btn');
|
||
if (btn) {
|
||
await btn.tap();
|
||
console.log('点击扫码签到按钮');
|
||
return true;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
console.log(`未找到「${courseName}」的签到按钮`);
|
||
return false;
|
||
}
|
||
|
||
async containsText(text) {
|
||
const page = await this.mp.currentPage();
|
||
const wxml = await page.wxml();
|
||
return wxml.includes(text);
|
||
}
|
||
|
||
async getPageData() {
|
||
const page = await this.mp.currentPage();
|
||
return await page.data();
|
||
}
|
||
}
|
||
|
||
module.exports = { MyCoursesPage };
|