Creating a Jira issue when a test run fails using Playwright
This post will demonstrate how you can integrate Jira into your automation framework to raise a bug ticket when a test failed in your test run. In this example, we will be using Playwright.
If you want to integrate Jira into your automation framework, you’re in the right place. This post will demonstrate how to integrate Jira into Playwright. This is just one scenario that we will be covering which is when a test run has failed, raise a Jira issue for each of the failed tests but there are many other scenarios that can be covered.
Note - this concept can be used in another automation framework not only Playwright.
Create Jira automation
Select Jira Project -> Project settings
-> Automation
-> Create rule
Note - you may need permission to access this section of Jira
An overview of what we have done:
- Created an Incoming webhook
- We look up any issue in the project that has a bug type and the status not in Declined, Done, or Deployed, and the summary name extracted from the webhook payload
- We use an IF statement to only create a New Bug ticket if the look-up result is equal to 0, this way you don’t have duplicate issues in your Jira.
Script to Send data to Jira
Create a Script to call the webhook in your automation framework, In this example, I will be using Playwright.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import axios from 'axios';
import fs from 'fs';
interface TestResult {
title: string;
status: string;
message: object;
}
interface reportJson {
suites: [
{
suites: [
{
specs: [
{
title: string;
ok: boolean;
tests: [
{
results: any;
annotations: [{ type: string; description: string }] | [];
},
];
},
];
},
];
},
];
}
const results: TestResult[] = [];
function createBugsOnJira() {
const url =
'https://automation.atlassian.com/pro/hooks/<your_webhook_id>';
const axiosConfig = { headers: { 'Content-Type': 'application/json' } };
results.map((result) => {
const postData = {
summary: `E2E tests failure - ${result.title}`,
description: `Details on failed test:\n
Test title: ${result.title}\n
Test status: ${result.status}\n
Test message: ${result.message}\n`,
};
axios
.post(url, postData, axiosConfig)
.then(() => {
console.log('Bug sent');
})
.catch((err) => {
console.error('Error sending bug:\n', err);
});
});
}
function getFailureDescriptionsFromReport(filePath: string) {
try {
const report: reportJson = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
report.suites.forEach((suite) => {
suite.suites.forEach((suite) => {
suite.specs.forEach((spec) => {
if (spec.ok == false) {
spec.tests.forEach((test) => {
results.push({
status: test.results[0].status,
title: spec.title,
message: test.results[0].error.message,
});
});
}
});
});
});
} catch (error) {
console.error('Error reading report file:\n', error);
}
}
getFailureDescriptionsFromReport('results.json');
if (results.length > 0) {
createBugsOnJira();
} else {
console.log('No Failed tests found - Nothing to report on Jira');
}
You will have two functions:
- getFailureDescriptionsFromReport()
- In Playwright when a test run has been completed, there is an Option to output the results in JSON format.
With this, we can create a function to loop through the JSON file and retrieve any failed scenarios. We then create a new array and push the data into it so we can be used later on
- In Playwright when a test run has been completed, there is an Option to output the results in JSON format.
- createBugsOnJira()
- In this function, we will be calling the webhook via Axios but you can choose any other request liberty. We then use the information from getFailureDescriptionsFromReport() to construct our payload.
- In this function, we will be calling the webhook via Axios but you can choose any other request liberty. We then use the information from getFailureDescriptionsFromReport() to construct our payload.
Once you have constructed the script, you call this script once the test run has been completed. If you are using Playwright, you will need to configure it to export results in JSON.
Bonus
See below for how to populate your Jira issue with the Payload you send from your automation framework.
If you enjoyed this article, please click the 👍 button and share to help others find it!