Skip to content

Commit a919b5f

Browse files
committed
changes for cypress 10 support
1 parent 9f8a5fa commit a919b5f

File tree

8 files changed

+141
-34
lines changed

8 files changed

+141
-34
lines changed

commands/init.js

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
var fs = require('fs');
2-
var path = require('path')
2+
var path = require('path');
3+
const { exit } = require('process');
34
const { config } = require('yargs');
45
const constants = require('./utils/constants.js')
56

67
function create_file(file_path, content) {
78
fs.writeFile(file_path, content, function (err) {
89
if (err) throw err;
9-
console.log('Saved at ', file_path);
10+
console.log('Saved at ',file_path);
1011
});
1112
}
1213

1314
function create_ltconfig_file(args) {
15+
// console.log("args - ", args);
16+
// console.log("args length - ", args._.length);
17+
// console.log("EXITING");
18+
// exit(1);
19+
1420
let config = require('./utils/default_config.js')
1521
let content = JSON.stringify(config, null, 3);
1622
if (args._.length == 1) {
@@ -42,7 +48,41 @@ function create_ltconfig_file(args) {
4248
}
4349
};
4450

51+
function create_base_reporter_config_file(args) {
52+
let config = require('./utils/default_reporter_config.js')
53+
let content = JSON.stringify(config, null, 3);
54+
if (args._.length == 1) {
55+
create_file(constants.LT_BASE_REPORTER_CONFIG_FILE_NAME, content)
56+
}
57+
// else if (args._.length > 1) {
58+
// //check if file or directory exists
59+
// if (fs.existsSync(args._[1])) {
60+
// let stats = fs.statSync(args._[1]);
61+
// if (stats.isFile()) {
62+
// make_file(args._[1], content)
63+
// }
64+
// else {
65+
// create_file(path.join(args._[1], constants.LT_CONFIG_NAME), content)
66+
// }
67+
// }
68+
// else {
69+
// filename = path.basename(args._[1])
70+
// var re = new RegExp(".+\\..+");
71+
// if (re.test(filename)) {
72+
// fs.mkdirSync(path.dirname(args._[1]), { recursive: true });
73+
// create_file(args._[1], content)
74+
// }
75+
// else {
76+
// fs.mkdirSync(args._[1], { recursive: true });
77+
// create_file(path.join(args._[1], constants.LT_CONFIG_NAME), content)
78+
// }
79+
// }
80+
// }
81+
};
82+
4583
module.exports = function (args) {
46-
create_ltconfig_file(args)
84+
85+
// create_ltconfig_file(args);
86+
create_base_reporter_config_file(args);
4787

4888
};

commands/run.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,25 @@ module.exports = function (args) {
6868
.then(function (lt_config) {
6969
//validate the config options
7070
validate(lt_config, resp)
71-
.then(function () {
71+
.then(function (cypressVersion) {
72+
console.log("cypressVersion requested - ", cypressVersion);
73+
74+
/*
75+
update ltconfig to contain the cypress_version
76+
case 1: user passed cypress_version in run_settings, this case will work as earlier
77+
case 2: user hasn't passed cypress_version in run_settting, then also we will pass it, so that we can track this parameter in further services
78+
*/
79+
80+
81+
/* TEST scenarios:
82+
- user passes cypress_version in run_settings with both cases- with semver/without semver
83+
- user doesnot pass cypress_version in run_settings
84+
*/
85+
86+
if (!("cypress_version" in lt_config.run_settings)){
87+
console.log("user has not passed cypress version in run_SEttings");
88+
lt_config.run_settings.cypress_version = cypressVersion;
89+
}
7290
batcher
7391
.make_batches(lt_config)
7492
.then(function (batches) {
@@ -90,7 +108,7 @@ module.exports = function (args) {
90108
.start(tunnelArguments)
91109
.then((status) => {
92110
batch_runner
93-
.run_batches(lt_config, batches, env)
111+
.run_batches(lt_config, batches, env, cypressVersion)
94112
.then(function (exit_code) {
95113
console.log("stopping tunnel");
96114
tunnelInstance
@@ -132,7 +150,7 @@ module.exports = function (args) {
132150
});
133151
} else {
134152
batch_runner
135-
.run_batches(lt_config, batches, env)
153+
.run_batches(lt_config, batches, env, cypressVersion)
136154
.then(function (exit_code) {
137155
if (lt_config["run_settings"]["exit-on-failure"]) {
138156
process.exit(exit_code);

commands/utils/batch/batch_runner.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function run_test(payload, env = "prod", rejectUnauthorized) {
7878
});
7979
}
8080

81-
async function run(lt_config, batches, env, i = 0) {
81+
async function run(lt_config, batches, env, cypressVersion) {
8282
totalBatches = batches.length;
8383
//console.log("Total number of batches " + totalBatches);
8484
return new Promise(function (resolve, reject) {
@@ -116,8 +116,11 @@ async function run(lt_config, batches, env, i = 0) {
116116
username: lt_config["lambdatest_auth"]["username"],
117117
access_key: lt_config["lambdatest_auth"]["access_key"],
118118
type: "cypress",
119+
cypressVersion: cypressVersion,
119120
});
120121

122+
// console.log("paylaod ", payload);
123+
// process.exit(1);
121124
run_test(
122125
payload,
123126
env,

commands/utils/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
PROJECT_UPLOAD_URL: "/url",
66
CLI: "/cy/versions",
77
LT_CONFIG_NAME: "lambdatest-config.json",
8+
LT_BASE_REPORTER_CONFIG_FILE_NAME: "base_reporter_config.json",
89
CYPRESS_CONFIG_NAME: "cypress.json",
910
DEFAULT_TEST_PATH: ".",
1011
LAMBDA_CONFIG: "./lambdatest-config.json",

commands/utils/default_config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = {
1717
],
1818
run_settings: {
1919
cypress_config_file: "cypress.json",
20+
reporter_config_file: "base_reporter_config.json",
2021
build_name: "build-name",
2122
parallels: 1,
2223
specs: "./*.spec.js",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
"reporterEnabled": "mochawesome",
3+
"mochawesomeReporterOptions": {
4+
"reportDir": "cypress/results/json",
5+
"overwrite": true,
6+
"html": true,
7+
"json": true
8+
}
9+
};

commands/utils/set_args.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ function sync_args_from_cmd(args) {
221221
lt_config["run_settings"]["ignore_files"] =
222222
args["ignore_files"].split(",");
223223
}
224+
225+
// if reporter_config_file parameter, add it in lt config alongwith a warning on console
226+
if (!lt_config["run_settings"]["reporter_config_file"]) {
227+
lt_config["run_settings"]["reporter_config_file"] = constants.LT_BASE_REPORTER_CONFIG_FILE_NAME;
228+
}
229+
230+
224231
if ("cypress_version" in args) {
225232
lt_config["run_settings"]["cypress_version"] = args["cypress_version"];
226233
} else if (lt_config["run_settings"]["cypress_version"]) {

commands/utils/validate.js

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
const fs = require("fs");
2+
const semver = require("semver");
3+
const semverCompare = require('semver/functions/compare');
4+
25
const constants = require("./constants.js");
36
module.exports = validate_config = function (lt_config, validation_configs) {
47
console.log("validating config");
@@ -48,29 +51,9 @@ module.exports = validate_config = function (lt_config, validation_configs) {
4851
reject("Error!! Parallels value not correct");
4952
}
5053

51-
//validate if cypress config file is passed and exists
52-
if (
53-
lt_config["run_settings"]["cypress_config_file"] &&
54-
lt_config["run_settings"]["cypress_config_file"] != ""
55-
) {
56-
if (!fs.existsSync(lt_config["run_settings"]["cypress_config_file"])) {
57-
reject("Error!! Cypress Config File does not exist");
58-
} else {
59-
let rawdata = fs.readFileSync(
60-
lt_config["run_settings"]["cypress_config_file"]
61-
);
62-
try {
63-
let cypress_config = JSON.parse(rawdata);
64-
} catch {
65-
console.log(
66-
"Cypress.json is not parsed, please provide a valid json"
67-
);
68-
reject("Error!! Cypress Config File does not has correct json");
69-
}
70-
}
71-
}
72-
54+
7355
//Validate if package.json is having the cypress dependency
56+
var cypress_version;
7457
if (!fs.existsSync("package.json")) {
7558
reject(
7659
"Error!! Package.json file does not exist in the root on the project"
@@ -89,6 +72,7 @@ module.exports = validate_config = function (lt_config, validation_configs) {
8972
for (const [key, value] of Object.entries(package["dependencies"])) {
9073
if (key == "cypress") {
9174
cypress_flag = true;
75+
cypress_version = value;
9276
break;
9377
}
9478
}
@@ -103,6 +87,7 @@ module.exports = validate_config = function (lt_config, validation_configs) {
10387
console.log(key, value);
10488
if (key == "cypress") {
10589
cypress_flag = true;
90+
cypress_version = value;
10691
break;
10792
}
10893
}
@@ -112,6 +97,7 @@ module.exports = validate_config = function (lt_config, validation_configs) {
11297
lt_config.run_settings.cypress_version != ""
11398
) {
11499
cypress_flag = true;
100+
cypress_version = lt_config.run_settings.cypress_version;
115101
} else if (
116102
lt_config.run_settings.hasOwnProperty("cypress_version") &&
117103
lt_config.run_settings.cypress_version == ""
@@ -133,10 +119,38 @@ module.exports = validate_config = function (lt_config, validation_configs) {
133119
"Package.json is not parsed, please provide a valid json",
134120
e
135121
);
136-
reject("Error!! Package.json File does not has correct json");
122+
reject("Error!! Package.json File does not have correct json");
123+
}
124+
}
125+
126+
//validate if cypress config file is passed and exists
127+
128+
// coerce cypress_version
129+
cypress_version = semver.coerce(cypress_version).version;
130+
// validate cypress.json only in case of cypress<10
131+
if (
132+
semverCompare(cypress_version, "10.0.0") == -1 &&
133+
lt_config["run_settings"]["cypress_config_file"] &&
134+
lt_config["run_settings"]["cypress_config_file"] != ""
135+
) {
136+
if (!fs.existsSync(lt_config["run_settings"]["cypress_config_file"])) {
137+
reject("Error!! Cypress Config File does not exist");
138+
} else {
139+
let rawdata = fs.readFileSync(
140+
lt_config["run_settings"]["cypress_config_file"]
141+
);
142+
try {
143+
let cypress_config = JSON.parse(rawdata);
144+
} catch {
145+
console.log(
146+
"Cypress.json is not parsed, please provide a valid json"
147+
);
148+
reject("Error!! Cypress Config File does not has correct json");
149+
}
137150
}
138151
}
139152

153+
140154
if (
141155
lt_config["run_settings"]["ignore_files"] &&
142156
lt_config["run_settings"]["ignore_files"].length > 0
@@ -209,13 +223,20 @@ module.exports = validate_config = function (lt_config, validation_configs) {
209223
}
210224
}
211225
//validate if reporter json file is passed and exists
226+
// if (
227+
// lt_config["run_settings"]["reporter_config_file"] &&
228+
// lt_config["run_settings"]["reporter_config_file"] == ""
229+
// ){
230+
231+
// }
232+
// console.log("config ", lt_config["run_settings"]["reporter_config_file"]);
212233
if (
213234
lt_config["run_settings"]["reporter_config_file"] &&
214235
lt_config["run_settings"]["reporter_config_file"] != ""
215236
) {
216237
if (!fs.existsSync(lt_config["run_settings"]["reporter_config_file"])) {
217238
reject(
218-
"Error!! Reporter Json File does not exist, either remove the key reporter_config_file or pass a valid path"
239+
"Error!! Reporter Config File does not exist, Pass a valid path"
219240
);
220241
} else {
221242
let rawdata = fs.readFileSync(
@@ -227,14 +248,21 @@ module.exports = validate_config = function (lt_config, validation_configs) {
227248
reject(
228249
"Error!! Reporter JSON File has no keys, either remove Key reporter_config_file from lambdatest config or pass some options"
229250
);
251+
}else if (reporter_config.reporterEnabled && reporter_config.reporterEnabled != ""){
252+
console.log("reporter_config.reporterEnabled ", reporter_config.reporterEnabled);
253+
if (!reporter_config.reporterEnabled.includes("mochawesome")){
254+
reject("Error!! Please add mochawesome in reporterEnabled and add the related config");
255+
}
230256
}
231257
} catch {
232258
console.log(
233-
"reporter_config_file is not parsed, please provide a valid json in Reporter Config"
259+
"reporter_config_file can not parsed, please provide a valid json in Reporter Config"
234260
);
235-
reject("Error!! Reporter JSON File does not has correct json");
261+
reject("Error!! Reporter JSON File does not have correct json");
236262
}
237263
}
264+
}else{
265+
console.log("Warning !! Value of reporter_config_file parameter missing. Proceeding with default reporter config")
238266
}
239267

240268
if (
@@ -336,6 +364,6 @@ module.exports = validate_config = function (lt_config, validation_configs) {
336364
})
337365

338366
}
339-
resolve("Validated the Config");
367+
resolve(cypress_version);
340368
});
341369
};

0 commit comments

Comments
 (0)