我是使用打字稿进行剧作家测试自动化的新手。我想获得通过、失败、片状计数的总数以及总执行持续时间。我们需要在电子邮件正文中执行 CI/CD 管道作业后添加这些详细信息。
我正在使用下面的.yml文件
阶段
构建
脚本
脚本执行后
Email.ts 是在构建执行后发送电子邮件的脚本
所以你所问的问题需要一些工作。
步骤 1 通过将 —reporter=JSON 添加到您的 playwright 命令来生成 json 报告
步骤 2
将您的电子邮件脚本修改为类似这样的内容,以便它可以处理步骤 1 中的 json
import * as fs from 'fs';
import { sendEmail } from './yourEmailSendingFunction'; // Assume you have a function to send emails
const reportFilePath = 'playwright-report/results.json'; // Adjust based on your actual report file path
fs.readFile(reportFilePath, { encoding: 'utf-8' }, (err, data) => {
if (err) {
console.error('Error reading Playwright report file:', err);
return;
}
try {
const report = JSON.parse(data);
const summary = report.suites.reduce((acc, suite) => {
suite.tests.forEach(test => {
acc.totalTests += 1;
acc.duration += test.duration;
if (test.outcomes.includes('flaky')) acc.flaky += 1;
if (test.outcomes.includes('passed')) acc.passed += 1;
if (test.outcomes.includes('failed')) acc.failed += 1;
});
return acc;
}, { totalTests: 0, passed: 0, failed: 0, flaky: 0, duration: 0 });
const emailBody = `Test Execution Summary:
Total Tests: ${summary.totalTests}
Passed: ${summary.passed}
Failed: ${summary.failed}
Flaky: ${summary.flaky}
Total Execution Duration: ${summary.duration}ms`;
// Assuming you have a pipeline ID and other details to send along
sendEmail('Your CI/CD Test Report', emailBody);
} catch (parseErr) {
console.error('Error parsing Playwright report JSON:', parseErr);
}
});