diff --git a/src/browser-installer/chrome/index.ts b/src/browser-installer/chrome/index.ts index ee8089d63..5ec7456c0 100644 --- a/src/browser-installer/chrome/index.ts +++ b/src/browser-installer/chrome/index.ts @@ -6,7 +6,7 @@ import { DRIVER_WAIT_TIMEOUT } from "../constants"; import { getMilestone } from "../utils"; import { installChrome } from "./browser"; import { installChromeDriver } from "./driver"; -import { isUbuntu, getUbuntuLinkerEnv, installUbuntuPackageDependencies } from "../ubuntu-packages"; +import { isUbuntu, getUbuntuLinkerEnv } from "../ubuntu-packages"; export { installChrome, installChromeDriver }; @@ -14,33 +14,22 @@ export const runChromeDriver = async ( chromeVersion: string, { debug = false } = {}, ): Promise<{ gridUrl: string; process: ChildProcess; port: number }> => { - const shouldInstallUbuntuPackageDependencies = await isUbuntu(); - - const [chromeDriverPath] = await Promise.all([ + const [chromeDriverPath, randomPort, chromeDriverEnv] = await Promise.all([ installChromeDriver(chromeVersion), - installChrome(chromeVersion), - shouldInstallUbuntuPackageDependencies ? installUbuntuPackageDependencies() : null, + getPort(), + isUbuntu() + .then(isUbuntu => (isUbuntu ? getUbuntuLinkerEnv() : null)) + .then(extraEnv => (extraEnv ? { ...process.env, ...extraEnv } : process.env)), ]); - const milestone = getMilestone(chromeVersion); - const randomPort = await getPort(); - const extraSpawnOpts = shouldInstallUbuntuPackageDependencies - ? { - env: { - ...process.env, - ...(await getUbuntuLinkerEnv()), - }, - } - : {}; - const chromeDriver = spawn(chromeDriverPath, [`--port=${randomPort}`, debug ? `--verbose` : "--silent"], { windowsHide: true, detached: false, - ...extraSpawnOpts, + env: chromeDriverEnv, }); if (debug) { - pipeLogsWithPrefix(chromeDriver, `[chromedriver@${milestone}] `); + pipeLogsWithPrefix(chromeDriver, `[chromedriver@${getMilestone(chromeVersion)}] `); } const gridUrl = `http://127.0.0.1:${randomPort}`; diff --git a/src/browser-installer/edge/index.ts b/src/browser-installer/edge/index.ts index 1aefbe84e..5860f5b2b 100644 --- a/src/browser-installer/edge/index.ts +++ b/src/browser-installer/edge/index.ts @@ -11,8 +11,7 @@ export const runEdgeDriver = async ( edgeVersion: string, { debug = false }: { debug?: boolean } = {}, ): Promise<{ gridUrl: string; process: ChildProcess; port: number }> => { - const edgeDriverPath = await installEdgeDriver(edgeVersion); - const randomPort = await getPort(); + const [edgeDriverPath, randomPort] = await Promise.all([installEdgeDriver(edgeVersion), getPort()]); const edgeDriver = spawn(edgeDriverPath, [`--port=${randomPort}`, debug ? `--verbose` : "--silent"], { windowsHide: true, diff --git a/src/browser-installer/firefox/index.ts b/src/browser-installer/firefox/index.ts index 06a5b33cc..682ec2373 100644 --- a/src/browser-installer/firefox/index.ts +++ b/src/browser-installer/firefox/index.ts @@ -6,7 +6,7 @@ import { installFirefox } from "./browser"; import { installLatestGeckoDriver } from "./driver"; import { pipeLogsWithPrefix } from "../../dev-server/utils"; import { DRIVER_WAIT_TIMEOUT } from "../constants"; -import { getUbuntuLinkerEnv, installUbuntuPackageDependencies, isUbuntu } from "../ubuntu-packages"; +import { getUbuntuLinkerEnv, isUbuntu } from "../ubuntu-packages"; export { installFirefox, installLatestGeckoDriver }; @@ -14,24 +14,14 @@ export const runGeckoDriver = async ( firefoxVersion: string, { debug = false } = {}, ): Promise<{ gridUrl: string; process: ChildProcess; port: number }> => { - const shouldInstallUbuntuPackageDependencies = await isUbuntu(); - - const [geckoDriverPath] = await Promise.all([ + const [geckoDriverPath, randomPort, geckoDriverEnv] = await Promise.all([ installLatestGeckoDriver(firefoxVersion), - installFirefox(firefoxVersion), - shouldInstallUbuntuPackageDependencies ? installUbuntuPackageDependencies() : null, + getPort(), + isUbuntu() + .then(isUbuntu => (isUbuntu ? getUbuntuLinkerEnv() : null)) + .then(extraEnv => (extraEnv ? { ...process.env, ...extraEnv } : process.env)), ]); - const randomPort = await getPort(); - const extraSpawnOpts = shouldInstallUbuntuPackageDependencies - ? { - env: { - ...process.env, - ...(await getUbuntuLinkerEnv()), - }, - } - : {}; - const geckoDriver = await startGeckoDriver({ customGeckoDriverPath: geckoDriverPath, port: randomPort, @@ -39,7 +29,7 @@ export const runGeckoDriver = async ( spawnOpts: { windowsHide: true, detached: false, - ...extraSpawnOpts, + env: geckoDriverEnv, }, }); diff --git a/src/browser-installer/index.ts b/src/browser-installer/index.ts index 939b0976e..e3b21c33f 100644 --- a/src/browser-installer/index.ts +++ b/src/browser-installer/index.ts @@ -1,4 +1,4 @@ export { installBrowser, installBrowsersWithDrivers, BrowserInstallStatus } from "./install"; export { runBrowserDriver } from "./run"; -export { getDriverNameForBrowserName } from "./utils"; +export { getNormalizedBrowserName } from "./utils"; export type { SupportedBrowser, SupportedDriver } from "./utils"; diff --git a/src/browser-installer/install.ts b/src/browser-installer/install.ts index ae9f88543..0e1a97261 100644 --- a/src/browser-installer/install.ts +++ b/src/browser-installer/install.ts @@ -1,24 +1,14 @@ import _ from "lodash"; +import { Browser, getNormalizedBrowserName, type SupportedBrowser } from "./utils"; /** - * @returns path to browser binary + * @returns path to installed browser binary */ export const installBrowser = async ( - browserName?: string, + browserName: SupportedBrowser, browserVersion?: string, - { force = false, shouldInstallWebDriver = false, shouldInstallUbuntuPackages = true } = {}, + { force = false, shouldInstallWebDriver = false, shouldInstallUbuntuPackages = false } = {}, ): Promise => { - const unsupportedBrowserError = new Error( - [ - `Couldn't install browser '${browserName}', as it is not supported`, - `Currently supported for installation browsers: 'chrome', 'firefox`, - ].join("\n"), - ); - - if (!browserName) { - throw unsupportedBrowserError; - } - if (!browserVersion) { throw new Error( `Couldn't install browser '${browserName}' because it has invalid version: '${browserVersion}'`, @@ -29,35 +19,46 @@ export const installBrowser = async ( const needToInstallUbuntuPackages = shouldInstallUbuntuPackages && (await isUbuntu()); - if (/chrome/i.test(browserName)) { - const { installChrome, installChromeDriver } = await import("./chrome"); - - return await Promise.all([ - installChrome(browserVersion, { force }), - shouldInstallWebDriver && installChromeDriver(browserVersion, { force }), - needToInstallUbuntuPackages && installUbuntuPackageDependencies(), - ]).then(result => result[0]); - } else if (/firefox/i.test(browserName)) { - const { installFirefox, installLatestGeckoDriver } = await import("./firefox"); - - return await Promise.all([ - installFirefox(browserVersion, { force }), - shouldInstallWebDriver && installLatestGeckoDriver(browserVersion, { force }), - needToInstallUbuntuPackages && installUbuntuPackageDependencies(), - ]).then(result => result[0]); - } else if (/edge/i.test(browserName)) { - const { installEdgeDriver } = await import("./edge"); - - if (shouldInstallWebDriver) { - await installEdgeDriver(browserVersion, { force }); + switch (browserName) { + case Browser.CHROME: + case Browser.CHROMIUM: { + const { installChrome, installChromeDriver } = await import("./chrome"); + + const [browserPath] = await Promise.all([ + installChrome(browserVersion, { force }), + shouldInstallWebDriver && installChromeDriver(browserVersion, { force }), + needToInstallUbuntuPackages && installUbuntuPackageDependencies(), + ]); + + return browserPath; } - return null; - } else if (/safari/i.test(browserName)) { - return null; - } + case Browser.FIREFOX: { + const { installFirefox, installLatestGeckoDriver } = await import("./firefox"); + + const [browserPath] = await Promise.all([ + installFirefox(browserVersion, { force }), + shouldInstallWebDriver && installLatestGeckoDriver(browserVersion, { force }), + needToInstallUbuntuPackages && installUbuntuPackageDependencies(), + ]); + + return browserPath; + } - throw unsupportedBrowserError; + case Browser.EDGE: { + const { installEdgeDriver } = await import("./edge"); + + if (shouldInstallWebDriver) { + await installEdgeDriver(browserVersion, { force }); + } + + return null; + } + + case Browser.SAFARI: { + return null; + } + } }; export const BrowserInstallStatus = { @@ -81,7 +82,16 @@ const forceInstallBinaries = async ( browserName?: string, browserVersion?: string, ): ForceInstallBinaryResult => { - return installFn(browserName, browserVersion, { force: true, shouldInstallWebDriver: true }) + const normalizedBrowserName = getNormalizedBrowserName(browserName); + + if (!normalizedBrowserName) { + return { + status: BrowserInstallStatus.Error, + reason: `Installing ${browserName} is unsupported. Supported browsers: "chrome", "firefox", "safari", "edge"`, + }; + } + + return installFn(normalizedBrowserName, browserVersion, { force: true, shouldInstallWebDriver: true }) .then(successResult => { return successResult ? { status: BrowserInstallStatus.Ok } diff --git a/src/browser-installer/run.ts b/src/browser-installer/run.ts index 9e3adc45c..c2dde8ebb 100644 --- a/src/browser-installer/run.ts +++ b/src/browser-installer/run.ts @@ -1,21 +1,27 @@ import type { ChildProcess } from "child_process"; -import { Driver, type SupportedDriver } from "./utils"; +import { installBrowser } from "./install"; +import { Browser, type SupportedBrowser } from "./utils"; export const runBrowserDriver = async ( - driverName: SupportedDriver, + browserName: SupportedBrowser, browserVersion: string, { debug = false } = {}, ): Promise<{ gridUrl: string; process: ChildProcess; port: number }> => { - switch (driverName) { - case Driver.CHROMEDRIVER: + const installBrowserOpts = { shouldInstallWebDriver: true, shouldInstallUbuntuPackages: true }; + + await installBrowser(browserName, browserVersion, installBrowserOpts); + + switch (browserName) { + case Browser.CHROME: + case Browser.CHROMIUM: return import("./chrome").then(module => module.runChromeDriver(browserVersion, { debug })); - case Driver.EDGEDRIVER: - return import("./edge").then(module => module.runEdgeDriver(browserVersion, { debug })); - case Driver.GECKODRIVER: + case Browser.FIREFOX: return import("./firefox").then(module => module.runGeckoDriver(browserVersion, { debug })); - case Driver.SAFARIDRIVER: + case Browser.EDGE: + return import("./edge").then(module => module.runEdgeDriver(browserVersion, { debug })); + case Browser.SAFARI: return import("./safari").then(module => module.runSafariDriver({ debug })); default: - throw new Error(`Invalid driver name: ${driverName}. Expected one of: ${Object.values(Driver).join(", ")}`); + throw new Error(`Invalid browser: ${browserName}. Expected one of: ${Object.values(Browser).join(", ")}`); } }; diff --git a/src/browser-installer/ubuntu-packages/collect-dependencies/browser-versions/index.ts b/src/browser-installer/ubuntu-packages/collect-dependencies/browser-versions/index.ts index 6cccca3f5..43360df23 100644 --- a/src/browser-installer/ubuntu-packages/collect-dependencies/browser-versions/index.ts +++ b/src/browser-installer/ubuntu-packages/collect-dependencies/browser-versions/index.ts @@ -2,15 +2,16 @@ import { fetchChromiumMilestoneVersions } from "./chromium"; import { fetchChromeMilestoneVersions } from "./chrome"; import { fetchFirefoxMilestoneVersions } from "./firefox"; import type { BrowserWithVersion } from "../utils"; +import { Browser, type SupportedBrowser } from "../../../utils"; export const fetchBrowsersMilestones = async (): Promise => { - const createMapToBrowser = (browserName: string) => (data: string[]) => + const createMapToBrowser = (browserName: SupportedBrowser) => (data: string[]) => data.map(browserVersion => ({ browserName, browserVersion })); const [chromiumVersions, chromeVersions, firefoxVersions] = await Promise.all([ - fetchChromiumMilestoneVersions().then(createMapToBrowser("chrome")), - fetchChromeMilestoneVersions().then(createMapToBrowser("chrome")), - fetchFirefoxMilestoneVersions().then(createMapToBrowser("firefox")), + fetchChromiumMilestoneVersions().then(createMapToBrowser(Browser.CHROME)), + fetchChromeMilestoneVersions().then(createMapToBrowser(Browser.CHROME)), + fetchFirefoxMilestoneVersions().then(createMapToBrowser(Browser.FIREFOX)), ]); return [...chromiumVersions, ...chromeVersions, ...firefoxVersions]; diff --git a/src/browser-installer/ubuntu-packages/collect-dependencies/utils.ts b/src/browser-installer/ubuntu-packages/collect-dependencies/utils.ts index ee3985fa1..52fefd993 100644 --- a/src/browser-installer/ubuntu-packages/collect-dependencies/utils.ts +++ b/src/browser-installer/ubuntu-packages/collect-dependencies/utils.ts @@ -1,4 +1,6 @@ -export type BrowserWithVersion = { browserName: string; browserVersion: string }; +import type { SupportedBrowser } from "../../utils"; + +export type BrowserWithVersion = { browserName: SupportedBrowser; browserVersion: string }; export const getCliArgs = >(flags?: T): string[] => { if (!flags) { diff --git a/src/browser-installer/utils.ts b/src/browser-installer/utils.ts index fbd41a03e..e6a0a20d9 100644 --- a/src/browser-installer/utils.ts +++ b/src/browser-installer/utils.ts @@ -29,21 +29,27 @@ export const Driver = { export type SupportedBrowser = (typeof Browser)[keyof typeof Browser]; export type SupportedDriver = (typeof Driver)[keyof typeof Driver]; -export const getDriverNameForBrowserName = (browserName: SupportedBrowser): SupportedDriver | null => { - if (browserName === Browser.CHROME || browserName === Browser.CHROMIUM) { - return Driver.CHROMEDRIVER; +export const getNormalizedBrowserName = ( + browserName?: string, +): Exclude | null => { + if (!browserName) { + return null; } - if (browserName === Browser.FIREFOX) { - return Driver.GECKODRIVER; + if (/chrome/i.test(browserName)) { + return Browser.CHROME; } - if (browserName === Browser.SAFARI) { - return Driver.SAFARIDRIVER; + if (/firefox/i.test(browserName)) { + return Browser.FIREFOX; } - if (browserName === Browser.EDGE) { - return Driver.EDGEDRIVER; + if (/edge/i.test(browserName)) { + return Browser.EDGE; + } + + if (/safari/i.test(browserName)) { + return Browser.SAFARI; } return null; diff --git a/src/browser-pool/webdriver-pool.ts b/src/browser-pool/webdriver-pool.ts index 715acb6b3..e102f49c1 100644 --- a/src/browser-pool/webdriver-pool.ts +++ b/src/browser-pool/webdriver-pool.ts @@ -1,14 +1,14 @@ import type { ChildProcess } from "child_process"; -import { runBrowserDriver, getDriverNameForBrowserName } from "../browser-installer"; -import type { SupportedBrowser, SupportedDriver } from "../browser-installer"; +import { runBrowserDriver, getNormalizedBrowserName } from "../browser-installer"; +import type { SupportedBrowser } from "../browser-installer"; -type DriverVersion = string; +type BrowserVersion = string; type Port = string; type ChildProcessWithStatus = { process: ChildProcess; gridUrl: string; isBusy: boolean }; export type WdProcess = { gridUrl: string; free: () => void; kill: () => void }; export class WebdriverPool { - private driverProcess: Map>>; + private driverProcess: Map>>; private portToDriverProcess: Map; constructor() { @@ -17,13 +17,13 @@ export class WebdriverPool { } async getWebdriver( - browserName: SupportedBrowser, - browserVersion: string, + browserName?: string, + browserVersion?: string, { debug = false } = {}, ): ReturnType { - const driverName = getDriverNameForBrowserName(browserName); + const browserNameNormalized = getNormalizedBrowserName(browserName); - if (!driverName) { + if (!browserNameNormalized) { throw new Error( [ `Couldn't run browser driver for "${browserName}", as this browser is not supported`, @@ -36,7 +36,7 @@ export class WebdriverPool { throw new Error(`Couldn't run browser driver for "${browserName}" because its version is undefined`); } - const wdProcesses = this.driverProcess.get(driverName)?.get(browserVersion) ?? {}; + const wdProcesses = this.driverProcess.get(browserNameNormalized)?.get(browserVersion) ?? {}; for (const port in wdProcesses) { if (!wdProcesses[port].isBusy) { @@ -45,12 +45,12 @@ export class WebdriverPool { return { gridUrl: wdProcesses[port].gridUrl, free: () => this.freeWebdriver(port), - kill: () => this.killWebdriver(driverName, browserVersion, port), + kill: () => this.killWebdriver(browserNameNormalized, browserVersion, port), }; } } - return this.createWebdriverProcess(driverName, browserVersion, { debug }); + return this.createWebdriverProcess(browserNameNormalized, browserVersion, { debug }); } private freeWebdriver(port: Port): void { @@ -61,9 +61,9 @@ export class WebdriverPool { } } - private killWebdriver(driverName: SupportedDriver, browserVersion: string, port: Port): void { + private killWebdriver(browserName: SupportedBrowser, browserVersion: string, port: Port): void { const wdProcess = this.portToDriverProcess.get(port); - const nodes = this.driverProcess.get(driverName)?.get(browserVersion); + const nodes = this.driverProcess.get(browserName)?.get(browserVersion); if (wdProcess && nodes) { wdProcess.process.kill(); @@ -73,21 +73,21 @@ export class WebdriverPool { } private async createWebdriverProcess( - driverName: SupportedDriver, + browserName: SupportedBrowser, browserVersion: string, { debug = false } = {}, ): Promise { - const driver = await runBrowserDriver(driverName, browserVersion, { debug }); + const driver = await runBrowserDriver(browserName, browserVersion, { debug }); - if (!this.driverProcess.has(driverName)) { - this.driverProcess.set(driverName, new Map()); + if (!this.driverProcess.has(browserName)) { + this.driverProcess.set(browserName, new Map()); } - if (!this.driverProcess.get(driverName)?.has(browserVersion)) { - this.driverProcess.get(driverName)?.set(browserVersion, {}); + if (!this.driverProcess.get(browserName)?.has(browserVersion)) { + this.driverProcess.get(browserName)?.set(browserVersion, {}); } - const nodes = this.driverProcess.get(driverName)?.get(browserVersion) as Record; + const nodes = this.driverProcess.get(browserName)?.get(browserVersion) as Record; const node = { process: driver.process, gridUrl: driver.gridUrl, isBusy: true }; nodes[driver.port] = node; @@ -97,7 +97,7 @@ export class WebdriverPool { return { gridUrl: driver.gridUrl, free: () => this.freeWebdriver(String(driver.port)), - kill: () => this.killWebdriver(driverName, browserVersion, String(driver.port)), + kill: () => this.killWebdriver(browserName, browserVersion, String(driver.port)), }; } } diff --git a/src/browser/new-browser.ts b/src/browser/new-browser.ts index 58f27c8ff..44b47a330 100644 --- a/src/browser/new-browser.ts +++ b/src/browser/new-browser.ts @@ -13,7 +13,6 @@ import { DEVTOOLS_PROTOCOL, WEBDRIVER_PROTOCOL, LOCAL_GRID_URL } from "../consta import { Config } from "../config"; import { BrowserConfig } from "../config/browser-config"; import { gridUrl as DEFAULT_GRID_URL } from "../config/defaults"; -import { installBrowser, type SupportedBrowser } from "../browser-installer"; export type CapabilityName = "goog:chromeOptions" | "moz:firefoxOptions" | "ms:edgeOptions"; export type HeadlessBrowserOptions = Record< @@ -214,8 +213,8 @@ export class NewBrowser extends Browser { } this._wdProcess = await this._wdPool.getWebdriver( - this._config.desiredCapabilities?.browserName as SupportedBrowser, - this._config.desiredCapabilities?.browserVersion as string, + this._config.desiredCapabilities?.browserName, + this._config.desiredCapabilities?.browserVersion, { debug: this._config.system.debug }, ); @@ -226,13 +225,26 @@ export class NewBrowser extends Browser { config: BrowserConfig, capabilities: WebdriverIO.Capabilities, ): Promise { - const browserNameLowerCase = config.desiredCapabilities?.browserName?.toLowerCase() as string; + const { getNormalizedBrowserName, installBrowser } = await import("../browser-installer"); + const normalizedBrowserName = getNormalizedBrowserName(this._config.desiredCapabilities?.browserName); + + if (!normalizedBrowserName) { + throw new Error( + [ + `Running auto local "${this._config.desiredCapabilities?.browserName}" is unsupported`, + `Supported browsers: "chrome", "firefox", "safari", "edge"`, + ].join("\n"), + ); + } + const executablePath = await installBrowser( - this._config.desiredCapabilities?.browserName as SupportedBrowser, - this._config.desiredCapabilities?.browserVersion as string, + normalizedBrowserName, + this._config.desiredCapabilities?.browserVersion, + { shouldInstallWebDriver: false, shouldInstallUbuntuPackages: true }, ); if (executablePath) { + const browserNameLowerCase = config.desiredCapabilities?.browserName?.toLowerCase() as string; const { capabilityName } = headlessBrowserOptions[browserNameLowerCase]; capabilities[capabilityName] ||= {}; capabilities[capabilityName]!.binary ||= executablePath; diff --git a/test/src/browser-installer/chrome/index.ts b/test/src/browser-installer/chrome/index.ts index c61bf55c8..3244f2edb 100644 --- a/test/src/browser-installer/chrome/index.ts +++ b/test/src/browser-installer/chrome/index.ts @@ -107,14 +107,6 @@ describe("browser-installer/chrome", () => { assert.notCalled(installUbuntuPackageDependenciesStub); }); - it(`should try to install ubuntu packages if its ubuntu`, async () => { - isUbuntuStub.resolves(true); - - await runChromeDriver("130"); - - assert.calledOnce(installUbuntuPackageDependenciesStub); - }); - it(`should not set ubuntu linker env variables if its not ubuntu`, async () => { installChromeDriverStub.resolves("/driver/path"); getPortStub.resolves(10050); @@ -126,6 +118,7 @@ describe("browser-installer/chrome", () => { assert.calledOnceWith(spawnStub, sinon.match.string, sinon.match.array, { windowsHide: true, detached: false, + env: process.env, }); }); diff --git a/test/src/browser-installer/firefox/index.ts b/test/src/browser-installer/firefox/index.ts index ce5a71269..d48179d12 100644 --- a/test/src/browser-installer/firefox/index.ts +++ b/test/src/browser-installer/firefox/index.ts @@ -62,6 +62,7 @@ describe("browser-installer/firefox", () => { spawnOpts: { windowsHide: true, detached: false, + env: process.env, }, }); }); @@ -99,7 +100,7 @@ describe("browser-installer/firefox", () => { customGeckoDriverPath: "/driver/path", port: 12345, log: "debug", - spawnOpts: { windowsHide: true, detached: false }, + spawnOpts: { windowsHide: true, detached: false, env: process.env }, }); assert.calledOnceWith(pipeLogsWithPrefixStub, result.process, "[geckodriver@130] "); }); @@ -119,14 +120,6 @@ describe("browser-installer/firefox", () => { assert.notCalled(installUbuntuPackageDependenciesStub); }); - it(`should try to install ubuntu packages if its ubuntu`, async () => { - isUbuntuStub.resolves(true); - - await runGeckoDriver("130"); - - assert.calledOnce(installUbuntuPackageDependenciesStub); - }); - it(`should not set ubuntu linker env variables if its not ubuntu`, async () => { installLatestGeckoDriverStub.resolves("/driver/path"); getPortStub.resolves(10050); @@ -142,6 +135,7 @@ describe("browser-installer/firefox", () => { spawnOpts: { windowsHide: true, detached: false, + env: process.env, }, }); }); diff --git a/test/src/browser-installer/install.ts b/test/src/browser-installer/install.ts index 1bc00052b..9dd3177bc 100644 --- a/test/src/browser-installer/install.ts +++ b/test/src/browser-installer/install.ts @@ -4,6 +4,7 @@ import type { installBrowser as InstallBrowser, installBrowsersWithDrivers as InstallBrowsersWithDrivers, } from "../../../src/browser-installer/install"; +import { Browser } from "../../../src/browser-installer/utils"; describe("browser-installer/install", () => { const sandbox = sinon.createSandbox(); @@ -53,7 +54,7 @@ describe("browser-installer/install", () => { it("should install browser", async () => { installChromeStub.withArgs("115").resolves("/browser/path"); - const binaryPath = await installBrowser("chrome", "115", { force }); + const binaryPath = await installBrowser(Browser.CHROME, "115", { force }); assert.equal(binaryPath, "/browser/path"); assert.calledOnceWith(installChromeStub, "115", { force }); @@ -63,7 +64,7 @@ describe("browser-installer/install", () => { it("should install browser with webdriver", async () => { installChromeStub.withArgs("115").resolves("/browser/path"); - const binaryPath = await installBrowser("chrome", "115", { + const binaryPath = await installBrowser(Browser.CHROME, "115", { force, shouldInstallWebDriver: true, }); @@ -78,7 +79,7 @@ describe("browser-installer/install", () => { it("should install browser", async () => { installFirefoxStub.withArgs("115").resolves("/browser/path"); - const binaryPath = await installBrowser("firefox", "115", { force }); + const binaryPath = await installBrowser(Browser.FIREFOX, "115", { force }); assert.equal(binaryPath, "/browser/path"); assert.calledOnceWith(installFirefoxStub, "115", { force }); @@ -88,7 +89,7 @@ describe("browser-installer/install", () => { it("should install browser with webdriver", async () => { installFirefoxStub.withArgs("115").resolves("/browser/path"); - const binaryPath = await installBrowser("firefox", "115", { + const binaryPath = await installBrowser(Browser.FIREFOX, "115", { force, shouldInstallWebDriver: true, }); @@ -129,27 +130,20 @@ describe("browser-installer/install", () => { }); }); - it("should throw exception on unsupported browser name", async () => { - await assert.isRejected( - installBrowser("foobar", "115", { force }), - /Couldn't install browser 'foobar', as it is not supported/, - ); - }); - it("should throw exception on empty browser version", async () => { await assert.isRejected( - installBrowser("chrome", "", { force }), + installBrowser(Browser.CHROME, "", { force }), /Couldn't install browser 'chrome' because it has invalid version: ''/, ); }); }); }); - ["chrome", "firefox"].forEach(browser => { - it(`should not install ubuntu dependencies if flag is unset for ${browser}`, async () => { + [Browser.CHROME, Browser.FIREFOX].forEach(browser => { + it(`should not install ubuntu dependencies by default for ${browser}`, async () => { isUbuntuStub.resolves(true); - await installBrowser(browser, "115", { shouldInstallUbuntuPackages: false }); + await installBrowser(browser, "115"); assert.notCalled(installUbuntuPackageDependenciesStub); }); @@ -157,15 +151,15 @@ describe("browser-installer/install", () => { it(`should not install ubuntu dependencies if its not ubuntu for ${browser}`, async () => { isUbuntuStub.resolves(false); - await installBrowser(browser, "115"); + await installBrowser(browser, "115", { shouldInstallUbuntuPackages: true }); assert.notCalled(installUbuntuPackageDependenciesStub); }); - it(`should install ubuntu dependencies by default if its ubuntu for ${browser}`, async () => { + it(`should install ubuntu dependencies if its ubuntu for ${browser}`, async () => { isUbuntuStub.resolves(true); - await installBrowser(browser, "115"); + await installBrowser(browser, "115", { shouldInstallUbuntuPackages: true }); assert.calledOnce(installUbuntuPackageDependenciesStub); }); diff --git a/test/src/browser-installer/run.ts b/test/src/browser-installer/run.ts index 9f61eac71..7f93547f6 100644 --- a/test/src/browser-installer/run.ts +++ b/test/src/browser-installer/run.ts @@ -1,18 +1,22 @@ import proxyquire from "proxyquire"; import sinon, { type SinonStub } from "sinon"; import type { runBrowserDriver as RunBrowserDriver } from "../../../src/browser-installer/run"; -import { Driver } from "../../../src/browser-installer/utils"; +import { Browser } from "../../../src/browser-installer/utils"; describe("browser-installer/run", () => { const sandbox = sinon.createSandbox(); let runBrowserDriver: typeof RunBrowserDriver; + + let installBrowserStub: SinonStub; let runChromeDriverStub: SinonStub; beforeEach(() => { + installBrowserStub = sandbox.stub(); runChromeDriverStub = sandbox.stub(); runBrowserDriver = proxyquire.noCallThru()("../../../src/browser-installer/run", { + "./install": { installBrowser: installBrowserStub }, "./chrome": { runChromeDriver: runChromeDriverStub }, }).runBrowserDriver; }); @@ -21,9 +25,20 @@ describe("browser-installer/run", () => { [true, false, undefined].forEach(debug => { it(`should run chrome driver with debug: ${debug}`, async () => { - await runBrowserDriver(Driver.CHROMEDRIVER, "some-version", { debug }); + await runBrowserDriver(Browser.CHROME, "some-version", { debug }); assert.calledOnceWith(runChromeDriverStub, "some-version", { debug: Boolean(debug) }); }); }); + + [Browser.CHROME, Browser.EDGE, Browser.FIREFOX].forEach(browser => { + it(`should try to install ${browser} before running its driver`, async () => { + await runBrowserDriver(browser, "some-version"); + + assert.calledOnceWith(installBrowserStub, browser, "some-version", { + shouldInstallWebDriver: true, + shouldInstallUbuntuPackages: true, + }); + }); + }); }); diff --git a/test/src/browser-installer/ubuntu-packages/collect-dependencies/cache.ts b/test/src/browser-installer/ubuntu-packages/collect-dependencies/cache.ts index 9b93c5ce5..64d7bbe64 100644 --- a/test/src/browser-installer/ubuntu-packages/collect-dependencies/cache.ts +++ b/test/src/browser-installer/ubuntu-packages/collect-dependencies/cache.ts @@ -4,6 +4,7 @@ import type { Cache as CacheType, CacheData, } from "../../../../../src/browser-installer/ubuntu-packages/collect-dependencies/cache"; +import { Browser } from "../../../../../src/browser-installer/utils"; describe("browser-installer/ubuntu-packages/collect-dependencies/shared-object", () => { const sandbox = sinon.createSandbox(); @@ -58,8 +59,8 @@ describe("browser-installer/ubuntu-packages/collect-dependencies/shared-object", }); const filteredBrowsers = cache.filterProcessedBrowsers([ - { browserName: "chrome", browserVersion: "80.0.123.17" }, - { browserName: "chrome", browserVersion: "82.0.123.17" }, + { browserName: Browser.CHROME, browserVersion: "80.0.123.17" }, + { browserName: Browser.CHROME, browserVersion: "82.0.123.17" }, ]); assert.deepEqual(filteredBrowsers, [{ browserName: "chrome", browserVersion: "82.0.123.17" }]); @@ -67,8 +68,8 @@ describe("browser-installer/ubuntu-packages/collect-dependencies/shared-object", it("should save processed browsers", async () => { cache.saveProcessedBrowsers([ - { browserName: "chrome", browserVersion: "80.0.123.17" }, - { browserName: "chrome", browserVersion: "82.0.123.17" }, + { browserName: Browser.CHROME, browserVersion: "80.0.123.17" }, + { browserName: Browser.CHROME, browserVersion: "82.0.123.17" }, ]); const cacheData = await getCache_(); diff --git a/test/src/browser-installer/utils.ts b/test/src/browser-installer/utils.ts index 18b420a76..abce0e804 100644 --- a/test/src/browser-installer/utils.ts +++ b/test/src/browser-installer/utils.ts @@ -1,29 +1,31 @@ -import { Browser, Driver } from "../../../src/browser-installer/utils"; +import { Browser } from "../../../src/browser-installer/utils"; import * as utils from "../../../src/browser-installer/utils"; describe("browser-installer/utils", () => { - describe("getDriverNameForBrowserName", () => { - it("CHROMEDRIVER", () => { - assert.equal(utils.getDriverNameForBrowserName(Browser.CHROME), Driver.CHROMEDRIVER); - assert.equal(utils.getDriverNameForBrowserName(Browser.CHROMIUM), Driver.CHROMEDRIVER); + describe("getNormalizedBrowserName", () => { + it("CHROME", () => { + assert.equal(utils.getNormalizedBrowserName("chrome"), Browser.CHROME); }); - it("GECKODRIVER", () => { - assert.equal(utils.getDriverNameForBrowserName(Browser.FIREFOX), Driver.GECKODRIVER); + it("FIREFOX", () => { + assert.equal(utils.getNormalizedBrowserName("firefox"), Browser.FIREFOX); }); - it("SAFARIDRIVER", () => { - assert.equal(utils.getDriverNameForBrowserName(Browser.SAFARI), Driver.SAFARIDRIVER); + it("EDGE", () => { + assert.equal(utils.getNormalizedBrowserName("edge"), Browser.EDGE); + assert.equal(utils.getNormalizedBrowserName("MicrosoftEdge"), Browser.EDGE); + assert.equal(utils.getNormalizedBrowserName("msedge"), Browser.EDGE); }); - it("EDGEDRIVER", () => { - assert.equal(utils.getDriverNameForBrowserName(Browser.EDGE), Driver.EDGEDRIVER); + it("SAFARI", () => { + assert.equal(utils.getNormalizedBrowserName("safari"), Browser.SAFARI); }); it("null", () => { const invalidValue = "unknown" as (typeof Browser)[keyof typeof Browser]; - assert.equal(utils.getDriverNameForBrowserName(invalidValue), null); + assert.equal(utils.getNormalizedBrowserName(invalidValue), null); + assert.equal(utils.getNormalizedBrowserName(), null); }); }); diff --git a/test/src/browser-pool/webdriver-pool.ts b/test/src/browser-pool/webdriver-pool.ts index 7fa2aaa74..226211364 100644 --- a/test/src/browser-pool/webdriver-pool.ts +++ b/test/src/browser-pool/webdriver-pool.ts @@ -41,7 +41,7 @@ describe("browser-pool/webdriver-pool", () => { const driver = await wdPool.getWebdriver("MicrosoftEdge", "135.0"); assert.equal(driver.gridUrl, "http://localhost:100500"); - assert.calledOnceWith(runBrowserDriverStub, "edgedriver", "135.0", { debug: false }); + assert.calledOnceWith(runBrowserDriverStub, "MicrosoftEdge", "135.0", { debug: false }); }); it("should run browser driver with debug mode", async () => {