Skip to content

Commit ab99aac

Browse files
committed
add code changes to capture, validate and send ip geolocation
1 parent fd3ae66 commit ab99aac

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

bin/commands/runs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ module.exports = function run(args, rawArgs) {
4848
// set cypress config filename
4949
utils.setCypressConfigFilename(bsConfig, args);
5050

51+
// set cypress geo location
52+
utils.setGeoLocation(bsConfig, args);
53+
5154
// accept the specs list from command line if provided
5255
utils.setUserSpecs(bsConfig, args);
5356

bin/helpers/capabilityHelper.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ const validate = (bsConfig, args) => {
164164

165165
if( Utils.searchForOption('--async') && ( !Utils.isUndefined(args.async) && bsConfig["connection_settings"]["local"])) reject(Constants.validationMessages.INVALID_LOCAL_ASYNC_ARGS);
166166

167+
if (bsConfig.run_settings.userProvidedGeoLocation && bsConfig.run_settings.geolocation.match(/^[A-Z]{2}$/g)) reject(Constants.validationMessages.INVALID_GEO_LOCATION);
168+
167169
// validate if config file provided exists or not when cypress_config_file provided
168170
// validate the cypressProjectDir key otherwise.
169171
let cypressConfigFilePath = bsConfig.run_settings.cypressConfigFilePath;

bin/helpers/constants.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ const validationMessages = {
8787
INVALID_LOCAL_IDENTIFIER: "Invalid value specified for local_identifier. For more info, check out https://www.browserstack.com/docs/automate/cypress/cli-reference",
8888
INVALID_BROWSER_ARGS: "Aborting as an unacceptable value was passed for --browser. Read more at https://www.browserstack.com/docs/automate/cypress/cli-reference",
8989
INVALID_LOCAL_ASYNC_ARGS: "Cannot run in --async mode when local is set to true. Please run the build after removing --async",
90+
INVALID_GEO_LOCATION: "[BROWSERSTACK_INVALID_COUNTRY_CODE] The country code specified for 'browserstack.geoLocation' is invalid. For list of supported countries, refer to - https://www.browserstack.com/ip-geolocation",
91+
NOT_SUPPORTED_GEO_LOCATION: "The country code you have passed for IP Geolocation is currently not supported. Please refer the link https://www.browserstack.com/ip-geolocation for a list of supported countries.",
92+
NOT_AVAILABLE_GEO_LOCATION: "The country code you have passed for IP Geolocation is not available at the moment. Please try again in a few hours.",
93+
ACCESS_DENIED_GEO_LOCATION: "'geolocation' (IP Geolocation feature) capability is not supported in your account. It is only available under Enterprise plans, refer https://www.browserstack.com/ip-geolocation for more details."
9094
};
9195

9296
const cliMessages = {
@@ -129,6 +133,7 @@ const cliMessages = {
129133
CONFIG_DESCRIPTION: "Set configuration values. Separate multiple values with a comma. The values set here override any values set in your configuration file.",
130134
REPORTER: "Specify the custom reporter to use",
131135
REPORTER_OPTIONS: "Specify reporter options for custom reporter",
136+
CYPRESS_GEO_LOCATION: "Enterprise feature to simulate website and mobile behavior from different locations."
132137
},
133138
COMMON: {
134139
DISABLE_USAGE_REPORTING: "Disable usage reporting",

bin/helpers/utils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,28 @@ exports.setCypressConfigFilename = (bsConfig, args) => {
272272
}
273273
}
274274

275+
exports.verifyGeoLocationOption = () => {
276+
let glOptionsSet = (this.searchForOption('-gl') || this.searchForOption('--gl'));
277+
let geoHyphenLocationOptionsSet = (this.searchForOption('-geo-location') || this.searchForOption('--geo-location'));
278+
let geoLocationOptionsSet = (this.searchForOption('-geolocation') || this.searchForOption('--geolocation'));
279+
return (glOptionsSet || geoHyphenLocationOptionsSet || geoLocationOptionsSet);
280+
}
281+
282+
exports.setGeoLocation = (bsConfig, args) => {
283+
let userProvidedGeoLocation = this.verifyGeoLocationOption();
284+
bsConfig.run_settings.userProvidedGeoLocation = (userProvidedGeoLocation || (!this.isUndefined(bsConfig.run_settings.geolocation)));
285+
286+
if (userProvidedGeoLocation && !this.isUndefined(args.geolocation)) {
287+
bsConfig.run_settings.geolocation = args.geolocation;
288+
}
289+
290+
if (this.isUndefined(bsConfig.run_settings.geolocation)){
291+
bsConfig.run_settings.geolocation = null;
292+
} else {
293+
bsConfig.run_settings.geolocation = bsConfig.run_settings.geolocation.toUpperCase();
294+
}
295+
}
296+
275297
// specs can be passed from bstack configuration file
276298
// specs can be passed via command line args as a string
277299
// command line args takes precedence over config

bin/runner.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ var argv = yargs
133133
demand: true,
134134
demand: Constants.cliMessages.RUN.CYPRESS_CONFIG_DEMAND
135135
},
136+
'gl': {
137+
alias: 'geolocation',
138+
describe: Constants.cliMessages.RUN.CYPRESS_GEO_LOCATION,
139+
default: undefined,
140+
type: 'string',
141+
nargs: 1
142+
},
136143
'p': {
137144
alias: ['parallels', 'parallel'],
138145
describe: Constants.cliMessages.RUN.PARALLEL_DESC,

0 commit comments

Comments
 (0)