Skip to content

Commit 2c4d3d4

Browse files
committed
Merge branch 'master' of github.com:roshan04/browserstack-cypress-cli into Handle_spec_level_timeouts
2 parents 14abaa9 + 2430b01 commit 2c4d3d4

File tree

9 files changed

+132
-158
lines changed

9 files changed

+132
-158
lines changed

bin/commands/runs.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ module.exports = function run(args, rawArgs) {
207207
}).finally(() =>{
208208
let terminalWidth = (process.stdout.columns) * 0.9;
209209
let lineSeparator = "\n" + "-".repeat(terminalWidth);
210-
console.log(lineSeparator)
211210
logger.info(Constants.userMessages.BUILD_FAILED_ERROR)
212211
process.exitCode = Constants.BUILD_FAILED_EXIT_CODE;
213212
});
@@ -261,7 +260,7 @@ module.exports = function run(args, rawArgs) {
261260
utils.sendUsageReport(bsConfig, args, `${message}\n${dashboardLink}`, Constants.messageTypes.SUCCESS, null, buildReportData, rawArgs);
262261
return;
263262
}).catch(async function (err) {
264-
if (err && err.includes('browserstack.geoLocation')) {
263+
if (err && err.toString().includes('browserstack.geoLocation')) {
265264
err = err.replace(/browserstack.geoLocation/g, 'geolocation');
266265
}
267266
// Build creation failed

bin/helpers/capabilityHelper.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,31 @@ const caps = (bsConfig, zip) => {
7171
obj.local = true;
7272
}
7373

74-
obj.localMode = null;
75-
// Local Mode
76-
if (obj.local === true && bsConfig.connection_settings.local_mode) {
77-
obj.localMode = bsConfig.connection_settings.local_mode;
78-
if (bsConfig.connection_settings.user_defined_local_mode_warning) {
79-
logger.warn(Constants.userMessages.INVALID_LOCAL_MODE_WARNING);
74+
// binary was spawned locally
75+
if(obj.local === true) {
76+
if (!Utils.isUndefined(process.env.BSTACK_CYPRESS_LOCAL_BINARY_RUNNING) && process.env.BSTACK_CYPRESS_LOCAL_BINARY_RUNNING == "true") {
77+
obj.localMode = null;
78+
79+
// Local Mode
80+
if (bsConfig.connection_settings.local_mode) {
81+
obj.localMode = bsConfig.connection_settings.local_mode;
82+
if (bsConfig.connection_settings.user_defined_local_mode_warning) {
83+
logger.warn(Constants.userMessages.INVALID_LOCAL_MODE_WARNING);
84+
}
85+
logger.info(`Local testing set up in ${obj.localMode} mode.`);
86+
}
87+
88+
// Local Identifier
89+
obj.localIdentifier = null;
90+
if (bsConfig.connection_settings.localIdentifier || bsConfig.connection_settings.local_identifier) {
91+
obj.localIdentifier = bsConfig.connection_settings.localIdentifier || bsConfig.connection_settings.local_identifier;
92+
logger.info(`Local testing identifier: ${obj.localIdentifier}`);
93+
}
8094
}
81-
logger.info(`Local testing set up in ${obj.localMode} mode.`);
8295
}
8396

84-
// Local Identifier
85-
obj.localIdentifier = null;
86-
if (obj.local === true && (bsConfig.connection_settings.localIdentifier || bsConfig.connection_settings.local_identifier)) {
87-
obj.localIdentifier = bsConfig.connection_settings.localIdentifier || bsConfig.connection_settings.local_identifier;
88-
logger.info(`Local testing identifier: ${obj.localIdentifier}`);
97+
if (!Utils.isUndefined(process.env.BSTACK_CYPRESS_LOCAL_BINARY_ALREADY_RUNNING) && process.env.BSTACK_CYPRESS_LOCAL_BINARY_ALREADY_RUNNING == "true") {
98+
logger.info(Constants.userMessages.LOCAL_BINARY_ALREADY_RUNNING);
8999
}
90100

91101
logger.info(`Local is set to: ${obj.local} (${obj.local ? Constants.userMessages.LOCAL_TRUE : Constants.userMessages.LOCAL_FALSE})`);
@@ -164,8 +174,6 @@ const validate = (bsConfig, args) => {
164174
// validate local args i.e --local-mode and --local-identifier
165175

166176
if( Utils.searchForOption('--local-identifier') && (Utils.isUndefined(args.localIdentifier) || (!Utils.isUndefined(args.localIdentifier) && !args.localIdentifier.trim()))) reject(Constants.validationMessages.INVALID_CLI_LOCAL_IDENTIFIER);
167-
168-
if( Utils.getLocalFlag(bsConfig.connection_settings) && (Utils.isUndefined(bsConfig["connection_settings"]["local_identifier"]) || ( !Utils.isUndefined(bsConfig["connection_settings"]["local_identifier"]) && !bsConfig["connection_settings"]["local_identifier"].trim()))) reject(Constants.validationMessages.INVALID_LOCAL_IDENTIFIER);
169177

170178
if( Utils.searchForOption('--local-mode') && ( Utils.isUndefined(args.localMode) || (!Utils.isUndefined(args.localMode) && !["always-on","on-demand"].includes(args.localMode)))) reject(Constants.validationMessages.INVALID_LOCAL_MODE);
171179

bin/helpers/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const userMessages = {
5656
LOCAL_START_FAILED: "Local Testing setup failed.",
5757
LOCAL_STOP_FAILED: "Local Binary stop failed.",
5858
INVALID_LOCAL_MODE_WARNING: "Invalid value specified for local_mode. local_mode: (\"always-on\" | \"on-demand\"). For more info, check out https://www.browserstack.com/docs/automate/cypress/cli-reference",
59+
LOCAL_BINARY_ALREADY_RUNNING: "We found an existing BrowserStack Local connection running from your account. Using the existing connection for this build. If you wish to use a new Local connection for your build, please specify a value for 'local_identifier' within 'connection_settings' in your browserstack.json config.",
5960
SPEC_LIMIT_WARNING: "You might not see all your results on the dashboard because of high spec count, please consider reducing the number of spec files in this folder.",
6061
DOWNLOAD_BUILD_ARTIFACTS_FAILED: "Downloading build artifacts for the build <build-id> failed for <machine-count> machines.",
6162
ASYNC_DOWNLOADS: "Test artifacts as specified under 'downloads' can be downloaded after the build has completed its run, using 'browserstack-cypress generate-downloads <build-id>'",

bin/helpers/utils.js

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -626,11 +626,6 @@ exports.setLocalIdentifier = (bsConfig, args) => {
626626
!this.isUndefined(bsConfig["connection_settings"]["local_identifier"])
627627
){
628628
bsConfig['connection_settings']['local_mode'] = 'always-on';
629-
} else if (
630-
bsConfig['connection_settings']['local'] &&
631-
this.isUndefined(bsConfig["connection_settings"]["local_identifier"])
632-
){
633-
bsConfig["connection_settings"]["local_identifier"] = this.generateLocalIdentifier(bsConfig['connection_settings']['local_mode']);
634629
}
635630
};
636631

@@ -670,9 +665,18 @@ exports.setLocalMode = (bsConfig, args) => {
670665
exports.setupLocalTesting = (bsConfig, args, rawArgs) => {
671666
return new Promise(async (resolve, reject) => {
672667
if( bsConfig['connection_settings'] && bsConfig['connection_settings']['local'] && String(bsConfig['connection_settings']['local']) === "true" ){
673-
let localIdentifierRunning = await this.checkLocalIdentifierRunning(
674-
bsConfig, bsConfig['connection_settings']['local_identifier']
675-
);
668+
let localBinaryRunning = await this.checkLocalBinaryRunning(bsConfig, bsConfig['connection_settings']['local_identifier']);
669+
let localIdentifierRunning;
670+
if (localBinaryRunning['should_spawn_binary'] == true) {
671+
localIdentifierRunning = false;
672+
if(this.isUndefined(bsConfig["connection_settings"]["local_identifier"])) {
673+
bsConfig["connection_settings"]["local_identifier"] = this.generateLocalIdentifier(bsConfig['connection_settings']['local_mode']);
674+
}
675+
} else {
676+
localIdentifierRunning = true;
677+
process.env.BSTACK_CYPRESS_LOCAL_BINARY_ALREADY_RUNNING = "true"; // shows message when binary is not spawned by the CLI process
678+
process.env.BSTACK_CYPRESS_LOCAL_BINARY_RUNNING = "true";
679+
}
676680
if (!localIdentifierRunning){
677681
bsConfig.connection_settings.usedAutoLocal = true;
678682
var bs_local = this.getLocalBinary();
@@ -681,6 +685,7 @@ exports.setupLocalTesting = (bsConfig, args, rawArgs) => {
681685
logger.info('Setting up Local testing...');
682686
bs_local.start(bs_local_args, function (localStartError) {
683687
if (that.isUndefined(localStartError)) {
688+
process.env.BSTACK_CYPRESS_LOCAL_BINARY_RUNNING = "true";
684689
resolve(bs_local);
685690
} else {
686691
let message = `name: ${localStartError.name}, message: ${localStartError.message}, extra: ${localStartError.extra}`,
@@ -708,22 +713,6 @@ exports.setupLocalTesting = (bsConfig, args, rawArgs) => {
708713

709714
exports.stopLocalBinary = (bsConfig, bs_local, args, rawArgs) => {
710715
return new Promise(async (resolve, reject) => {
711-
if(bsConfig['connection_settings'] && bsConfig['connection_settings']['local']){
712-
let localIdentifierRunning = await this.checkLocalIdentifierRunning(bsConfig,bsConfig["connection_settings"]["local_identifier"]);
713-
if(!localIdentifierRunning){
714-
let message = `Local Binary not running.`,
715-
errorCode = 'local_identifier_error';
716-
this.sendUsageReport(
717-
bsConfig,
718-
args,
719-
message,
720-
Constants.messageTypes.ERROR,
721-
errorCode,
722-
null,
723-
rawArgs
724-
);
725-
}
726-
}
727716
if (!this.isUndefined(bs_local) && bs_local.isRunning() && bsConfig['connection_settings'] && bsConfig['connection_settings']['local_mode'].toLowerCase() != "always-on") {
728717
let that = this;
729718
bs_local.stop(function (localStopError) {
@@ -777,35 +766,26 @@ exports.generateLocalIdentifier = (mode) => {
777766
return Buffer.from(local_identifier).toString("base64");
778767
};
779768

780-
exports.checkLocalIdentifierRunning = (bsConfig, localIdentifier) => {
769+
exports.checkLocalBinaryRunning = (bsConfig, localIdentifier) => {
781770
let options = {
782-
url: `${config.localTestingListUrl}?auth_token=${bsConfig.auth.access_key}&state=running`,
771+
url: `${config.cypress_v1}/local_binary_running_check`,
783772
auth: {
784773
user: bsConfig.auth.username,
785774
password: bsConfig.auth.access_key,
786775
},
787776
headers: {
777+
'Content-Type': 'application/json',
788778
'User-Agent': this.getUserAgent(),
789779
},
780+
body: JSON.stringify({ localIdentifier: localIdentifier}),
790781
};
791-
let that = this;
792782
return new Promise ( function(resolve, reject) {
793-
request.get(options, function (err, resp, body) {
783+
request.post(options, function (err, resp, body) {
794784
if(err){
795785
reject(err);
796786
}
797787
let response = JSON.parse(body);
798-
let localInstances = [];
799-
if(!that.isUndefined(response['instances'])){
800-
localInstances = response['instances'];
801-
}
802-
let localIdentifiers = [];
803-
804-
localInstances.forEach(function(instance){
805-
localIdentifiers.push(instance['localIdentifier']);
806-
});
807-
808-
resolve(localIdentifiers.includes(localIdentifier));
788+
resolve(response);
809789
});
810790
});
811791
};
@@ -1080,7 +1060,6 @@ exports.stopBrowserStackBuild = async (bsConfig, args, buildId, rawArgs) => {
10801060
logger.info(message);
10811061
}
10821062
} catch(err) {
1083-
console.log(err);
10841063
message = Constants.userMessages.BUILD_STOP_FAILED;
10851064
messageType = Constants.messageTypes.ERROR;
10861065
errorCode = 'api_failed_build_stop';

bin/runner.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var argv = yargs
6767
.showHelpOnFail(false, Constants.cliMessages.VERSION.HELP)
6868
.demand(1, Constants.cliMessages.VERSION.DEMAND)
6969
.command('init', Constants.cliMessages.INIT.INFO, function(yargs) {
70-
let rawArgv = yargs.argv;
70+
let rawArgv = process.argv.slice(2);
7171
argv = yargs
7272
.usage("usage: $0 init [filename] [options]")
7373
.options({
@@ -87,7 +87,7 @@ var argv = yargs
8787
}
8888
})
8989
.command('build-info', Constants.cliMessages.BUILD.INFO, function(yargs) {
90-
let rawArgv = yargs.argv;
90+
let rawArgv = process.argv.slice(2);
9191
argv = yargs
9292
.usage('usage: $0 <buildId>')
9393
.demand(1, Constants.cliMessages.BUILD.DEMAND)
@@ -103,7 +103,7 @@ var argv = yargs
103103
}
104104
})
105105
.command('build-stop', Constants.cliMessages.BUILD.STOP, function (yargs) {
106-
let rawArgv = yargs.argv;
106+
let rawArgv = process.argv.slice(2);
107107
argv = yargs
108108
.usage('usage: $0 <buildId>')
109109
.demand(1, Constants.cliMessages.BUILD.DEMAND)
@@ -119,7 +119,7 @@ var argv = yargs
119119
}
120120
})
121121
.command('run', Constants.cliMessages.RUN.INFO, function(yargs) {
122-
let rawArgv = yargs.argv;
122+
let rawArgv = process.argv.slice(2);
123123
argv = yargs
124124
.usage('usage: $0 run <options>')
125125
.options({
@@ -252,7 +252,7 @@ var argv = yargs
252252
}
253253
})
254254
.command('generate-report', Constants.cliMessages.GENERATE_REPORT.INFO, function(yargs) {
255-
let rawArgv = yargs.argv;
255+
let rawArgv = process.argv.slice(2);
256256
argv = yargs
257257
.usage('usage: $0 generate-report <buildId>')
258258
.demand(1, Constants.cliMessages.BUILD.DEMAND)
@@ -268,7 +268,7 @@ var argv = yargs
268268
}
269269
})
270270
.command('generate-downloads', Constants.cliMessages.GENERATE_DOWNLOADS.INFO, function(yargs) {
271-
let rawArgv = yargs.argv;
271+
let rawArgv = process.argv.slice(2);
272272
argv = yargs
273273
.usage('usage: $0 generate-downloads <buildId>')
274274
.demand(1, Constants.cliMessages.BUILD.DEMAND)

bin/templates/configTemplate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module.exports = function () {
5656
"project_name": "project-name",
5757
"build_name": "build-name",
5858
"exclude": [],
59-
"parallels": "Here goes the number of parallels you want to run",
59+
"parallels": "Number of parallels you want to run",
6060
"npm_dependencies": {
6161
},
6262
"package_config_options": {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "browserstack-cypress-cli",
3-
"version": "1.12.0",
3+
"version": "1.12.1",
44
"description": "BrowserStack Cypress CLI for Cypress integration with BrowserStack's remote devices.",
55
"main": "index.js",
66
"scripts": {

test/unit/bin/helpers/capabilityHelper.js

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -162,37 +162,47 @@ describe("capabilityHelper.js", () => {
162162
});
163163
});
164164

165-
it("handle local_identifier set", () => {
166-
let zip_url = "bs://<random>";
167-
let bsConfig = {
168-
auth: {
169-
username: "random",
170-
access_key: "random",
171-
},
172-
browsers: [
173-
{
174-
browser: "chrome",
175-
os: "Windows 10",
176-
versions: ["78", "77"],
165+
context("handle local_identifier set when local binary spawned", () => {
166+
beforeEach(() => {
167+
process.env.BSTACK_CYPRESS_LOCAL_BINARY_RUNNING = "true";
168+
});
169+
170+
afterEach(() => {
171+
delete process.env.BSTACK_CYPRESS_LOCAL_BINARY_RUNNING;
172+
});
173+
174+
it("handle local_identifier set", () => {
175+
let zip_url = "bs://<random>";
176+
let bsConfig = {
177+
auth: {
178+
username: "random",
179+
access_key: "random",
177180
},
178-
],
179-
connection_settings: {
180-
local: true,
181-
local_identifier: "abc"
182-
},
183-
run_settings: {
184-
}
185-
};
186-
return capabilityHelper
187-
.caps(bsConfig, { zip_url: zip_url })
188-
.then(function (data) {
189-
let parsed_data = JSON.parse(data);
190-
chai.assert.equal(parsed_data.local, true);
191-
chai.assert.equal(parsed_data.localIdentifier, "abc");
192-
})
193-
.catch((error) => {
194-
chai.assert.fail("Promise error");
195-
});
181+
browsers: [
182+
{
183+
browser: "chrome",
184+
os: "Windows 10",
185+
versions: ["78", "77"],
186+
},
187+
],
188+
connection_settings: {
189+
local: true,
190+
local_identifier: "abc"
191+
},
192+
run_settings: {
193+
}
194+
};
195+
return capabilityHelper
196+
.caps(bsConfig, { zip_url: zip_url })
197+
.then(function (data) {
198+
let parsed_data = JSON.parse(data);
199+
chai.assert.equal(parsed_data.local, true);
200+
chai.assert.equal(parsed_data.localIdentifier, "abc");
201+
})
202+
.catch((error) => {
203+
chai.assert.fail("Promise error");
204+
});
205+
});
196206
});
197207

198208
it("handle local_identifier not set", () => {

0 commit comments

Comments
 (0)