From 83cb4ef56ab8f13be47526bc63e73cca2d04ee31 Mon Sep 17 00:00:00 2001 From: Wim Selles Date: Wed, 28 Oct 2020 17:56:08 +0100 Subject: [PATCH] fix: fix indentation --- src/browser-info.ts | 2 +- src/launcher/launcher.ts | 220 +++++++++++++++++++-------------------- src/reporter/reporter.ts | 6 +- 3 files changed, 114 insertions(+), 114 deletions(-) diff --git a/src/browser-info.ts b/src/browser-info.ts index fd7c2bb..0cc9d3f 100644 --- a/src/browser-info.ts +++ b/src/browser-info.ts @@ -1,7 +1,7 @@ import {SauceLabsOptions} from 'saucelabs' -import {BrowserObject} from "webdriverio"; type SauceBaseOption = Pick + interface Results { status: string; message: string; diff --git a/src/launcher/launcher.ts b/src/launcher/launcher.ts index c4a287f..127e746 100644 --- a/src/launcher/launcher.ts +++ b/src/launcher/launcher.ts @@ -16,114 +16,114 @@ export function SaucelabsLauncher(args, captureTimeoutLauncherDecorator, retryLauncherDecorator) { - // Apply base class mixins. This would be nice to have typed, but this is a low-priority now. - baseLauncherDecorator(this); - captureTimeoutLauncherDecorator(this); - retryLauncherDecorator(this); - - // initiate driver with null to not close the tunnel too early - connectedDrivers.set(this.id, null) - - const log = logger.create('SaucelabsLauncher'); - const { - startConnect, - sauceConnectOptions, - seleniumCapabilities, - browserName - } = processConfig(config, args); - - // Setup Browser name that will be printed out by Karma. - this.name = browserName + ' on SauceLabs'; - - // Listen for the start event from Karma. I know, the API is a bit different to how you - // would expect, but we need to follow this approach unless we want to spend more work - // improving type safety. - this.on('start', async (pageUrl: string) => { - if (startConnect) { - try { - // In case the "startConnect" option has been enabled, establish a tunnel and wait - // for it being ready. In case a tunnel is already active, this will just continue - // without establishing a new one. - await sauceConnect.establishTunnel(seleniumCapabilities, sauceConnectOptions); - } catch (error) { - log.error(error); - - this._done('failure'); - return; - } - } - - try { - // See the following link for public API of the selenium server. - // https://wiki.saucelabs.com/display/DOCS/Instant+Selenium+Node.js+Tests - const driver = await remote(seleniumCapabilities); - - // Keep track of all connected drivers because it's possible that there are multiple - // driver instances (e.g. when running with concurrency) - connectedDrivers.set(this.id, driver); - - const sessionId = driver.sessionId - - log.info('%s session at https://saucelabs.com/tests/%s', browserName, sessionId); - log.debug('Opening "%s" on the selenium client', pageUrl); - - // Store the information about the current session in the browserMap. This is necessary - // because otherwise the Saucelabs reporter is not able to report results. - browserMap.set(this.id, { - sessionId, - username: seleniumCapabilities.user, - accessKey: seleniumCapabilities.key, - region: seleniumCapabilities.region, - headless: seleniumCapabilities.headless, - results:[], - }); - - await driver.url(pageUrl); - } catch (e) { - log.error(e); - - // Notify karma about the failure. - this._done('failure'); - } - }); - - this.on('kill', async (done: () => void) => { - try { - const driver = connectedDrivers.get(this.id); - const {sessionId} = driver; - await driver.deleteSession(); - - const browserData = browserMap.get(this.id); - const api = new SauceLabs({ - user: browserData.username, - key: browserData.accessKey, - region: browserData.region, - }); - - // Wait until the vm is destroyed and the assets are stored - await new Promise(resolve => setTimeout(() => resolve(), 5000)); - // Create a tmp dir - mkdirSync(sessionId); - writeFileSync(`${sessionId}/log.json`, JSON.stringify(browserData.results, null, 2)); - // Update the log assets - // @ts-ignore - await api.uploadJobAssets( - sessionId, - { files: [`${sessionId}/log.json`] }, - ); - // remove the temporary folder - removeSync(sessionId); - - } catch (e) { - // We need to ignore the exception here because we want to make sure that Karma is still - // able to retry connecting if Saucelabs itself terminated the session (and not Karma) - // For example if the "idleTimeout" is exceeded and Saucelabs errored the session. See: - // https://wiki.saucelabs.com/display/DOCS/Test+Didn%27t+See+a+New+Command+for+90+Seconds - log.error('Could not quit the Saucelabs selenium connection. Failure message:'); - log.error(e); - } - - connectedDrivers.delete(this.id) - return process.nextTick(done); - }) + // Apply base class mixins. This would be nice to have typed, but this is a low-priority now. + baseLauncherDecorator(this); + captureTimeoutLauncherDecorator(this); + retryLauncherDecorator(this); + + // initiate driver with null to not close the tunnel too early + connectedDrivers.set(this.id, null) + + const log = logger.create('SaucelabsLauncher'); + const { + startConnect, + sauceConnectOptions, + seleniumCapabilities, + browserName + } = processConfig(config, args); + + // Setup Browser name that will be printed out by Karma. + this.name = browserName + ' on SauceLabs'; + + // Listen for the start event from Karma. I know, the API is a bit different to how you + // would expect, but we need to follow this approach unless we want to spend more work + // improving type safety. + this.on('start', async (pageUrl: string) => { + if (startConnect) { + try { + // In case the "startConnect" option has been enabled, establish a tunnel and wait + // for it being ready. In case a tunnel is already active, this will just continue + // without establishing a new one. + await sauceConnect.establishTunnel(seleniumCapabilities, sauceConnectOptions); + } catch (error) { + log.error(error); + + this._done('failure'); + return; + } + } + + try { + // See the following link for public API of the selenium server. + // https://wiki.saucelabs.com/display/DOCS/Instant+Selenium+Node.js+Tests + const driver = await remote(seleniumCapabilities); + + // Keep track of all connected drivers because it's possible that there are multiple + // driver instances (e.g. when running with concurrency) + connectedDrivers.set(this.id, driver); + + const sessionId = driver.sessionId + + log.info('%s session at https://saucelabs.com/tests/%s', browserName, sessionId); + log.debug('Opening "%s" on the selenium client', pageUrl); + + // Store the information about the current session in the browserMap. This is necessary + // because otherwise the Saucelabs reporter is not able to report results. + browserMap.set(this.id, { + sessionId, + username: seleniumCapabilities.user, + accessKey: seleniumCapabilities.key, + region: seleniumCapabilities.region, + headless: seleniumCapabilities.headless, + results: [], + }); + + await driver.url(pageUrl); + } catch (e) { + log.error(e); + + // Notify karma about the failure. + this._done('failure'); + } + }); + + this.on('kill', async (done: () => void) => { + try { + const driver = connectedDrivers.get(this.id); + const {sessionId} = driver; + await driver.deleteSession(); + + const browserData = browserMap.get(this.id); + const api = new SauceLabs({ + user: browserData.username, + key: browserData.accessKey, + region: browserData.region, + }); + + // Wait until the vm is destroyed and the assets are stored + await new Promise(resolve => setTimeout(() => resolve(), 5000)); + // Create a tmp dir + mkdirSync(sessionId); + writeFileSync(`${sessionId}/log.json`, JSON.stringify(browserData.results, null, 2)); + // Update the log assets + // @ts-ignore + await api.uploadJobAssets( + sessionId, + {files: [`${sessionId}/log.json`]}, + ); + // remove the temporary folder + removeSync(sessionId); + + } catch (e) { + // We need to ignore the exception here because we want to make sure that Karma is still + // able to retry connecting if Saucelabs itself terminated the session (and not Karma) + // For example if the "idleTimeout" is exceeded and Saucelabs errored the session. See: + // https://wiki.saucelabs.com/display/DOCS/Test+Didn%27t+See+a+New+Command+for+90+Seconds + log.error('Could not quit the Saucelabs selenium connection. Failure message:'); + log.error(e); + } + + connectedDrivers.delete(this.id) + return process.nextTick(done); + }) } diff --git a/src/reporter/reporter.ts b/src/reporter/reporter.ts index a5bcae7..9ead9f4 100644 --- a/src/reporter/reporter.ts +++ b/src/reporter/reporter.ts @@ -10,7 +10,7 @@ const REGION_MAPPING = { * Get the Sauce Labs endpoint * @param region */ -function getSauceEndpoint (region) { +function getSauceEndpoint(region) { const shortRegion = REGION_MAPPING[region] ? region : 'us' return `https://app.${REGION_MAPPING[shortRegion]}saucelabs.com/tests/` @@ -26,7 +26,7 @@ export function SaucelabsReporter(logger, browserMap: BrowserMap) { // This fires when a single test is executed and will update the run in sauce labs with an annotation // of the test including the status of the test - this.onSpecComplete = function(browser, result) { + this.onSpecComplete = function (browser, result) { const status = result.success ? '✅' : '❌' browserMap.get(browser.id).results.push({ @@ -35,7 +35,7 @@ export function SaucelabsReporter(logger, browserMap: BrowserMap) { screenshot: null }) - if(!result.success && result.log.length > 0){ + if (!result.success && result.log.length > 0) { browserMap.get(browser.id).results.push({ status: 'info', message: `${result.log[0]}`,