Skip to content

Commit

Permalink
feat(test-runner-browserstack): Make the local proxy configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
fbuecklers committed Oct 16, 2024
1 parent aadcbea commit 3732d4b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-books-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@web/test-runner-browserstack': minor
---

Allow to disable the local proxy for CI environments
41 changes: 29 additions & 12 deletions packages/test-runner-browserstack/src/browserstackLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import {
export interface BrowserstackLauncherArgs {
capabilities: Record<string, unknown>;
localOptions?: Partial<browserstack.Options>;
local?: boolean
}

const REQUIRED_CAPABILITIES = ['name', 'browserstack.user', 'browserstack.key', 'project', 'build'];
const localIp = internalIp.v4.sync() as string;
if (!localIp) {
throw new Error('Can not determine the local IP.');
}

export class BrowserstackLauncher extends WebdriverLauncher {
private localIp?: string;

constructor(
private capabilities: Record<string, unknown>,
public name: string,
Expand All @@ -34,19 +33,35 @@ export class BrowserstackLauncher extends WebdriverLauncher {
user: capabilities['browserstack.user'] as string,
key: capabilities['browserstack.key'] as string,
});

if (this.capabilities['browserstack.local']) {
this.localIp = internalIp.v4.sync() as string;
if (!this.localIp) {
throw new Error('Can not determine the local IP.');
}
}
}

async initialize(config: TestRunnerCoreConfig) {
await registerBrowserstackLocal(
this,
this.capabilities['browserstack.key'] as string,
this.localOptions,
);
if (this.capabilities['browserstack.local']) {
await registerBrowserstackLocal(
this,
this.capabilities['browserstack.key'] as string,
this.localOptions,
);
}
await super.initialize(config);
}

startSession(sessionId: string, url: string) {
return super.startSession(sessionId, url.replace(/(localhost|127\.0\.0\.1)/, localIp));
if (url === 'localhost' || url === '127.0.0.1') {
if (!this.localIp) {
throw new Error('If you want to use a local domain, make sure to enable the browserstack.local capability.');
}
url = url.replace(/(localhost|127\.0\.0\.1)/, this.localIp)
}

return super.startSession(sessionId, url);
}

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

stop() {
const stopPromise = super.stop();
unregisterBrowserstackLocal(this);
if (this.capabilities['browserstack.local']) {
unregisterBrowserstackLocal(this);
}
return stopPromise;
}
}
Expand All @@ -79,7 +96,7 @@ export function browserstackLauncher(args: BrowserstackLauncherArgs): BrowserLau

const capabilities = { ...args.capabilities };
capabilities['timeout'] = 300;
capabilities['browserstack.local'] = true;
capabilities['browserstack.local'] = args.local ?? true;
capabilities['browserstack.localIdentifier'] = localId;

// we need to allow popups since we open new windows
Expand Down

0 comments on commit 3732d4b

Please sign in to comment.