add --list and npm run list

This commit is contained in:
robby 2025-01-12 13:48:57 +00:00
parent 6179b5b1d7
commit 27b5ad546d
2 changed files with 229 additions and 195 deletions

View file

@ -45,7 +45,8 @@
"roll": "node utils/roll_browser.js", "roll": "node utils/roll_browser.js",
"check-deps": "node utils/check_deps.js", "check-deps": "node utils/check_deps.js",
"build-android-driver": "./utils/build_android_driver.sh", "build-android-driver": "./utils/build_android_driver.sh",
"innerloop": "playwright run-server --reuse-browser" "innerloop": "playwright run-server --reuse-browser",
"list": "playwright install --list"
}, },
"workspaces": [ "workspaces": [
"packages/*" "packages/*"

View file

@ -46,13 +46,13 @@ program
.command('mark-docker-image [dockerImageNameTemplate]', { hidden: true }) .command('mark-docker-image [dockerImageNameTemplate]', { hidden: true })
.description('mark docker image') .description('mark docker image')
.allowUnknownOption(true) .allowUnknownOption(true)
.action(function(dockerImageNameTemplate) { .action(function (dockerImageNameTemplate) {
assert(dockerImageNameTemplate, 'dockerImageNameTemplate is required'); assert(dockerImageNameTemplate, 'dockerImageNameTemplate is required');
writeDockerVersion(dockerImageNameTemplate).catch(logErrorAndExit); writeDockerVersion(dockerImageNameTemplate).catch(logErrorAndExit);
}); });
commandWithOpenOptions('open [url]', 'open page in browser specified via -b, --browser', []) commandWithOpenOptions('open [url]', 'open page in browser specified via -b, --browser', [])
.action(function(url, options) { .action(function (url, options) {
open(options, url, codegenId()).catch(logErrorAndExit); open(options, url, codegenId()).catch(logErrorAndExit);
}) })
.addHelpText('afterAll', ` .addHelpText('afterAll', `
@ -67,9 +67,9 @@ commandWithOpenOptions('codegen [url]', 'open page and generate code for user ac
['--target <language>', `language to generate, one of javascript, playwright-test, python, python-async, python-pytest, csharp, csharp-mstest, csharp-nunit, java, java-junit`, codegenId()], ['--target <language>', `language to generate, one of javascript, playwright-test, python, python-async, python-pytest, csharp, csharp-mstest, csharp-nunit, java, java-junit`, codegenId()],
['--save-trace <filename>', 'record a trace for the session and save it to a file'], ['--save-trace <filename>', 'record a trace for the session and save it to a file'],
['--test-id-attribute <attributeName>', 'use the specified attribute to generate data test ID selectors'], ['--test-id-attribute <attributeName>', 'use the specified attribute to generate data test ID selectors'],
]).action(function(url, options) { ]).action(function (url, options) {
codegen(options, url).catch(logErrorAndExit); codegen(options, url).catch(logErrorAndExit);
}).addHelpText('afterAll', ` }).addHelpText('afterAll', `
Examples: Examples:
$ codegen $ codegen
@ -133,7 +133,7 @@ program
.option('--force', 'force reinstall of stable browser channels') .option('--force', 'force reinstall of stable browser channels')
.option('--only-shell', 'only install headless shell when installing chromium') .option('--only-shell', 'only install headless shell when installing chromium')
.option('--no-shell', 'do not install chromium headless shell') .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, shell?: boolean, noShell?: boolean, onlyShell?: boolean }) {
// For '--no-shell' option, commander sets `shell: false` instead. // For '--no-shell' option, commander sets `shell: false` instead.
if (options.shell === false) if (options.shell === false)
options.noShell = true; options.noShell = true;
@ -214,7 +214,7 @@ program
.command('install-deps [browser...]') .command('install-deps [browser...]')
.description('install dependencies necessary to run browsers (will ask for sudo permissions)') .description('install dependencies necessary to run browsers (will ask for sudo permissions)')
.option('--dry-run', 'Do not execute installation commands, only print them') .option('--dry-run', 'Do not execute installation commands, only print them')
.action(async function(args: string[], options: { dryRun?: boolean }) { .action(async function (args: string[], options: { dryRun?: boolean }) {
try { try {
if (!args.length) if (!args.length)
await registry.installDeps(defaultBrowsersToInstall({}), !!options.dryRun); await registry.installDeps(defaultBrowsersToInstall({}), !!options.dryRun);
@ -232,6 +232,39 @@ Examples:
- $ install-deps chrome firefox - $ install-deps chrome firefox
Install dependencies for specific browsers, supports ${suggestedBrowsersToInstall()}.`); Install dependencies for specific browsers, supports ${suggestedBrowsersToInstall()}.`);
program
.command('show-browsers') // Changed from 'install --list' to avoid conflicts
.alias('list-browsers') // Added alias for convenience
.description('lists installed browsers and their paths')
.action(async function () {
try {
const browsers = new Set();
// Filter once instead of twice
const installableBrowsers = registry.executables().filter(e =>
e.installType !== 'none' &&
e.type !== 'tool'
);
if (installableBrowsers.length === 0) {
console.log('No browsers installed. To install them use: npx playwright install');
return;
}
for (const executable of installableBrowsers) {
try {
const version = executable.browserVersion ? `v${executable.browserVersion}` : '';
const location = executable.directory || '<system>';
console.log(`${executable.name} ${version ? version + ' ' : ''}${location ? `(${location})` : ''}`);
browsers.add(executable.name);
} catch (e) {
console.error(`Failed to get info for browser ${executable.name}:`, e);
}
}
} catch (e) {
logErrorAndExit(e);
}
});
const browsers = [ const browsers = [
{ alias: 'cr', name: 'Chromium', type: 'chromium' }, { alias: 'cr', name: 'Chromium', type: 'chromium' },
{ alias: 'ff', name: 'Firefox', type: 'firefox' }, { alias: 'ff', name: 'Firefox', type: 'firefox' },
@ -240,7 +273,7 @@ const browsers = [
for (const { alias, name, type } of browsers) { for (const { alias, name, type } of browsers) {
commandWithOpenOptions(`${alias} [url]`, `open page in ${name}`, []) commandWithOpenOptions(`${alias} [url]`, `open page in ${name}`, [])
.action(function(url, options) { .action(function (url, options) {
open({ ...options, browser: type }, url, options.target).catch(logErrorAndExit); open({ ...options, browser: type }, url, options.target).catch(logErrorAndExit);
}).addHelpText('afterAll', ` }).addHelpText('afterAll', `
Examples: Examples:
@ -253,9 +286,9 @@ commandWithOpenOptions('screenshot <url> <filename>', 'capture a page screenshot
['--wait-for-selector <selector>', 'wait for selector before taking a screenshot'], ['--wait-for-selector <selector>', 'wait for selector before taking a screenshot'],
['--wait-for-timeout <timeout>', 'wait for timeout in milliseconds before taking a screenshot'], ['--wait-for-timeout <timeout>', 'wait for timeout in milliseconds before taking a screenshot'],
['--full-page', 'whether to take a full page screenshot (entire scrollable area)'], ['--full-page', 'whether to take a full page screenshot (entire scrollable area)'],
]).action(function(url, filename, command) { ]).action(function (url, filename, command) {
screenshot(command, command, url, filename).catch(logErrorAndExit); screenshot(command, command, url, filename).catch(logErrorAndExit);
}).addHelpText('afterAll', ` }).addHelpText('afterAll', `
Examples: Examples:
$ screenshot -b webkit https://example.com example.png`); $ screenshot -b webkit https://example.com example.png`);
@ -264,16 +297,16 @@ commandWithOpenOptions('pdf <url> <filename>', 'save page as pdf',
[ [
['--wait-for-selector <selector>', 'wait for given selector before saving as pdf'], ['--wait-for-selector <selector>', 'wait for given selector before saving as pdf'],
['--wait-for-timeout <timeout>', 'wait for given timeout in milliseconds before saving as pdf'], ['--wait-for-timeout <timeout>', 'wait for given timeout in milliseconds before saving as pdf'],
]).action(function(url, filename, options) { ]).action(function (url, filename, options) {
pdf(options, options, url, filename).catch(logErrorAndExit); pdf(options, options, url, filename).catch(logErrorAndExit);
}).addHelpText('afterAll', ` }).addHelpText('afterAll', `
Examples: Examples:
$ pdf https://example.com example.pdf`); $ pdf https://example.com example.pdf`);
program program
.command('run-driver', { hidden: true }) .command('run-driver', { hidden: true })
.action(function(options) { .action(function (options) {
runDriver(); runDriver();
}); });
@ -284,7 +317,7 @@ program
.option('--path <path>', 'Endpoint Path', '/') .option('--path <path>', 'Endpoint Path', '/')
.option('--max-clients <maxClients>', 'Maximum clients') .option('--max-clients <maxClients>', 'Maximum clients')
.option('--mode <mode>', 'Server mode, either "default" or "extension"') .option('--mode <mode>', 'Server mode, either "default" or "extension"')
.action(function(options) { .action(function (options) {
runServer({ runServer({
port: options.port ? +options.port : undefined, port: options.port ? +options.port : undefined,
host: options.host, host: options.host,
@ -296,7 +329,7 @@ program
program program
.command('print-api-json', { hidden: true }) .command('print-api-json', { hidden: true })
.action(function(options) { .action(function (options) {
printApiJson(); printApiJson();
}); });
@ -304,7 +337,7 @@ program
.command('launch-server', { hidden: true }) .command('launch-server', { hidden: true })
.requiredOption('--browser <browserName>', 'Browser name, one of "chromium", "firefox" or "webkit"') .requiredOption('--browser <browserName>', 'Browser name, one of "chromium", "firefox" or "webkit"')
.option('--config <path-to-config-file>', 'JSON file with launchServer options') .option('--config <path-to-config-file>', 'JSON file with launchServer options')
.action(function(options) { .action(function (options) {
launchBrowserServer(options.browser, options.config); launchBrowserServer(options.browser, options.config);
}); });
@ -315,7 +348,7 @@ program
.option('-p, --port <port>', 'Port to serve trace on, 0 for any free port; specifying this option opens trace in a browser tab') .option('-p, --port <port>', 'Port to serve trace on, 0 for any free port; specifying this option opens trace in a browser tab')
.option('--stdin', 'Accept trace URLs over stdin to update the viewer') .option('--stdin', 'Accept trace URLs over stdin to update the viewer')
.description('show trace viewer') .description('show trace viewer')
.action(function(traces, options) { .action(function (traces, options) {
if (options.browser === 'cr') if (options.browser === 'cr')
options.browser = 'chromium'; options.browser = 'chromium';
if (options.browser === 'ff') if (options.browser === 'ff')
@ -518,13 +551,13 @@ async function launchContext(options: Options, extraOptions: LaunchOptions): Pro
} }
context.on('page', page => { context.on('page', page => {
page.on('dialog', () => {}); // Prevent dialogs from being automatically dismissed. page.on('dialog', () => { }); // Prevent dialogs from being automatically dismissed.
page.on('close', () => { page.on('close', () => {
const hasPage = browser.contexts().some(context => context.pages().length > 0); const hasPage = browser.contexts().some(context => context.pages().length > 0);
if (hasPage) if (hasPage)
return; return;
// Avoid the error when the last page is closed because the browser has been closed. // Avoid the error when the last page is closed because the browser has been closed.
closeBrowser().catch(() => {}); closeBrowser().catch(() => { });
}); });
}); });
process.on('SIGINT', async () => { process.on('SIGINT', async () => {