Skip to content

Commit 36e216a

Browse files
authored
Merge pull request #284 from roshan04/record_flag_changes
Support for record flag
2 parents a66a004 + c2eb6e9 commit 36e216a

File tree

8 files changed

+283
-4
lines changed

8 files changed

+283
-4
lines changed

bin/commands/runs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ module.exports = function run(args, rawArgs) {
9191
// set the no-wrap
9292
utils.setNoWrap(bsConfig, args);
9393

94+
// set record feature caps
95+
utils.setRecordCaps(bsConfig, args);
96+
9497
//set browsers
9598
await utils.setBrowsers(bsConfig, args);
9699

bin/helpers/capabilityHelper.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,15 @@ const validate = (bsConfig, args) => {
245245
logger.warn(Constants.validationMessages.SPEC_TIMEOUT_NOT_PASSED_ERROR);
246246
}
247247

248+
if(!Utils.isUndefined(bsConfig.run_settings["record"]) && String(bsConfig.run_settings["record"]) == 'true') {
249+
if(Utils.isUndefined(bsConfig.run_settings.projectId) || bsConfig.run_settings.projectId == "" ) {
250+
logger.warn(Constants.validationMessages.PROJECT_ID_MISSING);
251+
}
252+
if (Utils.isUndefined(bsConfig.run_settings["record-key"]) || bsConfig.run_settings["record-key"] == "" ) {
253+
logger.warn(Constants.validationMessages.RECORD_KEY_MISSING);
254+
}
255+
}
256+
248257
resolve(cypressJson);
249258
});
250259
}

bin/helpers/constants.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ const validationMessages = {
102102
HOME_DIRECTORY_NOT_A_DIRECTORY: "Specified home directory is not a directory. The home directory can only be a directory and not a file.",
103103
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.",
104104
SPEC_TIMEOUT_LIMIT_ERROR: "The maximum allowed value of 'spec_timeout' is 120. Read more on https://browserstack.com/docs/automate/cypress/spec-timeout ",
105-
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 "
105+
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 ",
106+
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",
107+
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"
106108
};
107109

108110
const cliMessages = {
@@ -146,7 +148,10 @@ const cliMessages = {
146148
REPORTER: "Specify the custom reporter to use",
147149
REPORTER_OPTIONS: "Specify reporter options for custom reporter",
148150
CYPRESS_GEO_LOCATION: "Enterprise feature to simulate website and mobile behavior from different locations.",
149-
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."
151+
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.",
152+
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.",
153+
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",
154+
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."
150155
},
151156
COMMON: {
152157
DISABLE_USAGE_REPORTING: "Disable usage reporting",

bin/helpers/usageReporting.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const cp = require("child_process"),
88
const config = require('./config'),
99
fileLogger = require('./logger').fileLogger,
1010
utils = require('./utils');
11-
11+
1212
const { AUTH_REGEX, REDACTED_AUTH, REDACTED, CLI_ARGS_REGEX, RAW_ARGS_REGEX } = require("./constants");
1313

1414
function get_version(package_name) {
@@ -172,6 +172,27 @@ function isUsageReportingEnabled() {
172172
return process.env.DISABLE_USAGE_REPORTING;
173173
}
174174

175+
function redactBsConfig(bsConfig) {
176+
if(typeof bsConfig === 'object' && !utils.isUndefined(bsConfig.run_settings)) {
177+
if(!utils.isUndefined(bsConfig.run_settings["projectId"])) { bsConfig.run_settings["projectId"] = REDACTED }
178+
if(!utils.isUndefined(bsConfig.run_settings["record-key"])) { bsConfig.run_settings["record-key"] = REDACTED }
179+
}
180+
}
181+
182+
function redactArgs(args) {
183+
if(typeof args === 'object' && !utils.isUndefined(args.cli_args)) {
184+
if(!utils.isUndefined(args.cli_args["projectId"])) { args.cli_args["projectId"] = REDACTED }
185+
if(!utils.isUndefined(args.cli_args["project-id"])) { args.cli_args["project-id"] = REDACTED }
186+
if(!utils.isUndefined(args.cli_args["record-key"])) { args.cli_args["record-key"] = REDACTED }
187+
if(!utils.isUndefined(args.cli_args["recordKey"])) { args.cli_args["recordKey"] = REDACTED }
188+
}
189+
}
190+
191+
function redactRecordCaps(bsConfig, args) {
192+
redactBsConfig(bsConfig);
193+
redactArgs(args);
194+
}
195+
175196
function redactKeys(str, regex, redact) {
176197
return str.replace(regex, redact);
177198
}
@@ -183,8 +204,10 @@ function send(args) {
183204
let runSettings = "";
184205
let sanitizedbsConfig = "";
185206
let cli_details = cli_version_and_path(bsConfig);
186-
let data = utils.isUndefined(args.data) ? {} : args.data;
187207

208+
redactRecordCaps(bsConfig, args);
209+
210+
let data = utils.isUndefined(args.data) ? {} : args.data;
188211
if (bsConfig && bsConfig.run_settings) {
189212
runSettings = bsConfig.run_settings;
190213
data.cypress_version = bsConfig.run_settings.cypress_version;

bin/helpers/utils.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,41 @@ exports.setSpecTimeout = (bsConfig, args) => {
336336
logger.debug(`Setting spec timeout = ${specTimeout}`);
337337
}
338338

339+
exports.setRecordFlag = (bsConfig, args) => {
340+
if(!this.isUndefined(args["record"])) {
341+
return true;
342+
}
343+
return bsConfig.run_settings["record"];
344+
}
345+
346+
exports.setRecordKeyFlag = (bsConfig, args) => {
347+
if(!this.isUndefined(args["record-key"])) {
348+
return args["record-key"];
349+
} else if (!this.isUndefined(process.env.CYPRESS_RECORD_KEY)) {
350+
return process.env.CYPRESS_RECORD_KEY;
351+
}
352+
return bsConfig.run_settings["record-key"];
353+
}
354+
355+
exports.setProjectId = (bsConfig, args) => {
356+
if(!this.isUndefined(args["projectId"])) {
357+
return args["projectId"];
358+
} else if(!this.isUndefined(process.env.CYPRESS_PROJECT_ID)) {
359+
return process.env.CYPRESS_PROJECT_ID;
360+
} else if(!this.isUndefined(bsConfig.run_settings["projectId"])) {
361+
return bsConfig.run_settings["projectId"];
362+
} else {
363+
let cypressJson = this.getCypressJSON(bsConfig);
364+
if (!this.isUndefined(cypressJson) && !this.isUndefined(cypressJson["projectId"])) { return cypressJson["projectId"]; }
365+
}
366+
}
367+
368+
exports.setRecordCaps = (bsConfig, args) => {
369+
bsConfig.run_settings["record"] = this.setRecordFlag(bsConfig, args);
370+
bsConfig.run_settings["record-key"] = this.setRecordKeyFlag(bsConfig, args);
371+
bsConfig.run_settings["projectId"] = this.setProjectId(bsConfig, args);
372+
}
373+
339374
// specs can be passed from bstack configuration file
340375
// specs can be passed via command line args as a string
341376
// command line args takes precedence over config

bin/runner.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,20 @@ var argv = yargs
252252
describe: Constants.cliMessages.RUN.REPORTER_OPTIONS,
253253
type: "string"
254254
},
255+
'record': {
256+
describe: Constants.cliMessages.RUN.RECORD,
257+
type: "boolean"
258+
},
259+
'record-key': {
260+
default: undefined,
261+
describe: Constants.cliMessages.RUN.RECORD_KEY,
262+
type: "string"
263+
},
264+
'projectId': {
265+
default: undefined,
266+
describe: Constants.cliMessages.RUN.PROJECT_ID,
267+
type: "string"
268+
}
255269
})
256270
.help('help')
257271
.wrap(null)

test/unit/bin/commands/runs.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ describe("runs", () => {
119119
setCLIModeStub = sandbox.stub();
120120
setGeolocationStub = sandbox.stub();
121121
setSpecTimeoutStub = sandbox.stub().returns(undefined);
122+
setRecordCapsStub = sandbox.stub().returns(undefined);
122123
});
123124

124125
afterEach(() => {
@@ -161,6 +162,7 @@ describe("runs", () => {
161162
setCLIMode: setCLIModeStub,
162163
setGeolocation: setGeolocationStub,
163164
setSpecTimeout: setSpecTimeoutStub,
165+
setRecordCaps: setRecordCapsStub,
164166
setDebugMode: setDebugModeStub
165167
},
166168
'../helpers/capabilityHelper': {
@@ -203,6 +205,7 @@ describe("runs", () => {
203205
sinon.assert.calledOnce(setUsageReportingFlagStub);
204206
sinon.assert.calledOnce(setGeolocationStub);
205207
sinon.assert.calledOnce(setSpecTimeoutStub);
208+
sinon.assert.calledOnce(setRecordCapsStub);
206209
sinon.assert.calledOnceWithExactly(
207210
sendUsageReportStub,
208211
bsConfig,
@@ -261,6 +264,7 @@ describe("runs", () => {
261264
setGeolocationStub = sandbox.stub();
262265
getVideoConfigStub = sandbox.stub();
263266
setSpecTimeoutStub = sandbox.stub().returns(undefined);
267+
setRecordCapsStub = sandbox.stub().returns(undefined);
264268
});
265269

266270
afterEach(() => {
@@ -306,6 +310,7 @@ describe("runs", () => {
306310
setGeolocation: setGeolocationStub,
307311
getVideoConfig: getVideoConfigStub,
308312
setSpecTimeout: setSpecTimeoutStub,
313+
setRecordCaps: setRecordCapsStub,
309314
setDebugMode: setDebugModeStub
310315
},
311316
'../helpers/capabilityHelper': {
@@ -367,6 +372,7 @@ describe("runs", () => {
367372
sinon.assert.calledOnce(setSystemEnvsStub);
368373
sinon.assert.calledOnce(setGeolocationStub);
369374
sinon.assert.calledOnce(setSpecTimeoutStub);
375+
sinon.assert.calledOnce(setRecordCapsStub);
370376
sinon.assert.calledOnceWithExactly(
371377
sendUsageReportStub,
372378
bsConfig,
@@ -427,6 +433,7 @@ describe("runs", () => {
427433
setGeolocationStub = sandbox.stub();
428434
getVideoConfigStub = sandbox.stub();
429435
setSpecTimeoutStub = sandbox.stub().returns(undefined);
436+
setRecordCapsStub = sandbox.stub().returns(undefined);
430437
});
431438

432439
afterEach(() => {
@@ -473,6 +480,7 @@ describe("runs", () => {
473480
setGeolocation: setGeolocationStub,
474481
getVideoConfig: getVideoConfigStub,
475482
setSpecTimeout: setSpecTimeoutStub,
483+
setRecordCaps: setRecordCapsStub,
476484
setDebugMode: setDebugModeStub
477485
},
478486
'../helpers/capabilityHelper': {
@@ -536,6 +544,7 @@ describe("runs", () => {
536544
sinon.assert.calledOnce(setSystemEnvsStub);
537545
sinon.assert.calledOnce(setGeolocationStub);
538546
sinon.assert.calledOnce(setSpecTimeoutStub);
547+
sinon.assert.calledOnce(setRecordCapsStub);
539548
sinon.assert.calledOnceWithExactly(
540549
sendUsageReportStub,
541550
bsConfig,
@@ -601,6 +610,7 @@ describe("runs", () => {
601610
setGeolocationStub = sandbox.stub();
602611
getVideoConfigStub = sandbox.stub();
603612
setSpecTimeoutStub = sandbox.stub().returns(undefined);
613+
setRecordCapsStub = sandbox.stub().returns(undefined);
604614
});
605615

606616
afterEach(() => {
@@ -648,6 +658,7 @@ describe("runs", () => {
648658
setGeolocation: setGeolocationStub,
649659
getVideoConfig: getVideoConfigStub,
650660
setSpecTimeout: setSpecTimeoutStub,
661+
setRecordCaps: setRecordCapsStub,
651662
setDebugMode: setDebugModeStub
652663
},
653664
'../helpers/capabilityHelper': {
@@ -722,6 +733,7 @@ describe("runs", () => {
722733
sinon.assert.calledOnce(setSystemEnvsStub);
723734
sinon.assert.calledOnce(setGeolocationStub);
724735
sinon.assert.calledOnce(setSpecTimeoutStub);
736+
sinon.assert.calledOnce(setRecordCapsStub);
725737

726738
sinon.assert.calledOnceWithExactly(
727739
sendUsageReportStub,
@@ -801,6 +813,7 @@ describe("runs", () => {
801813
setGeolocationStub = sandbox.stub();
802814
getVideoConfigStub = sandbox.stub();
803815
setSpecTimeoutStub = sandbox.stub().returns(undefined);
816+
setRecordCapsStub = sandbox.stub().returns(undefined);
804817
});
805818

806819
afterEach(() => {
@@ -856,6 +869,7 @@ describe("runs", () => {
856869
setGeolocation: setGeolocationStub,
857870
getVideoConfig: getVideoConfigStub,
858871
setSpecTimeout: setSpecTimeoutStub,
872+
setRecordCaps: setRecordCapsStub,
859873
setDebugMode: setDebugModeStub
860874
},
861875
'../helpers/capabilityHelper': {
@@ -946,6 +960,7 @@ describe("runs", () => {
946960
sinon.assert.calledOnce(setDefaultsStub);
947961
sinon.assert.calledOnce(setSystemEnvsStub);
948962
sinon.assert.calledOnce(setGeolocationStub);
963+
sinon.assert.calledOnce(setRecordCapsStub);
949964
sinon.assert.match(
950965
sendUsageReportStub.getCall(0).args,
951966
[

0 commit comments

Comments
 (0)