Skip to content

Commit 86ddb1b

Browse files
committed
feat(test-runner-browserstack): Make the local proxy configurable
1 parent aadcbea commit 86ddb1b

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

.changeset/chilled-books-burn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@web/test-runner-browserstack': minor
3+
---
4+
5+
Allow to disable the local proxy for CI environments

docs/docs/test-runner/browser-launchers/browserstack.md

+12
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ export default {
7070
os_version: '7',
7171
},
7272
}),
73+
74+
browserstackLauncher({
75+
capabilities: {
76+
...sharedCapabilities,
77+
browserName: 'Chrome',
78+
os: 'Windows',
79+
os_version: '10',
80+
},
81+
local: false, // disables the local proxy
82+
}),
7383
],
7484
};
7585
```
@@ -81,3 +91,5 @@ The Browserstack launcher takes two properties, `capabilities` and `localOptions
8191
`capabilities` are the selenium capabilities used to configure the browser to launch in Browserstack. You can generate most of these on the Saucelabs. It must contain a `browserstack.user` and `browserstack.key` property to authenticate with Browserstack, as well as `name`, `build` and `project` to identify the test run.
8292

8393
`localOptions` are options to configure the [browserstack-local](https://www.npmjs.com/package/browserstack-local) proxy. For most use cases, you don't need to configure this property.
94+
95+
`local` is a property which, when set to `false`, disables the local proxy entirely. This can be particularly helpful in CI environment where you may not need to proxy local resources through Browserstack.

packages/test-runner-browserstack/src/browserstackLauncher.ts

+29-12
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ import {
1111
export interface BrowserstackLauncherArgs {
1212
capabilities: Record<string, unknown>;
1313
localOptions?: Partial<browserstack.Options>;
14+
local?: boolean
1415
}
1516

1617
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-
}
2118

2219
export class BrowserstackLauncher extends WebdriverLauncher {
20+
private localIp?: string;
21+
2322
constructor(
2423
private capabilities: Record<string, unknown>,
2524
public name: string,
@@ -34,19 +33,35 @@ export class BrowserstackLauncher extends WebdriverLauncher {
3433
user: capabilities['browserstack.user'] as string,
3534
key: capabilities['browserstack.key'] as string,
3635
});
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+
}
3743
}
3844

3945
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+
}
4553
await super.initialize(config);
4654
}
4755

4856
startSession(sessionId: string, url: string) {
49-
return super.startSession(sessionId, url.replace(/(localhost|127\.0\.0\.1)/, localIp));
57+
if (url.includes('localhost') || url.includes('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(/(localhost|127\.0\.0\.1)/, this.localIp)
62+
}
63+
64+
return super.startSession(sessionId, url);
5065
}
5166

5267
async startDebugSession() {
@@ -55,7 +70,9 @@ export class BrowserstackLauncher extends WebdriverLauncher {
5570

5671
stop() {
5772
const stopPromise = super.stop();
58-
unregisterBrowserstackLocal(this);
73+
if (this.capabilities['browserstack.local']) {
74+
unregisterBrowserstackLocal(this);
75+
}
5976
return stopPromise;
6077
}
6178
}
@@ -79,7 +96,7 @@ export function browserstackLauncher(args: BrowserstackLauncherArgs): BrowserLau
7996

8097
const capabilities = { ...args.capabilities };
8198
capabilities['timeout'] = 300;
82-
capabilities['browserstack.local'] = true;
99+
capabilities['browserstack.local'] = args.local ?? true;
83100
capabilities['browserstack.localIdentifier'] = localId;
84101

85102
// we need to allow popups since we open new windows

0 commit comments

Comments
 (0)