k6-cucumber-steps 1.2.22 → 1.2.24
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.
- package/bin/k6-cucumber-steps.js +34 -39
- package/package.json +1 -1
- package/scripts/linkReports.js +42 -17
package/bin/k6-cucumber-steps.js
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
import path from "path";
|
|
3
|
+
import fs from "fs/promises";
|
|
4
|
+
import { spawn } from "child_process";
|
|
5
|
+
import { Command } from "commander";
|
|
6
|
+
import { fileURLToPath } from "url";
|
|
7
|
+
import dotenv from "dotenv";
|
|
8
|
+
import { linkReports } from "../scripts/linkReports.js";
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
dotenv.config();
|
|
11
|
+
|
|
12
|
+
// Allow __dirname in ESM
|
|
13
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
14
|
+
const __dirname = path.dirname(__filename);
|
|
9
15
|
|
|
10
16
|
console.log(`
|
|
11
17
|
-----------------------------------------
|
|
@@ -26,9 +32,9 @@ program
|
|
|
26
32
|
.option("--reporter", "Enable report generation", false)
|
|
27
33
|
.option("--clean", "Alias for --cleanReports", false)
|
|
28
34
|
.option("-p, --payloadPath <dir>", "Directory for payload files")
|
|
29
|
-
|
|
35
|
+
.option("--script <file>", "k6 script file to run")
|
|
36
|
+
.option("--k6Config <file>", "k6 config file to use")
|
|
30
37
|
.action(async (argv) => {
|
|
31
|
-
// Load config file
|
|
32
38
|
const configFileInput =
|
|
33
39
|
argv.config || process.env.CUCUMBER_CONFIG_FILE || "cucumber.js";
|
|
34
40
|
const configFilePath = path.isAbsolute(configFileInput)
|
|
@@ -36,20 +42,16 @@ program
|
|
|
36
42
|
: path.resolve(process.cwd(), configFileInput);
|
|
37
43
|
|
|
38
44
|
let configOptions = {};
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
console.warn("⚠️ Could not load config file:", err.message);
|
|
45
|
-
}
|
|
45
|
+
try {
|
|
46
|
+
const configModule = await import(pathToFileURL(configFilePath).href);
|
|
47
|
+
configOptions = configModule.default || configModule;
|
|
48
|
+
} catch (err) {
|
|
49
|
+
console.warn("⚠️ Could not load config file:", err.message);
|
|
46
50
|
}
|
|
47
51
|
|
|
48
|
-
// Build the cucumber-js command from config
|
|
49
52
|
const cucumberConfig = configOptions.default || configOptions;
|
|
50
|
-
|
|
53
|
+
const cliParts = [];
|
|
51
54
|
|
|
52
|
-
// Add paths
|
|
53
55
|
if (cucumberConfig.paths && Array.isArray(cucumberConfig.paths)) {
|
|
54
56
|
cliParts.push(...cucumberConfig.paths);
|
|
55
57
|
}
|
|
@@ -57,21 +59,14 @@ program
|
|
|
57
59
|
cliParts.push(argv.feature);
|
|
58
60
|
}
|
|
59
61
|
|
|
60
|
-
// Add require
|
|
61
62
|
if (cucumberConfig.require && Array.isArray(cucumberConfig.require)) {
|
|
62
|
-
cucumberConfig.require.forEach((req) =>
|
|
63
|
-
cliParts.push("--require", req);
|
|
64
|
-
});
|
|
63
|
+
cucumberConfig.require.forEach((req) => cliParts.push("--require", req));
|
|
65
64
|
}
|
|
66
65
|
|
|
67
|
-
// Add format
|
|
68
66
|
if (cucumberConfig.format && Array.isArray(cucumberConfig.format)) {
|
|
69
|
-
cucumberConfig.format.forEach((fmt) =>
|
|
70
|
-
cliParts.push("--format", fmt);
|
|
71
|
-
});
|
|
67
|
+
cucumberConfig.format.forEach((fmt) => cliParts.push("--format", fmt));
|
|
72
68
|
}
|
|
73
69
|
|
|
74
|
-
// Add tags
|
|
75
70
|
if (cucumberConfig.tags) {
|
|
76
71
|
cliParts.push("--tags", cucumberConfig.tags);
|
|
77
72
|
}
|
|
@@ -79,26 +74,23 @@ program
|
|
|
79
74
|
cliParts.push("--tags", argv.tags);
|
|
80
75
|
}
|
|
81
76
|
|
|
82
|
-
// Determine project root (where your main package.json is)
|
|
83
77
|
const projectRoot = process.cwd();
|
|
84
78
|
|
|
85
|
-
// Determine payload directory from CLI or config, always relative to project root
|
|
86
79
|
let payloadDirRaw =
|
|
87
80
|
argv.payloadPath ||
|
|
88
81
|
(cucumberConfig.worldParameters &&
|
|
89
82
|
cucumberConfig.worldParameters.payloadPath) ||
|
|
90
83
|
"payloads";
|
|
84
|
+
|
|
91
85
|
const payloadDir = path.resolve(projectRoot, payloadDirRaw);
|
|
92
86
|
console.log("📦 Resolved payload path:", payloadDir);
|
|
93
87
|
|
|
94
|
-
// Add --world-parameters to CLI args
|
|
95
88
|
const worldParams = {
|
|
96
89
|
...(cucumberConfig.worldParameters || {}),
|
|
97
90
|
payloadPath: payloadDir,
|
|
98
91
|
};
|
|
99
92
|
cliParts.push("--world-parameters", JSON.stringify(worldParams));
|
|
100
93
|
|
|
101
|
-
// Add CLI options for k6 script and config
|
|
102
94
|
if (argv.script) {
|
|
103
95
|
cliParts.push("--script", argv.script);
|
|
104
96
|
}
|
|
@@ -107,12 +99,9 @@ program
|
|
|
107
99
|
}
|
|
108
100
|
|
|
109
101
|
const finalCommand = ["npx", "cucumber-js", ...cliParts].join(" ");
|
|
110
|
-
|
|
111
102
|
console.log("▶️ Final arguments passed to cucumber-js:", finalCommand);
|
|
112
103
|
|
|
113
|
-
// Collect options to pass as env vars
|
|
114
104
|
const extraEnv = {
|
|
115
|
-
// Priority: cucumberConfig > CLI
|
|
116
105
|
SAVE_K6_SCRIPT:
|
|
117
106
|
cucumberConfig.saveK6Script === true || argv.saveK6Script === true
|
|
118
107
|
? "true"
|
|
@@ -133,19 +122,19 @@ program
|
|
|
133
122
|
: "false",
|
|
134
123
|
};
|
|
135
124
|
|
|
136
|
-
// Clean reports directory if requested
|
|
137
125
|
const shouldCleanReports =
|
|
138
126
|
argv.cleanReports || argv.clean || cucumberConfig.cleanReports;
|
|
139
127
|
|
|
140
128
|
if (shouldCleanReports) {
|
|
141
129
|
const reportsDir = path.join(projectRoot, "reports");
|
|
142
|
-
|
|
143
|
-
fs.
|
|
144
|
-
fs.
|
|
130
|
+
try {
|
|
131
|
+
await fs.rm(reportsDir, { recursive: true, force: true });
|
|
132
|
+
await fs.mkdir(reportsDir, { recursive: true });
|
|
133
|
+
} catch (err) {
|
|
134
|
+
console.error("Failed to clean reports folder:", err.message);
|
|
145
135
|
}
|
|
146
136
|
}
|
|
147
137
|
|
|
148
|
-
// Now spawn the process
|
|
149
138
|
const cucumberProcess = spawn("npx", ["cucumber-js", ...cliParts], {
|
|
150
139
|
stdio: "inherit",
|
|
151
140
|
env: { ...process.env, ...extraEnv },
|
|
@@ -174,3 +163,9 @@ program
|
|
|
174
163
|
});
|
|
175
164
|
|
|
176
165
|
program.parse(process.argv);
|
|
166
|
+
|
|
167
|
+
// Helper to handle dynamic import path
|
|
168
|
+
function pathToFileURL(p) {
|
|
169
|
+
const url = new URL("file://" + path.resolve(p));
|
|
170
|
+
return url;
|
|
171
|
+
}
|
package/package.json
CHANGED
package/scripts/linkReports.js
CHANGED
|
@@ -1,16 +1,44 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { minify } from "html-minifier-terser";
|
|
4
|
+
import { fileURLToPath } from "url";
|
|
5
|
+
import { exec } from "child_process";
|
|
6
|
+
import os from "os";
|
|
4
7
|
|
|
5
|
-
|
|
8
|
+
// ESM __dirname workaround
|
|
9
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
10
|
+
const __dirname = path.dirname(__filename);
|
|
6
11
|
|
|
7
|
-
|
|
12
|
+
const reportsDir = path.resolve(__dirname, "../reports");
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Opens a file in the default browser based on OS.
|
|
16
|
+
* @param {string} filePath - Absolute file path to open.
|
|
17
|
+
*/
|
|
18
|
+
function openInBrowser(filePath) {
|
|
19
|
+
const platform = os.platform();
|
|
20
|
+
const command =
|
|
21
|
+
platform === "darwin"
|
|
22
|
+
? `open "${filePath}"`
|
|
23
|
+
: platform === "win32"
|
|
24
|
+
? `start "" "${filePath}"`
|
|
25
|
+
: `xdg-open "${filePath}"`; // Linux
|
|
26
|
+
|
|
27
|
+
exec(command, (err) => {
|
|
28
|
+
if (err) {
|
|
29
|
+
console.error("⚠️ Failed to open the report in browser:", err.message);
|
|
30
|
+
} else {
|
|
31
|
+
console.log("🌐 Report opened in your default browser.");
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function linkReports() {
|
|
8
37
|
if (!fs.existsSync(reportsDir)) {
|
|
9
38
|
console.warn("⚠️ No reports directory found.");
|
|
10
39
|
return;
|
|
11
40
|
}
|
|
12
41
|
|
|
13
|
-
// Get all HTML files in the reports directory, excluding any with "combined-" in the name
|
|
14
42
|
const htmlFiles = fs
|
|
15
43
|
.readdirSync(reportsDir)
|
|
16
44
|
.filter((f) => f.endsWith(".html") && !f.includes("combined-"))
|
|
@@ -25,43 +53,40 @@ async function linkReports() {
|
|
|
25
53
|
return;
|
|
26
54
|
}
|
|
27
55
|
|
|
28
|
-
// Find k6 report: file starting with "k6-" or fallback to most recent
|
|
29
56
|
let k6Report = htmlFiles.find((f) => f.name.startsWith("k6-"));
|
|
30
57
|
if (!k6Report) {
|
|
31
58
|
k6Report = htmlFiles.reduce((a, b) => (a.mtime > b.mtime ? a : b));
|
|
32
59
|
}
|
|
33
60
|
|
|
34
|
-
// Find all other HTML reports (excluding k6 report)
|
|
35
61
|
const otherReports = htmlFiles.filter((f) => f.name !== k6Report.name);
|
|
36
|
-
|
|
37
62
|
if (!k6Report || otherReports.length === 0) {
|
|
38
63
|
console.warn("⚠️ K6 or any other HTML report not found.");
|
|
39
64
|
return;
|
|
40
65
|
}
|
|
41
66
|
|
|
42
|
-
// Read k6 report content
|
|
43
67
|
let content = fs.readFileSync(k6Report.path, "utf8");
|
|
44
68
|
|
|
45
|
-
// Build tabs for each other report
|
|
46
69
|
const tabs = otherReports
|
|
47
70
|
.map(
|
|
48
71
|
(report, idx) => `
|
|
49
72
|
<input type="radio" name="tabs" id="tab${idx + 2}">
|
|
50
|
-
<label for="tab${idx + 2}"><i class="fas fa-file-alt"></i> ${
|
|
73
|
+
<label for="tab${idx + 2}"><i class="fas fa-file-alt"></i> ${
|
|
74
|
+
report.name
|
|
75
|
+
}</label>
|
|
51
76
|
<div class="tab">
|
|
52
|
-
<iframe src="${
|
|
77
|
+
<iframe src="${
|
|
78
|
+
report.name
|
|
79
|
+
}" style="width:100%; height:600px; border:none;"></iframe>
|
|
53
80
|
</div>
|
|
54
81
|
`
|
|
55
82
|
)
|
|
56
83
|
.join("\n");
|
|
57
84
|
|
|
58
|
-
// Insert tabs after the first tab (assumes k6 report has a tab structure)
|
|
59
85
|
content = content.replace(
|
|
60
86
|
/<input type="radio" name="tabs" id="tabone"/,
|
|
61
87
|
`${tabs}\n<input type="radio" name="tabs" id="tabone"`
|
|
62
88
|
);
|
|
63
89
|
|
|
64
|
-
// Remove any existing footer
|
|
65
90
|
content = content.replace(
|
|
66
91
|
/<div style="padding:10px;margin-top:20px;text-align:center">[\s\S]*?<\/div>/,
|
|
67
92
|
""
|
|
@@ -77,6 +102,6 @@ async function linkReports() {
|
|
|
77
102
|
fs.writeFileSync(combinedPath, minified, "utf8");
|
|
78
103
|
|
|
79
104
|
console.log(`📄 Combined report generated at: ${combinedPath}`);
|
|
80
|
-
}
|
|
81
105
|
|
|
82
|
-
|
|
106
|
+
openInBrowser(combinedPath);
|
|
107
|
+
}
|