UNPKG

2.61 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
4 * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5 * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6 * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7 * Code distributed by Google as part of the polymer project is also
8 * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9 */
10var _ = require('lodash');
11
12var ALL_BROWSERS = require('../default-sauce-browsers.json');
13
14var TRAVIS_BROWSERS = require('../travis-browsers.json');
15
16// if running under travis, ENV{TRAVIS} = true, use the travis browser set
17var DEFAULT_BROWSERS = process.env.TRAVIS ? TRAVIS_BROWSERS : ALL_BROWSERS;
18
19// "<PLATFORM>/<BROWSER>[@<VERSION>]"
20var BROWSER_SPEC = /^([^\/@]+)\/([^\/@]+)(?:@(.*))?$/;
21
22var PROPERTY_BLACKLIST = ['accessKey', 'browsers', 'disabled', 'username'];
23
24/**
25 * Expands an array of browser identifiers for sauce browsers into their
26 * webdriver capabilities objects.
27 *
28 * @param {!Object} pluginOptions
29 * @param {function(*, Array<!Object>)} done
30 */
31function expand(pluginOptions, done) {
32 var browsers = pluginOptions.browsers;
33 // 'all' is really 'default', just to be consistent with wct-local.
34 if (browsers.indexOf('default') !== -1 || browsers.indexOf('all') !== -1) {
35 // TODO(nevir): Figure out the latest version of each browser and pick
36 // appropriate spreads of versions & OSes.
37 browsers = browsers.concat(_.cloneDeep(DEFAULT_BROWSERS));
38 browsers = _.difference(browsers, ['default', 'all']);
39 }
40
41 done(null, _.compact(browsers.map(_expandBrowser.bind(
42 null,
43 pluginOptions,
44 _.omit(pluginOptions, PROPERTY_BLACKLIST)
45 ))));
46}
47
48/**
49 * @param {string} username
50 * @param {string} accessKey
51 * @param {!Object} options
52 * @param {string|!Object} browser
53 * @return {Object}
54 */
55function _expandBrowser(options, extend, browser) {
56 if (!_.isObject(browser)) {
57 var match = _.isString(browser) && browser.match(BROWSER_SPEC);
58
59 if (!match) {
60 console.log('Invalid sauce browser spec:', browser);
61 return null;
62 }
63 else {
64 browser = {
65 browserName: match[2],
66 platform: match[1],
67 version: match[3] || '',
68 };
69 }
70 }
71
72 return _.extend(browser, {
73 url: {
74 accessKey: options.accessKey,
75 hostname: 'ondemand.saucelabs.com',
76 port: 80,
77 username: options.username,
78 },
79 }, extend);
80}
81
82module.exports = {
83 expand: expand,
84};