后端: - /ai/collaboration-analysis 平台资料不足时自动合并拆书帮会员资料 (tags/description/分舵/城市计入完善度,AI prompt 附会员身份) 修复:杨珂鸣分析刘艳红误报「资料不足」,现 78 分推荐 - /chaishu/members/:uid 详情路由补 boundUserId/boundNickname/boundAvatar/boundPosition - 等级措辞统一:非拆书家→非会员(AI prompt + 存量数据已修正) 前端: - 新增 pages/chaishu-member-detail 会员名片页:未入驻→「邀请 TA 使用拆约」分享; 已入驻→「邀请 TA 共拆」跳协作分析 - home onBuddyTap / chaishu-members onMemberTap:未绑定会员跳名片页(原误跳会员列表/toast) - profile/activity-detail:非会员灰显「非会员」无徽章;edit-profile 选项更新 - app.js verifyUserExists 网络异常不再清登录态;clearLoginInfo 对齐 clearLoginState - home/more-activities handleQuickJoin catch 补 silent 检查,修「报名误弹请登录」
337 lines
12 KiB
JavaScript
337 lines
12 KiB
JavaScript
// 主流程自动化测试 - 12条核心流程
|
|
// 每次操作前获取全新page引用,避免 page destroyed
|
|
const automator = require('miniprogram-automator');
|
|
const DIR = 'D:/codeProject/邀约-ClaudeCode开发/yao-yue-app/wechat-miniprogram/test/screenshots';
|
|
let okN = 0, failN = 0, skipN = 0;
|
|
const results = [];
|
|
function ok(m) { okN++; console.log(' ✅ ' + m); results.push('✅ ' + m); }
|
|
function no(m) { failN++; console.error(' ❌ ' + m); results.push('❌ ' + m); }
|
|
function sk(m) { skipN++; console.log(' ⏭ ' + m); results.push('⏭ ' + m); }
|
|
function hdr(t) { console.log('\n' + '='.repeat(50) + '\n' + t + '\n' + '='.repeat(50)); }
|
|
const wait = ms => new Promise(r => setTimeout(r, ms));
|
|
|
|
// 带超时的导航
|
|
async function navTo(mp, method, url, timeout = 10000) {
|
|
try { await Promise.race([mp[method](url), wait(timeout)]); }
|
|
catch(e) { /* 导航超时或失败,继续 */ }
|
|
await wait(3000);
|
|
}
|
|
|
|
// 核心:每次操作都重新获取page,操作完立即丢弃引用
|
|
async function withPage(mp, fn, retries = 5) {
|
|
for (let i = 0; i < retries; i++) {
|
|
try {
|
|
const pg = await mp.currentPage();
|
|
return await fn(pg); // pg 仅在 fn 内部有效,fn 返回后丢弃
|
|
} catch(e) {
|
|
const msg = (e && e.message) || String(e);
|
|
if (i === retries - 1) throw e; // 最后一次,抛出
|
|
await wait(500); // 短暂等待后重试
|
|
}
|
|
}
|
|
}
|
|
|
|
async function main() {
|
|
const mp = await automator.connect({ wsEndpoint: 'ws://127.0.0.1:9420' });
|
|
console.log('✅ 已连接\n');
|
|
|
|
try {
|
|
// ============ 流程1: 首页加载 ============
|
|
hdr('流程1: 首页加载');
|
|
await Promise.race([mp.reLaunch('/pages/home/index'), wait(15000)]).catch(() => {}); await wait(4000);
|
|
|
|
await withPage(mp, async pg => {
|
|
ok('路径=' + pg.path);
|
|
});
|
|
await withPage(mp, async pg => {
|
|
const d = await pg.data();
|
|
ok('Tab=' + d.currentTab);
|
|
ok('活动字段=' + (d.activities ? '✓' : '✗'));
|
|
});
|
|
await withPage(mp, async pg => {
|
|
const items = await pg.$$('.activity-item');
|
|
ok('活动卡片=' + items.length);
|
|
});
|
|
await withPage(mp, async pg => {
|
|
const banners = await pg.$$('.banner-item');
|
|
ok('轮播图=' + banners.length);
|
|
});
|
|
await mp.screenshot({ path: DIR + '/flow-01-home.png' });
|
|
|
|
// ============ 流程2: Tab切换("最新活动"已并入"推荐"页) ============
|
|
hdr('流程2: Tab切换');
|
|
for (const { tab, label } of [
|
|
{ tab: 'joined', label: '参与' },
|
|
{ tab: 'created', label: '发起' },
|
|
]) {
|
|
await withPage(mp, async pg => {
|
|
const els = await pg.$$('.tab-item');
|
|
for (const el of els) {
|
|
const txt = await el.text();
|
|
if (txt && txt.includes(label)) {
|
|
await el.tap();
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}).then(hit => {
|
|
if (hit) ok('切换"' + label + '"');
|
|
else sk('tab未找到: ' + label);
|
|
});
|
|
await wait(2000);
|
|
}
|
|
|
|
await withPage(mp, async pg => {
|
|
await pg.callMethod('onTabChange', { currentTarget: { dataset: { tab: 'joined' } } });
|
|
});
|
|
await wait(2000);
|
|
await mp.screenshot({ path: DIR + '/flow-02-tabs.png' });
|
|
|
|
// ============ 流程3: 活动详情 ============
|
|
hdr('流程3: 活动详情');
|
|
let targetActivityId = null, targetActivityTitle = null;
|
|
await withPage(mp, async pg => {
|
|
const d = await pg.data();
|
|
if (d.activities && d.activities[0]) {
|
|
targetActivityId = d.activities[0].id;
|
|
targetActivityTitle = d.activities[0].title;
|
|
}
|
|
});
|
|
|
|
if (!targetActivityId) { sk('无活动数据'); } else {
|
|
ok('活动: ' + targetActivityTitle);
|
|
await Promise.race([
|
|
mp.navigateTo('/pages/activity-detail/index?id=' + targetActivityId),
|
|
wait(15000)
|
|
]).catch(() => {});
|
|
await wait(5000);
|
|
|
|
await withPage(mp, async pg => {
|
|
if (pg.path !== 'pages/activity-detail/index') {
|
|
sk('导航失败=' + pg.path);
|
|
return;
|
|
}
|
|
const d = await pg.data();
|
|
ok('详情: ' + (d.activity ? '✓' : '✗'));
|
|
if (d.activity) {
|
|
ok('标题: ' + d.activity.title);
|
|
ok('地点: ' + d.activity.location);
|
|
}
|
|
});
|
|
await mp.screenshot({ path: DIR + '/flow-03-detail.png' });
|
|
await Promise.race([mp.navigateBack(), wait(10000)]).catch(() => {}); await wait(1500);
|
|
}
|
|
|
|
// ============ 流程4: 更多活动页 ============
|
|
hdr('流程4: 更多活动页');
|
|
await navTo(mp, 'switchTab', '/pages/more-activities/index');
|
|
await wait(3000);
|
|
|
|
await withPage(mp, async pg => {
|
|
ok('当前页=' + pg.path);
|
|
const d = await pg.data();
|
|
ok('列表=' + (d.activities?.length || 0) + '条');
|
|
const items = await pg.$$('.activity-item');
|
|
if (items.length > 0) {
|
|
await items[0].tap();
|
|
return true;
|
|
}
|
|
return false;
|
|
}).then(clicked => { if (clicked) { wait(3000); } });
|
|
if (true) { await wait(3000); }
|
|
await withPage(mp, async pg => ok('点击进入=' + pg.path));
|
|
await Promise.race([mp.navigateBack(), wait(10000)]).catch(() => {}); await wait(1500);
|
|
await mp.screenshot({ path: DIR + '/flow-04-more.png' });
|
|
|
|
// ============ 流程5: 登录 ============
|
|
hdr('流程5: 登录流程');
|
|
await navTo(mp, 'navigateTo', '/pages/login/index');
|
|
await wait(2000);
|
|
await withPage(mp, async pg => ok('登录页=' + pg.path));
|
|
|
|
await mp.evaluate(() => {
|
|
wx.setStorageSync('token', 'test-token');
|
|
wx.setStorageSync('userId', 'test-user');
|
|
wx.setStorageSync('isLoggedIn', true);
|
|
return 'ok';
|
|
});
|
|
await wait(1000);
|
|
const li = await mp.evaluate(() => wx.getStorageSync('isLoggedIn'));
|
|
ok('模拟登录: ' + (li ? '成功' : '失败'));
|
|
await mp.screenshot({ path: DIR + '/flow-05-login.png' });
|
|
|
|
// ============ 流程6: 创建活动 ============
|
|
hdr('流程6: 创建活动');
|
|
await navTo(mp, 'navigateTo', '/pages/create-activity/index');
|
|
await wait(3000);
|
|
|
|
await withPage(mp, async pg => {
|
|
ok('创建页=' + pg.path);
|
|
const d = await pg.data();
|
|
ok('模式=' + d.mode);
|
|
});
|
|
|
|
// 填表 - 用 setData 代替 callMethod('onInput')
|
|
await withPage(mp, async pg => {
|
|
await pg.setData({ 'form.title': '自动化测试活动' });
|
|
await pg.setData({ 'form.location': '测试地点' });
|
|
});
|
|
const tm = new Date(); tm.setDate(tm.getDate() + 1);
|
|
const ds = tm.getFullYear() + '-' + String(tm.getMonth()+1).padStart(2,'0') + '-' + String(tm.getDate()).padStart(2,'0');
|
|
await withPage(mp, async pg => {
|
|
await pg.setData({ 'form.date': ds, 'form.startTime': '14:00' });
|
|
});
|
|
await withPage(mp, async pg => {
|
|
const d = await pg.data();
|
|
ok('标题: ' + d.form.title);
|
|
ok('日期: ' + d.form.date);
|
|
});
|
|
await withPage(mp, async pg => {
|
|
try { await pg.callMethod('handleSubmit'); await wait(3000); ok('提交成功'); }
|
|
catch(e) { sk('提交异常'); }
|
|
});
|
|
await mp.screenshot({ path: DIR + '/flow-06-create.png' });
|
|
|
|
// ============ 流程7: 报名 ============
|
|
hdr('流程7: 报名活动');
|
|
await Promise.race([mp.reLaunch('/pages/home/index'), wait(15000)]).catch(() => {});
|
|
await wait(3000);
|
|
let joinId = null, joinTitle = null;
|
|
await withPage(mp, async pg => {
|
|
const d = await pg.data();
|
|
const t = d.activities?.find(a => !a.isCreator && !a.isJoined && !a.isExpired);
|
|
if (t) { joinId = t.id; joinTitle = t.title; }
|
|
});
|
|
if (joinId) {
|
|
ok('目标: ' + joinTitle);
|
|
await withPage(mp, async pg => {
|
|
try { await pg.callMethod('handleQuickJoin', { currentTarget: { dataset: { id: joinId } } }); }
|
|
catch(e) { throw e; }
|
|
});
|
|
await wait(2500);
|
|
ok('报名已触发');
|
|
} else { sk('无可报名活动'); }
|
|
await mp.screenshot({ path: DIR + '/flow-07-join.png' });
|
|
|
|
// ============ 流程8: 编辑资料 ============
|
|
hdr('流程8: 编辑资料');
|
|
await navTo(mp, 'navigateTo', '/pages/edit-profile/index');
|
|
await wait(3000);
|
|
|
|
await withPage(mp, async pg => {
|
|
ok('编辑页=' + pg.path);
|
|
const d = await pg.data();
|
|
if (d.form) {
|
|
ok('昵称: ' + (d.form.nickname || '未设置'));
|
|
await pg.setData({ 'form.nickname': '自动化测试用户' });
|
|
await wait(500);
|
|
try { await pg.callMethod('handleSubmit'); await wait(2000); ok('更新提交'); }
|
|
catch(e) { sk('更新异常'); }
|
|
}
|
|
});
|
|
await mp.screenshot({ path: DIR + '/flow-08-edit-profile.png' });
|
|
|
|
// ============ 流程9: 个人中心 ============
|
|
hdr('流程9: 个人中心');
|
|
await navTo(mp, 'switchTab', '/pages/profile/index');
|
|
await wait(3000);
|
|
|
|
await withPage(mp, async pg => {
|
|
ok('个人中心=' + pg.path);
|
|
const d = await pg.data();
|
|
ok('登录: ' + (d.isLoggedIn ? '已登录' : '未登录'));
|
|
if (d.userInfo) ok('用户: ' + (d.userInfo.nickname || '未设'));
|
|
});
|
|
await mp.screenshot({ path: DIR + '/flow-09-profile.png' });
|
|
|
|
// ============ 流程10: 签到 ============
|
|
hdr('流程10: 签到');
|
|
await Promise.race([mp.reLaunch('/pages/home/index'), wait(15000)]).catch(() => {}); await wait(3000);
|
|
let checkinId = null;
|
|
await withPage(mp, async pg => {
|
|
const d = await pg.data();
|
|
const j = d.activities?.find(a => a.isJoined);
|
|
if (j) checkinId = j.id;
|
|
});
|
|
if (checkinId) {
|
|
await navTo(mp, 'navigateTo', '/pages/checkin/index?id=' + checkinId);
|
|
await wait(3000);
|
|
await withPage(mp, async pg => {
|
|
const d = await pg.data();
|
|
ok('签到状态: ' + d.status);
|
|
if (d.status === 'can_checkin') {
|
|
try { await pg.callMethod('handleCheckin'); await wait(2000); ok('签到完成'); }
|
|
catch(e) { sk('签到异常'); }
|
|
}
|
|
});
|
|
} else { sk('无已报名活动'); }
|
|
await mp.screenshot({ path: DIR + '/flow-10-checkin.png' });
|
|
|
|
// ============ 流程11: AI合作分析 ============
|
|
hdr('流程11: AI合作分析');
|
|
await navTo(mp, 'switchTab', '/pages/home/index');
|
|
await wait(2000);
|
|
|
|
let recUserId = null, recUserName = null;
|
|
await withPage(mp, async pg => {
|
|
const d = await pg.data();
|
|
if (!d.activities?.[0]) return;
|
|
await navTo(mp, 'navigateTo', '/pages/activity-detail/index?id=' + d.activities[0].id);
|
|
await wait(5000);
|
|
const pg2 = await mp.currentPage();
|
|
const d2 = await pg2.data();
|
|
if (d2.aiRecommendation?.participants?.[0]) {
|
|
recUserId = d2.aiRecommendation.participants[0].id;
|
|
recUserName = d2.aiRecommendation.participants[0].nickname || recUserId;
|
|
}
|
|
});
|
|
|
|
if (recUserId) {
|
|
ok('AI推荐: ' + recUserName);
|
|
await navTo(mp, 'navigateTo', '/pages/collaboration-analysis/index?targetUserId=' + recUserId);
|
|
await wait(5000);
|
|
await withPage(mp, async pg => {
|
|
const ad = await pg.data();
|
|
if (ad.analysis) { ok('评分: ' + ad.analysis.score); ok('等级: ' + ad.analysis.level); }
|
|
else { sk('分析: ' + (ad.error || '加载中')); }
|
|
});
|
|
} else { sk('无AI推荐'); }
|
|
await mp.screenshot({ path: DIR + '/flow-11-ai.png' });
|
|
|
|
// ============ 流程12: 退出登录 ============
|
|
hdr('流程12: 退出登录');
|
|
await navTo(mp, 'switchTab', '/pages/profile/index');
|
|
await wait(3000);
|
|
|
|
await mp.mockWxMethod('showModal', { confirm: true, cancel: false });
|
|
await withPage(mp, async pg => {
|
|
try { await pg.callMethod('handleLogout'); } catch(e) {}
|
|
});
|
|
await wait(2000);
|
|
await mp.restoreWxMethod('showModal');
|
|
|
|
const state = await mp.evaluate(() => ({
|
|
li: wx.getStorageSync('isLoggedIn'),
|
|
tk: !!wx.getStorageSync('token'),
|
|
}));
|
|
ok('isLoggedIn已清除: ' + !state.li);
|
|
ok('token已清除: ' + !state.tk);
|
|
await mp.screenshot({ path: DIR + '/flow-12-logout.png' });
|
|
|
|
} catch(err) {
|
|
console.error('\n❌ 异常: ' + err.message);
|
|
no('崩溃: ' + err.message);
|
|
} finally {
|
|
hdr('测试汇总');
|
|
console.log('总计' + (okN+failN+skipN) + ' | ✅' + okN + ' ❌' + failN + ' ⏭' + skipN);
|
|
const rate = (okN+failN) > 0 ? Math.round(okN/(okN+failN)*100) : 100;
|
|
console.log('通过率: ' + rate + '%');
|
|
results.forEach((r, i) => console.log((i+1) + '. ' + r));
|
|
await mp.disconnect();
|
|
console.log('\n已断开');
|
|
if (failN > 0) process.exit(1);
|
|
}
|
|
}
|
|
|
|
main();
|