From efb6b1ba715e57158dc6c2a9bdf5c371e4148ff9 Mon Sep 17 00:00:00 2001 From: Sean Poulter Date: Tue, 27 Feb 2024 14:47:52 -0500 Subject: [PATCH] fix: Prevent "Connection closed" errors when deleting the session for @wdio/cucumber-framework (#116) * fix: Prevent "Connection closed" errors when deleting the session for @wdio/cucumber-framework * Update src/service.ts Co-authored-by: Christian Bromann * Add comment and guard for Cucumber --------- Co-authored-by: Christian Bromann --- src/service.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/service.ts b/src/service.ts index cf46d452..b6134a20 100644 --- a/src/service.ts +++ b/src/service.ts @@ -33,6 +33,8 @@ export default class VSCodeWorkerService implements Services.ServiceInstance { private _proxyOptions: VSCodeProxyOptions private _vscodeOptions: VSCodeOptions private _isWebSession = false + private _isCucumberSession = false + private _deletingSession = false constructor (_: never, private _capabilities: VSCodeCapabilities) { this._vscodeOptions = this._capabilities[VSCODE_CAPABILITY_KEY] || {} @@ -56,6 +58,14 @@ export default class VSCodeWorkerService implements Services.ServiceInstance { } private _handleSocketClose (code: number, reason: Buffer) { + /* + * Prevent this block from running when deleting a session using the Cucumber framework. + * Otherwise the specs fail with the "Connection closed" error. + */ + if (this._isCucumberSession && this._deletingSession) { + return + } + const msg = `Connection closed. Code: ${code}, reason: ${reason.toString()}` this._promisedSocket = Promise.reject(new Error(msg)) this._pendingMessages.forEach((resolver) => resolver(msg, null)) @@ -63,6 +73,7 @@ export default class VSCodeWorkerService implements Services.ServiceInstance { async beforeSession (option: Options.Testrunner, capabilities: VSCodeCapabilities) { this._isWebSession = capabilities.browserName !== 'vscode' + this._isCucumberSession = option.framework === 'cucumber' /** * only run setup for VSCode capabilities @@ -102,6 +113,7 @@ export default class VSCodeWorkerService implements Services.ServiceInstance { userSettings[SETTINGS_KEY].port = port log.info(`Start VSCode proxy server on port ${port}`) const wss = this._wss = new WebSocketServer({ port }) + this._deletingSession = false this._promisedSocket = new Promise((resolve, reject) => { const socketTimeout = setTimeout( () => reject(new Error('Connection timeout exceeded')), @@ -163,6 +175,12 @@ export default class VSCodeWorkerService implements Services.ServiceInstance { log.info(`Start VSCode: ${binary} ${args.join(' ')}`) } + beforeCommand (commandName: string): void { + if (commandName === 'deleteSession') { + this._deletingSession = true + } + } + async before (capabilities: VSCodeCapabilities, __: never, browser: WebdriverIO.Browser) { /** * only run setup for VSCode capabilities