- 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
206 lines
6.6 KiB
JavaScript
206 lines
6.6 KiB
JavaScript
// 微信小程序自动化测试 - 刷新AI建议功能
|
|
const automator = require('miniprogram-automator');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const OUTPUT_DIR = path.join(__dirname, 'screenshots');
|
|
const WS_ENDPOINT = 'ws://localhost:9420';
|
|
|
|
// 确保截图目录存在
|
|
if (!fs.existsSync(OUTPUT_DIR)) {
|
|
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
|
|
}
|
|
|
|
// 日志收集
|
|
const logs = {
|
|
errors: [],
|
|
warnings: [],
|
|
exceptions: []
|
|
};
|
|
|
|
let passed = 0;
|
|
let failed = 0;
|
|
|
|
function assert(condition, message) {
|
|
if (condition) {
|
|
passed++;
|
|
console.log(` ✅ ${message}`);
|
|
} else {
|
|
failed++;
|
|
console.log(` ❌ ${message}`);
|
|
}
|
|
}
|
|
|
|
async function sleep(ms) {
|
|
return new Promise(r => setTimeout(r, ms));
|
|
}
|
|
|
|
async function runTest() {
|
|
console.log('========================================');
|
|
console.log('自动化测试 - 刷新AI建议按钮');
|
|
console.log('========================================\n');
|
|
|
|
// 1. 连接开发者工具
|
|
console.log('[1] 连接开发者工具...');
|
|
const mp = await automator.connect({ wsEndpoint: WS_ENDPOINT });
|
|
console.log('✅ 连接成功\n');
|
|
|
|
// 2. 监听控制台
|
|
mp.on('console', msg => {
|
|
const args = msg.args.map(a =>
|
|
typeof a === 'object' ? JSON.stringify(a) : String(a)
|
|
).join(' ');
|
|
if (msg.type === 'error') {
|
|
logs.errors.push(args);
|
|
console.error(` [ERROR] ${args}`);
|
|
} else if (msg.type === 'warn') {
|
|
logs.warnings.push(args);
|
|
}
|
|
});
|
|
mp.on('exception', err => {
|
|
logs.exceptions.push(err.message);
|
|
console.error(` [EXCEPTION] ${err.message}`);
|
|
});
|
|
|
|
// 3. 导航到活动详情页
|
|
console.log('[2] 导航到活动详情页...');
|
|
const page = await mp.reLaunch('/pages/activity-detail/index?id=activity-3');
|
|
console.log(' 页面:', page.path, '\n');
|
|
|
|
// 4. 等待初始加载
|
|
console.log('[3] 等待初始加载...');
|
|
await sleep(5000);
|
|
|
|
let data = await page.data();
|
|
console.log(` aiLoading: ${data.aiLoading}`);
|
|
console.log(` aiRefreshing: ${data.aiRefreshing}`);
|
|
console.log(` aiRecommendation: ${data.aiRecommendation ? '已加载' : '未加载'}`);
|
|
if (data.aiRecommendation?.participants) {
|
|
console.log(` 参与者数量: ${data.aiRecommendation.participants.length}`);
|
|
}
|
|
|
|
assert(data.aiLoading === false, '初始AI加载完成 (aiLoading=false)');
|
|
assert(data.aiRefreshing === false, '初始状态非刷新中 (aiRefreshing=false)');
|
|
|
|
// 5. 截图 - 初始状态
|
|
await mp.screenshot({ path: path.join(OUTPUT_DIR, 'refresh-before.png') });
|
|
console.log('\n 📸 截图: refresh-before.png');
|
|
|
|
// 6. 查找刷新按钮
|
|
console.log('\n[4] 查找刷新按钮...');
|
|
const refreshBtn = await page.$('.refresh-btn');
|
|
assert(refreshBtn !== null, '刷新按钮存在');
|
|
|
|
if (!refreshBtn) {
|
|
throw new Error('未找到刷新按钮 .refresh-btn');
|
|
}
|
|
|
|
// 检查按钮文本
|
|
const btnText = await refreshBtn.text();
|
|
console.log(` 按钮文本: "${btnText}"`);
|
|
assert(btnText.includes('刷新意见'), '按钮文本为"刷新意见"');
|
|
|
|
// 检查按钮 disabled 状态
|
|
const btnDisabled = await refreshBtn.attribute('disabled');
|
|
console.log(` 按钮 disabled: ${btnDisabled}`);
|
|
assert(btnDisabled === null || btnDisabled === undefined || btnDisabled === false, '初始状态按钮可点击');
|
|
|
|
// 7. 点击刷新按钮
|
|
console.log('\n[5] 点击刷新按钮...');
|
|
await refreshBtn.tap();
|
|
await sleep(800);
|
|
|
|
// 8. 验证刷新中状态
|
|
console.log('\n[6] 验证刷新中状态...');
|
|
data = await page.data();
|
|
console.log(` aiRefreshing: ${data.aiRefreshing}`);
|
|
console.log(` aiLoading: ${data.aiLoading}`);
|
|
assert(data.aiRefreshing === true, '刷新中 aiRefreshing=true');
|
|
assert(data.aiLoading === true, '刷新中 aiLoading=true');
|
|
|
|
// 按钮文本变为"刷新中..."
|
|
const btnTextDuring = await refreshBtn.text();
|
|
console.log(` 按钮文本: "${btnTextDuring}"`);
|
|
assert(btnTextDuring.includes('刷新中'), '按钮文本变为"刷新中..."');
|
|
|
|
// 📸 刷新中截图
|
|
await mp.screenshot({ path: path.join(OUTPUT_DIR, 'refresh-loading.png') });
|
|
console.log(' 📸 截图: refresh-loading.png');
|
|
|
|
// 9. 等待刷新完成
|
|
console.log('\n[7] 等待刷新完成...');
|
|
let retries = 0;
|
|
let refreshDone = false;
|
|
while (retries < 40) {
|
|
await sleep(1000);
|
|
data = await page.data();
|
|
if (!data.aiRefreshing && !data.aiLoading) {
|
|
refreshDone = true;
|
|
break;
|
|
}
|
|
retries++;
|
|
if (retries % 5 === 0) {
|
|
console.log(` 等待中... (${retries}s) aiRefreshing=${data.aiRefreshing} aiLoading=${data.aiLoading}`);
|
|
}
|
|
}
|
|
|
|
console.log(` 刷新完成,耗时约 ${retries} 秒`);
|
|
assert(refreshDone, '刷新完成 (超时40s内)');
|
|
|
|
// 10. 验证刷新后状态
|
|
console.log('\n[8] 验证刷新后状态...');
|
|
data = await page.data();
|
|
console.log(` aiRefreshing: ${data.aiRefreshing}`);
|
|
console.log(` aiLoading: ${data.aiLoading}`);
|
|
assert(data.aiRefreshing === false, '刷新后 aiRefreshing=false');
|
|
assert(data.aiLoading === false, '刷新后 aiLoading=false');
|
|
|
|
if (data.aiRecommendation?.participants) {
|
|
console.log(` 参与者数量: ${data.aiRecommendation.participants.length}`);
|
|
assert(data.aiRecommendation.participants.length > 0, '刷新后有参与者数据');
|
|
}
|
|
|
|
// 按钮恢复可点击
|
|
const btnTextAfter = await refreshBtn.text();
|
|
console.log(` 按钮文本: "${btnTextAfter}"`);
|
|
assert(btnTextAfter.includes('刷新意见'), '刷新后按钮文本恢复"刷新意见"');
|
|
|
|
// 📸 刷新后截图
|
|
await mp.screenshot({ path: path.join(OUTPUT_DIR, 'refresh-after.png') });
|
|
console.log(' 📸 截图: refresh-after.png');
|
|
|
|
// 11. 测试报告
|
|
console.log('\n========================================');
|
|
console.log('测试结果');
|
|
console.log('========================================');
|
|
console.log(`通过: ${passed}`);
|
|
console.log(`失败: ${failed}`);
|
|
console.log(`控制台错误: ${logs.errors.length}`);
|
|
console.log(`控制台异常: ${logs.exceptions.length}`);
|
|
console.log('========================================');
|
|
|
|
if (logs.errors.length > 0) {
|
|
console.log('\n控制台错误详情:');
|
|
logs.errors.forEach((e, i) => console.log(` ${i + 1}. ${e}`));
|
|
}
|
|
if (logs.exceptions.length > 0) {
|
|
console.log('\n控制台异常详情:');
|
|
logs.exceptions.forEach((e, i) => console.log(` ${i + 1}. ${e}`));
|
|
}
|
|
|
|
await mp.close();
|
|
|
|
if (failed > 0) {
|
|
console.log(`\n❌ ${failed} 个断言失败`);
|
|
process.exit(1);
|
|
} else {
|
|
console.log(`\n✅ 全部 ${passed} 个断言通过`);
|
|
process.exit(0);
|
|
}
|
|
}
|
|
|
|
runTest().catch(e => {
|
|
console.error('测试运行失败:', e.message);
|
|
process.exit(1);
|
|
});
|