Skip to content

Cypress 10 #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions commands/default_custom_support_file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const addContext = require('mochawesome/addContext')

// NOTE: import this file in cypress/support/e2e.js
Cypress.on('test:after:run', (test, runnable) => {
if (test.state === 'failed') {
let item = runnable
const nameParts = [runnable.title]

// Iterate through all parents and grab the titles
while (item.parent) {
nameParts.unshift(item.parent.title)
item = item.parent
}

if(runnable.hookName) {
nameParts.push(`${runnable.hookName} hook`)
}
const fullTestName = nameParts
.filter(Boolean)
.join(' -- ') // this is how cypress joins the test title fragments

const imageUrl = `${fullTestName} (failed).png`

addContext({ test }, imageUrl)


}
})
31 changes: 27 additions & 4 deletions commands/init.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
var fs = require('fs');
var path = require('path')
var path = require('path');
const { exit } = require('process');
const { config } = require('yargs');
const constants = require('./utils/constants.js')

function create_file(file_path, content) {
fs.writeFile(file_path, content, function (err) {
if (err) throw err;
console.log('Saved at ', file_path);
console.log('Saved at ',file_path);
});
}

Expand Down Expand Up @@ -42,7 +43,29 @@ function create_ltconfig_file(args) {
}
};

module.exports = function (args) {
create_ltconfig_file(args)
function create_base_reporter_config_file(args) {
let config = require('./utils/default_reporter_config.js')
let content = JSON.stringify(config, null, 3);
if (args._.length == 1) {
create_file(constants.LT_BASE_REPORTER_CONFIG_FILE_NAME, content)
}
};

function create_custom_support_file(args){
const pathToFile = path.join(__dirname, "default_custom_support_file.js");
const pathToNewDestination = constants.LT_BASE_CUSTOM_SUPPORT_FILE_NAME;
fs.copyFile(pathToFile, pathToNewDestination, (err) => {
if (err) {
console.log("Error while copying custom support file", err);
}
else {
console.log("Successfully saved custom support file at - ", pathToNewDestination);
}
});
}

module.exports = function (args) {
create_ltconfig_file(args);
create_base_reporter_config_file(args);
create_custom_support_file(args);
};
17 changes: 16 additions & 1 deletion commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,22 @@ module.exports = function (args) {
.then(function (lt_config) {
//validate the config options
validate(lt_config, resp)
.then(function () {
.then(function (cypressVersion) {
/*
update ltconfig to contain the cypress_version
case 1: user passed cypress_version in run_settings, this case will work as earlier
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
*/


/* TEST scenarios:
- user passes cypress_version in run_settings with both cases- with semver/without semver
- user doesnot pass cypress_version in run_settings
*/

if (!("cypress_version" in lt_config.run_settings)){
lt_config.run_settings.cypress_version = cypressVersion;
}
batcher
.make_batches(lt_config)
.then(function (batches) {
Expand Down
4 changes: 2 additions & 2 deletions commands/utils/batch/batch_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function run_test(payload, env = "prod", rejectUnauthorized) {
});
}

async function run(lt_config, batches, env, i = 0) {
async function run(lt_config, batches, env) {
totalBatches = batches.length;
//console.log("Total number of batches " + totalBatches);
return new Promise(function (resolve, reject) {
Expand Down Expand Up @@ -115,7 +115,7 @@ async function run(lt_config, batches, env, i = 0) {
},
username: lt_config["lambdatest_auth"]["username"],
access_key: lt_config["lambdatest_auth"]["access_key"],
type: "cypress",
type: "cypress"
});

run_test(
Expand Down
2 changes: 2 additions & 0 deletions commands/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module.exports = {
PROJECT_UPLOAD_URL: "/url",
CLI: "/cy/versions",
LT_CONFIG_NAME: "lambdatest-config.json",
LT_BASE_REPORTER_CONFIG_FILE_NAME: "base_reporter_config.json",
LT_BASE_CUSTOM_SUPPORT_FILE_NAME: "custom_support_file.js",
CYPRESS_CONFIG_NAME: "cypress.json",
DEFAULT_TEST_PATH: ".",
LAMBDA_CONFIG: "./lambdatest-config.json",
Expand Down
7 changes: 3 additions & 4 deletions commands/utils/default_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@ module.exports = {
},
],
run_settings: {
cypress_config_file: "cypress.json",
cypress_config_file: "cypress.config.js",
reporter_config_file: "base_reporter_config.json",
build_name: "build-name",
parallels: 1,
specs: "./*.spec.js",
ignore_files: "",
feature_file_suppport: false,
network: false,
headless: false,
reporter_config_file: "",
npm_dependencies: {
cypress: "9.0.0",
cypress: "10.0.0",
},
},
tunnel_settings: {
Expand Down
9 changes: 9 additions & 0 deletions commands/utils/default_reporter_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
"reporterEnabled": "mochawesome",
"mochawesomeReporterOptions": {
"reportDir": "cypress/results/mochawesome",
"overwrite": true,
"html": true,
"json": true
}
};
8 changes: 8 additions & 0 deletions commands/utils/set_args.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ function sync_args_from_cmd(args) {
lt_config["run_settings"]["ignore_files"] =
args["ignore_files"].split(",");
}

// if reporter_config_file parameter, add it in lt config alongwith a warning on console
if (!lt_config["run_settings"]["reporter_config_file"]) {
console.log("Warning !! Value of reporter_config_file parameter missing. Proceeding with default reporter config");
lt_config["run_settings"]["reporter_config_file"] = constants.LT_BASE_REPORTER_CONFIG_FILE_NAME;
}


if ("cypress_version" in args) {
lt_config["run_settings"]["cypress_version"] = args["cypress_version"];
} else if (lt_config["run_settings"]["cypress_version"]) {
Expand Down
75 changes: 46 additions & 29 deletions commands/utils/validate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const fs = require("fs");
const semver = require("semver");
const semverCompare = require('semver/functions/compare');

const constants = require("./constants.js");
module.exports = validate_config = function (lt_config, validation_configs) {
console.log("validating config");
Expand Down Expand Up @@ -48,29 +51,9 @@ module.exports = validate_config = function (lt_config, validation_configs) {
reject("Error!! Parallels value not correct");
}

//validate if cypress config file is passed and exists
if (
lt_config["run_settings"]["cypress_config_file"] &&
lt_config["run_settings"]["cypress_config_file"] != ""
) {
if (!fs.existsSync(lt_config["run_settings"]["cypress_config_file"])) {
reject("Error!! Cypress Config File does not exist");
} else {
let rawdata = fs.readFileSync(
lt_config["run_settings"]["cypress_config_file"]
);
try {
let cypress_config = JSON.parse(rawdata);
} catch {
console.log(
"Cypress.json is not parsed, please provide a valid json"
);
reject("Error!! Cypress Config File does not has correct json");
}
}
}


//Validate if package.json is having the cypress dependency
var cypress_version;
if (!fs.existsSync("package.json")) {
reject(
"Error!! Package.json file does not exist in the root on the project"
Expand All @@ -89,6 +72,7 @@ module.exports = validate_config = function (lt_config, validation_configs) {
for (const [key, value] of Object.entries(package["dependencies"])) {
if (key == "cypress") {
cypress_flag = true;
cypress_version = value;
break;
}
}
Expand All @@ -100,9 +84,9 @@ module.exports = validate_config = function (lt_config, validation_configs) {
for (const [key, value] of Object.entries(
package["devDependencies"]
)) {
console.log(key, value);
if (key == "cypress") {
cypress_flag = true;
cypress_version = value;
break;
}
}
Expand All @@ -112,6 +96,7 @@ module.exports = validate_config = function (lt_config, validation_configs) {
lt_config.run_settings.cypress_version != ""
) {
cypress_flag = true;
cypress_version = lt_config.run_settings.cypress_version;
} else if (
lt_config.run_settings.hasOwnProperty("cypress_version") &&
lt_config.run_settings.cypress_version == ""
Expand All @@ -133,10 +118,37 @@ module.exports = validate_config = function (lt_config, validation_configs) {
"Package.json is not parsed, please provide a valid json",
e
);
reject("Error!! Package.json File does not has correct json");
reject("Error!! Package.json File does not have correct json");
}
}

//validate if cypress config file is passed and exists

cypress_version = semver.coerce(cypress_version).version;
// validate cypress.json only in case of cypress<10
if (
semverCompare(cypress_version, "10.0.0") == -1 &&
lt_config["run_settings"]["cypress_config_file"] &&
lt_config["run_settings"]["cypress_config_file"] != ""
) {
if (!fs.existsSync(lt_config["run_settings"]["cypress_config_file"])) {
reject("Error!! Cypress Config File does not exist");
} else {
let rawdata = fs.readFileSync(
lt_config["run_settings"]["cypress_config_file"]
);
try {
let cypress_config = JSON.parse(rawdata);
} catch {
console.log(
"Cypress.json is not parsed, please provide a valid json"
);
reject("Error!! Cypress Config File does not has correct json");
}
}
}


if (
lt_config["run_settings"]["ignore_files"] &&
lt_config["run_settings"]["ignore_files"].length > 0
Expand Down Expand Up @@ -208,14 +220,13 @@ module.exports = validate_config = function (lt_config, validation_configs) {
reject("Smart UI porject name can not be blank");
}
}
//validate if reporter json file is passed and exists
if (
lt_config["run_settings"]["reporter_config_file"] &&
lt_config["run_settings"]["reporter_config_file"] != ""
) {
if (!fs.existsSync(lt_config["run_settings"]["reporter_config_file"])) {
reject(
"Error!! Reporter Json File does not exist, either remove the key reporter_config_file or pass a valid path"
"Error!! Reporter Config File does not exist, Pass a valid path"
);
} else {
let rawdata = fs.readFileSync(
Expand All @@ -227,14 +238,20 @@ module.exports = validate_config = function (lt_config, validation_configs) {
reject(
"Error!! Reporter JSON File has no keys, either remove Key reporter_config_file from lambdatest config or pass some options"
);
}else if (reporter_config.reporterEnabled && reporter_config.reporterEnabled != ""){
if (!reporter_config.reporterEnabled.includes("mochawesome")){
console.log("Warning!! mochawesome reporter config not present. Command log may not be visible on dashboard");
}
}
} catch {
console.log(
"reporter_config_file is not parsed, please provide a valid json in Reporter Config"
"reporter_config_file could not be parsed, please provide a valid json in Reporter Config"
);
reject("Error!! Reporter JSON File does not has correct json");
reject("Error!! Reporter JSON File does not have correct json");
}
}
}else{
console.log("Warning !! Value of reporter_config_file parameter missing. Proceeding with default reporter config")
}

if (
Expand Down Expand Up @@ -336,6 +353,6 @@ module.exports = validate_config = function (lt_config, validation_configs) {
})

}
resolve("Validated the Config");
resolve(cypress_version);
});
};
Loading