Skip to content

Commit

Permalink
Add automatically determination of the RDC hostname for US/EU (#3468)
Browse files Browse the repository at this point in the history
## Proposed changes
Previously if you wanted to use the RDC cloud of Sauce Labs you needed to provide your hostname yourself. This PR will set the hostname automatically by defaulting it to the US-hostname and if the region is provided, it is determined based on the region

## Types of changes

- [ ] Bugfix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)

## Checklist

- [x] I have read the [CONTRIBUTING](https://github.com/webdriverio/webdriverio/blob/master/CONTRIBUTING.md) doc
- [x] I have added tests that prove my fix is effective or that my feature works
- [x] I have added necessary documentation (if appropriate)

@webdriverio/technical-committee
  • Loading branch information
wswebcreation authored and christian-bromann committed Feb 2, 2019
1 parent 4881ab7 commit 0b38899
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 20 deletions.
6 changes: 3 additions & 3 deletions docs/ConfigurationFile.md
Expand Up @@ -39,9 +39,9 @@ exports.config = {
key: 'xxxxxxxxxxxxxxxx-xxxxxx-xxxxx-xxxxxxxxx',
//
// If you run your tests on SauceLabs you can specify the region you want to run your tests
// in via the `region` property. Available short handles for regions are:
// us: us-west-1 (default)
// eu: eu-central-1
// in via the `region` property. Available short handles for regions are `us` (default) and `eu`.
// These regions are used for the Sauce Labs VM cloud and the Sauce Labs Real Device Cloud.
// If you don't provide the region it will default for the `us`
region: 'us',
//
// ==================
Expand Down
6 changes: 3 additions & 3 deletions packages/wdio-cli/src/templates/wdio.conf.tpl.ejs
Expand Up @@ -34,9 +34,9 @@ exports.config = {
key: process.env.<%= answers.env_key %>,
//
// If you run your tests on SauceLabs you can specify the region you want to run your tests
// in via the `region` property. Available short handles for regions are:
// us: us-west-1 (default)
// eu: eu-central-1
// in via the `region` property. Available short handles for regions are `us` (default) and `eu`.
// These regions are used for the Sauce Labs VM cloud and the Sauce Labs Real Device Cloud.
// If you don't provide the region it will default for the `us`
<% }
if(answers.backend.indexOf('In the cloud') > -1) { %>
<% } %>
Expand Down
8 changes: 7 additions & 1 deletion packages/wdio-config/src/lib/ConfigParser.js
Expand Up @@ -52,10 +52,16 @@ export default class ConfigParser {

this._config = merge(this._config, fileConfig, MERGE_OPTIONS)

/**
* For Sauce Labs RDC we need to determine if the config file has a `testobject_api_key`
* If so, we need to provide a boolean to the `detectBackend` to set the correct hostname
*/
const isRDC = this._capabilities.some(capability => 'testobject_api_key' in capability)

/**
* detect Selenium backend
*/
this._config = merge(detectBackend(this._config), this._config, MERGE_OPTIONS)
this._config = merge(detectBackend(this._config, isRDC), this._config, MERGE_OPTIONS)
} catch (e) {
log.error(`Failed loading configuration file: ${filePath}:`, e.message)
throw e
Expand Down
28 changes: 19 additions & 9 deletions packages/wdio-config/src/utils.js
Expand Up @@ -7,18 +7,20 @@ const REGION_MAPPING = {
'eu': 'eu-central-1.'
}

export function getSauceEndpoint (region) {
const dc = region ?
typeof REGION_MAPPING[region] !== 'undefined' ?
REGION_MAPPING[region] : (region + '.')
: ''
return `${dc}saucelabs.com`
export function getSauceEndpoint (region, isRDC) {
const dcRegion = REGION_MAPPING[region] ? region : 'us'

if (isRDC){
return `${dcRegion}1.appium.testobject.com`
}

return `${REGION_MAPPING[dcRegion]}saucelabs.com`
}

/**
* helper to detect the Selenium backend according to given capabilities
*/
export function detectBackend (options = {}) {
export function detectBackend (options = {}, isRDC = false) {
const { port, hostname, user, key, protocol, region } = options

/**
Expand Down Expand Up @@ -48,10 +50,18 @@ export function detectBackend (options = {}) {
* Sauce Labs
* e.g. 50aa152c-1932-B2f0-9707-18z46q2n1mb0
*/
if (typeof user === 'string' && key.length === 36) {
if ((typeof user === 'string' && key.length === 36) ||
// When SC is used a user needs to be provided and `isRDC` needs to be true
(typeof user === 'string' && isRDC) ||
// Or only RDC
isRDC
) {
// For the VM cloud a prefix needs to be added, the RDC cloud doesn't have that
const preFix = isRDC ? '' : 'ondemand.'

return {
protocol: protocol || 'https',
hostname: hostname || `ondemand.${getSauceEndpoint(region)}`,
hostname: hostname || (preFix + getSauceEndpoint(region, isRDC)),
port: port || 443
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/wdio-config/tests/__fixtures__/wdio.conf.js
Expand Up @@ -10,9 +10,9 @@ exports.config = {
path.join(TEST_ROOT, '/detectBackend.test.js'),
path.join(TEST_ROOT, '/validateConfig.test.js')
],
capabilities: {
capabilities: [{
browserName: 'chrome'
},
}],
suites: {
unit: [path.join(TEST_ROOT, 'configparser.test.js')],
mobile: [path.join(TEST_ROOT, 'detectBackend.test.js')],
Expand Down
25 changes: 23 additions & 2 deletions packages/wdio-config/tests/detectBackend.test.js
Expand Up @@ -81,13 +81,13 @@ describe('detectBackend', () => {
expect(caps.protocol).toBe('https')
})

it('should detect saucelabs user running on a random DC', () => {
it('should detect saucelabs user running on a random DC and default to the us', () => {
const caps = detectBackend({
user: 'foobar',
key: '50aa152c-1932-B2f0-9707-18z46q2n1mb0',
region: 'foobar'
})
expect(caps.hostname).toBe('ondemand.foobar.saucelabs.com')
expect(caps.hostname).toBe('ondemand.saucelabs.com')
expect(caps.port).toBe(443)
expect(caps.protocol).toBe('https')
})
Expand All @@ -104,4 +104,25 @@ describe('detectBackend', () => {
expect(caps.port).toBe(1234)
expect(caps.protocol).toBe('tcp')
})

it('should detect saucelabs rdc user that had not defaulted a region', () => {
const caps = detectBackend({}, true)
expect(caps.hostname).toBe('us1.appium.testobject.com')
expect(caps.port).toBe(443)
expect(caps.protocol).toBe('https')
})

it('should detect saucelabs us rdc user', () => {
const caps = detectBackend({ region: 'us'}, true)
expect(caps.hostname).toBe('us1.appium.testobject.com')
expect(caps.port).toBe(443)
expect(caps.protocol).toBe('https')
})

it('should detect saucelabs eu rdc user', () => {
const caps = detectBackend({ region: 'eu'}, true)
expect(caps.hostname).toBe('eu1.appium.testobject.com')
expect(caps.port).toBe(443)
expect(caps.protocol).toBe('https')
})
})

0 comments on commit 0b38899

Please sign in to comment.