Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feature(cli): Support install --list command (#36049)

Manish Ranjan89224df00942
diff --git a/docs/src/browsers.md b/docs/src/browsers.md
index f0131b7d24..738dac7eb6 100644
--- a/docs/src/browsers.md
+++ b/docs/src/browsers.md
@@ -1084,6 +1084,26 @@ Playwright keeps track of the clients that use its browsers. When there are no m
To opt-out from the unused browser removal, you can set the `PLAYWRIGHT_SKIP_BROWSER_GC=1` environment variable.
+### List all installed browsers:
+
+Prints list of browsers from all playwright installations on the machine.
+
+```bash js
+npx playwright install --list
+```
+
+```bash java
+mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install --list"
+```
+
+```bash python
+playwright install --list
+```
+
+```bash csharp
+pwsh bin/Debug/netX/playwright.ps1 install --list
+```
+
### Uninstall browsers
This will remove the browsers (chromium, firefox, webkit) of the current Playwright installation:
diff --git a/packages/playwright-core/src/cli/program.ts b/packages/playwright-core/src/cli/program.ts
index 6350781bec..7a1f4da209 100644
--- a/packages/playwright-core/src/cli/program.ts
+++ b/packages/playwright-core/src/cli/program.ts
@@ -133,10 +133,11 @@ program
.description('ensure browsers necessary for this version of Playwright are installed')
.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('--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, shell?: boolean, noShell?: boolean, onlyShell?: boolean }) {
+ .action(async function(args: string[], options: { withDeps?: boolean, force?: boolean, dryRun?: boolean, list?: boolean, shell?: boolean, noShell?: boolean, onlyShell?: boolean }) {
// For '--no-shell' option, commander sets `shell: false` instead.
if (options.shell === false)
options.noShell = true;
@@ -165,6 +166,8 @@ program
const executables = hasNoArguments ? defaultBrowsersToInstall(options) : checkBrowsersToInstall(args, options);
if (options.withDeps)
await registry.installDeps(executables, !!options.dryRun);
+ if (options.dryRun && options.list)
+ throw new Error(`Only one of --dry-run and --list can be specified`);
if (options.dryRun) {
for (const executable of executables) {
const version = executable.browserVersion ? `version ` + executable.browserVersion : '';
@@ -178,6 +181,19 @@ program
}
console.log(``);
}
+ } else if (options.list) {
+ const browsersMap = await registry.list();
+ for (const browserName in browsersMap) {
+ console.log(`Browser: ${browserName}`);
+ const browsers = browsersMap[browserName];
+ for (const browser of browsers) {
+ console.log(` Version: ${browser.browserVersion}`);
+ console.log(` Directory: ${browser.hostDir}`);
+ console.log(` Location: ${browser.browserPath}`);
+ console.log(``);
+ }
+ console.log(``);
+ }
} else {
const forceReinstall = hasNoArguments ? false : !!options.force;
await registry.install(executables, forceReinstall);
diff --git a/packages/playwright-core/src/server/registry/index.ts b/packages/playwright-core/src/server/registry/index.ts
index be066ecb5f..47f3ec1dbc 100644
--- a/packages/playwright-core/src/server/registry/index.ts
+++ b/packages/playwright-core/src/server/registry/index.ts
@@ -449,6 +449,13 @@ type BrowsersJSONDescriptor = {
dir: string,
};
+type BrowsersInfo = {
+ browserName: string,
+ browserVersion: number,
+ hostDir: string,
+ browserPath: string
+};
+
function readDescriptors(browsersJSON: BrowsersJSON): BrowsersJSONDescriptor[] {
return (browsersJSON['browsers']).map(obj => {
const name = obj.name;
@@ -1043,7 +1050,9 @@ export class Registry {
await fs.promises.writeFile(path.join(linksDir, calculateSha1(PACKAGE_PATH)), PACKAGE_PATH);
// Remove stale browsers.
- await this._validateInstallationCache(linksDir);
+ const [browsers, brokenLinks] = await this._traverseBrowserInstallations(linksDir);
+ await this._deleteStaleBrowsers(browsers);
+ await this._deleteBrokenInstallations(brokenLinks);
// Install browsers for this package.
for (const executable of executables) {
@@ -1108,7 +1117,10 @@ export class Registry {
}
// Remove stale browsers.
- await this._validateInstallationCache(linksDir);
+ const [browsers, brokenLinks] = await this._traverseBrowserInstallations(linksDir);
+ await this._deleteStaleBrowsers(browsers);
+ await this._deleteBrokenInstallations(brokenLinks);
+
return {
numberOfBrowsersLeft: (await fs.promises.readdir(registryDirectory).catch(() => [])).filter(browserDirectory => isBrowserDirectory(browserDirectory)).length
};
@@ -1248,9 +1260,26 @@ export class Registry {
}
}
- private async _validateInstallationCache(linksDir: string) {
- // 1. Collect used downloads and package descriptors.
- const usedBrowserPaths: Set<string> = new Set();
+ async list() {
+ const linksDir = path.join(registryDirectory, '.links');
+ const [browsers] = await this._traverseBrowserInstallations(linksDir);
+
+ // Group browsers by browserName
+ const groupedBrowsers: Record<string, BrowsersInfo[]> = {};
+
+ for (const browser of browsers) {
+ if (!groupedBrowsers[browser.browserName])
+ groupedBrowsers[browser.browserName] = [];
+
+ groupedBrowsers[browser.browserName].push(browser);
+ }
+
+ return groupedBrowsers;
+ }
+
+ private async _traverseBrowserInstallations(linksDir: string): Promise<[browsers: BrowsersInfo[], brokenLinks: string[]]> {
+ const browserList: BrowsersInfo[] = [];
+ const brokenLinks: string[] = [];
for (const fileName of await fs.promises.readdir(linksDir)) {
const linkPath = path.join(linksDir, fileName);
let linkTarget = '';
@@ -1267,34 +1296,56 @@ export class Registry {
const descriptor = descriptors.find(d => d.name === browserName);
if (!descriptor)
continue;
- const usedBrowserPath = descriptor.dir;
- const browserRevision = parseInt(descriptor.revision, 10);
- // Old browser installations don't have marker file.
- // We switched chromium from 999999 to 1000, 300000 is the new Y2K.
- const shouldHaveMarkerFile = (browserName === 'chromium' && (browserRevision >= 786218 || browserRevision < 300000)) ||
- (browserName === 'firefox' && browserRevision >= 1128) ||
- (browserName === 'webkit' && browserRevision >= 1307) ||
- // All new applications have a marker file right away.
- (browserName !== 'firefox' && browserName !== 'chromium' && browserName !== 'webkit');
- if (!shouldHaveMarkerFile || (await existsAsync(browserDirectoryToMarkerFilePath(usedBrowserPath))))
- usedBrowserPaths.add(usedBrowserPath);
+
+ const browserPath = descriptor.dir;
+ const browserVersion = parseInt(descriptor.revision, 10);
+ browserList.push({
+ browserName,
+ browserVersion,
+ hostDir: linkTarget,
+ browserPath
+ });
}
} catch (e) {
- await fs.promises.unlink(linkPath).catch(e => {});
+ brokenLinks.push(linkPath);
}
}
- // 2. Delete all unused browsers.
- if (!getAsBooleanFromENV('PLAYWRIGHT_SKIP_BROWSER_GC')) {
- let downloadedBrowsers = (await fs.promises.readdir(registryDirectory)).map(file => path.join(registryDirectory, file));
- downloadedBrowsers = downloadedBrowsers.filter(file => isBrowserDirectory(file));
- const directories = new Set<string>(downloadedBrowsers);
- for (const browserDirectory of usedBrowserPaths)
- directories.delete(browserDirectory);
- for (const directory of directories)
- logPolitely('Removing unused browser at ' + directory);
- await removeFolders([...directories]);
+ return [browserList, brokenLinks];
+ }
+
+ private async _deleteStaleBrowsers(browserList: BrowsersInfo[]) {
+ if (getAsBooleanFromENV('PLAYWRIGHT_SKIP_BROWSER_GC'))
+ return;
+
+ const usedBrowserPaths: Set<string> = new Set();
+ for (const browser of browserList) {
+ const { browserName, browserVersion, browserPath } = browser;
+
+ // Old browser installations don't have marker file.
+ // We switched chromium from 999999 to 1000, 300000 is the new Y2K.
+ const shouldHaveMarkerFile = (browserName === 'chromium' && (browserVersion >= 786218 || browserVersion < 300000)) ||
+ (browserName === 'firefox' && browserVersion >= 1128) ||
+ (browserName === 'webkit' && browserVersion >= 1307) ||
+ // All new applications have a marker file right away.
+ (browserName !== 'firefox' && browserName !== 'chromium' && browserName !== 'webkit');
+ if (!shouldHaveMarkerFile || (await existsAsync(browserDirectoryToMarkerFilePath(browserPath))))
+ usedBrowserPaths.add(browserPath);
}
+
+ let downloadedBrowsers = (await fs.promises.readdir(registryDirectory)).map(file => path.join(registryDirectory, file));
+ downloadedBrowsers = downloadedBrowsers.filter(file => isBrowserDirectory(file));
+ const directories = new Set<string>(downloadedBrowsers);
+ for (const browserDirectory of usedBrowserPaths)
+ directories.delete(browserDirectory);
+ for (const directory of directories)
+ logPolitely('Removing unused browser at ' + directory);
+ await removeFolders([...directories]);
+ }
+
+ private async _deleteBrokenInstallations(brokenLinks: string[]) {
+ for (const linkPath of brokenLinks)
+ await fs.promises.unlink(linkPath).catch(e => {});
}
}
diff --git a/tests/installation/npmTest.ts b/tests/installation/npmTest.ts
index c3e5053013..09849077b5 100644
--- a/tests/installation/npmTest.ts
+++ b/tests/installation/npmTest.ts
@@ -134,7 +134,7 @@ export const test = _test
tmpWorkspace: async ({}, use) => {
// We want a location that won't have a node_modules dir anywhere along its path
const tmpWorkspace = path.join(TMP_WORKSPACES, path.basename(test.info().outputDir));
- await fs.promises.mkdir(tmpWorkspace);
+ await fs.promises.mkdir(tmpWorkspace, { recursive: true });
debug(`Workspace Folder: ${tmpWorkspace}`);
await use(tmpWorkspace);
},
diff --git a/tests/installation/playwright-cli-install-should-work.spec.ts b/tests/installation/playwright-cli-install-should-work.spec.ts
index 2825a9a44f..65e6005aab 100755
--- a/tests/installation/playwright-cli-install-should-work.spec.ts
+++ b/tests/installation/playwright-cli-install-should-work.spec.ts
@@ -36,6 +36,20 @@ test('install command should work', async ({ exec, checkInstalledSoftwareOnDisk
await checkInstalledSoftwareOnDisk(['chromium', 'chromium-headless-shell', 'ffmpeg', 'firefox', 'webkit', ...extraInstalledSoftware]);
});
+ await test.step('playwright install --list', async () => {
+ const result = await exec('npx playwright install --list');
+ const listed = new Set();
+ const regex = /Browser:\s+([\w-]+)/g;
+ let match;
+ while ((match = regex.exec(result)) !== null)
+ listed.add(match[1]);
+
+ const expected = ['android', 'chromium', 'ffmpeg', 'firefox', 'webkit'];
+ expected.forEach(browser => {
+ expect(listed.has(browser)).toBe(true);
+ });
+ });
+
await exec('node sanity.js playwright', { env: { PLAYWRIGHT_BROWSERS_PATH: '0' } });
await exec('node sanity.js playwright chromium firefox webkit');