- wechat-miniprogram: 原生小程序,品牌化拆约+玄龙logo,API 指向 https://xhermes.aibay.asia - yaoyue-sever: Express 单文件 server.js + better-sqlite3,GLM glm-5.3-flash(thinking disabled) - 排除 node_modules/.env/*.db
159 lines
6.2 KiB
JavaScript
159 lines
6.2 KiB
JavaScript
// 微信小程序自动化测试 - 完整版
|
|
const automator = require('miniprogram-automator');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const OUTPUT_DIR = path.join(__dirname, 'screenshots');
|
|
|
|
if (!fs.existsSync(OUTPUT_DIR)) {
|
|
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
|
|
}
|
|
|
|
const logs = { errors: [], warnings: [], logs: [], exceptions: [] };
|
|
|
|
async function runTest() {
|
|
console.log('========================================');
|
|
console.log('微信小程序自动化测试');
|
|
console.log('========================================\n');
|
|
|
|
// 1. 连接
|
|
console.log('[1] 连接自动化端口...');
|
|
const mp = await automator.connect({ wsEndpoint: 'ws://localhost:9420' });
|
|
console.log('✅ 连接成功\n');
|
|
|
|
// 2. 控制台监听
|
|
console.log('[2] 设置控制台监听...');
|
|
mp.on('console', msg => {
|
|
const args = msg.args.map(a => typeof a === 'object' ? JSON.stringify(a) : String(a)).join(' ');
|
|
const entry = { type: msg.type, message: args, time: new Date().toISOString() };
|
|
if (msg.type === 'error') { logs.errors.push(entry); console.error(`[ERROR] ${args}`); }
|
|
else if (msg.type === 'warn') { logs.warnings.push(entry); }
|
|
else { logs.logs.push(entry); }
|
|
});
|
|
mp.on('exception', err => {
|
|
logs.exceptions.push({ message: err.message, time: new Date().toISOString() });
|
|
console.error(`[EXCEPTION] ${err.message}`);
|
|
});
|
|
console.log('✅ 监听器已设置\n');
|
|
|
|
// 3. 首页截图
|
|
console.log('[3] 首页截图...');
|
|
await mp.screenshot({ path: path.join(OUTPUT_DIR, '01-home.png') });
|
|
const homeData = await (await mp.currentPage()).data();
|
|
console.log('首页活动数量:', homeData.activities?.length || 0);
|
|
console.log('✅ 首页截图完成\n');
|
|
|
|
// 4. 使用 reLaunch 导航到活动详情页
|
|
console.log('[4] 导航到活动详情页 (activity-6)...');
|
|
const detailPage = await mp.reLaunch('/pages/activity-detail/index?id=activity-6');
|
|
console.log('✅ 已导航到:', detailPage.path, '\n');
|
|
|
|
// 5. 等待数据加载
|
|
console.log('[5] 等待数据加载 (10秒)...');
|
|
await new Promise(r => setTimeout(r, 10000));
|
|
|
|
// 6. 获取页面数据
|
|
console.log('[6] 获取页面数据...');
|
|
const pageData = await detailPage.data();
|
|
console.log('活动标题:', pageData.activity?.title);
|
|
console.log('活动状态:', pageData.activityStatus);
|
|
console.log('参与者:', pageData.participants?.length || 0, '人');
|
|
console.log('AI推荐:', pageData.aiRecommendation ? '已加载' : '未加载');
|
|
if (pageData.aiRecommendation) {
|
|
console.log('AI建议参与者数:', pageData.aiRecommendation.participants?.length || 0);
|
|
console.log('AI建议摘要:', (pageData.aiRecommendation.suggestions || '').substring(0, 80) + '...');
|
|
}
|
|
console.log('');
|
|
|
|
// 7. 截图
|
|
console.log('[7] 活动详情页截图...');
|
|
await mp.screenshot({ path: path.join(OUTPUT_DIR, '02-activity-detail.png') });
|
|
console.log('✅ 截图完成\n');
|
|
|
|
// 8. 测试其他活动页
|
|
console.log('[8] 导航到 activity-7...');
|
|
await mp.reLaunch('/pages/activity-detail/index?id=activity-7');
|
|
await new Promise(r => setTimeout(r, 8000));
|
|
const page2Data = await (await mp.currentPage()).data();
|
|
console.log('活动标题:', page2Data.activity?.title);
|
|
await mp.screenshot({ path: path.join(OUTPUT_DIR, '03-activity-7.png') });
|
|
console.log('✅ 完成\n');
|
|
|
|
// 9. 导航到 activity-8
|
|
console.log('[9] 导航到 activity-8...');
|
|
await mp.reLaunch('/pages/activity-detail/index?id=activity-8');
|
|
await new Promise(r => setTimeout(r, 8000));
|
|
const page3Data = await (await mp.currentPage()).data();
|
|
console.log('活动标题:', page3Data.activity?.title);
|
|
await mp.screenshot({ path: path.join(OUTPUT_DIR, '04-activity-8.png') });
|
|
console.log('✅ 完成\n');
|
|
|
|
// 10. 返回首页
|
|
console.log('[10] 返回首页...');
|
|
await mp.reLaunch('/pages/home/index');
|
|
await new Promise(r => setTimeout(r, 3000));
|
|
await mp.screenshot({ path: path.join(OUTPUT_DIR, '05-home-final.png') });
|
|
console.log('✅ 首页截图完成\n');
|
|
|
|
// 11. 生成报告
|
|
console.log('[11] 生成测试报告...');
|
|
const report = {
|
|
testTime: new Date().toISOString(),
|
|
pages: {
|
|
home: { activities: homeData.activities?.length || 0 },
|
|
activity6: {
|
|
title: pageData.activity?.title,
|
|
status: pageData.activityStatus,
|
|
participants: pageData.participants?.length || 0,
|
|
aiLoaded: !!pageData.aiRecommendation,
|
|
aiSuggestions: !!pageData.aiRecommendation?.suggestions,
|
|
},
|
|
activity7: { title: page2Data.activity?.title },
|
|
activity8: { title: page3Data.activity?.title },
|
|
},
|
|
logs: {
|
|
errors: logs.errors.length,
|
|
warnings: logs.warnings.length,
|
|
logs: logs.logs.length,
|
|
exceptions: logs.exceptions.length,
|
|
},
|
|
details: logs,
|
|
};
|
|
|
|
fs.writeFileSync(path.join(OUTPUT_DIR, 'test-report.json'), JSON.stringify(report, null, 2));
|
|
console.log('✅ 报告已保存\n');
|
|
|
|
// 12. 输出总结
|
|
console.log('========================================');
|
|
console.log('测试完成');
|
|
console.log('========================================');
|
|
console.log(`activity-6: ${report.pages.activity6.title}`);
|
|
console.log(` 参与者: ${report.pages.activity6.participants} 人`);
|
|
console.log(` AI推荐: ${report.pages.activity6.aiLoaded ? '✅ 已加载' : '❌ 未加载'}`);
|
|
console.log(`activity-7: ${report.pages.activity7.title}`);
|
|
console.log(`activity-8: ${report.pages.activity8.title}`);
|
|
console.log(`控制台: ${logs.errors.length} 错误, ${logs.warnings.length} 警告`);
|
|
console.log('========================================');
|
|
|
|
// 列出 iOS 日期格式警告
|
|
const dateWarnings = logs.warnings.filter(w => w.message && w.message.includes('new Date'));
|
|
if (dateWarnings.length > 0) {
|
|
console.log(`\niOS 日期格式警告: ${dateWarnings.length} 个`);
|
|
dateWarnings.slice(0, 3).forEach((w, i) => console.log(` ${i + 1}. ${w.message.substring(0, 100)}`));
|
|
}
|
|
|
|
// 列出错误
|
|
if (logs.errors.length > 0) {
|
|
console.log('\n错误详情:');
|
|
logs.errors.slice(0, 5).forEach((e, i) => console.log(` ${i + 1}. ${e.message.substring(0, 150)}`));
|
|
}
|
|
|
|
await mp.disconnect();
|
|
process.exit(logs.errors.length > 0 ? 1 : 0);
|
|
}
|
|
|
|
runTest().catch(e => {
|
|
console.error('测试失败:', e.message);
|
|
process.exit(1);
|
|
});
|