diff --git a/packages/server/lib/browsers/chrome.ts b/packages/server/lib/browsers/chrome.ts index 2dd14e614d22..8b89c2b5c045 100644 --- a/packages/server/lib/browsers/chrome.ts +++ b/packages/server/lib/browsers/chrome.ts @@ -258,7 +258,16 @@ const _connectToChromeRemoteInterface = function (port, onError, browserDisplayN .then((wsUrl) => { debug('received wsUrl %s for port %d', wsUrl, port) - return CriClient.create(wsUrl, onError) + return CriClient.create({ + target: wsUrl, + onError, + onReconnect (client) { + // if the client disconnects (e.g. due to a computer sleeping), update + // the frame tree on reconnect in cases there were changes while + // the client was disconnected + _updateFrameTree(client)() + }, + }) }) } diff --git a/packages/server/lib/browsers/cri-client.ts b/packages/server/lib/browsers/cri-client.ts index 5ebbad6bcbf5..a9901d2b6c45 100644 --- a/packages/server/lib/browsers/cri-client.ts +++ b/packages/server/lib/browsers/cri-client.ts @@ -139,7 +139,14 @@ export { chromeRemoteInterface } type DeferredPromise = { resolve: Function, reject: Function } -export const create = async (target: websocketUrl, onAsynchronousError: Function): Promise => { +interface CriClientOptions { + target: websocketUrl + onError: Function + onReconnect?: (client: CRIWrapper) => void +} + +export const create = async (options: CriClientOptions): Promise => { + const { target, onError, onReconnect } = options const subscriptions: {eventName: CRI.EventName, cb: Function}[] = [] const enableCommands: CRI.Command[] = [] let enqueuedCommands: {command: CRI.Command, params: any, p: DeferredPromise }[] = [] @@ -180,8 +187,12 @@ export const create = async (target: websocketUrl, onAsynchronousError: Function }) enqueuedCommands = [] + + if (onReconnect) { + onReconnect(client) + } } catch (err) { - onAsynchronousError(errors.get('CDP_COULD_NOT_RECONNECT', err)) + onError(errors.get('CDP_COULD_NOT_RECONNECT', err)) } } @@ -285,9 +296,6 @@ export const create = async (target: websocketUrl, onAsynchronousError: Function return cri.close() }, - - // @ts-ignore - reconnect, } return client diff --git a/packages/server/lib/browsers/firefox-util.ts b/packages/server/lib/browsers/firefox-util.ts index 832355cdd1b0..43a22fe97f26 100644 --- a/packages/server/lib/browsers/firefox-util.ts +++ b/packages/server/lib/browsers/firefox-util.ts @@ -103,7 +103,10 @@ const attachToTabMemory = Bluebird.method((tab) => { async function setupRemote (remotePort, automation, options) { const wsUrl = await protocol.getWsTargetFor(remotePort, 'Firefox') - const criClient = await CriClient.create(wsUrl, options.onError) + const criClient = await CriClient.create({ + target: wsUrl, + onError: options.onError, + }) const cdpAutomation = new CdpAutomation({ sendDebuggerCommandFn: criClient.send, onFn: criClient.on, diff --git a/packages/server/test/unit/browsers/cri-client_spec.ts b/packages/server/test/unit/browsers/cri-client_spec.ts index 597fa1463a4a..e8dae3f359ed 100644 --- a/packages/server/test/unit/browsers/cri-client_spec.ts +++ b/packages/server/test/unit/browsers/cri-client_spec.ts @@ -40,7 +40,12 @@ describe('lib/browsers/cri-client', function () { 'chrome-remote-interface': criImport, }) - getClient = () => criClient.create(DEBUGGER_URL, onError) + getClient = () => { + return criClient.create({ + target: DEBUGGER_URL, + onError, + }) + } }) context('.create', function () {