|
| 1 | +import { promisify } from 'util' |
| 2 | +import { performance, PerformanceObserver } from 'perf_hooks' |
| 3 | + |
| 4 | +import * as BrowserstackLocalLauncher from 'browserstack-local' |
| 5 | +import logger from '@wdio/logger' |
| 6 | +import type { Capabilities, Services, Options } from '@wdio/types' |
| 7 | + |
| 8 | +import { BrowserstackConfig } from './types' |
| 9 | + |
| 10 | +const log = logger('@wdio/browserstack-service') |
| 11 | + |
| 12 | +type BrowserstackLocal = BrowserstackLocalLauncher.Local & { |
| 13 | + pid?: number; |
| 14 | + stop(callback: (err?: any) => void): void; |
| 15 | +} |
| 16 | + |
| 17 | +export default class BrowserstackLauncherService implements Services.ServiceInstance { |
| 18 | + browserstackLocal?: BrowserstackLocal |
| 19 | + |
| 20 | + constructor ( |
| 21 | + private _options: BrowserstackConfig, |
| 22 | + capabilities: Capabilities.RemoteCapability, |
| 23 | + private _config: Options.Testrunner |
| 24 | + ) {} |
| 25 | + |
| 26 | + onPrepare (config?: Options.Testrunner, capabilities?: Capabilities.RemoteCapabilities) { |
| 27 | + if (!this._options.browserstackLocal) { |
| 28 | + return log.info('browserstackLocal is not enabled - skipping...') |
| 29 | + } |
| 30 | + |
| 31 | + const opts = { |
| 32 | + key: this._config.key, |
| 33 | + forcelocal: true, |
| 34 | + onlyAutomate: true, |
| 35 | + ...this._options.opts |
| 36 | + } |
| 37 | + |
| 38 | + this.browserstackLocal = new BrowserstackLocalLauncher.Local() |
| 39 | + |
| 40 | + if (Array.isArray(capabilities)) { |
| 41 | + capabilities.forEach((capability: Capabilities.DesiredCapabilities) => { |
| 42 | + if (!capability['bstack:options']) { |
| 43 | + capability['bstack:options'] = {} |
| 44 | + } |
| 45 | + capability['bstack:options'].local = true |
| 46 | + }) |
| 47 | + } else if (typeof capabilities === 'object') { |
| 48 | + Object.entries(capabilities as Capabilities.MultiRemoteCapabilities).forEach(([, caps]) => { |
| 49 | + if (!(caps.capabilities as Capabilities.Capabilities)['bstack:options']) { |
| 50 | + (caps.capabilities as Capabilities.Capabilities)['bstack:options'] = {} |
| 51 | + } |
| 52 | + (caps.capabilities as Capabilities.Capabilities)['bstack:options']!.local = true |
| 53 | + }) |
| 54 | + } else { |
| 55 | + throw TypeError('Capabilities should be an object or Array!') |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * measure TestingBot tunnel boot time |
| 60 | + */ |
| 61 | + const obs = new PerformanceObserver((list) => { |
| 62 | + const entry = list.getEntries()[0] |
| 63 | + log.info(`Browserstack Local successfully started after ${entry.duration}ms`) |
| 64 | + }) |
| 65 | + |
| 66 | + obs.observe({ entryTypes: ['measure'] }) |
| 67 | + |
| 68 | + let timer: NodeJS.Timeout |
| 69 | + performance.mark('tbTunnelStart') |
| 70 | + return Promise.race([ |
| 71 | + promisify(this.browserstackLocal.start.bind(this.browserstackLocal))(opts), |
| 72 | + new Promise((resolve, reject) => { |
| 73 | + /* istanbul ignore next */ |
| 74 | + timer = setTimeout(function () { |
| 75 | + reject('Browserstack Local failed to start within 60 seconds!') |
| 76 | + }, 60000) |
| 77 | + })] |
| 78 | + ).then(function (result) { |
| 79 | + clearTimeout(timer) |
| 80 | + performance.mark('tbTunnelEnd') |
| 81 | + performance.measure('bootTime', 'tbTunnelStart', 'tbTunnelEnd') |
| 82 | + return Promise.resolve(result) |
| 83 | + }, function (err) { |
| 84 | + clearTimeout(timer) |
| 85 | + return Promise.reject(err) |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + onComplete () { |
| 90 | + if (!this.browserstackLocal || !this.browserstackLocal.isRunning()) { |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + if (this._options.forcedStop) { |
| 95 | + return process.kill(this.browserstackLocal.pid as number) |
| 96 | + } |
| 97 | + |
| 98 | + let timer: NodeJS.Timeout |
| 99 | + return Promise.race([ |
| 100 | + new Promise<void>((resolve, reject) => { |
| 101 | + this.browserstackLocal?.stop((err: Error) => { |
| 102 | + if (err) { |
| 103 | + return reject(err) |
| 104 | + } |
| 105 | + resolve() |
| 106 | + }) |
| 107 | + }), |
| 108 | + new Promise((resolve, reject) => { |
| 109 | + /* istanbul ignore next */ |
| 110 | + timer = setTimeout( |
| 111 | + () => reject(new Error('Browserstack Local failed to stop within 60 seconds!')), |
| 112 | + 60000 |
| 113 | + ) |
| 114 | + })] |
| 115 | + ).then(function (result) { |
| 116 | + clearTimeout(timer) |
| 117 | + return Promise.resolve(result) |
| 118 | + }, function (err) { |
| 119 | + clearTimeout(timer) |
| 120 | + return Promise.reject(err) |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
0 commit comments