Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto browser download #1029

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,319 changes: 513 additions & 806 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@babel/code-frame": "7.24.2",
"@gemini-testing/commander": "2.15.4",
"@jspm/core": "2.0.1",
"@puppeteer/browsers": "2.4.0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • download chrome
  • download chromium (replaces chrome for versions >73, <115)
  • download firefox

"@types/debug": "4.1.12",
"@types/yallist": "4.0.4",
"@vitest/spy": "2.1.4",
Expand All @@ -65,12 +66,16 @@
"bluebird": "3.5.1",
"chalk": "2.4.2",
"clear-require": "1.0.1",
"cli-progress": "3.12.0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multiple progress bar
image

"debug": "2.6.9",
"devtools": "8.39.0",
"edgedriver": "5.6.1",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • download edge driver

"error-stack-parser": "2.1.4",
"expect-webdriverio": "3.6.0",
"extract-zip": "2.0.1",
Copy link
Member Author

@KuznetsovRoman KuznetsovRoman Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • extract zip archive with chromedriver, which is installed manually (>73, <115)

"fastq": "1.13.0",
"fs-extra": "5.0.0",
"geckodriver": "4.5.0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • download and run geckodriver

"gemini-configparser": "1.4.1",
"get-port": "5.1.1",
"glob-extra": "5.0.2",
Expand All @@ -96,6 +101,7 @@
"urijs": "1.19.11",
"url-join": "4.0.1",
"vite": "5.1.6",
"wait-port": "1.1.0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • wait for port to be ready to receive requests when launching webdriver on this port

"webdriverio": "8.39.0",
"worker-farm": "1.7.0",
"yallist": "3.1.1"
Expand All @@ -118,6 +124,7 @@
"@types/chai": "4.3.4",
"@types/chai-as-promised": "7.1.5",
"@types/clear-require": "3.2.1",
"@types/cli-progress": "3.11.6",
"@types/escape-string-regexp": "2.0.1",
"@types/fs-extra": "11.0.4",
"@types/lodash": "4.14.191",
Expand Down
61 changes: 61 additions & 0 deletions src/browser-installer/chrome/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { resolveBuildId, canDownload, install as puppeteerInstall } from "@puppeteer/browsers";
import { MIN_CHROME_FOR_TESTING_VERSION } from "../constants";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

min_supported_chrome_version?

Copy link
Member Author

@KuznetsovRoman KuznetsovRoman Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No.
We have these chrome versions:

  • MIN_CHROMIUM_VERSION (73)
  • MIN_CHROMIUM_MAC_ARM_VERSION (93)
  • MIN_CHROME_FOR_TESTING_VERSION (113)

So there are 3 ranges:

  • [73, 92]. Minimal chrome to use. Chromium is used. (mac x64 version for mac arm, because there is no mac arm for these versions)
  • [93, 112]. Chromium is still used, but now Mac_arm version for mac.
  • 113+. Chrome for testing is used.

import {
browserInstallerDebug,
getBrowserPlatform,
getBrowsersDir,
getMilestone,
Browser,
type DownloadProgressCallback,
} from "../utils";
import { getBinaryPath, getMatchedBrowserVersion, installBinary } from "../registry";
import { normalizeChromeVersion } from "../utils";

export const installChrome = async (version: string, { force = false } = {}): Promise<string> => {
const milestone = getMilestone(version);

if (Number(milestone) < MIN_CHROME_FOR_TESTING_VERSION) {
browserInstallerDebug(`couldn't install chrome@${version}, installing chromium instead`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to inform user about min supported version here. it's not clear why we are going to install chromium instead of chrome

Copy link
Member Author

@KuznetsovRoman KuznetsovRoman Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not user warning log. It won't be logged out to the user, and user don't need to distinguish between "chromium" and "chrome for testing" It is debug log.


const { installChromium } = await import("../chromium");

return installChromium(version, { force });
}

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

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

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

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

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

if (!canBeInstalled) {
throw new Error(
[
`chrome@${version} can't be installed.`,
`Probably the version '${version}' is invalid, please try another version.`,
"Version examples: '120', '120.0'",
].join("\n"),
);
}

const installFn = (downloadProgressCallback: DownloadProgressCallback): Promise<string> =>
puppeteerInstall({
platform,
buildId,
cacheDir,
downloadProgressCallback,
browser: Browser.CHROME,
unpack: true,
}).then(result => result.executablePath);

return installBinary(Browser.CHROME, platform, buildId, installFn);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

registry.installBinary does not mean we will call "installFn" function.
If multiple "installBinary" are called with same browser platform and buildId, "installFn" would only be called at most once. And if we already have it installed, "installFn" wouldn't be called

};
63 changes: 63 additions & 0 deletions src/browser-installer/chrome/driver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { resolveBuildId, install as puppeteerInstall, canDownload } from "@puppeteer/browsers";
import { MIN_CHROMEDRIVER_FOR_TESTING_VERSION } from "../constants";
import {
browserInstallerDebug,
getBrowserPlatform,
getChromeDriverDir,
getMilestone,
Driver,
type DownloadProgressCallback,
} from "../utils";
import { getBinaryPath, getMatchedDriverVersion, installBinary } from "../registry";

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

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

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

const milestone = getMilestone(chromeVersion);

if (Number(milestone) < MIN_CHROMEDRIVER_FOR_TESTING_VERSION) {
browserInstallerDebug(
`installing chromedriver for chrome@${chromeVersion} from chromedriver.storage.googleapis.com manually`,
);

const { installChromeDriverManually } = await import("../chromium");

return installChromeDriverManually(milestone);
}

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

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

if (!canBeInstalled) {
throw new Error(
[
`chromedriver@${buildId} can't be installed.`,
`Probably the major browser version '${milestone}' is invalid`,
"Correct chrome version examples: '123', '124'",
].join("\n"),
);
}

const installFn = (downloadProgressCallback: DownloadProgressCallback): Promise<string> =>
puppeteerInstall({
platform,
buildId,
cacheDir: getChromeDriverDir(),
browser: Driver.CHROMEDRIVER,
unpack: true,
downloadProgressCallback,
}).then(result => result.executablePath);

return installBinary(Driver.CHROMEDRIVER, platform, buildId, installFn);
};
37 changes: 37 additions & 0 deletions src/browser-installer/chrome/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { spawn, type ChildProcess } from "child_process";
import getPort from "get-port";
import waitPort from "wait-port";
import { pipeLogsWithPrefix } from "../../dev-server/utils";
import { DRIVER_WAIT_TIMEOUT } from "../constants";
import { getMilestone } from "../utils";
import { installChrome } from "./browser";
import { installChromeDriver } from "./driver";

export { installChrome, installChromeDriver };

export const runChromeDriver = async (
chromeVersion: string,
{ debug = false } = {},
): Promise<{ gridUrl: string; process: ChildProcess; port: number }> => {
const [chromeDriverPath] = await Promise.all([installChromeDriver(chromeVersion), installChrome(chromeVersion)]);

const milestone = getMilestone(chromeVersion);
const randomPort = await getPort();

const chromeDriver = spawn(chromeDriverPath, [`--port=${randomPort}`, debug ? `--verbose` : "--silent"], {
windowsHide: true,
detached: false,
});

if (debug) {
pipeLogsWithPrefix(chromeDriver, `[chromedriver@${milestone}] `);
}

const gridUrl = `http://127.0.0.1:${randomPort}`;

process.once("exit", () => chromeDriver.kill());

await waitPort({ port: randomPort, output: "silent", timeout: DRIVER_WAIT_TIMEOUT });

return { gridUrl, process: chromeDriver, port: randomPort };
};
56 changes: 56 additions & 0 deletions src/browser-installer/chromium/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { install as puppeteerInstall, canDownload } from "@puppeteer/browsers";
import { installBinary, getBinaryPath, getMatchedBrowserVersion } from "../registry";
import { getMilestone, browserInstallerDebug, getBrowsersDir, Browser, type DownloadProgressCallback } from "../utils";
import { getChromiumBuildId } from "./utils";
import { getChromePlatform } from "../utils";
import { MIN_CHROMIUM_VERSION } from "../constants";

export const installChromium = async (version: string, { force = false } = {}): Promise<string> => {
const milestone = getMilestone(version);

if (Number(milestone) < MIN_CHROMIUM_VERSION) {
throw new Error(
[
`chrome@${version} can't be installed.`,
`Automatic browser downloader is not available for chrome versions < ${MIN_CHROMIUM_VERSION}`,
].join("\n"),
);
}

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

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

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

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

if (!canBeInstalled) {
throw new Error(
[
`chrome@${version} can't be installed.`,
`Probably the version '${version}' is invalid, please try another version.`,
"Version examples: '93', '93.0'",
].join("\n"),
);
}

browserInstallerDebug(`installing chromium@${buildId} (${milestone}) for ${platform}`);

const installFn = (downloadProgressCallback: DownloadProgressCallback): Promise<string> =>
puppeteerInstall({
platform,
buildId,
cacheDir,
downloadProgressCallback,
browser: Browser.CHROMIUM,
unpack: true,
}).then(result => result.executablePath);

return installBinary(Browser.CHROMIUM, platform, milestone, installFn);
};
54 changes: 54 additions & 0 deletions src/browser-installer/chromium/driver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import fs from "fs-extra";
import path from "path";
import { noop } from "lodash";
import { CHROMEDRIVER_STORAGE_API, MIN_CHROMIUM_VERSION } from "../constants";
import { installBinary } from "../registry";
import {
downloadFile,
getChromiumDriverDir,
retryFetch,
unzipFile,
normalizeChromeVersion,
Driver,
getBrowserPlatform,
} from "../utils";
import { getChromeDriverArchiveTmpPath, getChromeDriverArchiveUrl } from "./utils";

const getChromeDriverVersionByChromiumVersion = async (chromiumVersion: string | number): Promise<string> => {
const suffix = typeof chromiumVersion === "number" ? chromiumVersion : normalizeChromeVersion(chromiumVersion);

const result = await retryFetch(`${CHROMEDRIVER_STORAGE_API}/LATEST_RELEASE_${suffix}`).then(res => res.text());

return result;
};

export const installChromeDriverManually = async (milestone: string): Promise<string> => {
const platform = getBrowserPlatform();

if (Number(milestone) < MIN_CHROMIUM_VERSION) {
throw new Error(
[
`chromedriver@${milestone} can't be installed.`,
`Automatic driver downloader is not available for chrome versions < ${MIN_CHROMIUM_VERSION}`,
].join("\n"),
);
}

const driverVersion = await getChromeDriverVersionByChromiumVersion(milestone);

const installFn = async (): Promise<string> => {
const archiveUrl = getChromeDriverArchiveUrl(driverVersion);
const archivePath = getChromeDriverArchiveTmpPath(driverVersion);
const chromeDriverDirPath = getChromiumDriverDir(driverVersion);
const chromeDriverPath = path.join(chromeDriverDirPath, "chromedriver");

await downloadFile(archiveUrl, archivePath);
await unzipFile(archivePath, chromeDriverDirPath);

fs.remove(archivePath).then(noop, noop);

return chromeDriverPath;
};

return installBinary(Driver.CHROMEDRIVER, platform, driverVersion, installFn);
};
2 changes: 2 additions & 0 deletions src/browser-installer/chromium/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { installChromium } from "./browser";
export { installChromeDriverManually } from "./driver";
42 changes: 42 additions & 0 deletions src/browser-installer/chromium/revisions/linux.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export default {
73: 625983,
74: 638903,
75: 652459,
76: 665032,
77: 681154,
78: 694594,
79: 707231,
80: 722374,
81: 737198,
82: 750023,
83: 756143,
84: 769125,
85: 782822,
86: 800433,
87: 813060,
88: 827143,
89: 843934,
90: 858016,
91: 870827,
92: 885357,
93: 902296,
94: 911605,
95: 920070,
96: 929514,
97: 938637,
98: 950416,
99: 961779,
100: 972803,
101: 982577,
102: 992824,
103: 1002974,
104: 1012822,
105: 1027072,
106: 1036920,
107: 1047812,
108: 1059082,
109: 1070158,
110: 1084167,
111: 1097778,
112: 1107206,
} as Record<number, number>;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add as Record<number, number> so .d.ts would see this file as "just map" and don't save each value as constant

Loading