Skip to content

Commit

Permalink
Merge pull request #1045 from gemini-testing/TESTPLANE-386.chore_move…
Browse files Browse the repository at this point in the history
…_utils

chore: refactor(move) browser-installer utils
  • Loading branch information
KuznetsovRoman authored Dec 26, 2024
2 parents b6465df + 0a7f786 commit 812bfe8
Show file tree
Hide file tree
Showing 32 changed files with 325 additions and 285 deletions.
14 changes: 7 additions & 7 deletions src/browser-installer/chrome/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import {
getBrowserPlatform,
getBrowsersDir,
getMilestone,
Browser,
type DownloadProgressCallback,
} from "../utils";
import registry from "../registry";
import { normalizeChromeVersion } from "../utils";
import { installUbuntuPackageDependencies } from "../ubuntu-packages";
import { installChromeDriver } from "./driver";
import { BrowserName } from "../../browser/types";

const installChromeBrowser = async (version: string, { force = false } = {}): Promise<string> => {
const milestone = getMilestone(version);
Expand All @@ -25,19 +25,19 @@ const installChromeBrowser = async (version: string, { force = false } = {}): Pr
}

const platform = getBrowserPlatform();
const existingLocallyBrowserVersion = registry.getMatchedBrowserVersion(Browser.CHROME, platform, version);
const existingLocallyBrowserVersion = registry.getMatchedBrowserVersion(BrowserName.CHROME, platform, version);

if (existingLocallyBrowserVersion && !force) {
browserInstallerDebug(`A locally installed chrome@${version} browser was found. Skipping the installation`);

return registry.getBinaryPath(Browser.CHROME, platform, existingLocallyBrowserVersion);
return registry.getBinaryPath(BrowserName.CHROME, platform, existingLocallyBrowserVersion);
}

const normalizedVersion = normalizeChromeVersion(version);
const buildId = await resolveBuildId(Browser.CHROME, platform, normalizedVersion);
const buildId = await resolveBuildId(BrowserName.CHROME, platform, normalizedVersion);

const cacheDir = getBrowsersDir();
const canBeInstalled = await canDownload({ browser: Browser.CHROME, platform, buildId, cacheDir });
const canBeInstalled = await canDownload({ browser: BrowserName.CHROME, platform, buildId, cacheDir });

if (!canBeInstalled) {
throw new Error(
Expand All @@ -55,11 +55,11 @@ const installChromeBrowser = async (version: string, { force = false } = {}): Pr
buildId,
cacheDir,
downloadProgressCallback,
browser: Browser.CHROME,
browser: BrowserName.CHROME,
unpack: true,
}).then(result => result.executablePath);

return registry.installBinary(Browser.CHROME, platform, buildId, installFn);
return registry.installBinary(BrowserName.CHROME, platform, buildId, installFn);
};

export const installChrome = async (
Expand Down
18 changes: 11 additions & 7 deletions src/browser-installer/chrome/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@ import {
getBrowserPlatform,
getChromeDriverDir,
getMilestone,
Driver,
DriverName,
type DownloadProgressCallback,
} from "../utils";
import registry from "../registry";

export const installChromeDriver = async (chromeVersion: string, { force = false } = {}): Promise<string> => {
const platform = getBrowserPlatform();
const existingLocallyDriverVersion = registry.getMatchedDriverVersion(Driver.CHROMEDRIVER, platform, chromeVersion);
const existingLocallyDriverVersion = registry.getMatchedDriverVersion(
DriverName.CHROMEDRIVER,
platform,
chromeVersion,
);

if (existingLocallyDriverVersion && !force) {
browserInstallerDebug(
`A locally installed chromedriver for chrome@${chromeVersion} was found. Skipping the installation`,
);

return registry.getBinaryPath(Driver.CHROMEDRIVER, platform, existingLocallyDriverVersion);
return registry.getBinaryPath(DriverName.CHROMEDRIVER, platform, existingLocallyDriverVersion);
}

const milestone = getMilestone(chromeVersion);
Expand All @@ -34,10 +38,10 @@ export const installChromeDriver = async (chromeVersion: string, { force = false
return installChromeDriverManually(milestone);
}

const buildId = await resolveBuildId(Driver.CHROMEDRIVER, platform, milestone);
const buildId = await resolveBuildId(DriverName.CHROMEDRIVER, platform, milestone);

const cacheDir = getChromeDriverDir();
const canBeInstalled = await canDownload({ browser: Driver.CHROMEDRIVER, platform, buildId, cacheDir });
const canBeInstalled = await canDownload({ browser: DriverName.CHROMEDRIVER, platform, buildId, cacheDir });

if (!canBeInstalled) {
throw new Error(
Expand All @@ -54,10 +58,10 @@ export const installChromeDriver = async (chromeVersion: string, { force = false
platform,
buildId,
cacheDir: getChromeDriverDir(),
browser: Driver.CHROMEDRIVER,
browser: DriverName.CHROMEDRIVER,
unpack: true,
downloadProgressCallback,
}).then(result => result.executablePath);

return registry.installBinary(Driver.CHROMEDRIVER, platform, buildId, installFn);
return registry.installBinary(DriverName.CHROMEDRIVER, platform, buildId, installFn);
};
13 changes: 7 additions & 6 deletions src/browser-installer/chromium/browser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { install as puppeteerInstall, canDownload } from "@puppeteer/browsers";
import registry from "../registry";
import { getMilestone, browserInstallerDebug, getBrowsersDir, Browser, type DownloadProgressCallback } from "../utils";
import { getMilestone, browserInstallerDebug, getBrowsersDir, type DownloadProgressCallback } from "../utils";
import { getChromiumBuildId } from "./utils";
import { getChromePlatform } from "../utils";
import { MIN_CHROMIUM_VERSION } from "../constants";
import { BrowserName } from "../../browser/types";

export const installChromium = async (version: string, { force = false } = {}): Promise<string> => {
const milestone = getMilestone(version);
Expand All @@ -18,17 +19,17 @@ export const installChromium = async (version: string, { force = false } = {}):
}

const platform = getChromePlatform(version);
const existingLocallyBrowserVersion = registry.getMatchedBrowserVersion(Browser.CHROMIUM, platform, version);
const existingLocallyBrowserVersion = registry.getMatchedBrowserVersion(BrowserName.CHROMIUM, platform, version);

if (existingLocallyBrowserVersion && !force) {
browserInstallerDebug(`A locally installed chromium@${version} browser was found. Skipping the installation`);

return registry.getBinaryPath(Browser.CHROMIUM, platform, existingLocallyBrowserVersion);
return registry.getBinaryPath(BrowserName.CHROMIUM, platform, existingLocallyBrowserVersion);
}

const buildId = await getChromiumBuildId(platform, milestone);
const cacheDir = getBrowsersDir();
const canBeInstalled = await canDownload({ browser: Browser.CHROMIUM, platform, buildId, cacheDir });
const canBeInstalled = await canDownload({ browser: BrowserName.CHROMIUM, platform, buildId, cacheDir });

if (!canBeInstalled) {
throw new Error(
Expand All @@ -48,9 +49,9 @@ export const installChromium = async (version: string, { force = false } = {}):
buildId,
cacheDir,
downloadProgressCallback,
browser: Browser.CHROMIUM,
browser: BrowserName.CHROMIUM,
unpack: true,
}).then(result => result.executablePath);

return registry.installBinary(Browser.CHROMIUM, platform, milestone, installFn);
return registry.installBinary(BrowserName.CHROMIUM, platform, milestone, installFn);
};
4 changes: 2 additions & 2 deletions src/browser-installer/chromium/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
retryFetch,
unzipFile,
normalizeChromeVersion,
Driver,
DriverName,
getBrowserPlatform,
} from "../utils";
import { getChromeDriverArchiveTmpPath, getChromeDriverArchiveUrl } from "./utils";
Expand Down Expand Up @@ -50,5 +50,5 @@ export const installChromeDriverManually = async (milestone: string): Promise<st
return chromeDriverPath;
};

return registry.installBinary(Driver.CHROMEDRIVER, platform, driverVersion, installFn);
return registry.installBinary(DriverName.CHROMEDRIVER, platform, driverVersion, installFn);
};
8 changes: 4 additions & 4 deletions src/browser-installer/edge/driver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { download as downloadEdgeDriver } from "edgedriver";
import {
Driver,
DriverName,
browserInstallerDebug,
getBrowserPlatform,
getEdgeDriverDir,
Expand Down Expand Up @@ -29,14 +29,14 @@ const getLatestMajorEdgeDriverVersion = async (milestone: string): Promise<strin

export const installEdgeDriver = async (edgeVersion: string, { force = false } = {}): Promise<string> => {
const platform = getBrowserPlatform();
const existingLocallyDriverVersion = registry.getMatchedDriverVersion(Driver.EDGEDRIVER, platform, edgeVersion);
const existingLocallyDriverVersion = registry.getMatchedDriverVersion(DriverName.EDGEDRIVER, platform, edgeVersion);

if (existingLocallyDriverVersion && !force) {
browserInstallerDebug(
`A locally installed edgedriver for edge@${edgeVersion} browser was found. Skipping the installation`,
);

return registry.getBinaryPath(Driver.EDGEDRIVER, platform, existingLocallyDriverVersion);
return registry.getBinaryPath(DriverName.EDGEDRIVER, platform, existingLocallyDriverVersion);
}

const milestone = getMilestone(edgeVersion);
Expand All @@ -49,5 +49,5 @@ export const installEdgeDriver = async (edgeVersion: string, { force = false } =

const installFn = (): Promise<string> => downloadEdgeDriver(driverVersion, getEdgeDriverDir(driverVersion));

return registry.installBinary(Driver.EDGEDRIVER, platform, driverVersion, installFn);
return registry.installBinary(DriverName.EDGEDRIVER, platform, driverVersion, installFn);
};
19 changes: 7 additions & 12 deletions src/browser-installer/firefox/browser.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
import { canDownload, install as puppeteerInstall } from "@puppeteer/browsers";
import {
Browser,
browserInstallerDebug,
getBrowserPlatform,
getBrowsersDir,
type DownloadProgressCallback,
} from "../utils";
import { browserInstallerDebug, getBrowserPlatform, getBrowsersDir, type DownloadProgressCallback } from "../utils";
import registry from "../registry";
import { getFirefoxBuildId, normalizeFirefoxVersion } from "./utils";
import { installLatestGeckoDriver } from "./driver";
import { installUbuntuPackageDependencies } from "../ubuntu-packages";
import { BrowserName } from "../../browser/types";

const installFirefoxBrowser = async (version: string, { force = false } = {}): Promise<string> => {
const platform = getBrowserPlatform();
const existingLocallyBrowserVersion = registry.getMatchedBrowserVersion(Browser.FIREFOX, platform, version);
const existingLocallyBrowserVersion = registry.getMatchedBrowserVersion(BrowserName.FIREFOX, platform, version);

if (existingLocallyBrowserVersion && !force) {
browserInstallerDebug(`A locally installed firefox@${version} browser was found. Skipping the installation`);

return registry.getBinaryPath(Browser.FIREFOX, platform, existingLocallyBrowserVersion);
return registry.getBinaryPath(BrowserName.FIREFOX, platform, existingLocallyBrowserVersion);
}

const normalizedVersion = normalizeFirefoxVersion(version);
const buildId = getFirefoxBuildId(normalizedVersion);

const cacheDir = getBrowsersDir();
const canBeInstalled = await canDownload({ browser: Browser.FIREFOX, platform, buildId, cacheDir });
const canBeInstalled = await canDownload({ browser: BrowserName.FIREFOX, platform, buildId, cacheDir });

if (!canBeInstalled) {
throw new Error(
Expand All @@ -45,11 +40,11 @@ const installFirefoxBrowser = async (version: string, { force = false } = {}): P
buildId,
cacheDir,
downloadProgressCallback,
browser: Browser.FIREFOX,
browser: BrowserName.FIREFOX,
unpack: true,
}).then(result => result.executablePath);

return registry.installBinary(Browser.FIREFOX, platform, buildId, installFn);
return registry.installBinary(BrowserName.FIREFOX, platform, buildId, installFn);
};

export const installFirefox = async (
Expand Down
12 changes: 8 additions & 4 deletions src/browser-installer/firefox/driver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { download as downloadGeckoDriver } from "geckodriver";
import { GECKODRIVER_CARGO_TOML } from "../constants";
import registry from "../registry";
import { Driver, browserInstallerDebug, getBrowserPlatform, getGeckoDriverDir, retryFetch } from "../utils";
import { DriverName, browserInstallerDebug, getBrowserPlatform, getGeckoDriverDir, retryFetch } from "../utils";

const getLatestGeckoDriverVersion = async (): Promise<string> => {
const cargoVersionsToml = await retryFetch(GECKODRIVER_CARGO_TOML).then(res => res.text());
Expand All @@ -20,19 +20,23 @@ const getLatestGeckoDriverVersion = async (): Promise<string> => {

export const installLatestGeckoDriver = async (firefoxVersion: string, { force = false } = {}): Promise<string> => {
const platform = getBrowserPlatform();
const existingLocallyDriverVersion = registry.getMatchedDriverVersion(Driver.GECKODRIVER, platform, firefoxVersion);
const existingLocallyDriverVersion = registry.getMatchedDriverVersion(
DriverName.GECKODRIVER,
platform,
firefoxVersion,
);

if (existingLocallyDriverVersion && !force) {
browserInstallerDebug(
`A locally installed geckodriver for firefox@${firefoxVersion} browser was found. Skipping the installation`,
);

return registry.getBinaryPath(Driver.GECKODRIVER, platform, existingLocallyDriverVersion);
return registry.getBinaryPath(DriverName.GECKODRIVER, platform, existingLocallyDriverVersion);
}

const latestVersion = await getLatestGeckoDriverVersion();

const installFn = (): Promise<string> => downloadGeckoDriver(latestVersion, getGeckoDriverDir(latestVersion));

return registry.installBinary(Driver.GECKODRIVER, platform, latestVersion, installFn);
return registry.installBinary(DriverName.GECKODRIVER, platform, latestVersion, installFn);
};
1 change: 0 additions & 1 deletion src/browser-installer/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { installBrowser, installBrowsersWithDrivers, BrowserInstallStatus } from "./install";
export { runBrowserDriver } from "./run";
export { getNormalizedBrowserName } from "./utils";
export type { SupportedBrowser, SupportedDriver } from "./utils";
14 changes: 8 additions & 6 deletions src/browser-installer/install.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import _ from "lodash";
import { Browser, browserInstallerDebug, getNormalizedBrowserName, type SupportedBrowser } from "./utils";
import { browserInstallerDebug, type SupportedBrowser } from "./utils";
import { getNormalizedBrowserName } from "../utils/browser";
import { BrowserName } from "../browser/types";

/**
* @returns path to installed browser binary
Expand Down Expand Up @@ -29,20 +31,20 @@ export const installBrowser = async (
);

switch (browserName) {
case Browser.CHROME:
case Browser.CHROMIUM: {
case BrowserName.CHROME:
case BrowserName.CHROMIUM: {
const { installChrome } = await import("./chrome");

return installChrome(browserVersion, { force, needUbuntuPackages, needWebDriver: shouldInstallWebDriver });
}

case Browser.FIREFOX: {
case BrowserName.FIREFOX: {
const { installFirefox } = await import("./firefox");

return installFirefox(browserVersion, { force, needUbuntuPackages, needWebDriver: shouldInstallWebDriver });
}

case Browser.EDGE: {
case BrowserName.EDGE: {
const { installEdgeDriver } = await import("./edge");

if (shouldInstallWebDriver) {
Expand All @@ -52,7 +54,7 @@ export const installBrowser = async (
return null;
}

case Browser.SAFARI: {
case BrowserName.SAFARI: {
return null;
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/browser-installer/registry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import path from "path";
import {
getRegistryPath,
browserInstallerDebug,
Driver,
Browser,
DriverName,
getMilestone,
normalizeChromeVersion,
semverVersionsComparator,
Expand All @@ -16,6 +15,7 @@ import {
} from "../utils";
import { getFirefoxBuildId } from "../firefox/utils";
import logger from "../../utils/logger";
import { BrowserName } from "../../browser/types";

type VersionToPathMap = Record<string, string | Promise<string>>;
type BinaryName = Exclude<SupportedBrowser | SupportedDriver, SupportedBrowser & SupportedDriver>;
Expand Down Expand Up @@ -98,7 +98,7 @@ class Registry {
return null;
}

if (driverName === Driver.CHROMEDRIVER || driverName === Driver.EDGEDRIVER) {
if (driverName === DriverName.CHROMEDRIVER || driverName === DriverName.EDGEDRIVER) {
const milestone = getMilestone(browserVersion);
const buildIds = this.getBinaryVersions(driverName, platform);
const suitableBuildIds = buildIds.filter(buildId => buildId.startsWith(milestone));
Expand All @@ -110,7 +110,7 @@ class Registry {
return suitableBuildIds.sort(semverVersionsComparator).pop() as string;
}

if (driverName === Driver.GECKODRIVER) {
if (driverName === DriverName.GECKODRIVER) {
const buildIds = Object.keys(this.registry.binaries[registryKey]);
const buildIdsSorted = buildIds.sort(semverVersionsComparator);

Expand All @@ -134,15 +134,15 @@ class Registry {
let buildPrefix: string;

switch (browserName) {
case Browser.CHROME:
case BrowserName.CHROME:
buildPrefix = normalizeChromeVersion(browserVersion);
break;

case Browser.CHROMIUM:
case BrowserName.CHROMIUM:
buildPrefix = getMilestone(browserVersion);
break;

case Browser.FIREFOX:
case BrowserName.FIREFOX:
buildPrefix = getFirefoxBuildId(browserVersion);
break;

Expand All @@ -166,7 +166,7 @@ class Registry {
return parseInt(a.replace(".", ""), 16) - parseInt(b.replace(".", ""), 16);
};

const comparator = browserName === Browser.FIREFOX ? firefoxVersionComparator : semverVersionsComparator;
const comparator = browserName === BrowserName.FIREFOX ? firefoxVersionComparator : semverVersionsComparator;
const suitableBuildIdsSorted = suitableBuildIds.sort(comparator);

return suitableBuildIdsSorted[suitableBuildIdsSorted.length - 1];
Expand Down
Loading

0 comments on commit 812bfe8

Please sign in to comment.