Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(install): --force should work for downloadable browsers (#38709)
Dmitry Gozman0fafe2dd82d1
diff --git a/packages/playwright-core/src/cli/program.ts b/packages/playwright-core/src/cli/program.tsindex cd6e05794c..dfe49e05ac 100644--- a/packages/playwright-core/src/cli/program.ts+++ b/packages/playwright-core/src/cli/program.ts@@ -142,7 +142,7 @@ program.option('--with-deps', 'install system dependencies for browsers').option('--dry-run', 'do not execute installation, only print information').option('--list', 'prints list of browsers from all playwright installations')- .option('--force', 'force reinstall of stable browser channels')+ .option('--force', 'force reinstall of already installed browsers').option('--only-shell', 'only install headless shell when installing chromium').option('--no-shell', 'do not install chromium headless shell').action(async function(args: string[], options: { withDeps?: boolean, force?: boolean, dryRun?: boolean, list?: boolean, shell?: boolean, noShell?: boolean, onlyShell?: boolean }) {@@ -191,8 +191,7 @@ programconst browsers = await registry.listInstalledBrowsers();printGroupedByPlaywrightVersion(browsers);} else {- const force = args.length === 0 ? false : !!options.force;- await registry.install(executables, { force });+ await registry.install(executables, { force: options.force });await registry.validateHostRequirementsForExecutablesIfNeeded(executables, process.env.PW_LANG_NAME || 'javascript').catch((e: Error) => {e.name = 'Playwright Host validation warning';console.error(e);diff --git a/packages/playwright-core/src/server/registry/browserFetcher.ts b/packages/playwright-core/src/server/registry/browserFetcher.tsindex 64000580f7..f4c4d7df83 100644--- a/packages/playwright-core/src/server/registry/browserFetcher.ts+++ b/packages/playwright-core/src/server/registry/browserFetcher.ts@@ -30,11 +30,14 @@ import { browserDirectoryToMarkerFilePath } from '.';import type { DownloadParams } from './oopDownloadBrowserMain';-export async function downloadBrowserWithProgressBar(title: string, browserDirectory: string, executablePath: string | undefined, downloadURLs: string[], downloadFileName: string, downloadSocketTimeout: number): Promise<boolean> {+export async function downloadBrowserWithProgressBar(title: string, browserDirectory: string, executablePath: string | undefined, downloadURLs: string[], downloadFileName: string, downloadSocketTimeout: number, force: boolean) {if (await existsAsync(browserDirectoryToMarkerFilePath(browserDirectory))) {// Already downloaded.debugLogger.log('install', `${title} is already downloaded.`);- return false;+ if (force)+ debugLogger.log('install', `force-downloading ${title}.`);+ else+ return;}// Create a unique temporary directory for this download to prevent concurrent downloads from clobbering each other@@ -55,7 +58,7 @@ export async function downloadBrowserWithProgressBar(title: string, browserDirecif (await existsAsync(zipPath))await fs.promises.unlink(zipPath);if (await existsAsync(browserDirectory))- await fs.promises.rmdir(browserDirectory, { recursive: true });+ await removeFolders([browserDirectory]);const errorMessage = error?.message || '';debugLogger.log('install', `attempt #${attempt} - ERROR: ${errorMessage}`);if (attempt >= retryCount)@@ -70,7 +73,6 @@ export async function downloadBrowserWithProgressBar(title: string, browserDirecawait removeFolders([uniqueTempDir]);}logPolitely(`${title} downloaded to ${browserDirectory}`);- return true;}/**diff --git a/packages/playwright-core/src/server/registry/index.ts b/packages/playwright-core/src/server/registry/index.tsindex bfefdf27a2..66adff7899 100644--- a/packages/playwright-core/src/server/registry/index.ts+++ b/packages/playwright-core/src/server/registry/index.ts@@ -567,7 +567,7 @@ export interface Executable {}interface ExecutableImpl extends Executable {- _install?: () => Promise<void>;+ _install?: (force: boolean) => Promise<void>;_dependencyGroup?: DependencyGroup;_isHermeticInstallation?: boolean;}@@ -624,7 +624,7 @@ export class Registry {title: chromium.title,revision: chromium.revision,browserVersion: chromium.browserVersion,- _install: () => this._downloadExecutable(chromium, chromiumExecutable),+ _install: force => this._downloadExecutable(chromium, force, chromiumExecutable),_dependencyGroup: 'chromium',_isHermeticInstallation: true,});@@ -643,7 +643,7 @@ export class Registry {title: chromiumHeadlessShell.title,revision: chromiumHeadlessShell.revision,browserVersion: chromiumHeadlessShell.browserVersion,- _install: () => this._downloadExecutable(chromiumHeadlessShell, chromiumHeadlessShellExecutable),+ _install: force => this._downloadExecutable(chromiumHeadlessShell, force, chromiumHeadlessShellExecutable),_dependencyGroup: 'chromium',_isHermeticInstallation: true,});@@ -662,7 +662,7 @@ export class Registry {title: chromiumTipOfTreeHeadlessShell.title,revision: chromiumTipOfTreeHeadlessShell.revision,browserVersion: chromiumTipOfTreeHeadlessShell.browserVersion,- _install: () => this._downloadExecutable(chromiumTipOfTreeHeadlessShell, chromiumTipOfTreeHeadlessShellExecutable),+ _install: force => this._downloadExecutable(chromiumTipOfTreeHeadlessShell, force, chromiumTipOfTreeHeadlessShellExecutable),_dependencyGroup: 'chromium',_isHermeticInstallation: true,});@@ -681,7 +681,7 @@ export class Registry {title: chromiumTipOfTree.title,revision: chromiumTipOfTree.revision,browserVersion: chromiumTipOfTree.browserVersion,- _install: () => this._downloadExecutable(chromiumTipOfTree, chromiumTipOfTreeExecutable),+ _install: force => this._downloadExecutable(chromiumTipOfTree, force, chromiumTipOfTreeExecutable),_dependencyGroup: 'chromium',_isHermeticInstallation: true,});@@ -795,7 +795,7 @@ export class Registry {title: firefox.title,revision: firefox.revision,browserVersion: firefox.browserVersion,- _install: () => this._downloadExecutable(firefox, firefoxExecutable),+ _install: force => this._downloadExecutable(firefox, force, firefoxExecutable),_dependencyGroup: 'firefox',_isHermeticInstallation: true,});@@ -814,7 +814,7 @@ export class Registry {title: firefoxBeta.title,revision: firefoxBeta.revision,browserVersion: firefoxBeta.browserVersion,- _install: () => this._downloadExecutable(firefoxBeta, firefoxBetaExecutable),+ _install: force => this._downloadExecutable(firefoxBeta, force, firefoxBetaExecutable),_dependencyGroup: 'firefox',_isHermeticInstallation: true,});@@ -843,7 +843,7 @@ export class Registry {title: webkit.title,revision: webkit.revision,browserVersion: webkit.browserVersion,- _install: () => this._downloadExecutable(webkit, webkitExecutable),+ _install: force => this._downloadExecutable(webkit, force, webkitExecutable),_dependencyGroup: 'webkit',_isHermeticInstallation: true,});@@ -885,7 +885,7 @@ export class Registry {downloadURLs: this._downloadURLs(ffmpeg),title: ffmpeg.title,revision: ffmpeg.revision,- _install: () => this._downloadExecutable(ffmpeg, ffmpegExecutable),+ _install: force => this._downloadExecutable(ffmpeg, force, ffmpegExecutable),_dependencyGroup: 'tools',_isHermeticInstallation: true,});@@ -902,7 +902,7 @@ export class Registry {downloadURLs: this._downloadURLs(winldd),title: winldd.title,revision: winldd.revision,- _install: () => this._downloadExecutable(winldd, winlddExecutable),+ _install: force => this._downloadExecutable(winldd, force, winlddExecutable),_dependencyGroup: 'tools',_isHermeticInstallation: true,});@@ -918,7 +918,7 @@ export class Registry {downloadURLs: this._downloadURLs(android),title: android.title,revision: android.revision,- _install: () => this._downloadExecutable(android),+ _install: force => this._downloadExecutable(android, force),_dependencyGroup: 'tools',_isHermeticInstallation: true,});@@ -1139,7 +1139,7 @@ export class Registry {].join('\n'), 1) + '\n\n');return;}- await executable._install();+ await executable._install(!!options?.force);}} catch (e) {if (e.code === 'ELOCKED') {@@ -1247,7 +1247,7 @@ export class Registry {return mirrors.map(mirror => `${mirror}/${downloadPath}`);}- private async _downloadExecutable(descriptor: BrowsersJSONDescriptor, executablePath?: string) {+ private async _downloadExecutable(descriptor: BrowsersJSONDescriptor, force: boolean, executablePath?: string) {const downloadURLs = this._downloadURLs(descriptor);if (!downloadURLs.length)throw new Error(`ERROR: Playwright does not support ${descriptor.name} on ${hostPlatform}`);@@ -1267,7 +1267,7 @@ export class Registry {// max idle timeout. Unfortunately, we cannot rename it without breaking existing user workflows.const downloadSocketTimeoutEnv = getFromENV('PLAYWRIGHT_DOWNLOAD_CONNECTION_TIMEOUT');const downloadSocketTimeout = +(downloadSocketTimeoutEnv || '0') || NET_DEFAULT_TIMEOUT;- await downloadBrowserWithProgressBar(title, descriptor.dir, executablePath, downloadURLs, downloadFileName, downloadSocketTimeout).catch(e => {+ await downloadBrowserWithProgressBar(title, descriptor.dir, executablePath, downloadURLs, downloadFileName, downloadSocketTimeout, force).catch(e => {throw new Error(`Failed to download ${title}, caused by\n${e.stack}`);});}diff --git a/packages/playwright-core/src/server/registry/oopDownloadBrowserMain.ts b/packages/playwright-core/src/server/registry/oopDownloadBrowserMain.tsindex ba227584d6..5923201206 100644--- a/packages/playwright-core/src/server/registry/oopDownloadBrowserMain.ts+++ b/packages/playwright-core/src/server/registry/oopDownloadBrowserMain.ts@@ -20,6 +20,7 @@ import path from 'path';import { ManualPromise } from '../../utils/isomorphic/manualPromise';import { httpRequest } from '../utils/network';import { extract } from '../../zipBundle';+import { removeFolders } from '../utils/fileUtils';export type DownloadParams = {title: string;@@ -113,6 +114,8 @@ function downloadFile(options: DownloadParams): Promise<void> {async function main(options: DownloadParams) {await downloadFile(options);log(`SUCCESS downloading ${options.title}`);+ log(`removing existing browser directory if any`);+ await removeFolders([options.browserDirectory]);log(`extracting archive`);await extract(options.zipPath, { dir: options.browserDirectory });if (options.executablePath) {