From 3732d4b14b17014eb9fc581dc33787b57d9fff09 Mon Sep 17 00:00:00 2001 From: fbeucklers Date: Wed, 16 Oct 2024 13:54:38 +0200 Subject: [PATCH] feat(test-runner-browserstack): Make the local proxy configurable --- .changeset/chilled-books-burn.md | 5 +++ .../src/browserstackLauncher.ts | 41 +++++++++++++------ 2 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 .changeset/chilled-books-burn.md diff --git a/.changeset/chilled-books-burn.md b/.changeset/chilled-books-burn.md new file mode 100644 index 000000000..de1c370f2 --- /dev/null +++ b/.changeset/chilled-books-burn.md @@ -0,0 +1,5 @@ +--- +'@web/test-runner-browserstack': minor +--- + +Allow to disable the local proxy for CI environments diff --git a/packages/test-runner-browserstack/src/browserstackLauncher.ts b/packages/test-runner-browserstack/src/browserstackLauncher.ts index bf86abff8..f09cf1766 100644 --- a/packages/test-runner-browserstack/src/browserstackLauncher.ts +++ b/packages/test-runner-browserstack/src/browserstackLauncher.ts @@ -11,15 +11,14 @@ import { export interface BrowserstackLauncherArgs { capabilities: Record; localOptions?: Partial; + 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, public name: string, @@ -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() { @@ -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; } } @@ -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