|
| 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 | +import * as request from 'request'; |
| 7 | +import * as q from 'q'; |
| 8 | +import * as util from 'util'; |
| 9 | + |
| 10 | +import {Config} from '../configParser'; |
| 11 | +import {DriverProvider} from './driverProvider'; |
| 12 | +import {Logger} from '../logger2'; |
| 13 | + |
| 14 | +let logger = new Logger('browserstack'); |
| 15 | + |
| 16 | +export class BrowserStack extends DriverProvider { |
| 17 | + constructor(config: Config) { super(config); } |
| 18 | + |
| 19 | + /** |
| 20 | + * Hook to update the BrowserStack job status. |
| 21 | + * @public |
| 22 | + * @param {Object} update |
| 23 | + * @return {q.promise} A promise that will resolve when the update is complete. |
| 24 | + */ |
| 25 | + updateJob(update: any): q.Promise<any> { |
| 26 | + let deferredArray = this.drivers_.map((driver: WebDriver) => { |
| 27 | + let deferred = q.defer(); |
| 28 | + driver.getSession().then((session: Session) => { |
| 29 | + var jobStatus = update.passed ? 'completed' : 'error'; |
| 30 | + logger.info( |
| 31 | + 'BrowserStack results available at ' + |
| 32 | + 'https://www.browserstack.com/automate'); |
| 33 | + request( |
| 34 | + { |
| 35 | + url: 'https://www.browserstack.com/automate/sessions/' + |
| 36 | + session.getId() + '.json', |
| 37 | + headers: { |
| 38 | + 'Content-Type': 'application/json', |
| 39 | + 'Authorization': 'Basic ' + |
| 40 | + new Buffer( |
| 41 | + this.config_.browserstackUser + ':' + |
| 42 | + this.config_.browserstackKey) |
| 43 | + .toString('base64') |
| 44 | + }, |
| 45 | + method: 'PUT', |
| 46 | + form: {'status': jobStatus} |
| 47 | + }, |
| 48 | + (error: Error) => { |
| 49 | + if (error) { |
| 50 | + throw new Error( |
| 51 | + 'Error updating BrowserStack pass/fail status: ' + |
| 52 | + util.inspect(error)); |
| 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 | + setupEnv(): q.Promise<any> { |
| 69 | + var deferred = q.defer(); |
| 70 | + this.config_.capabilities['browserstack.user'] = |
| 71 | + this.config_.browserstackUser; |
| 72 | + this.config_.capabilities['browserstack.key'] = |
| 73 | + this.config_.browserstackKey; |
| 74 | + this.config_.seleniumAddress = 'http://hub.browserstack.com/wd/hub'; |
| 75 | + |
| 76 | + // Append filename to capabilities.name so that it's easier to identify |
| 77 | + // tests. |
| 78 | + if (this.config_.capabilities.name && |
| 79 | + this.config_.capabilities.shardTestFiles) { |
| 80 | + this.config_.capabilities.name += |
| 81 | + (':' + this.config_.specs.toString().replace(/^.*[\\\/]/, '')); |
| 82 | + } |
| 83 | + |
| 84 | + logger.info( |
| 85 | + 'Using BrowserStack selenium server at ' + |
| 86 | + this.config_.seleniumAddress); |
| 87 | + deferred.resolve(); |
| 88 | + return deferred.promise; |
| 89 | + } |
| 90 | +} |
0 commit comments