k6-cucumber-steps 1.0.28 → 1.0.30

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,8 +1,7 @@
1
1
  name: build
2
2
 
3
3
  on:
4
- push:
5
- branches: ["main"]
4
+ workflow_dispatch:
6
5
 
7
6
  jobs:
8
7
  release:
package/README.md CHANGED
@@ -110,17 +110,17 @@ Here's a step-by-step guide to using `k6-cucumber-steps` in your project:
110
110
  Create a `cucumber.js` file at the root of your project with the following content:
111
111
 
112
112
  ```javascript
113
- // cucumber.js
114
- require("dotenv").config();
113
+ // cucumber.js as default name but you can optionally give it any name of choice
115
114
 
116
115
  module.exports = {
117
116
  require: [
118
117
  // You can add paths to your local step definitions here if needed
119
118
  ],
119
+ reporter:true // To provide HTML and JSON report
120
120
  format: [
121
121
  "summary",
122
122
  "json:reports/load-report.json", // For JSON report
123
- "html:reports/report.html", // For HTML report (requires @cucumber/html-formatter)
123
+ "html:reports/report.html", // For HTML report
124
124
  ],
125
125
  tags: process.env.TAGS,
126
126
  overwrite: false, // Default to not overwrite the report file
@@ -129,16 +129,16 @@ Here's a step-by-step guide to using `k6-cucumber-steps` in your project:
129
129
 
130
130
  **Running Tests:**
131
131
 
132
- From the root of your project, use the CLI command:
132
+ From the root of your project, use the CLI command for default config:
133
133
 
134
134
  ```bash
135
135
  npx k6-cucumber-steps run
136
136
  ```
137
137
 
138
- You can also specify a feature file or tags:
138
+ You can also specify a configFile:
139
139
 
140
140
  ```bash
141
- npx k6-cucumber-steps run --feature features/example.feature -t "@yourTag"
141
+ npx k6-cucumber-steps run --configFile cucumber.prod.js
142
142
  ```
143
143
 
144
144
  ---
@@ -0,0 +1,161 @@
1
+ #!/usr/bin/env node
2
+
3
+ const path = require("path");
4
+ const fs = require("fs");
5
+ const { spawn } = require("child_process");
6
+ const yargs = require("yargs");
7
+ require("dotenv").config();
8
+
9
+ // 🚀 Welcome Message
10
+ console.log(`
11
+ -----------------------------------------
12
+ 🚀 Starting k6-cucumber-steps execution...
13
+ -----------------------------------------
14
+ `);
15
+
16
+ const argv = yargs
17
+ .usage("Usage: $0 run [options]")
18
+ .option("feature", {
19
+ alias: "f",
20
+ describe: "Path to the feature file",
21
+ type: "string",
22
+ })
23
+ .option("tags", {
24
+ alias: "t",
25
+ describe: "Cucumber tags to filter scenarios",
26
+ type: "string",
27
+ })
28
+ .option("reporter", {
29
+ alias: "r",
30
+ describe: "Enable HTML and JSON reports",
31
+ type: "boolean",
32
+ default: false,
33
+ })
34
+ .option("overwrite", {
35
+ alias: "o",
36
+ describe: "Overwrite existing reports",
37
+ type: "boolean",
38
+ default: false,
39
+ })
40
+ .option("configFile", {
41
+ alias: "c",
42
+ describe: "Custom cucumber config file",
43
+ type: "string",
44
+ })
45
+ .help().argv;
46
+
47
+ const cucumberArgs = ["cucumber-js"];
48
+
49
+ const configFileName =
50
+ argv.configFile || process.env.CUCUMBER_CONFIG_FILE || "cucumber.js";
51
+ const configFilePath = path.resolve(process.cwd(), configFileName);
52
+
53
+ console.log(`🔍 Looking for config file: ${configFileName}`);
54
+ console.log(`📁 Resolved config file path: ${configFilePath}`);
55
+
56
+ let configOptions = {};
57
+ if (fs.existsSync(configFilePath)) {
58
+ console.log("✅ Config file found, including in cucumber arguments...");
59
+ cucumberArgs.push("--config", configFileName);
60
+
61
+ try {
62
+ const loadedConfig = require(configFilePath);
63
+ configOptions = loadedConfig.default || loadedConfig;
64
+ } catch (err) {
65
+ console.warn("⚠️ Failed to load options from config file.");
66
+ }
67
+ } else {
68
+ console.warn("⚠️ Config file not found, skipping...");
69
+ }
70
+
71
+ // Tags
72
+ const tags = argv.tags || process.env.TAGS || configOptions.tags;
73
+ if (tags) {
74
+ cucumberArgs.push("--tags", tags);
75
+ }
76
+
77
+ // Feature file(s)
78
+ const feature = argv.feature || process.env.FEATURE_PATH;
79
+ if (feature) {
80
+ cucumberArgs.push(path.resolve(process.cwd(), feature));
81
+ } else if (configOptions.paths && configOptions.paths.length > 0) {
82
+ cucumberArgs.push(...configOptions.paths);
83
+ }
84
+
85
+ // Reporting
86
+ const reportsDir = path.join(process.cwd(), "reports");
87
+ const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
88
+
89
+ const shouldGenerateReports = argv.reporter || configOptions.reporter || false;
90
+
91
+ const shouldOverwrite =
92
+ argv.overwrite ||
93
+ process.env.K6_CUCUMBER_OVERWRITE === "true" ||
94
+ configOptions.overwrite === true;
95
+
96
+ // Build base names from feature file(s)
97
+ let baseNames = [];
98
+
99
+ if (feature) {
100
+ baseNames = [path.basename(feature, path.extname(feature))];
101
+ } else if (configOptions.paths && configOptions.paths.length > 0) {
102
+ baseNames = configOptions.paths.map((p) => path.basename(p, path.extname(p)));
103
+ } else {
104
+ baseNames = ["load-results"];
105
+ }
106
+
107
+ // Generate report paths
108
+ const reportPaths = baseNames.map((base) => {
109
+ const jsonName = shouldOverwrite
110
+ ? `${base}.json`
111
+ : `${base}-${timestamp}.json`;
112
+ const htmlName = shouldOverwrite
113
+ ? `${base}.html`
114
+ : `${base}-${timestamp}.html`;
115
+
116
+ return {
117
+ json: path.join(reportsDir, jsonName),
118
+ html: path.join(reportsDir, htmlName),
119
+ };
120
+ });
121
+
122
+ // Inject report formats into cucumber arguments
123
+ if (shouldGenerateReports) {
124
+ fs.mkdirSync(reportsDir, { recursive: true });
125
+
126
+ cucumberArgs.push("--format", "summary");
127
+
128
+ reportPaths.forEach(({ json, html }) => {
129
+ cucumberArgs.push("--format", `json:${json}`);
130
+ cucumberArgs.push("--format", `html:${html}`);
131
+ });
132
+ }
133
+
134
+ console.log("📝 Reporter Enabled:", shouldGenerateReports);
135
+ console.log("📝 Overwrite Enabled:", shouldOverwrite);
136
+
137
+ console.log("\n▶️ Final arguments passed to cucumber-js:");
138
+ console.log(["npx", ...cucumberArgs].join(" ") + "\n");
139
+
140
+ const cucumberProcess = spawn("npx", cucumberArgs, {
141
+ stdio: "inherit",
142
+ env: {
143
+ ...process.env,
144
+ K6_CUCUMBER_OVERWRITE: shouldOverwrite ? "true" : "false",
145
+ TAGS: tags,
146
+ FEATURE_PATH: feature,
147
+ REPORT_JSON_PATH: reportPaths[0]?.json,
148
+ REPORT_HTML_PATH: reportPaths[0]?.html,
149
+ },
150
+ });
151
+
152
+ cucumberProcess.on("close", (code) => {
153
+ console.log(`\n-----------------------------------------`);
154
+ if (code === 0) {
155
+ console.log("✅ k6-cucumber-steps execution completed successfully.");
156
+ } else {
157
+ console.error("❌ k6-cucumber-steps execution failed.");
158
+ }
159
+ console.log(`-----------------------------------------\n`);
160
+ process.exit(code);
161
+ });
package/cucumber.js CHANGED
@@ -1,14 +1,14 @@
1
- // cucumber.js
2
-
3
1
  module.exports = {
4
- require: ["./step_definitions/**/*.js"],
5
- format: [
6
- "summary",
7
- "json:src/examples/reports/load-report.json",
8
- "html:src/examples/reports/report.html",
9
- ],
10
- // Specify the path to your features folder here
11
- paths: ["./src/examples/features/loadTestTemplate.feature"],
12
- tags: "@loadTest", // Default tag for load tests
13
- overwrite: false, // Default to not overwrite the report file
2
+ default: {
3
+ require: ["./step_definitions/**/*.js"],
4
+ format: [
5
+ "summary",
6
+ "json:./src/examples/reports/load-report.json",
7
+ "html:./src/examples/reports/report.html",
8
+ ],
9
+ paths: ["./src/examples/features/loadTestTemplate.feature"],
10
+ tags: process.env.TAGS,
11
+ overwrite: true,
12
+ reporter: true,
13
+ },
14
14
  };
package/index.js CHANGED
@@ -1 +1,23 @@
1
- export * from "./step_definitions/load_test_steps.js";
1
+ // Import necessary modules
2
+ const buildK6Script = require("./libs/helpers/buildK6Script");
3
+ const generateHeaders = require("./libs/helpers/generateHeaders");
4
+ const resolveBody = require("./libs/helpers/resolveBody");
5
+ const {
6
+ generateK6Script: generateTempK6Script,
7
+ runK6Script,
8
+ } = require("./libs/utils/k6Runner");
9
+
10
+ // Export the main functionalities of the package
11
+ module.exports = {
12
+ // Helper functions
13
+ buildK6Script,
14
+ generateHeaders,
15
+ resolveBody,
16
+
17
+ // k6 script generation and execution utilities
18
+ generateK6Script: generateTempK6Script,
19
+ runK6Script,
20
+
21
+ // Step definitions (optional, if users need direct access)
22
+ stepDefinitions: require("./step_definitions/load_test_steps"),
23
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "k6-cucumber-steps",
3
- "version": "1.0.28",
3
+ "version": "1.0.30",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -174,23 +174,19 @@ Then(
174
174
  );
175
175
  }
176
176
  try {
177
- // Generate the k6 script content
178
177
  const scriptContent = buildK6Script(this.config);
179
-
180
- // Generate the temporary k6 script file with overwrite option
181
178
  const scriptPath = await generateK6Script(
182
179
  scriptContent,
183
180
  "load",
184
- this.overwrite
181
+ process.env.K6_CUCUMBER_OVERWRITE === "true"
182
+ );
183
+ const stdout = await runK6Script(
184
+ scriptPath,
185
+ process.env.K6_CUCUMBER_OVERWRITE === "true"
185
186
  );
186
-
187
- // Run the k6 script with overwrite option
188
- const stdout = await runK6Script(scriptPath, this.overwrite);
189
-
190
187
  console.log(stdout);
191
188
  } catch (error) {
192
189
  console.error("k6 execution failed:", error.message);
193
- console.error("k6 stderr:", error.stderr); // Log stderr for debugging
194
190
  throw new Error("k6 test execution failed");
195
191
  }
196
192
  }
@@ -1,118 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- console.log("-----------------------------------------");
4
- console.log("🚀 Starting k6-cucumber-steps execution...");
5
- console.log("-----------------------------------------");
6
-
7
- const path = require("path");
8
- const { spawn } = require("child_process");
9
- require("dotenv").config();
10
- const argv = require("yargs")
11
- .usage("Usage: $0 run [options]")
12
- .option("feature", {
13
- alias: "f",
14
- describe: "Path to the feature file",
15
- type: "string",
16
- })
17
- .option("tags", {
18
- alias: "t",
19
- describe: "Cucumber tags to filter scenarios (e.g., @smoke)",
20
- type: "string",
21
- })
22
- .option("reporter", {
23
- alias: "r",
24
- describe: "Generate HTML and JSON reports in the `reports` directory",
25
- type: "boolean",
26
- default: false,
27
- })
28
- .option("overwrite", {
29
- alias: "o",
30
- describe: "Overwrite existing reports instead of appending them",
31
- type: "boolean",
32
- default: false,
33
- })
34
- .help().argv;
35
-
36
- // Base Cucumber command and arguments
37
- const cucumberCommand = "npx";
38
- const cucumberArgs = [
39
- "cucumber-js",
40
- "--require-module",
41
- "@babel/register",
42
- "--require",
43
- path.resolve(
44
- process.cwd(),
45
- "node_modules",
46
- "k6-cucumber-steps",
47
- "step_definitions"
48
- ),
49
- "--require",
50
- path.resolve(process.cwd(), "step_definitions"), // Include user's local step definitions
51
- "--format",
52
- "summary",
53
- "--format",
54
- "progress", // Progress bar format
55
- ];
56
-
57
- // Add tags from CLI or environment variables
58
- if (argv.tags) {
59
- cucumberArgs.push("--tags", argv.tags);
60
- } else if (process.env.TAGS) {
61
- cucumberArgs.push("--tags", process.env.TAGS);
62
- }
63
-
64
- // Add feature file if provided
65
- if (argv.feature) {
66
- cucumberArgs.push(path.resolve(process.cwd(), argv.feature));
67
- }
68
-
69
- // Add reporting options if enabled
70
- if (argv.reporter) {
71
- const reportsDir = path.resolve(process.cwd(), "reports");
72
- try {
73
- require("fs").mkdirSync(reportsDir, { recursive: true }); // Ensure reports directory exists
74
- } catch (err) {
75
- console.error(`Failed to create reports directory: ${err.message}`);
76
- process.exit(1);
77
- }
78
- cucumberArgs.push(
79
- "--format",
80
- `json:${path.join(reportsDir, "load-results.json")}`,
81
- "--format",
82
- `html:${path.join(reportsDir, "load-results.html")}`
83
- );
84
- }
85
-
86
- /**
87
- * Main function to execute the Cucumber process.
88
- */
89
- async function main() {
90
- try {
91
- const cucumberProcess = spawn(cucumberCommand, cucumberArgs, {
92
- cwd: process.cwd(),
93
- stdio: "inherit", // Inherit stdout/stderr for real-time logging
94
- env: {
95
- ...process.env,
96
- K6_CUCUMBER_OVERWRITE: argv.overwrite, // Pass overwrite flag to environment
97
- },
98
- });
99
-
100
- cucumberProcess.on("close", (code) => {
101
- if (code === 0) {
102
- console.log("-----------------------------------------");
103
- console.log("✅ k6-cucumber-steps execution completed successfully.");
104
- console.log("-----------------------------------------");
105
- } else {
106
- console.error("-----------------------------------------");
107
- console.error("❌ k6-cucumber-steps execution failed.");
108
- console.error("-----------------------------------------");
109
- }
110
- process.exit(code);
111
- });
112
- } catch (error) {
113
- console.error("An unexpected error occurred:", error.message);
114
- process.exit(1);
115
- }
116
- }
117
-
118
- main().catch((err) => console.error(err));