k6-cucumber-steps 1.1.2 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,122 +1,65 @@
1
1
  const fs = require("fs");
2
2
  const path = require("path");
3
- const { minify } = require("html-minifier-terser");
4
3
 
5
4
  const reportsDir = path.resolve("reports");
6
5
 
7
- function findCucumberReportPath() {
8
- const files = fs.readdirSync(reportsDir);
9
- return files.find((f) => f.includes("cucumber") && f.endsWith(".html"));
10
- }
11
-
12
- function extractCucumberBody(filepath) {
13
- const html = fs.readFileSync(filepath, "utf-8");
14
-
15
- // Remove <script> tags to avoid JS conflicts
16
- const withoutScripts = html.replace(/<script[\s\S]*?<\/script>/gi, "");
17
-
18
- const match = withoutScripts.match(/<body[^>]*>([\s\S]*)<\/body>/i);
19
- return match
20
- ? match[1]
21
- : "<p>⚠️ Failed to extract body of Cucumber report.</p>";
22
- }
23
-
24
- function extractK6ReportBody(file) {
25
- const html = fs.readFileSync(file, "utf-8");
26
- const match = html.match(/<body[^>]*>([\s\S]*)<\/body>/i);
27
- return match ? match[1] : "";
28
- }
29
-
30
- function buildCombinedHtml({ title, k6Bodies, cucumberBody }) {
31
- const tabs = k6Bodies
32
- .map(
33
- (_, i) => `
34
- <input type="radio" name="tabs" id="tabk6${i}" ${i === 0 ? "checked" : ""}>
35
- <label for="tabk6${i}">K6 Report ${i + 1}</label>
36
- <div class="tab">
37
- ${k6Bodies[i]}
38
- </div>
39
- `
40
- )
41
- .join("\n");
6
+ /**
7
+ * Adds internal cross-links between reports.
8
+ */
9
+ function addLinksToReport(targetFile, otherFiles) {
10
+ const content = fs.readFileSync(targetFile, "utf-8");
42
11
 
12
+ // Inject the Cucumber Report tab before the Request Metrics tab
43
13
  const cucumberTab = `
44
14
  <input type="radio" name="tabs" id="tabcucumber">
45
- <label for="tabcucumber">Cucumber Report</label>
15
+ <label for="tabcucumber"><i class="fas fa-file-alt"></i> &nbsp; Cucumber Report</label>
46
16
  <div class="tab">
47
- <div style="max-height:80vh; overflow:auto; padding:1rem;">
48
- ${cucumberBody}
49
- </div>
17
+ <iframe src="cucumber-report.html" style="width:100%; height:600px; border:none;"></iframe>
50
18
  </div>
51
19
  `;
52
20
 
53
- const fullTabs = cucumberBody ? cucumberTab + tabs : tabs;
54
-
55
- return `
56
- <!DOCTYPE html>
57
- <html lang="en">
58
- <head>
59
- <meta charset="UTF-8" />
60
- <title>${title}</title>
61
- <link rel="stylesheet" href="https://unpkg.com/purecss@2.0.3/build/pure-min.css" crossorigin="anonymous">
62
- <style>
63
- body { margin:1rem; font-family:sans-serif; }
64
- .tabs { display:flex; flex-wrap:wrap; }
65
- .tabs label { order:1; padding:1rem; cursor:pointer; background:#ddd; margin-right:0.2rem; border-radius:5px 5px 0 0; }
66
- .tabs .tab { order:99; flex-grow:1; width:100%; display:none; padding:1rem; background:#fff; }
67
- .tabs input[type="radio"] { display:none; }
68
- .tabs input[type="radio"]:checked + label { background:#fff; font-weight:bold; }
69
- .tabs input[type="radio"]:checked + label + .tab { display:block; }
70
- </style>
71
- </head>
72
- <body>
73
- <h1>${title}</h1>
74
- <div class="tabs">
75
- ${fullTabs}
76
- </div>
77
- </body>
78
- </html>`;
21
+ const modifiedContent = content
22
+ .replace(
23
+ /<input type="radio" name="tabs" id="tabone"/,
24
+ `${cucumberTab}\n<input type="radio" name="tabs" id="tabone"`
25
+ )
26
+ // Remove standalone links to cucumber-report.html or k6-report.html
27
+ .replace(
28
+ /<div style="padding:10px;margin-top:20px;text-align:center">[\s\S]*?View (Cucumber|k6).*?<\/div>/,
29
+ ""
30
+ );
31
+
32
+ fs.writeFileSync(targetFile, modifiedContent, "utf-8");
33
+ console.log(
34
+ `🔗 Updated ${path.basename(
35
+ targetFile
36
+ )} with Cucumber tab and removed standalone links.`
37
+ );
79
38
  }
80
39
 
81
- async function linkReports() {
40
+ /**
41
+ * Auto-link all HTML reports in the reports directory.
42
+ */
43
+ function linkReports() {
82
44
  if (!fs.existsSync(reportsDir)) {
83
45
  console.warn("⚠️ No reports directory found.");
84
46
  return;
85
47
  }
86
48
 
87
- const cucumberFile = findCucumberReportPath();
88
- const cucumberBody = cucumberFile
89
- ? extractCucumberBody(path.join(reportsDir, cucumberFile))
90
- : null;
91
-
92
- const k6Files = fs
49
+ const htmlFiles = fs
93
50
  .readdirSync(reportsDir)
94
- .filter((f) => /^k6-report.*\.html$/.test(f));
95
- if (k6Files.length === 0) {
96
- console.warn("⚠️ No K6 reports found.");
51
+ .filter((f) => f.endsWith(".html"))
52
+ .map((f) => path.join(reportsDir, f));
53
+
54
+ if (htmlFiles.length < 2) {
55
+ console.warn("⚠️ Not enough HTML files to link.");
97
56
  return;
98
57
  }
99
58
 
100
- const k6Bodies = k6Files.map((f) =>
101
- extractK6ReportBody(path.join(reportsDir, f))
102
- );
103
-
104
- const combinedHtml = buildCombinedHtml({
105
- title: "Combined K6 + Cucumber Report",
106
- k6Bodies,
107
- cucumberBody,
108
- });
109
-
110
- const finalHtml = await minify(combinedHtml, {
111
- collapseWhitespace: true,
112
- removeComments: true,
113
- minifyCSS: true,
114
- });
115
-
116
- const outPath = path.join(reportsDir, "combined-report.html");
117
- fs.writeFileSync(outPath, finalHtml, "utf-8");
118
-
119
- console.log(`✅ Combined and minified report saved to: ${outPath}`);
59
+ for (const file of htmlFiles) {
60
+ const others = htmlFiles.filter((f) => f !== file);
61
+ addLinksToReport(file, others);
62
+ }
120
63
  }
121
64
 
122
65
  module.exports = { linkReports };
@@ -0,0 +1,65 @@
1
+
2
+ import http from 'k6/http';
3
+ import { check } from 'k6';
4
+ import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";
5
+ import { textSummary } from "https://jslib.k6.io/k6-summary/0.0.1/index.js";
6
+
7
+ export const options = {
8
+ "vus": 10,
9
+ "duration": "5s",
10
+ "thresholds": {
11
+ "http_req_failed": [
12
+ "rate<0.05"
13
+ ],
14
+ "http_req_duration": [
15
+ "p(95)<5000"
16
+ ]
17
+ }
18
+ };
19
+
20
+ export default function () {
21
+ const headers = {
22
+ "Content-Type": "application/json"
23
+ };
24
+ const body = undefined;
25
+
26
+
27
+ const resolvedUrl0 = "https://sandbox-decide-api.indicina.net/get?foo1=bar1&foo2=bar2";
28
+ const res0 = http.request("GET", resolvedUrl0, null, { headers });
29
+ console.log(`Response Body for ${resolvedUrl0}: ${res0.body}`);
30
+ check(res0, {
31
+ "status is 2xx": (r) => r.status >= 200 && r.status < 300
32
+ });
33
+
34
+
35
+ const resolvedUrl1 = "https://postman-echo.com/get?foo1=bar1&foo2=bar2";
36
+ const res1 = http.request("GET", resolvedUrl1, null, { headers });
37
+ console.log(`Response Body for ${resolvedUrl1}: ${res1.body}`);
38
+ check(res1, {
39
+ "status is 2xx": (r) => r.status >= 200 && r.status < 300
40
+ });
41
+
42
+ }
43
+
44
+ export function handleSummary(data) {
45
+ const outputDir = __ENV.REPORT_OUTPUT_DIR || "reports";
46
+ const html = htmlReport(data);
47
+ const cucumberReportFile = "cucumber-report.html";
48
+ const cucumberLink = `
49
+ <div style="text-align:center; margin-top:20px;">
50
+ <a href="${cucumberReportFile}" style="font-size:18px; color:#0066cc;">
51
+ 🔗 View Cucumber Test Report
52
+ </a>
53
+ </div>
54
+ `;
55
+
56
+ const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
57
+ const k6ReportFilename = `${outputDir}/k6-report-${timestamp}.html`;
58
+
59
+ console.log(`📄 K6 HTML report ${k6ReportFilename} generated successfully 👍`);
60
+
61
+ return {
62
+ [k6ReportFilename]: html.replace("</body>", `${cucumberLink}</body>`),
63
+ stdout: textSummary(data, { indent: " ", enableColors: true }),
64
+ };
65
+ }