@@ -17,7 +17,6 @@ import BrowserConnectionStatus from './status';
1717import HeartbeatStatus from './heartbeat-status' ;
1818import { GeneralError , TimeoutError } from '../../errors/runtime' ;
1919import { RUNTIME_ERRORS } from '../../errors/types' ;
20- import { Dictionary } from '../../configuration/interfaces' ;
2120import BrowserConnectionGateway from './gateway' ;
2221import BrowserJob from '../../runner/browser-job' ;
2322import WarningLog from '../../notifications/warning-log' ;
@@ -32,11 +31,16 @@ import {
3231 REMOTE_BROWSER_INIT_TIMEOUT ,
3332} from '../../utils/browser-connection-timeouts' ;
3433import MessageBus from '../../utils/message-bus' ;
34+ import BrowserConnectionTracker from './browser-connection-tracker' ;
35+ import TestRun from '../../test-run' ;
36+ // @ts -ignore
37+ import { TestRun as LegacyTestRun } from 'testcafe-legacy-api' ;
38+ import { Proxy } from 'testcafe-hammerhead' ;
3539
3640const getBrowserConnectionDebugScope = ( id : string ) : string => `testcafe:browser:connection:${ id } ` ;
3741
38- const IDLE_PAGE_TEMPLATE = read ( '../../client/browser/idle-page/index.html.mustache' ) ;
39- const connections : Dictionary < BrowserConnection > = { } ;
42+ const IDLE_PAGE_TEMPLATE = read ( '../../client/browser/idle-page/index.html.mustache' ) ;
43+
4044
4145interface DisconnectionPromise < T > extends Promise < T > {
4246 resolve : Function ;
@@ -83,31 +87,31 @@ export default class BrowserConnection extends EventEmitter {
8387 public permanent : boolean ;
8488 public previousActiveWindowId : string | null ;
8589 private readonly disableMultipleWindows : boolean ;
86- private readonly proxyless : boolean ;
90+ public readonly proxyless : boolean ;
8791 private readonly HEARTBEAT_TIMEOUT : number ;
8892 private readonly BROWSER_CLOSE_TIMEOUT : number ;
8993 private readonly BROWSER_RESTART_TIMEOUT : number ;
9094 public readonly id : string ;
9195 private readonly jobQueue : BrowserJob [ ] ;
9296 private readonly initScriptsQueue : InitScriptTask [ ] ;
93- private browserConnectionGateway : BrowserConnectionGateway ;
97+ public browserConnectionGateway : BrowserConnectionGateway ;
9498 private disconnectionPromise : DisconnectionPromise < void > | null ;
9599 private testRunAborted : boolean ;
96100 public status : BrowserConnectionStatus ;
97101 private heartbeatTimeout : NodeJS . Timeout | null ;
98102 private pendingTestRunUrl : string | null ;
99- public readonly url : string ;
100- public readonly idleUrl : string ;
101- private forcedIdleUrl : string ;
102- private readonly initScriptUrl : string ;
103- public readonly heartbeatRelativeUrl : string ;
104- public readonly statusRelativeUrl : string ;
105- public readonly statusDoneRelativeUrl : string ;
106- private readonly heartbeatUrl : string ;
107- private readonly statusUrl : string ;
108- public readonly activeWindowIdUrl : string ;
109- public readonly closeWindowUrl : string ;
110- private statusDoneUrl : string ;
103+ public url = '' ;
104+ public idleUrl = '' ;
105+ private forcedIdleUrl = '' ;
106+ private initScriptUrl = '' ;
107+ public heartbeatUrl = '' ;
108+ public statusUrl = '' ;
109+ public activeWindowIdUrl = '' ;
110+ public closeWindowUrl = '' ;
111+ public statusDoneUrl = '' ;
112+ public heartbeatRelativeUrl = '' ;
113+ public statusRelativeUrl = '' ;
114+ public statusDoneRelativeUrl = '' ;
111115 private readonly debugLogger : debug . Debugger ;
112116 private osInfo : OSInfo | null = null ;
113117
@@ -155,24 +159,10 @@ export default class BrowserConnection extends EventEmitter {
155159 this . disableMultipleWindows = disableMultipleWindows ;
156160 this . proxyless = proxyless ;
157161
158- this . url = `${ gateway . domain } /browser/connect/${ this . id } ` ;
159- this . idleUrl = `${ gateway . domain } /browser/idle/${ this . id } ` ;
160- this . forcedIdleUrl = `${ gateway . domain } /browser/idle-forced/${ this . id } ` ;
161- this . initScriptUrl = `${ gateway . domain } /browser/init-script/${ this . id } ` ;
162-
163- this . heartbeatRelativeUrl = `/browser/heartbeat/${ this . id } ` ;
164- this . statusRelativeUrl = `/browser/status/${ this . id } ` ;
165- this . statusDoneRelativeUrl = `/browser/status-done/${ this . id } ` ;
166- this . activeWindowIdUrl = `/browser/active-window-id/${ this . id } ` ;
167- this . closeWindowUrl = `/browser/close-window/${ this . id } ` ;
168-
169- this . heartbeatUrl = `${ gateway . domain } ${ this . heartbeatRelativeUrl } ` ;
170- this . statusUrl = `${ gateway . domain } ${ this . statusRelativeUrl } ` ;
171- this . statusDoneUrl = `${ gateway . domain } ${ this . statusDoneRelativeUrl } ` ;
172-
162+ this . _buildCommunicationUrls ( gateway . proxy ) ;
173163 this . _setEventHandlers ( ) ;
174164
175- connections [ this . id ] = this ;
165+ BrowserConnectionTracker . add ( this ) ;
176166
177167 this . previousActiveWindowId = null ;
178168
@@ -182,6 +172,24 @@ export default class BrowserConnection extends EventEmitter {
182172 process . nextTick ( ( ) => this . _runBrowser ( ) ) ;
183173 }
184174
175+ private _buildCommunicationUrls ( proxy : Proxy ) : void {
176+ this . url = proxy . resolveRelativeServiceUrl ( `/browser/connect/${ this . id } ` ) ;
177+ this . idleUrl = proxy . resolveRelativeServiceUrl ( `/browser/idle/${ this . id } ` ) ;
178+ this . forcedIdleUrl = proxy . resolveRelativeServiceUrl ( `/browser/idle-forced/${ this . id } ` ) ;
179+ this . initScriptUrl = proxy . resolveRelativeServiceUrl ( `/browser/init-script/${ this . id } ` ) ;
180+
181+ this . heartbeatRelativeUrl = `/browser/heartbeat/${ this . id } ` ;
182+ this . statusRelativeUrl = `/browser/status/${ this . id } ` ;
183+ this . statusDoneRelativeUrl = `/browser/status-done/${ this . id } ` ;
184+ this . activeWindowIdUrl = `/browser/active-window-id/${ this . id } ` ;
185+ this . closeWindowUrl = `/browser/close-window/${ this . id } ` ;
186+
187+ this . heartbeatUrl = proxy . resolveRelativeServiceUrl ( this . heartbeatRelativeUrl ) ;
188+ this . statusUrl = proxy . resolveRelativeServiceUrl ( this . statusRelativeUrl ) ;
189+ this . statusDoneUrl = proxy . resolveRelativeServiceUrl ( this . statusDoneRelativeUrl ) ;
190+
191+ }
192+
185193 public set messageBus ( messageBus : MessageBus ) {
186194 this . _messageBus = messageBus ;
187195 this . warningLog . callback = WarningLog . createAddWarningCallback ( this . _messageBus ) ;
@@ -278,8 +286,12 @@ export default class BrowserConnection extends EventEmitter {
278286 return this . hasQueuedJobs ? await this . currentJob . popNextTestRunUrl ( this ) : null ;
279287 }
280288
289+ public getCurrentTestRun ( ) : LegacyTestRun | TestRun | null {
290+ return this . currentJob ? this . currentJob . currentTestRun : null ;
291+ }
292+
281293 public static getById ( id : string ) : BrowserConnection | null {
282- return connections [ id ] || null ;
294+ return BrowserConnectionTracker . activeBrowserConnections [ id ] || null ;
283295 }
284296
285297 private async _restartBrowser ( ) : Promise < void > {
@@ -454,7 +466,7 @@ export default class BrowserConnection extends EventEmitter {
454466 if ( this . heartbeatTimeout )
455467 clearTimeout ( this . heartbeatTimeout ) ;
456468
457- delete connections [ this . id ] ;
469+ BrowserConnectionTracker . remove ( this ) ;
458470
459471 this . status = BrowserConnectionStatus . closed ;
460472 this . emit ( BrowserConnectionStatus . closed ) ;
0 commit comments