Skip to content

Commit 3d82316

Browse files
authored
Merge pull request #177 from asad9711/MLE-10219
Cypress 10 support
2 parents bf131c7 + 03ef34a commit 3d82316

File tree

11 files changed

+1564
-66
lines changed

11 files changed

+1564
-66
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const addContext = require('mochawesome/addContext')
2+
3+
// NOTE: import this file in cypress/support/e2e.js
4+
Cypress.on('test:after:run', (test, runnable) => {
5+
if (test.state === 'failed') {
6+
let item = runnable
7+
const nameParts = [runnable.title]
8+
9+
// Iterate through all parents and grab the titles
10+
while (item.parent) {
11+
nameParts.unshift(item.parent.title)
12+
item = item.parent
13+
}
14+
15+
if(runnable.hookName) {
16+
nameParts.push(`${runnable.hookName} hook`)
17+
}
18+
const fullTestName = nameParts
19+
.filter(Boolean)
20+
.join(' -- ') // this is how cypress joins the test title fragments
21+
22+
const imageUrl = `${fullTestName} (failed).png`
23+
24+
addContext({ test }, imageUrl)
25+
26+
27+
}
28+
})

commands/init.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
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

@@ -42,7 +43,29 @@ function create_ltconfig_file(args) {
4243
}
4344
};
4445

45-
module.exports = function (args) {
46-
create_ltconfig_file(args)
46+
function create_base_reporter_config_file(args) {
47+
let config = require('./utils/default_reporter_config.js')
48+
let content = JSON.stringify(config, null, 3);
49+
if (args._.length == 1) {
50+
create_file(constants.LT_BASE_REPORTER_CONFIG_FILE_NAME, content)
51+
}
52+
};
4753

54+
function create_custom_support_file(args){
55+
const pathToFile = path.join(__dirname, "default_custom_support_file.js");
56+
const pathToNewDestination = constants.LT_BASE_CUSTOM_SUPPORT_FILE_NAME;
57+
fs.copyFile(pathToFile, pathToNewDestination, (err) => {
58+
if (err) {
59+
console.log("Error while copying custom support file", err);
60+
}
61+
else {
62+
console.log("Successfully saved custom support file at - ", pathToNewDestination);
63+
}
64+
});
65+
}
66+
67+
module.exports = function (args) {
68+
create_ltconfig_file(args);
69+
create_base_reporter_config_file(args);
70+
create_custom_support_file(args);
4871
};

commands/run.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,22 @@ 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+
/*
73+
update ltconfig to contain the cypress_version
74+
case 1: user passed cypress_version in run_settings, this case will work as earlier
75+
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
76+
*/
77+
78+
79+
/* TEST scenarios:
80+
- user passes cypress_version in run_settings with both cases- with semver/without semver
81+
- user doesnot pass cypress_version in run_settings
82+
*/
83+
84+
if (!("cypress_version" in lt_config.run_settings)){
85+
lt_config.run_settings.cypress_version = cypressVersion;
86+
}
7287
batcher
7388
.make_batches(lt_config)
7489
.then(function (batches) {

commands/utils/batch/batch_runner.js

Lines changed: 2 additions & 2 deletions
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) {
8282
totalBatches = batches.length;
8383
//console.log("Total number of batches " + totalBatches);
8484
return new Promise(function (resolve, reject) {
@@ -115,7 +115,7 @@ async function run(lt_config, batches, env, i = 0) {
115115
},
116116
username: lt_config["lambdatest_auth"]["username"],
117117
access_key: lt_config["lambdatest_auth"]["access_key"],
118-
type: "cypress",
118+
type: "cypress"
119119
});
120120

121121
run_test(

commands/utils/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ 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",
9+
LT_BASE_CUSTOM_SUPPORT_FILE_NAME: "custom_support_file.js",
810
CYPRESS_CONFIG_NAME: "cypress.json",
911
DEFAULT_TEST_PATH: ".",
1012
LAMBDA_CONFIG: "./lambdatest-config.json",

commands/utils/default_config.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@ module.exports = {
1616
},
1717
],
1818
run_settings: {
19-
cypress_config_file: "cypress.json",
19+
cypress_config_file: "cypress.config.js",
20+
reporter_config_file: "base_reporter_config.json",
2021
build_name: "build-name",
2122
parallels: 1,
2223
specs: "./*.spec.js",
2324
ignore_files: "",
24-
feature_file_suppport: false,
2525
network: false,
2626
headless: false,
27-
reporter_config_file: "",
2827
npm_dependencies: {
29-
cypress: "9.0.0",
28+
cypress: "10.0.0",
3029
},
3130
},
3231
tunnel_settings: {
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/mochawesome",
5+
"overwrite": true,
6+
"html": true,
7+
"json": true
8+
}
9+
};

commands/utils/set_args.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ 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+
console.log("Warning !! Value of reporter_config_file parameter missing. Proceeding with default reporter config");
228+
lt_config["run_settings"]["reporter_config_file"] = constants.LT_BASE_REPORTER_CONFIG_FILE_NAME;
229+
}
230+
231+
224232
if ("cypress_version" in args) {
225233
lt_config["run_settings"]["cypress_version"] = args["cypress_version"];
226234
} else if (lt_config["run_settings"]["cypress_version"]) {

commands/utils/validate.js

Lines changed: 46 additions & 29 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
}
@@ -100,9 +84,9 @@ module.exports = validate_config = function (lt_config, validation_configs) {
10084
for (const [key, value] of Object.entries(
10185
package["devDependencies"]
10286
)) {
103-
console.log(key, value);
10487
if (key == "cypress") {
10588
cypress_flag = true;
89+
cypress_version = value;
10690
break;
10791
}
10892
}
@@ -112,6 +96,7 @@ module.exports = validate_config = function (lt_config, validation_configs) {
11296
lt_config.run_settings.cypress_version != ""
11397
) {
11498
cypress_flag = true;
99+
cypress_version = lt_config.run_settings.cypress_version;
115100
} else if (
116101
lt_config.run_settings.hasOwnProperty("cypress_version") &&
117102
lt_config.run_settings.cypress_version == ""
@@ -133,10 +118,37 @@ module.exports = validate_config = function (lt_config, validation_configs) {
133118
"Package.json is not parsed, please provide a valid json",
134119
e
135120
);
136-
reject("Error!! Package.json File does not has correct json");
121+
reject("Error!! Package.json File does not have correct json");
137122
}
138123
}
139124

125+
//validate if cypress config file is passed and exists
126+
127+
cypress_version = semver.coerce(cypress_version).version;
128+
// validate cypress.json only in case of cypress<10
129+
if (
130+
semverCompare(cypress_version, "10.0.0") == -1 &&
131+
lt_config["run_settings"]["cypress_config_file"] &&
132+
lt_config["run_settings"]["cypress_config_file"] != ""
133+
) {
134+
if (!fs.existsSync(lt_config["run_settings"]["cypress_config_file"])) {
135+
reject("Error!! Cypress Config File does not exist");
136+
} else {
137+
let rawdata = fs.readFileSync(
138+
lt_config["run_settings"]["cypress_config_file"]
139+
);
140+
try {
141+
let cypress_config = JSON.parse(rawdata);
142+
} catch {
143+
console.log(
144+
"Cypress.json is not parsed, please provide a valid json"
145+
);
146+
reject("Error!! Cypress Config File does not has correct json");
147+
}
148+
}
149+
}
150+
151+
140152
if (
141153
lt_config["run_settings"]["ignore_files"] &&
142154
lt_config["run_settings"]["ignore_files"].length > 0
@@ -208,14 +220,13 @@ module.exports = validate_config = function (lt_config, validation_configs) {
208220
reject("Smart UI porject name can not be blank");
209221
}
210222
}
211-
//validate if reporter json file is passed and exists
212223
if (
213224
lt_config["run_settings"]["reporter_config_file"] &&
214225
lt_config["run_settings"]["reporter_config_file"] != ""
215226
) {
216227
if (!fs.existsSync(lt_config["run_settings"]["reporter_config_file"])) {
217228
reject(
218-
"Error!! Reporter Json File does not exist, either remove the key reporter_config_file or pass a valid path"
229+
"Error!! Reporter Config File does not exist, Pass a valid path"
219230
);
220231
} else {
221232
let rawdata = fs.readFileSync(
@@ -227,14 +238,20 @@ module.exports = validate_config = function (lt_config, validation_configs) {
227238
reject(
228239
"Error!! Reporter JSON File has no keys, either remove Key reporter_config_file from lambdatest config or pass some options"
229240
);
241+
}else if (reporter_config.reporterEnabled && reporter_config.reporterEnabled != ""){
242+
if (!reporter_config.reporterEnabled.includes("mochawesome")){
243+
console.log("Warning!! mochawesome reporter config not present. Command log may not be visible on dashboard");
244+
}
230245
}
231246
} catch {
232247
console.log(
233-
"reporter_config_file is not parsed, please provide a valid json in Reporter Config"
248+
"reporter_config_file could not be parsed, please provide a valid json in Reporter Config"
234249
);
235-
reject("Error!! Reporter JSON File does not has correct json");
250+
reject("Error!! Reporter JSON File does not have correct json");
236251
}
237252
}
253+
}else{
254+
console.log("Warning !! Value of reporter_config_file parameter missing. Proceeding with default reporter config")
238255
}
239256

240257
if (
@@ -336,6 +353,6 @@ module.exports = validate_config = function (lt_config, validation_configs) {
336353
})
337354

338355
}
339-
resolve("Validated the Config");
356+
resolve(cypress_version);
340357
});
341358
};

0 commit comments

Comments
 (0)