如何在Playwright(TypeScript)测试完成且XML报告生成后调用函数上传至Jira?
在Playwright(TypeScript)中测试完成且XML报告生成后执行自定义逻辑
当然可以,有两种可靠的方案实现这个需求,具体如下:
方案1:自定义Playwright Reporter(推荐)
Playwright的Reporter API允许监听测试运行的全生命周期,其中onEnd事件会在所有测试执行完毕、所有报告文件(包括XML)生成完成后触发,完全匹配你的需求。
实现步骤:
- 创建自定义Reporter文件(比如
src/reporters/jira-reporter.ts):
import { Reporter, FullConfig, Suite, TestCase, TestResult } from '@playwright/test'; import axios from 'axios'; // 需提前安装:npm i axios class JiraReporter implements Reporter { private jiraUrl: string; private apiToken: string; private projectKey: string; // 初始化Jira配置参数 constructor(options: { jiraUrl: string; apiToken: string; projectKey: string }) { this.jiraUrl = options.jiraUrl; this.apiToken = options.apiToken; this.projectKey = options.projectKey; } // 测试全部结束后触发(此时XML报告已生成) async onEnd(config: FullConfig) { // 获取XML报告的输出路径(对应playwright.config中的配置) const xmlReportPath = config.reporters.find(r => r[0] === 'junit')?.[1]?.outputFile || 'test-results/junit.xml'; const fs = require('fs').promises; const xmlContent = await fs.readFile(xmlReportPath, 'utf-8'); // 发送POST请求到Jira导入端点 try { await axios.post(`${this.jiraUrl}/rest/api/2/import/execution`, { projectKey: this.projectKey, testResults: xmlContent // 根据Jira API要求补充其他必填参数 }, { auth: { username: '你的Jira用户名', password: this.apiToken }, headers: { 'Content-Type': 'application/xml' } } ); console.log('测试结果已成功导入Jira'); } catch (error) { console.error('导入Jira失败:', error); process.exitCode = 1; // 标记任务执行失败 } } // 实现Reporter接口的其他必填方法(空实现即可) onBegin(config: FullConfig, suite: Suite) {} onTestBegin(test: TestCase) {} onTestEnd(test: TestCase, result: TestResult) {} onError(error: Error) {} } export default JiraReporter;
- 在
playwright.config.ts中配置自定义Reporter:
import { defineConfig } from '@playwright/test'; import JiraReporter from './src/reporters/jira-reporter'; export default defineConfig({ reporter: [ ['junit', { outputFile: 'test-results/junit.xml' }], // 先生成标准XML报告 [JiraReporter, { jiraUrl: 'https://你的Jira实例地址', apiToken: '你的Jira API Token', projectKey: 'PROJ' // 替换为你的Jira项目Key }] ], // 其他Playwright配置... });
方案2:用npm脚本串联命令
如果不需要深度集成到Playwright内部,更简单的方式是利用npx playwright test在所有报告生成完毕后才退出的特性,通过npm脚本串联测试命令和自定义上传脚本。
实现步骤:
- 创建上传脚本(比如
scripts/upload-to-jira.ts):
import axios from 'axios'; import fs from 'fs/promises'; async function uploadToJira() { const xmlReportPath = 'test-results/junit.xml'; // 对应playwright.config中的XML输出路径 const xmlContent = await fs.readFile(xmlReportPath, 'utf-8'); try { await axios.post('https://你的Jira实例地址/rest/api/2/import/execution', { projectKey: 'PROJ', testResults: xmlContent }, { auth: { username: '你的Jira用户名', password: '你的Jira API Token' }, headers: { 'Content-Type': 'application/xml' } } ); console.log('测试结果已成功导入Jira'); } catch (error) { console.error('导入Jira失败:', error); process.exit(1); } } uploadToJira();
- 在
package.json中添加串联脚本:
{ "scripts": { "test": "npx playwright test", "test:upload": "npm run test && ts-node scripts/upload-to-jira.ts" } }
执行时运行npm run test:upload即可,Playwright测试完成并生成XML报告后,会自动执行上传逻辑。
内容的提问来源于stack exchange,提问作者Reed_Xia
相关产品推荐
相关产品推荐

