Skip to content

Commit 7315e14

Browse files
committed
Add pre-request and test scripts for postman
1 parent 84e8f3c commit 7315e14

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

scripts/collection_pre_request.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Clear testResults variable at the start of the collection
2+
if (!pm.collectionVariables.has("testResults")) {
3+
pm.collectionVariables.set("testResults", "[]");
4+
console.log("Cleared testResults variable at the start of the collection");
5+
} else {
6+
console.log("testResults variable already exists:", pm.collectionVariables.get("testResults"));
7+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Fetch the consolidated test results
2+
const testResults = JSON.parse(pm.collectionVariables.get("testResults") || "[]");
3+
4+
if (!testResults.length) {
5+
console.error("No test results found in testResults variable!");
6+
} else {
7+
console.log("Final testResults variable before posting to Jira:", testResults);
8+
}
9+
10+
// Remove duplicate test scenarios by using a Set to track unique names
11+
const uniqueTestResults = Array.from(
12+
new Map(testResults.map(result => [result.name, result])).values()
13+
);
14+
15+
console.log("Unique testResults after deduplication:", uniqueTestResults);
16+
17+
// Function to create a heading in Jira doc format
18+
function createHeading(text, level = 3) {
19+
return {
20+
type: "heading",
21+
attrs: { level },
22+
content: [
23+
{
24+
text,
25+
type: "text"
26+
}
27+
]
28+
};
29+
}
30+
31+
// Function to create a paragraph in Jira doc format
32+
function createParagraph(textArray) {
33+
return {
34+
type: "paragraph",
35+
content: textArray.map(text => (
36+
typeof text === "string"
37+
? { text, type: "text" }
38+
: text // If it's already a formatted object (e.g., bold text), include it as is
39+
))
40+
};
41+
}
42+
43+
// Function to create a bold text block in Jira doc format
44+
function createBoldText(text) {
45+
return {
46+
text,
47+
type: "text",
48+
marks: [{ type: "strong" }]
49+
};
50+
}
51+
52+
// Function to create a code block in Jira doc format
53+
function createCodeBlock(text) {
54+
return {
55+
type: "codeBlock",
56+
content: [
57+
{
58+
text,
59+
type: "text"
60+
}
61+
]
62+
};
63+
}
64+
65+
// Construct the Jira comment body
66+
const jiraComment = {
67+
body: {
68+
type: "doc",
69+
version: 1,
70+
content: [
71+
createHeading("Consolidated Test Results"), // Main heading
72+
...uniqueTestResults.flatMap(result => [
73+
createHeading(result.name, 3), // Heading for each test scenario
74+
createParagraph([
75+
createBoldText("Request Method: "),
76+
{
77+
text: `${result.details.method}`,
78+
type: "text"
79+
}
80+
]),
81+
createParagraph([
82+
createBoldText("Request URI: "),
83+
{
84+
text: `${result.details.uri}`,
85+
type: "text"
86+
}
87+
]),
88+
createParagraph([
89+
createBoldText("Request Payload: "),
90+
{
91+
text: JSON.stringify(result.details.payload || {}, null, 2),
92+
type: "text"
93+
}
94+
]),
95+
createParagraph([
96+
createBoldText("Request Query Params: "),
97+
{
98+
text: JSON.stringify(result.details.queryParams || {}, null, 2),
99+
type: "text"
100+
}
101+
]),
102+
createParagraph([
103+
createBoldText("Response Status Code: "),
104+
{
105+
text: `${result.details.statusCode}`,
106+
type: "text"
107+
}
108+
]),
109+
createParagraph([
110+
createBoldText("Response Body:")
111+
]),
112+
createCodeBlock(JSON.stringify(result.details.responseBody, null, 2)) // JSON-formatted response body
113+
])
114+
]
115+
}
116+
};
117+
118+
// Log the constructed payload for Jira
119+
console.log("Constructed Jira Comment Payload:", JSON.stringify(jiraComment, null, 2));
120+
121+
// Set the constructed comment as the request body
122+
pm.request.body.raw = JSON.stringify(jiraComment);

scripts/request_test_script.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Fetch existing test results or initialize an empty array
2+
let testResults = pm.collectionVariables.get("testResults") || "[]";
3+
testResults = JSON.parse(testResults);
4+
5+
// Helper function to construct the full URL
6+
function getFullRequestURL() {
7+
return pm.request.url.toString(); // Use toString() to get the full URL as a string
8+
}
9+
10+
// Add the current test case results
11+
testResults.push({
12+
name: pm.info.requestName,
13+
status: pm.response.code === 200 ? "PASS" : "FAIL",
14+
details: {
15+
method: pm.request.method,
16+
uri: getFullRequestURL(), // Use the helper function to get the URL
17+
payload: pm.request.body ? pm.request.body.raw : null,
18+
queryParams: pm.request.url.query ? pm.request.url.query.toObject() : null,
19+
statusCode: pm.response.code,
20+
responseBody: pm.response.json()
21+
}
22+
});
23+
24+
// Update the collection variable with the new results
25+
pm.collectionVariables.set("testResults", JSON.stringify(testResults));
26+
console.log(`Updated testResults variable after ${pm.info.requestName}:`, testResults);

0 commit comments

Comments
 (0)