@@ -6,6 +6,7 @@ import logger from '@wdio/logger'
6
6
import type { Capabilities , Services , Options } from '@wdio/types'
7
7
8
8
import { BrowserstackConfig } from './types'
9
+ import { getWebdriverIOVersion , getBrowserstackWdioServiceVersion } from './util'
9
10
10
11
// @ts -ignore
11
12
import { version } from '../package.json'
@@ -31,35 +32,18 @@ export default class BrowserstackLauncherService implements Services.ServiceInst
31
32
onPrepare ( config ?: Options . Testrunner , capabilities ?: Capabilities . RemoteCapabilities ) {
32
33
if ( Array . isArray ( capabilities ) ) {
33
34
capabilities . forEach ( ( capability : Capabilities . DesiredCapabilities | any ) => {
34
- const wdioServiceVersion = version ;
35
- let webdriverIOVersion : any = undefined ;
36
- const packageFile : any = process . env . npm_package_json ;
37
- if ( packageFile !== undefined ) {
38
- const { devDependencies, dependencies } = require ( packageFile ) ;
39
- if ( devDependencies !== undefined ) {
40
- webdriverIOVersion = devDependencies [ 'webdriverio' ]
41
- } else if ( dependencies !== undefined ) {
42
- webdriverIOVersion = dependencies [ 'webdriverio' ]
43
- }
44
- } else {
45
- webdriverIOVersion = process . env . npm_package_dependencies_webdriverio ;
46
- if ( webdriverIOVersion === undefined ) {
47
- webdriverIOVersion = process . env . npm_package_devDependencies_webdriverio ;
48
- }
49
- }
50
- if ( webdriverIOVersion !== undefined ) {
51
- webdriverIOVersion = webdriverIOVersion . split ( '.' ) [ 0 ] ;
52
- if ( webdriverIOVersion [ 0 ] === '^' ) {
53
- webdriverIOVersion = webdriverIOVersion . substring ( 1 ) ;
54
- }
55
- webdriverIOVersion = parseInt ( webdriverIOVersion ) ;
56
- }
35
+ const wdioServiceVersion : string = getBrowserstackWdioServiceVersion ( ) ;
36
+ const webdriverIOVersion : any = getWebdriverIOVersion ( ) ;
57
37
if ( capability [ 'bstack:options' ] ) {
38
+ // if bstack:options present add wdioService inside it
58
39
capability [ 'bstack:options' ] . wdioService = wdioServiceVersion ;
59
40
} else if ( webdriverIOVersion >= 7 ) {
41
+ // in case of webdriver version 7 we need to add wdioService inside bstack:options,
42
+ // so need to add bstack:options key first
60
43
capability [ 'bstack:options' ] = { } ;
61
44
capability [ 'bstack:options' ] . wdioService = wdioServiceVersion ;
62
45
} else {
46
+ // on webdriver 6 and below can directly add at root level
63
47
capability [ 'browserstack.wdioService' ] = wdioServiceVersion ;
64
48
}
65
49
} )
0 commit comments