k6-cucumber-steps 1.2.19 → 1.2.20
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
CHANGED
|
@@ -24,11 +24,9 @@ program
|
|
|
24
24
|
.option("--overwrite", "Overwrite report files", false)
|
|
25
25
|
.option("--cleanReports", "Clean the reports folder before running", false)
|
|
26
26
|
.option("--reporter", "Enable report generation", false)
|
|
27
|
-
.option("--clean", "Alias for --cleanReports")
|
|
27
|
+
.option("--clean", "Alias for --cleanReports", false)
|
|
28
28
|
.option("-p, --payloadPath <dir>", "Directory for payload files")
|
|
29
29
|
// Add CLI options for all config options
|
|
30
|
-
.option("--script <file>", "k6 script file to run")
|
|
31
|
-
.option("--k6Config <file>", "k6 config file to use")
|
|
32
30
|
.action(async (argv) => {
|
|
33
31
|
// Load config file
|
|
34
32
|
const configFileInput =
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
module.exports = function buildK6Script(config) {
|
|
2
|
-
const { method, endpoints, endpoint, body, headers, options, baseUrl } =
|
|
2
|
+
const { method, endpoints, endpoint, body, headers, options, baseUrl } =
|
|
3
|
+
config;
|
|
3
4
|
|
|
4
5
|
const BASE_URL =
|
|
5
6
|
baseUrl ||
|
|
@@ -54,7 +55,8 @@ export default function () {
|
|
|
54
55
|
: "JSON.stringify(body)"
|
|
55
56
|
}, { headers });
|
|
56
57
|
check(res${i}, {
|
|
57
|
-
"status is 2xx": (r) => r.status >= 200 && r.status < 300
|
|
58
|
+
"status is 2xx": (r) => r.status >= 200 && r.status < 300,
|
|
59
|
+
"response body is not empty": (r) => r.body && r.body.length > 0,
|
|
58
60
|
});
|
|
59
61
|
`
|
|
60
62
|
)
|
|
@@ -67,7 +69,8 @@ export default function () {
|
|
|
67
69
|
: "JSON.stringify(body)"
|
|
68
70
|
}, { headers });
|
|
69
71
|
check(res, {
|
|
70
|
-
"status is 2xx": (r) => r.status >= 200 && r.status < 300
|
|
72
|
+
"status is 2xx": (r) => r.status >= 200 && r.status < 300,
|
|
73
|
+
"response body is not empty": (r) => r.body && r.body.length > 0,
|
|
71
74
|
});
|
|
72
75
|
`
|
|
73
76
|
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const { generateK6Script, runK6Script } = require("./runner");
|
|
2
|
+
const buildK6Script = require("./buildK6Script");
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Runs a K6 script using values from the World instance.
|
|
6
|
+
* Supports both single and multiple endpoints.
|
|
7
|
+
*
|
|
8
|
+
* @param {object} world - Cucumber World instance
|
|
9
|
+
* @param {string} method - HTTP method
|
|
10
|
+
* @param {boolean} isMulti - Flag to use `world.endpoints` instead of `endpoint`
|
|
11
|
+
*/
|
|
12
|
+
async function runK6ScriptFromWorld(world, method, isMulti = false) {
|
|
13
|
+
const scriptContent = buildK6Script({
|
|
14
|
+
method,
|
|
15
|
+
endpoint: isMulti ? undefined : world.endpoint,
|
|
16
|
+
endpoints: isMulti ? world.endpoints : undefined,
|
|
17
|
+
body: world.body,
|
|
18
|
+
headers: world.headers,
|
|
19
|
+
options: world.k6Options,
|
|
20
|
+
baseUrl: world.baseUrl,
|
|
21
|
+
worldParameters: world.worldParameters,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const scriptPath = await generateK6Script(scriptContent, "api", true);
|
|
25
|
+
const { stdout, stderr, code } = await runK6Script(scriptPath, true);
|
|
26
|
+
|
|
27
|
+
console.log(stdout);
|
|
28
|
+
if (stderr) console.error(stderr);
|
|
29
|
+
if (code !== 0) {
|
|
30
|
+
throw new Error("❌ K6 script execution failed.");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
console.log("✅ K6 script ran successfully.");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = runK6ScriptFromWorld;
|
package/package.json
CHANGED
|
@@ -7,9 +7,10 @@ import crypto from "crypto";
|
|
|
7
7
|
import * as dotenv from "dotenv";
|
|
8
8
|
import resolvePayloadPath from "../lib/helpers/resolvePayloadPath.js";
|
|
9
9
|
import resolveBody from "../lib/helpers/resolveBody.js";
|
|
10
|
-
import buildK6Script from "../lib/helpers/buildK6Script.js";
|
|
10
|
+
// import buildK6Script from "../lib/helpers/buildK6Script.js";
|
|
11
11
|
import generateHeaders from "../lib/helpers/generateHeaders.js";
|
|
12
|
-
import { runK6Script } from "../lib/utils/k6Runner.js";
|
|
12
|
+
// import { runK6Script } from "../lib/utils/k6Runner.js";
|
|
13
|
+
const runK6ScriptFromWorld = require("../../../helpers/runK6ScriptFromWorld");
|
|
13
14
|
|
|
14
15
|
dotenv.config();
|
|
15
16
|
|
|
@@ -608,79 +609,95 @@ if (!fs.existsSync(reportDir)) {
|
|
|
608
609
|
fs.mkdirSync(reportDir, { recursive: true });
|
|
609
610
|
}
|
|
610
611
|
|
|
612
|
+
// Then(
|
|
613
|
+
// /^I see the API should handle the (\w+) request successfully$/,
|
|
614
|
+
// { timeout: 300000 },
|
|
615
|
+
// async function (method) {
|
|
616
|
+
// if (!this.config || !this.config.method) {
|
|
617
|
+
// throw new Error("Configuration is missing or incomplete.");
|
|
618
|
+
// }
|
|
619
|
+
// const expectedMethod = method.toUpperCase();
|
|
620
|
+
// const actualMethod = this.config.method.toUpperCase();
|
|
621
|
+
// if (actualMethod !== expectedMethod) {
|
|
622
|
+
// throw new Error(
|
|
623
|
+
// `Mismatched HTTP method: expected "${expectedMethod}", got "${actualMethod}"`
|
|
624
|
+
// );
|
|
625
|
+
// }
|
|
626
|
+
// try {
|
|
627
|
+
// const scriptContent = buildK6Script(this.config);
|
|
628
|
+
// const uniqueId = crypto.randomBytes(8).toString("hex");
|
|
629
|
+
// const scriptFileName = `k6-script-${uniqueId}.js`;
|
|
630
|
+
// const scriptPath = path.join(reportDir, scriptFileName);
|
|
631
|
+
// fs.writeFileSync(scriptPath, scriptContent, "utf-8");
|
|
632
|
+
// this.log?.(`✅ k6 script generated at: "${scriptPath}"`);
|
|
633
|
+
|
|
634
|
+
// this.log?.(`🚀 Running k6 script: "${scriptFileName}"...`);
|
|
635
|
+
// const { stdout, stderr, code } = await runK6Script(
|
|
636
|
+
// scriptPath,
|
|
637
|
+
// process.env.K6_CUCUMBER_OVERWRITE === "true"
|
|
638
|
+
// );
|
|
639
|
+
// if (stdout) this.log?.(`k6 STDOUT:\n${stdout}`);
|
|
640
|
+
// if (stderr) this.log?.(`k6 STDERR:\n${stderr}`);
|
|
641
|
+
|
|
642
|
+
// if (code !== 0) {
|
|
643
|
+
// throw new Error(
|
|
644
|
+
// `k6 process exited with code ${code}. Check k6 output for details.`
|
|
645
|
+
// );
|
|
646
|
+
// }
|
|
647
|
+
// this.log?.(
|
|
648
|
+
// `✅ k6 script executed successfully for ${expectedMethod} request.`
|
|
649
|
+
// );
|
|
650
|
+
|
|
651
|
+
// const saveK6Script =
|
|
652
|
+
// process.env.saveK6Script === "true" ||
|
|
653
|
+
// process.env.SAVE_K6_SCRIPT === "true" ||
|
|
654
|
+
// this.parameters?.saveK6Script === true;
|
|
655
|
+
|
|
656
|
+
// if (!saveK6Script) {
|
|
657
|
+
// try {
|
|
658
|
+
// fs.unlinkSync(scriptPath);
|
|
659
|
+
// this.log?.(`🧹 Temporary k6 script deleted: "${scriptPath}"`);
|
|
660
|
+
// } catch (cleanupErr) {
|
|
661
|
+
// this.log?.(
|
|
662
|
+
// `⚠️ Warning: Could not delete temporary k6 script file: "${scriptPath}". Error: ${
|
|
663
|
+
// cleanupErr instanceof Error
|
|
664
|
+
// ? cleanupErr.message
|
|
665
|
+
// : String(cleanupErr)
|
|
666
|
+
// }`
|
|
667
|
+
// );
|
|
668
|
+
// }
|
|
669
|
+
// } else {
|
|
670
|
+
// this.log?.(
|
|
671
|
+
// `ℹ️ k6 script kept at: "${scriptPath}". Set SAVE_K6_SCRIPT=false to delete automatically.`
|
|
672
|
+
// );
|
|
673
|
+
// }
|
|
674
|
+
// } catch (error) {
|
|
675
|
+
// this.log?.(
|
|
676
|
+
// `❌ Failed to generate or run k6 script: ${
|
|
677
|
+
// error instanceof Error ? error.message : String(error)
|
|
678
|
+
// }`
|
|
679
|
+
// );
|
|
680
|
+
// throw new Error(
|
|
681
|
+
// `k6 script generation or execution failed: ${
|
|
682
|
+
// error instanceof Error ? error.message : String(error)
|
|
683
|
+
// }`
|
|
684
|
+
// );
|
|
685
|
+
// }
|
|
686
|
+
// }
|
|
687
|
+
// );
|
|
688
|
+
|
|
689
|
+
// Single endpoint
|
|
611
690
|
Then(
|
|
612
|
-
|
|
613
|
-
{ timeout: 300000 },
|
|
691
|
+
"I see the API should handle the {word} request successfully",
|
|
614
692
|
async function (method) {
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
const expectedMethod = method.toUpperCase();
|
|
619
|
-
const actualMethod = this.config.method.toUpperCase();
|
|
620
|
-
if (actualMethod !== expectedMethod) {
|
|
621
|
-
throw new Error(
|
|
622
|
-
`Mismatched HTTP method: expected "${expectedMethod}", got "${actualMethod}"`
|
|
623
|
-
);
|
|
624
|
-
}
|
|
625
|
-
try {
|
|
626
|
-
const scriptContent = buildK6Script(this.config);
|
|
627
|
-
const uniqueId = crypto.randomBytes(8).toString("hex");
|
|
628
|
-
const scriptFileName = `k6-script-${uniqueId}.js`;
|
|
629
|
-
const scriptPath = path.join(reportDir, scriptFileName);
|
|
630
|
-
fs.writeFileSync(scriptPath, scriptContent, "utf-8");
|
|
631
|
-
this.log?.(`✅ k6 script generated at: "${scriptPath}"`);
|
|
632
|
-
|
|
633
|
-
this.log?.(`🚀 Running k6 script: "${scriptFileName}"...`);
|
|
634
|
-
const { stdout, stderr, code } = await runK6Script(
|
|
635
|
-
scriptPath,
|
|
636
|
-
process.env.K6_CUCUMBER_OVERWRITE === "true"
|
|
637
|
-
);
|
|
638
|
-
if (stdout) this.log?.(`k6 STDOUT:\n${stdout}`);
|
|
639
|
-
if (stderr) this.log?.(`k6 STDERR:\n${stderr}`);
|
|
640
|
-
|
|
641
|
-
if (code !== 0) {
|
|
642
|
-
throw new Error(
|
|
643
|
-
`k6 process exited with code ${code}. Check k6 output for details.`
|
|
644
|
-
);
|
|
645
|
-
}
|
|
646
|
-
this.log?.(
|
|
647
|
-
`✅ k6 script executed successfully for ${expectedMethod} request.`
|
|
648
|
-
);
|
|
693
|
+
await runK6ScriptFromWorld(this, method);
|
|
694
|
+
}
|
|
695
|
+
);
|
|
649
696
|
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
if (!saveK6Script) {
|
|
656
|
-
try {
|
|
657
|
-
fs.unlinkSync(scriptPath);
|
|
658
|
-
this.log?.(`🧹 Temporary k6 script deleted: "${scriptPath}"`);
|
|
659
|
-
} catch (cleanupErr) {
|
|
660
|
-
this.log?.(
|
|
661
|
-
`⚠️ Warning: Could not delete temporary k6 script file: "${scriptPath}". Error: ${
|
|
662
|
-
cleanupErr instanceof Error
|
|
663
|
-
? cleanupErr.message
|
|
664
|
-
: String(cleanupErr)
|
|
665
|
-
}`
|
|
666
|
-
);
|
|
667
|
-
}
|
|
668
|
-
} else {
|
|
669
|
-
this.log?.(
|
|
670
|
-
`ℹ️ k6 script kept at: "${scriptPath}". Set SAVE_K6_SCRIPT=false to delete automatically.`
|
|
671
|
-
);
|
|
672
|
-
}
|
|
673
|
-
} catch (error) {
|
|
674
|
-
this.log?.(
|
|
675
|
-
`❌ Failed to generate or run k6 script: ${
|
|
676
|
-
error instanceof Error ? error.message : String(error)
|
|
677
|
-
}`
|
|
678
|
-
);
|
|
679
|
-
throw new Error(
|
|
680
|
-
`k6 script generation or execution failed: ${
|
|
681
|
-
error instanceof Error ? error.message : String(error)
|
|
682
|
-
}`
|
|
683
|
-
);
|
|
684
|
-
}
|
|
697
|
+
// Multi-endpoint
|
|
698
|
+
Then(
|
|
699
|
+
"I run the script for multiple endpoints with method {word}",
|
|
700
|
+
async function (method) {
|
|
701
|
+
await runK6ScriptFromWorld(this, method, true);
|
|
685
702
|
}
|
|
686
703
|
);
|