Skip to content

Commit

Permalink
added test and refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickathompson authored and Patrick Thompson committed Aug 6, 2024
1 parent b9f0ff4 commit f3a0778
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,27 +138,23 @@ module.exports = {
},

_getCapabilitiesFromConfig () {
const configPath = path.resolve(process.env.BROWSERSTACK_CAPABILITIES_CONFIG_PATH);
let relativeConfigPath;

if (!configPath)
return {};
const relativeConfigPath = path.relative(__dirname, configPath);
if (process.env.BROWSERSTACK_CAPABILITIES_CONFIG_PATH) {
const configPath = path.resolve(process.env.BROWSERSTACK_CAPABILITIES_CONFIG_PATH);

if (!relativeConfigPath)
relativeConfigPath = path.relative(__dirname, configPath);
}
else
return {};

return require(relativeConfigPath);
},

_getAdditionalCapabilities () {
const capabilitiesFromEnvironment = pickBy(this._getCapabilitiesFromEnvironment(), value => value !== void 0);
let additionalCapabilities = '';

if (process.env.BROWSERSTACK_CAPABILITIES_CONFIG_PATH)
additionalCapabilities = { ...this._getCapabilitiesFromConfig(), ...capabilitiesFromEnvironment };
else
additionalCapabilities = { ...capabilitiesFromEnvironment };

return additionalCapabilities;
return { ...this._getCapabilitiesFromConfig(), ...capabilitiesFromEnvironment };
},

_filterPlatformInfo (query) {
Expand Down
35 changes: 35 additions & 0 deletions test/mocha/browserstack-capabilities-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('Browserstack capabilities', function () {
});

it('Should read additional capabilities from a config file', () => {

process.env.BROWSERSTACK_CAPABILITIES_CONFIG_PATH = require.resolve('./data/capabilities-config.json');

const capabilities = browserStackProvider._getAdditionalCapabilities();
Expand All @@ -63,4 +64,38 @@ describe('Browserstack capabilities', function () {
'browserstack.networkProfile': '4g-lte-lossy'
});
});

it('Should add additional capabilities when both config file and environment variables used', () => {
process.env['BROWSERSTACK_BUILD_ID'] = 'build-1';
process.env['BROWSERSTACK_PROJECT_NAME'] = 'project-1';
process.env['BROWSERSTACK_DISPLAY_RESOLUTION'] = '1024x768';
process.env['BROWSERSTACK_TEST_RUN_NAME'] = 'Testcafe test run 1';
process.env['BROWSERSTACK_DEBUG'] = 'false'; //env var should take precedence
process.env['BROWSERSTACK_CONSOLE'] = 'errors';
process.env['BROWSERSTACK_NETWORK_LOGS'] = 'true';
process.env['BROWSERSTACK_VIDEO'] = 'true';
process.env['BROWSERSTACK_TIMEZONE'] = 'Asia/Taipei';
process.env['BROWSERSTACK_GEO_LOCATION'] = 'ZA';
process.env['BROWSERSTACK_CUSTOM_NETWORK'] = '"1000", "1000", "100", "1"';
process.env['BROWSERSTACK_NETWORK_PROFILE'] = '4g-lte-lossy';

process.env.BROWSERSTACK_CAPABILITIES_CONFIG_PATH = require.resolve('./data/capabilities-config.json');

const capabilities = browserStackProvider._getAdditionalCapabilities();

expect(capabilities).to.deep.equal({
'build': 'build-1',
'project': 'project-1',
'resolution': '1024x768',
'name': 'Testcafe test run 1',
'browserstack.debug': 'false', //env var should take precedence
'browserstack.console': 'errors',
'browserstack.networkLogs': 'true',
'browserstack.video': 'true',
'browserstack.timezone': 'Asia/Taipei',
'browserstack.geoLocation': 'ZA',
'browserstack.customNetwork': '"1000", "1000", "100", "1"',
'browserstack.networkProfile': '4g-lte-lossy'
});
});
});

0 comments on commit f3a0778

Please sign in to comment.