|
| 1 | +/* |
| 2 | + * This is an implementation of the Browserstack Driver Provider. |
| 3 | + * It is responsible for setting up the account object, tearing |
| 4 | + * it down, and setting up the driver correctly. |
| 5 | + */ |
| 6 | + |
| 7 | +var util = require('util'), |
| 8 | + log = require('../logger.js'), |
| 9 | + request = require('request'), |
| 10 | + q = require('q'), |
| 11 | + DriverProvider = require('./driverProvider'); |
| 12 | + |
| 13 | + |
| 14 | +var BrowserStackDriverProvider = function(config) { |
| 15 | + DriverProvider.call(this, config); |
| 16 | +}; |
| 17 | +util.inherits(BrowserStackDriverProvider, DriverProvider); |
| 18 | + |
| 19 | + |
| 20 | +/** |
| 21 | + * Hook to update the BrowserStack job status. |
| 22 | + * @public |
| 23 | + * @param {Object} update |
| 24 | + * @return {q.promise} A promise that will resolve when the update is complete. |
| 25 | + */ |
| 26 | +BrowserStackDriverProvider.prototype.updateJob = function(update) { |
| 27 | + |
| 28 | + var self = this; |
| 29 | + var deferredArray = this.drivers_.map(function(driver) { |
| 30 | + var deferred = q.defer(); |
| 31 | + driver.getSession().then(function(session) { |
| 32 | + var jobStatus = update.passed ? 'completed' : 'error'; |
| 33 | + log.puts('BrowserStack results available at ' + |
| 34 | + 'https://www.browserstack.com/automate'); |
| 35 | + request({ |
| 36 | + url: 'https://www.browserstack.com/automate/sessions/' + |
| 37 | + session.getId() + '.json', |
| 38 | + headers: { |
| 39 | + 'Content-Type': 'application/json', |
| 40 | + 'Authorization': 'Basic ' + new Buffer(self.config_.browserstackUser |
| 41 | + + ':' + self.config_.browserstackKey).toString('base64') |
| 42 | + }, |
| 43 | + method: 'PUT', |
| 44 | + form: { |
| 45 | + 'status': jobStatus |
| 46 | + } |
| 47 | + }, function(error){ |
| 48 | + if (error) { |
| 49 | + throw new Error( |
| 50 | + 'Error updating BrowserStack pass/fail status: ' + |
| 51 | + util.inspect(error) |
| 52 | + ); |
| 53 | + } |
| 54 | + }); |
| 55 | + deferred.resolve(); |
| 56 | + }); |
| 57 | + return deferred.promise; |
| 58 | + }); |
| 59 | + return q.all(deferredArray); |
| 60 | +}; |
| 61 | + |
| 62 | +/** |
| 63 | + * Configure and launch (if applicable) the object's environment. |
| 64 | + * @public |
| 65 | + * @return {q.promise} A promise which will resolve when the environment is |
| 66 | + * ready to test. |
| 67 | + */ |
| 68 | +BrowserStackDriverProvider.prototype.setupEnv = function() { |
| 69 | + var deferred = q.defer(); |
| 70 | + this.config_.capabilities['browserstack.user'] = |
| 71 | + this.config_.browserstackUser; |
| 72 | + this.config_.capabilities['browserstack.key'] = this.config_.browserstackKey; |
| 73 | + this.config_.seleniumAddress = 'http://hub.browserstack.com/wd/hub'; |
| 74 | + |
| 75 | + // Append filename to capabilities.name so that it's easier to identify tests. |
| 76 | + if (this.config_.capabilities.name && |
| 77 | + this.config_.capabilities.shardTestFiles) { |
| 78 | + this.config_.capabilities.name += ( |
| 79 | + ':' + this.config_.specs.toString().replace(/^.*[\\\/]/, '')); |
| 80 | + } |
| 81 | + |
| 82 | + log.puts('Using BrowserStack selenium server at ' + |
| 83 | + this.config_.seleniumAddress); |
| 84 | + deferred.resolve(); |
| 85 | + return deferred.promise; |
| 86 | +}; |
| 87 | + |
| 88 | +// new instance w/ each include |
| 89 | +module.exports = function(config) { |
| 90 | + return new BrowserStackDriverProvider(config); |
| 91 | +}; |
0 commit comments