Skip to content

Commit

Permalink
Merge pull request #284 from roshan04/record_flag_changes
Browse files Browse the repository at this point in the history
Support for record flag
  • Loading branch information
francisf authored Apr 27, 2022
2 parents a66a004 + c2eb6e9 commit 36e216a
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 4 deletions.
3 changes: 3 additions & 0 deletions bin/commands/runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ module.exports = function run(args, rawArgs) {
// set the no-wrap
utils.setNoWrap(bsConfig, args);

// set record feature caps
utils.setRecordCaps(bsConfig, args);

//set browsers
await utils.setBrowsers(bsConfig, args);

Expand Down
9 changes: 9 additions & 0 deletions bin/helpers/capabilityHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ const validate = (bsConfig, args) => {
logger.warn(Constants.validationMessages.SPEC_TIMEOUT_NOT_PASSED_ERROR);
}

if(!Utils.isUndefined(bsConfig.run_settings["record"]) && String(bsConfig.run_settings["record"]) == 'true') {
if(Utils.isUndefined(bsConfig.run_settings.projectId) || bsConfig.run_settings.projectId == "" ) {
logger.warn(Constants.validationMessages.PROJECT_ID_MISSING);
}
if (Utils.isUndefined(bsConfig.run_settings["record-key"]) || bsConfig.run_settings["record-key"] == "" ) {
logger.warn(Constants.validationMessages.RECORD_KEY_MISSING);
}
}

resolve(cypressJson);
});
}
Expand Down
9 changes: 7 additions & 2 deletions bin/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ const validationMessages = {
HOME_DIRECTORY_NOT_A_DIRECTORY: "Specified home directory is not a directory. The home directory can only be a directory and not a file.",
CYPRESS_CONFIG_FILE_NOT_PART_OF_HOME_DIRECTORY: "Could not find cypress.json within the specified home directory. Please make sure cypress.json resides within the home directory.",
SPEC_TIMEOUT_LIMIT_ERROR: "The maximum allowed value of 'spec_timeout' is 120. Read more on https://browserstack.com/docs/automate/cypress/spec-timeout ",
SPEC_TIMEOUT_NOT_PASSED_ERROR: "'spec_timeout' key not specified. Going ahead with 30 mins as the default spec timeout. Read more about how to specify the option in https://browserstack.com/docs/automate/cypress/spec-timeout "
SPEC_TIMEOUT_NOT_PASSED_ERROR: "'spec_timeout' key not specified. Going ahead with 30 mins as the default spec timeout. Read more about how to specify the option in https://browserstack.com/docs/automate/cypress/spec-timeout ",
PROJECT_ID_MISSING: "You have specified '--record' flag but you've not provided the 'projectId' in cypress.json or in browserstack.json. Your record functionality on cypress.io dashboard might not work as it needs both the key and the projectId",
RECORD_KEY_MISSING: "You have specified '--record' flag but you've not provided the '--record-key' and we could not find any value in 'CYPRESS_RECORD_KEY' environment variable. Your record functionality on cypress.io dashboard might not work as it needs the key and projectId"
};

const cliMessages = {
Expand Down Expand Up @@ -146,7 +148,10 @@ const cliMessages = {
REPORTER: "Specify the custom reporter to use",
REPORTER_OPTIONS: "Specify reporter options for custom reporter",
CYPRESS_GEO_LOCATION: "Enterprise feature to simulate website and mobile behavior from different locations.",
SPEC_TIMEOUT: "Specify a value for a hard timeout for each spec execution in the 1-120 mins range. Read https://browserstack.com/docs/automate/cypress/spec-timeout for more details."
SPEC_TIMEOUT: "Specify a value for a hard timeout for each spec execution in the 1-120 mins range. Read https://browserstack.com/docs/automate/cypress/spec-timeout for more details.",
RECORD: "Pass the --record flag to record your Cypress runs on Cypress.io dashboard. Note: You also need to specify '--record-key' and '--projectId' arguments either in CLI or in browserstack.json.",
RECORD_KEY: "You can specify the 'key' that is needed to record your runs on Cypress.io dashboard using the '--record-key' argument. Alternatively, you can also pass it on browserstack.json",
PROJECT_ID: "You can pass the 'projectId' of your Cypress.io project where you want to record your runs if specifying the '--record' key. You can also specify this in your cypress.json or in your browserstack.json."
},
COMMON: {
DISABLE_USAGE_REPORTING: "Disable usage reporting",
Expand Down
27 changes: 25 additions & 2 deletions bin/helpers/usageReporting.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const cp = require("child_process"),
const config = require('./config'),
fileLogger = require('./logger').fileLogger,
utils = require('./utils');

const { AUTH_REGEX, REDACTED_AUTH, REDACTED, CLI_ARGS_REGEX, RAW_ARGS_REGEX } = require("./constants");

function get_version(package_name) {
Expand Down Expand Up @@ -172,6 +172,27 @@ function isUsageReportingEnabled() {
return process.env.DISABLE_USAGE_REPORTING;
}

function redactBsConfig(bsConfig) {
if(typeof bsConfig === 'object' && !utils.isUndefined(bsConfig.run_settings)) {
if(!utils.isUndefined(bsConfig.run_settings["projectId"])) { bsConfig.run_settings["projectId"] = REDACTED }
if(!utils.isUndefined(bsConfig.run_settings["record-key"])) { bsConfig.run_settings["record-key"] = REDACTED }
}
}

function redactArgs(args) {
if(typeof args === 'object' && !utils.isUndefined(args.cli_args)) {
if(!utils.isUndefined(args.cli_args["projectId"])) { args.cli_args["projectId"] = REDACTED }
if(!utils.isUndefined(args.cli_args["project-id"])) { args.cli_args["project-id"] = REDACTED }
if(!utils.isUndefined(args.cli_args["record-key"])) { args.cli_args["record-key"] = REDACTED }
if(!utils.isUndefined(args.cli_args["recordKey"])) { args.cli_args["recordKey"] = REDACTED }
}
}

function redactRecordCaps(bsConfig, args) {
redactBsConfig(bsConfig);
redactArgs(args);
}

function redactKeys(str, regex, redact) {
return str.replace(regex, redact);
}
Expand All @@ -183,8 +204,10 @@ function send(args) {
let runSettings = "";
let sanitizedbsConfig = "";
let cli_details = cli_version_and_path(bsConfig);
let data = utils.isUndefined(args.data) ? {} : args.data;

redactRecordCaps(bsConfig, args);

let data = utils.isUndefined(args.data) ? {} : args.data;
if (bsConfig && bsConfig.run_settings) {
runSettings = bsConfig.run_settings;
data.cypress_version = bsConfig.run_settings.cypress_version;
Expand Down
35 changes: 35 additions & 0 deletions bin/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,41 @@ exports.setSpecTimeout = (bsConfig, args) => {
logger.debug(`Setting spec timeout = ${specTimeout}`);
}

exports.setRecordFlag = (bsConfig, args) => {
if(!this.isUndefined(args["record"])) {
return true;
}
return bsConfig.run_settings["record"];
}

exports.setRecordKeyFlag = (bsConfig, args) => {
if(!this.isUndefined(args["record-key"])) {
return args["record-key"];
} else if (!this.isUndefined(process.env.CYPRESS_RECORD_KEY)) {
return process.env.CYPRESS_RECORD_KEY;
}
return bsConfig.run_settings["record-key"];
}

exports.setProjectId = (bsConfig, args) => {
if(!this.isUndefined(args["projectId"])) {
return args["projectId"];
} else if(!this.isUndefined(process.env.CYPRESS_PROJECT_ID)) {
return process.env.CYPRESS_PROJECT_ID;
} else if(!this.isUndefined(bsConfig.run_settings["projectId"])) {
return bsConfig.run_settings["projectId"];
} else {
let cypressJson = this.getCypressJSON(bsConfig);
if (!this.isUndefined(cypressJson) && !this.isUndefined(cypressJson["projectId"])) { return cypressJson["projectId"]; }
}
}

exports.setRecordCaps = (bsConfig, args) => {
bsConfig.run_settings["record"] = this.setRecordFlag(bsConfig, args);
bsConfig.run_settings["record-key"] = this.setRecordKeyFlag(bsConfig, args);
bsConfig.run_settings["projectId"] = this.setProjectId(bsConfig, args);
}

// specs can be passed from bstack configuration file
// specs can be passed via command line args as a string
// command line args takes precedence over config
Expand Down
14 changes: 14 additions & 0 deletions bin/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,20 @@ var argv = yargs
describe: Constants.cliMessages.RUN.REPORTER_OPTIONS,
type: "string"
},
'record': {
describe: Constants.cliMessages.RUN.RECORD,
type: "boolean"
},
'record-key': {
default: undefined,
describe: Constants.cliMessages.RUN.RECORD_KEY,
type: "string"
},
'projectId': {
default: undefined,
describe: Constants.cliMessages.RUN.PROJECT_ID,
type: "string"
}
})
.help('help')
.wrap(null)
Expand Down
15 changes: 15 additions & 0 deletions test/unit/bin/commands/runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ describe("runs", () => {
setCLIModeStub = sandbox.stub();
setGeolocationStub = sandbox.stub();
setSpecTimeoutStub = sandbox.stub().returns(undefined);
setRecordCapsStub = sandbox.stub().returns(undefined);
});

afterEach(() => {
Expand Down Expand Up @@ -161,6 +162,7 @@ describe("runs", () => {
setCLIMode: setCLIModeStub,
setGeolocation: setGeolocationStub,
setSpecTimeout: setSpecTimeoutStub,
setRecordCaps: setRecordCapsStub,
setDebugMode: setDebugModeStub
},
'../helpers/capabilityHelper': {
Expand Down Expand Up @@ -203,6 +205,7 @@ describe("runs", () => {
sinon.assert.calledOnce(setUsageReportingFlagStub);
sinon.assert.calledOnce(setGeolocationStub);
sinon.assert.calledOnce(setSpecTimeoutStub);
sinon.assert.calledOnce(setRecordCapsStub);
sinon.assert.calledOnceWithExactly(
sendUsageReportStub,
bsConfig,
Expand Down Expand Up @@ -261,6 +264,7 @@ describe("runs", () => {
setGeolocationStub = sandbox.stub();
getVideoConfigStub = sandbox.stub();
setSpecTimeoutStub = sandbox.stub().returns(undefined);
setRecordCapsStub = sandbox.stub().returns(undefined);
});

afterEach(() => {
Expand Down Expand Up @@ -306,6 +310,7 @@ describe("runs", () => {
setGeolocation: setGeolocationStub,
getVideoConfig: getVideoConfigStub,
setSpecTimeout: setSpecTimeoutStub,
setRecordCaps: setRecordCapsStub,
setDebugMode: setDebugModeStub
},
'../helpers/capabilityHelper': {
Expand Down Expand Up @@ -367,6 +372,7 @@ describe("runs", () => {
sinon.assert.calledOnce(setSystemEnvsStub);
sinon.assert.calledOnce(setGeolocationStub);
sinon.assert.calledOnce(setSpecTimeoutStub);
sinon.assert.calledOnce(setRecordCapsStub);
sinon.assert.calledOnceWithExactly(
sendUsageReportStub,
bsConfig,
Expand Down Expand Up @@ -427,6 +433,7 @@ describe("runs", () => {
setGeolocationStub = sandbox.stub();
getVideoConfigStub = sandbox.stub();
setSpecTimeoutStub = sandbox.stub().returns(undefined);
setRecordCapsStub = sandbox.stub().returns(undefined);
});

afterEach(() => {
Expand Down Expand Up @@ -473,6 +480,7 @@ describe("runs", () => {
setGeolocation: setGeolocationStub,
getVideoConfig: getVideoConfigStub,
setSpecTimeout: setSpecTimeoutStub,
setRecordCaps: setRecordCapsStub,
setDebugMode: setDebugModeStub
},
'../helpers/capabilityHelper': {
Expand Down Expand Up @@ -536,6 +544,7 @@ describe("runs", () => {
sinon.assert.calledOnce(setSystemEnvsStub);
sinon.assert.calledOnce(setGeolocationStub);
sinon.assert.calledOnce(setSpecTimeoutStub);
sinon.assert.calledOnce(setRecordCapsStub);
sinon.assert.calledOnceWithExactly(
sendUsageReportStub,
bsConfig,
Expand Down Expand Up @@ -601,6 +610,7 @@ describe("runs", () => {
setGeolocationStub = sandbox.stub();
getVideoConfigStub = sandbox.stub();
setSpecTimeoutStub = sandbox.stub().returns(undefined);
setRecordCapsStub = sandbox.stub().returns(undefined);
});

afterEach(() => {
Expand Down Expand Up @@ -648,6 +658,7 @@ describe("runs", () => {
setGeolocation: setGeolocationStub,
getVideoConfig: getVideoConfigStub,
setSpecTimeout: setSpecTimeoutStub,
setRecordCaps: setRecordCapsStub,
setDebugMode: setDebugModeStub
},
'../helpers/capabilityHelper': {
Expand Down Expand Up @@ -722,6 +733,7 @@ describe("runs", () => {
sinon.assert.calledOnce(setSystemEnvsStub);
sinon.assert.calledOnce(setGeolocationStub);
sinon.assert.calledOnce(setSpecTimeoutStub);
sinon.assert.calledOnce(setRecordCapsStub);

sinon.assert.calledOnceWithExactly(
sendUsageReportStub,
Expand Down Expand Up @@ -801,6 +813,7 @@ describe("runs", () => {
setGeolocationStub = sandbox.stub();
getVideoConfigStub = sandbox.stub();
setSpecTimeoutStub = sandbox.stub().returns(undefined);
setRecordCapsStub = sandbox.stub().returns(undefined);
});

afterEach(() => {
Expand Down Expand Up @@ -856,6 +869,7 @@ describe("runs", () => {
setGeolocation: setGeolocationStub,
getVideoConfig: getVideoConfigStub,
setSpecTimeout: setSpecTimeoutStub,
setRecordCaps: setRecordCapsStub,
setDebugMode: setDebugModeStub
},
'../helpers/capabilityHelper': {
Expand Down Expand Up @@ -946,6 +960,7 @@ describe("runs", () => {
sinon.assert.calledOnce(setDefaultsStub);
sinon.assert.calledOnce(setSystemEnvsStub);
sinon.assert.calledOnce(setGeolocationStub);
sinon.assert.calledOnce(setRecordCapsStub);
sinon.assert.match(
sendUsageReportStub.getCall(0).args,
[
Expand Down
Loading

0 comments on commit 36e216a

Please sign in to comment.