如何自动生成崩溃复盘报告?
解读
“崩溃复盘报告”在 Cypress 语境下,指的是用例失败或浏览器崩溃后,自动收集环境快照、日志、视频、网络轨迹、堆栈、性能指标并生成可读文件的过程。国内面试问此题,核心想验证两点:
- 你是否能把 Cypress 原生能力(Screenshots、Videos、Test Replay、cy.intercept 等)与 Node 脚本无缝整合;
- 你是否了解国内 CI(Jenkins、GitLab-CI、阿里云 Flow、腾讯云 CODING)的制品归档规范,以及飞书/企业微信机器人推送格式,从而让报告“一次生成、多方消费”。
回答必须体现“自动生成”四字——即无需人工点击、无需本地 IDE,Pipeline 跑完即可在钉钉群看到带链接的 PDF 报告。
知识点
- Cypress 原生事件:
after:run、after:spec、afterScreenshot,是官方推荐的“插桩点”。 - Mochawesome 系列:
mochawesome、mochawesome-merge、mochawesome-report-generator,可输出带堆栈、截图、视频路径的 JSON,再转 HTML/PDF。 - Node 侧聚合脚本:利用
cypress.config.js里setupNodeEvents(on, config)注册任务,把failures、duration、browser、git commit、buildNumber写入report.meta。 - 国内 IM 推送:飞书/钉钉机器人只接受 Markdown,单个消息不得超过 20 KB,因此需把详细报告上传至 OSS 或 CODING 制品库,再发“摘要 + 链接”。
- 合规与隐私:金融类公司要求脱敏,需用
cy.mask()或cy.origin()把用户手机号、身份证号打码后再截图。 - 性能指标:通过
cy.window().then(win => win.performance.timing)获取 FCP、LCP,写入报告,方便前端性能回溯。
答案
分四步落地,全部在 Pipeline 里无人值守:
第一步,注册 Cypress 事件钩子
在 cypress.config.js 中:
const { defineConfig } = require('cypress')
const fs = require('fs')
const path = require('path')
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
on('after:run', async (results) => {
if (results.totalFailed === 0) return
const meta = {
browser: results.browserName,
version: results.browserVersion,
totalFailed: results.totalFailed,
duration: results.totalDuration,
commit: process.env.GIT_COMMIT,
build: process.env.BUILD_NUMBER
}
fs.writeFileSync('cypress/report/meta.json', JSON.stringify(meta, null, 2))
})
return config
},
video: true,
screenshotOnRunFailure: true
}
})
第二步,生成统一 JSON
安装 mochawesome 并配置 reporterOptions 把 json、html、inlineAssets 全部打开;跑完用 mochawesome-merge 合并为 cypress/report/report.json。
第三步,Node 脚本二次加工
新建 scripts/generateCrashReport.js:
const merge = require('mochawesome-merge')
const generator = require('mochawesome-report-generator')
const fs = require('fs')
const OSS = require('ali-oss')
;(async () => {
const json = await merge({ files: ['cypress/results/*.json'] })
json.meta = JSON.parse(fs.readFileSync('cypress/report/meta.json', 'utf8'))
await generator.create(json, { reportDir: 'cypress/report', overwrite: true })
const client = new OSS({ region, accessKeyId, accessKeySecret, bucket })
const upload = async file => await client.put(`cypress/${Date.now()}-${path.basename(file)}`, file)
const [pdfUrl] = await Promise.all([
upload('cypress/report/report.pdf'),
upload('cypress/report/report.html')
])
await fetch(process.env.DINGTALK_WEBHOOK, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
msgtype: 'markdown',
markdown: {
title: 'Cypress 崩溃复盘',
text: `**失败用例数**: ${json.meta.totalFailed}\n**报告地址**: [点击查看](${pdfUrl})`
}
})
})
})()
第四步,CI 集成
在 Jenkinsfile 或 .gitlab-ci.yml 里加:
after_script:
- npm run cypress:run || true
- node scripts/generateCrashReport.js
至此,任何失败都会自动产生 PDF 报告并推送到钉钉群,全程零人工。
拓展思考
- 多浏览器矩阵:若 Pipeline 同时跑 Chrome、Electron、WebKit,需把
browserName写进文件名,避免覆盖。 - 失败重试与去重:Cypress 10+ 支持
retries,after:spec会触发多次,需在脚本里按spec.absolutePath + retryIndex去重截图。 - 合规审计:国企/券商要求报告落库 7 年以上,可把 PDF 同时写进 S3 兼容的“金融云 OBS”并加 KMS 加密。
- 智能归因:接入阿里 SLS 或腾讯 CLS,把
console.error与后端链路日志关联,实现“前端断言失败 = 后端慢查询”的跨域根因定位。