@@ -11,15 +11,14 @@ import {
11
11
export interface BrowserstackLauncherArgs {
12
12
capabilities : Record < string , unknown > ;
13
13
localOptions ?: Partial < browserstack . Options > ;
14
+ local ?: boolean
14
15
}
15
16
16
17
const REQUIRED_CAPABILITIES = [ 'name' , 'browserstack.user' , 'browserstack.key' , 'project' , 'build' ] ;
17
- const localIp = internalIp . v4 . sync ( ) as string ;
18
- if ( ! localIp ) {
19
- throw new Error ( 'Can not determine the local IP.' ) ;
20
- }
21
18
22
19
export class BrowserstackLauncher extends WebdriverLauncher {
20
+ private localIp ?: string ;
21
+
23
22
constructor (
24
23
private capabilities : Record < string , unknown > ,
25
24
public name : string ,
@@ -34,19 +33,35 @@ export class BrowserstackLauncher extends WebdriverLauncher {
34
33
user : capabilities [ 'browserstack.user' ] as string ,
35
34
key : capabilities [ 'browserstack.key' ] as string ,
36
35
} ) ;
36
+
37
+ if ( this . capabilities [ 'browserstack.local' ] ) {
38
+ this . localIp = internalIp . v4 . sync ( ) as string ;
39
+ if ( ! this . localIp ) {
40
+ throw new Error ( 'Can not determine the local IP.' ) ;
41
+ }
42
+ }
37
43
}
38
44
39
45
async initialize ( config : TestRunnerCoreConfig ) {
40
- await registerBrowserstackLocal (
41
- this ,
42
- this . capabilities [ 'browserstack.key' ] as string ,
43
- this . localOptions ,
44
- ) ;
46
+ if ( this . capabilities [ 'browserstack.local' ] ) {
47
+ await registerBrowserstackLocal (
48
+ this ,
49
+ this . capabilities [ 'browserstack.key' ] as string ,
50
+ this . localOptions ,
51
+ ) ;
52
+ }
45
53
await super . initialize ( config ) ;
46
54
}
47
55
48
56
startSession ( sessionId : string , url : string ) {
49
- return super . startSession ( sessionId , url . replace ( / ( l o c a l h o s t | 1 2 7 \. 0 \. 0 \. 1 ) / , localIp ) ) ;
57
+ if ( url === 'localhost' || url === '127.0.0.1' ) {
58
+ if ( ! this . localIp ) {
59
+ throw new Error ( 'If you want to use a local domain, make sure to enable the browserstack.local capability.' ) ;
60
+ }
61
+ url = url . replace ( / ( l o c a l h o s t | 1 2 7 \. 0 \. 0 \. 1 ) / , this . localIp )
62
+ }
63
+
64
+ return super . startSession ( sessionId , url ) ;
50
65
}
51
66
52
67
async startDebugSession ( ) {
@@ -55,7 +70,9 @@ export class BrowserstackLauncher extends WebdriverLauncher {
55
70
56
71
stop ( ) {
57
72
const stopPromise = super . stop ( ) ;
58
- unregisterBrowserstackLocal ( this ) ;
73
+ if ( this . capabilities [ 'browserstack.local' ] ) {
74
+ unregisterBrowserstackLocal ( this ) ;
75
+ }
59
76
return stopPromise ;
60
77
}
61
78
}
@@ -79,7 +96,7 @@ export function browserstackLauncher(args: BrowserstackLauncherArgs): BrowserLau
79
96
80
97
const capabilities = { ...args . capabilities } ;
81
98
capabilities [ 'timeout' ] = 300 ;
82
- capabilities [ 'browserstack.local' ] = true ;
99
+ capabilities [ 'browserstack.local' ] = args . local ?? true ;
83
100
capabilities [ 'browserstack.localIdentifier' ] = localId ;
84
101
85
102
// we need to allow popups since we open new windows
0 commit comments