feat(cli): first few cli commands (#4773)

This sets up cli capabilities and brings in a few commands from playwright-cli.
This commit is contained in:
Dmitry Gozman 2020-12-22 14:54:13 -08:00 committed by GitHub
parent cc32217e84
commit d40afa2fbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 470 additions and 31 deletions

View File

@ -16,10 +16,4 @@
const { setUnderTest } = require('./lib/utils/utils');
setUnderTest(); // Note: we must call setUnderTest before initializing.
const { Playwright } = require('./lib/server/playwright');
const { setupInProcess } = require('./lib/inprocess');
const path = require('path');
const playwright = new Playwright(__dirname, require(path.join(__dirname, 'browsers.json'))['browsers']);
module.exports = setupInProcess(playwright);
module.exports = require('./lib/inprocess');

3
package-lock.json generated
View File

@ -2389,8 +2389,7 @@
"commander": {
"version": "6.2.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-6.2.0.tgz",
"integrity": "sha512-zP4jEKbe8SHzKJYQmq8Y9gYjtO/POJLgIdKgV7B9qNmABVFVc+ctqSX6iXh4mCpJfRBOabiZ2YKPg8ciDw6C+Q==",
"dev": true
"integrity": "sha512-zP4jEKbe8SHzKJYQmq8Y9gYjtO/POJLgIdKgV7B9qNmABVFVc+ctqSX6iXh4mCpJfRBOabiZ2YKPg8ciDw6C+Q=="
},
"commondir": {
"version": "1.0.1",

View File

@ -38,7 +38,11 @@
"name": "Microsoft Corporation"
},
"license": "Apache-2.0",
"bin": {
"playwright": "./lib/cli/cli.js"
},
"dependencies": {
"commander": "^6.1.0",
"debug": "^4.1.1",
"extract-zip": "^2.0.1",
"https-proxy-agent": "^5.0.0",

View File

@ -139,6 +139,9 @@ if (!args.some(arg => arg === '--no-cleanup')) {
engines: pwInternalJSON.engines,
homepage: pwInternalJSON.homepage,
main: 'index.js',
bin: {
playwright: './lib/cli/cli.js',
},
exports: {
// Root import: we have a wrapper ES Module to support the following syntax.
// const { chromium } = require('playwright');

View File

@ -14,7 +14,4 @@
* limitations under the License.
*/
const { Playwright } = require('./lib/server/playwright');
const { setupInProcess } = require('./lib/inprocess');
module.exports = setupInProcess(new Playwright(__dirname, require('./browsers.json')['browsers']));
module.exports = require('./lib/inprocess');

View File

@ -52,6 +52,8 @@ function run_tests {
test_playwright_global_installation_cross_package
test_playwright_electron_should_work
test_electron_types
test_playwright_cli_should_work
test_playwright_cli_install_should_work
}
function test_screencast {
@ -64,10 +66,13 @@ function test_screencast {
PLAYWRIGHT_BROWSERS_PATH="${BROWSERS}" npm install ${PLAYWRIGHT_WEBKIT_TGZ}
PLAYWRIGHT_BROWSERS_PATH="${BROWSERS}" npm install ${PLAYWRIGHT_CHROMIUM_TGZ}
echo "Running screencast.js"
PLAYWRIGHT_BROWSERS_PATH="${BROWSERS}" node screencast.js playwright
PLAYWRIGHT_BROWSERS_PATH="${BROWSERS}" node screencast.js playwright-chromium
PLAYWRIGHT_BROWSERS_PATH="${BROWSERS}" node screencast.js playwright-webkit
PLAYWRIGHT_BROWSERS_PATH="${BROWSERS}" node screencast.js playwright-firefox
echo "${FUNCNAME[0]} success"
}
function test_typescript_types {
@ -90,6 +95,8 @@ function test_typescript_types {
echo "Checking types of ${PKG_NAME}"
echo "import { Page } from '${PKG_NAME}';" > "${PKG_NAME}.ts" && tsc "${PKG_NAME}.ts"
done;
echo "${FUNCNAME[0]} success"
}
function test_playwright_global_installation {
@ -102,8 +109,12 @@ function test_playwright_global_installation {
exit 1
fi
copy_test_scripts
echo "Running sanity.js"
node sanity.js playwright none
PLAYWRIGHT_BROWSERS_PATH="${BROWSERS}" node sanity.js playwright
echo "${FUNCNAME[0]} success"
}
function test_playwright_global_installation_cross_package {
@ -122,10 +133,13 @@ function test_playwright_global_installation_cross_package {
copy_test_scripts
echo "Running sanity.js"
# Every package should be able to launch.
PLAYWRIGHT_BROWSERS_PATH="${BROWSERS}" node sanity.js playwright-chromium all
PLAYWRIGHT_BROWSERS_PATH="${BROWSERS}" node sanity.js playwright-firefox all
PLAYWRIGHT_BROWSERS_PATH="${BROWSERS}" node sanity.js playwright-webkit all
echo "${FUNCNAME[0]} success"
}
# @see https://github.com/microsoft/playwright/issues/1651
@ -142,6 +156,8 @@ function test_playwright_global_installation_subsequent_installs {
# Note: the flag `--unahdnled-rejections=strict` will force node to terminate in case
# of UnhandledPromiseRejection.
PLAYWRIGHT_BROWSERS_PATH="${BROWSERS}" node --unhandled-rejections=strict node_modules/playwright/install.js
echo "${FUNCNAME[0]} success"
}
function test_skip_browser_download {
@ -157,6 +173,8 @@ function test_skip_browser_download {
echo "local browsers folder should be empty"
exit 1
fi
echo "${FUNCNAME[0]} success"
}
function test_playwright_should_work {
@ -176,10 +194,15 @@ function test_playwright_should_work {
exit 1
fi
copy_test_scripts
echo "Running sanity.js"
node sanity.js playwright
if [[ "${NODE_VERSION}" == *"v14."* ]]; then
echo "Running esm.js"
node esm-playwright.mjs
fi
echo "${FUNCNAME[0]} success"
}
function test_playwright_chromium_should_work {
@ -199,10 +222,15 @@ function test_playwright_chromium_should_work {
exit 1
fi
copy_test_scripts
echo "Running sanity.js"
node sanity.js playwright-chromium
if [[ "${NODE_VERSION}" == *"v14."* ]]; then
echo "Running esm.js"
node esm-playwright-chromium.mjs
fi
echo "${FUNCNAME[0]} success"
}
function test_playwright_webkit_should_work {
@ -222,10 +250,15 @@ function test_playwright_webkit_should_work {
exit 1
fi
copy_test_scripts
echo "Running sanity.js"
node sanity.js playwright-webkit
if [[ "${NODE_VERSION}" == *"v14."* ]]; then
echo "Running esm.js"
node esm-playwright-webkit.mjs
fi
echo "${FUNCNAME[0]} success"
}
function test_playwright_firefox_should_work {
@ -245,10 +278,15 @@ function test_playwright_firefox_should_work {
exit 1
fi
copy_test_scripts
echo "Running sanity.js"
node sanity.js playwright-firefox
if [[ "${NODE_VERSION}" == *"v14."* ]]; then
echo "Running esm.js"
node esm-playwright-firefox.mjs
fi
echo "${FUNCNAME[0]} success"
}
function test_playwright_electron_should_work {
@ -257,7 +295,11 @@ function test_playwright_electron_should_work {
npm install ${PLAYWRIGHT_ELECTRON_TGZ}
npm install electron@9.0
copy_test_scripts
echo "Running sanity-electron.js"
xvfb-run --auto-servernum -- bash -c "node sanity-electron.js"
echo "${FUNCNAME[0]} success"
}
function test_electron_types {
@ -267,13 +309,59 @@ function test_electron_types {
npm install -D typescript@3.8
npm install -D @types/node@10.17
echo "import { Page, electron, ElectronApplication, ElectronLauncher } from 'playwright-electron';" > "test.ts"
echo "Running tsc"
npx tsc "test.ts"
echo "${FUNCNAME[0]} success"
}
function test_playwright_cli_should_work {
initialize_test "${FUNCNAME[0]}"
npm install ${PLAYWRIGHT_TGZ}
echo "Running playwright screenshot"
node_modules/.bin/playwright screenshot about:blank one.png
if [[ ! -f one.png ]]; then
echo 'node_modules/.bin/playwright does not work'
exit 1
fi
npx playwright screenshot about:blank two.png
if [[ ! -f two.png ]]; then
echo 'npx playwright does not work'
exit 1
fi
echo "${FUNCNAME[0]} success"
}
function test_playwright_cli_install_should_work {
initialize_test "${FUNCNAME[0]}"
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 npm install ${PLAYWRIGHT_TGZ}
local BROWSERS="$(pwd -P)/browsers"
echo "Running playwright install"
PLAYWRIGHT_BROWSERS_PATH="${BROWSERS}" npx playwright install
if [[ ! -d "${BROWSERS}" ]]; then
echo "Directory for shared browsers was not created!"
exit 1
fi
copy_test_scripts
echo "Running sanity.js"
node sanity.js playwright none
PLAYWRIGHT_BROWSERS_PATH="${BROWSERS}" node sanity.js playwright
echo "${FUNCNAME[0]} success"
}
function initialize_test {
cd ${TEST_ROOT}
local TEST_NAME="./$1"
mkdir ${TEST_NAME} && cd ${TEST_NAME} && npm init -y
echo "====================================================================================="
echo "====================================================================================="
echo
@ -281,6 +369,7 @@ function initialize_test {
echo
echo "====================================================================================="
echo "====================================================================================="
mkdir ${TEST_NAME} && cd ${TEST_NAME} && npm init -y
}
# Run all tests

View File

@ -42,7 +42,7 @@ const installer = require(requireName + '/lib/install/installer');
await browser.close();
} catch (e) {
console.error(`Should be able to launch ${browserType} from ${requireName}`);
console.error(err);
console.error(e);
process.exit(1);
}
}

View File

@ -14,10 +14,5 @@
* limitations under the License.
*/
const { Playwright } = require('./lib/server/playwright');
const { setupInProcess } = require('./lib/inprocess');
const playwrightServer = new Playwright(__dirname, require('./browsers.json')['browsers']);
const playwright = setupInProcess(playwrightServer);
playwright.android = playwright._android;
module.exports = playwright;
module.exports = require('./lib/inprocess');
module.exports.android = module.exports._android;

View File

@ -14,10 +14,5 @@
* limitations under the License.
*/
const { Playwright } = require('./lib/server/playwright');
const { setupInProcess } = require('./lib/inprocess');
const playwrightServer = new Playwright(__dirname, require('./browsers.json')['browsers']);
const playwright = setupInProcess(playwrightServer);
playwright.electron = playwright._electron;
module.exports = playwright;
module.exports = require('./lib/inprocess');
module.exports.electron = module.exports._electron;

355
src/cli/cli.ts Executable file
View File

@ -0,0 +1,355 @@
#!/usr/bin/env node
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable no-console */
import * as path from 'path';
import * as program from 'commander';
import * as os from 'os';
import * as fs from 'fs';
import { installBrowsersWithProgressBar } from '../install/installer';
// TODO: we can import from '../..' instead, but that requires generating types
// before build, and currently type generator depends on the build.
import type { Browser, BrowserContext, Page, BrowserType } from '../client/api';
import type { Playwright } from '../client/playwright';
import type { BrowserContextOptions, LaunchOptions } from '../client/types';
const playwright = require('../inprocess') as Playwright;
program
.version('Version ' + require('../../package.json').version)
.option('-b, --browser <browserType>', 'browser to use, one of cr, chromium, ff, firefox, wk, webkit', 'chromium')
.option('--color-scheme <scheme>', 'emulate preferred color scheme, "light" or "dark"')
.option('--device <deviceName>', 'emulate device, for example "iPhone 11"')
.option('--geolocation <coordinates>', 'specify geolocation coordinates, for example "37.819722,-122.478611"')
.option('--lang <language>', 'specify language / locale, for example "en-GB"')
.option('--load-storage <filename>', 'load context storage state from the file, previously saved with --save-storage')
.option('--proxy-server <proxy>', 'specify proxy server, for example "http://myproxy:3128" or "socks5://myproxy:8080"')
.option('--save-storage <filename>', 'save context storage state at the end, for later use with --load-storage')
.option('--timezone <time zone>', 'time zone to emulate, for example "Europe/Rome"')
.option('--timeout <timeout>', 'timeout for Playwright actions in milliseconds', '10000')
.option('--user-agent <ua string>', 'specify user agent string')
.option('--viewport-size <size>', 'specify browser viewport size in pixels, for example "1280, 720"');
program
.command('open [url]')
.description('open page in browser specified via -b, --browser')
.action(function(url, command) {
open(command.parent, url);
}).on('--help', function() {
console.log('');
console.log('Examples:');
console.log('');
console.log(' $ open');
console.log(' $ -b webkit open https://example.com');
});
const browsers = [
{ alias: 'cr', name: 'Chromium', type: 'chromium' },
{ alias: 'ff', name: 'Firefox', type: 'firefox' },
{ alias: 'wk', name: 'WebKit', type: 'webkit' },
];
for (const {alias, name, type} of browsers) {
program
.command(`${alias} [url]`)
.description(`open page in ${name}`)
.action(function(url, command) {
open({ ...command.parent, browser: type }, url);
}).on('--help', function() {
console.log('');
console.log('Examples:');
console.log('');
console.log(` $ ${alias} https://example.com`);
});
}
program
.command('screenshot <url> <filename>')
.description('capture a page screenshot')
.option('--wait-for-selector <selector>', 'wait for selector before taking a screenshot')
.option('--wait-for-timeout <timeout>', 'wait for timeout in milliseconds before taking a screenshot')
.option('--full-page', 'whether to take a full page screenshot (entire scrollable area)')
.action(function(url, filename, command) {
screenshot(command.parent, command, url, filename);
}).on('--help', function() {
console.log('');
console.log('Examples:');
console.log('');
console.log(' $ -b webkit screenshot https://example.com example.png');
});
program
.command('pdf <url> <filename>')
.description('save page as pdf')
.option('--wait-for-selector <selector>', 'wait for given selector before saving as pdf')
.option('--wait-for-timeout <timeout>', 'wait for given timeout in milliseconds before saving as pdf')
.action(function(url, filename, command) {
pdf(command.parent, command, url, filename);
}).on('--help', function() {
console.log('');
console.log('Examples:');
console.log('');
console.log(' $ pdf https://example.com example.pdf');
});
program
.command('install')
.description('Ensure browsers necessary for this version of Playwright are installed')
.action(function() {
let browsersJsonDir = path.dirname(process.execPath);
if (!fs.existsSync(path.join(browsersJsonDir, 'browsers.json'))) {
browsersJsonDir = path.join(__dirname, '..', '..');
if (!fs.existsSync(path.join(browsersJsonDir, 'browsers.json')))
throw new Error('Failed to find browsers.json in ' + browsersJsonDir);
}
installBrowsersWithProgressBar(browsersJsonDir).catch((e: any) => {
console.log(`Failed to install browsers\n${e}`);
process.exit(1);
});
});
program.parse(process.argv);
type Options = {
browser: string;
colorScheme?: string;
device?: string;
geolocation?: string;
lang?: string;
loadStorage?: string;
proxyServer?: string;
saveStorage?: string;
timeout: string;
timezone?: string;
viewportSize?: string;
userAgent?: string;
};
type CaptureOptions = {
waitForSelector?: string;
waitForTimeout?: string;
fullPage: boolean;
};
async function launchContext(options: Options, headless: boolean): Promise<{ browser: Browser, browserName: string, launchOptions: LaunchOptions, contextOptions: BrowserContextOptions, context: BrowserContext }> {
validateOptions(options);
const browserType = lookupBrowserType(options);
const launchOptions: LaunchOptions = { headless };
const contextOptions: BrowserContextOptions =
// Copy the device descriptor since we have to compare and modify the options.
options.device ? { ...playwright.devices[options.device] } : {};
// In headful mode, use host device scale factor for things to look nice.
// In headless, keep things the way it works in Playwright by default.
// Assume high-dpi on MacOS. TODO: this is not perfect.
if (!headless)
contextOptions.deviceScaleFactor = os.platform() === 'darwin' ? 2 : 1;
// Work around the WebKit GTK scrolling issue.
if (browserType.name() === 'webkit' && process.platform === 'linux') {
delete contextOptions.hasTouch;
delete contextOptions.isMobile;
}
if (contextOptions.isMobile && browserType.name() === 'firefox')
contextOptions.isMobile = undefined;
// Proxy
if (options.proxyServer) {
launchOptions.proxy = {
server: options.proxyServer
};
}
const browser = await browserType.launch(launchOptions);
// Viewport size
if (options.viewportSize) {
try {
const [ width, height ] = options.viewportSize.split(',').map(n => parseInt(n, 10));
contextOptions.viewport = { width, height };
} catch (e) {
console.log('Invalid window size format: use "width, height", for example --window-size=800,600');
process.exit(0);
}
}
// Geolocation
if (options.geolocation) {
try {
const [latitude, longitude] = options.geolocation.split(',').map(n => parseFloat(n.trim()));
contextOptions.geolocation = {
latitude,
longitude
};
} catch (e) {
console.log('Invalid geolocation format: user lat, long, for example --geolocation="37.819722,-122.478611"');
process.exit(0);
}
contextOptions.permissions = ['geolocation'];
}
// User agent
if (options.userAgent)
contextOptions.userAgent = options.userAgent;
// Lang
if (options.lang)
contextOptions.locale = options.lang;
// Color scheme
if (options.colorScheme)
contextOptions.colorScheme = options.colorScheme as 'dark' | 'light';
// Timezone
if (options.timezone)
contextOptions.timezoneId = options.timezone;
// Storage
if (options.loadStorage)
contextOptions.storageState = options.loadStorage;
// Close app when the last window closes.
const context = await browser.newContext(contextOptions);
let closingBrowser = false;
async function closeBrowser() {
// We can come here multiple times. For example, saving storage creates
// a temporary page and we call closeBrowser again when that page closes.
if (closingBrowser)
return;
closingBrowser = true;
if (options.saveStorage)
await context.storageState({ path: options.saveStorage }).catch(e => null);
await browser.close();
}
context.on('page', page => {
page.on('close', () => {
const hasPage = browser.contexts().some(context => context.pages().length > 0);
if (hasPage)
return;
// Avoid the error when the last page is closed because the browser has been closed.
closeBrowser().catch(e => null);
});
});
if (options.timeout) {
context.setDefaultTimeout(parseInt(options.timeout, 10));
context.setDefaultNavigationTimeout(parseInt(options.timeout, 10));
}
// Omit options that we add automatically for presentation purpose.
delete launchOptions.headless;
delete contextOptions.deviceScaleFactor;
return { browser, browserName: browserType.name(), context, contextOptions, launchOptions };
}
async function openPage(context: BrowserContext, url: string | undefined): Promise<Page> {
const page = await context.newPage();
if (url) {
if (fs.existsSync(url))
url = 'file://' + path.resolve(url);
else if (!url.startsWith('http') && !url.startsWith('file://') && !url.startsWith('about:'))
url = 'http://' + url;
await page.goto(url);
}
return page;
}
async function open(options: Options, url: string | undefined) {
const { context } = await launchContext(options, false);
await openPage(context, url);
if (process.env.PWCLI_EXIT_FOR_TEST)
await Promise.all(context.pages().map(p => p.close()));
}
async function waitForPage(page: Page, captureOptions: CaptureOptions) {
if (captureOptions.waitForSelector) {
console.log(`Waiting for selector ${captureOptions.waitForSelector}...`);
await page.waitForSelector(captureOptions.waitForSelector);
}
if (captureOptions.waitForTimeout) {
console.log(`Waiting for timeout ${captureOptions.waitForTimeout}...`);
await page.waitForTimeout(parseInt(captureOptions.waitForTimeout, 10));
}
}
async function screenshot(options: Options, captureOptions: CaptureOptions, url: string, path: string) {
const { browser, context } = await launchContext(options, true);
console.log('Navigating to ' + url);
const page = await openPage(context, url);
await waitForPage(page, captureOptions);
console.log('Capturing screenshot into ' + path);
await page.screenshot({ path, fullPage: !!captureOptions.fullPage });
await browser.close();
}
async function pdf(options: Options, captureOptions: CaptureOptions, url: string, path: string) {
if (options.browser !== 'chromium') {
console.error('PDF creation is only working with Chromium');
process.exit(1);
}
const { browser, context } = await launchContext({ ...options, browser: 'chromium' }, true);
console.log('Navigating to ' + url);
const page = await openPage(context, url);
await waitForPage(page, captureOptions);
console.log('Saving as pdf into ' + path);
await page.pdf!({ path });
await browser.close();
}
function lookupBrowserType(options: Options): BrowserType {
let name = options.browser;
if (options.device) {
const device = playwright.devices[options.device];
name = device.defaultBrowserType;
}
switch (name) {
case 'chromium': return playwright.chromium!;
case 'webkit': return playwright.webkit!;
case 'firefox': return playwright.firefox!;
case 'cr': return playwright.chromium!;
case 'wk': return playwright.webkit!;
case 'ff': return playwright.firefox!;
}
program.help();
}
function validateOptions(options: Options) {
if (options.device && !(options.device in playwright.devices)) {
console.log(`Device descriptor not found: '${options.device}', available devices are:`);
for (const name in playwright.devices)
console.log(` "${name}"`);
process.exit(0);
}
if (options.colorScheme && !['light', 'dark'].includes(options.colorScheme)) {
console.log('Invalid color scheme, should be one of "light", "dark"');
process.exit(0);
}
}

View File

@ -15,7 +15,7 @@
*/
import { DispatcherConnection } from './dispatchers/dispatcher';
import type { Playwright as PlaywrightImpl } from './server/playwright';
import { Playwright as PlaywrightImpl } from './server/playwright';
import type { Playwright as PlaywrightAPI } from './client/playwright';
import { PlaywrightDispatcher } from './dispatchers/playwrightDispatcher';
import { Connection } from './client/connection';
@ -23,8 +23,11 @@ import { BrowserServerLauncherImpl } from './browserServerImpl';
import { installDebugController } from './debug/debugController';
import { installTracer } from './trace/tracer';
import { installHarTracer } from './trace/harTracer';
import * as path from 'path';
function setupInProcess(): PlaywrightAPI {
const playwright = new PlaywrightImpl(path.join(__dirname, '..'), require(path.join(__dirname, '..', 'browsers.json'))['browsers']);
export function setupInProcess(playwright: PlaywrightImpl): PlaywrightAPI {
installDebugController();
installTracer();
installHarTracer();
@ -50,3 +53,5 @@ export function setupInProcess(playwright: PlaywrightImpl): PlaywrightAPI {
(playwrightAPI as any)._toImpl = (x: any) => dispatcherConnection._dispatchers.get(x._guid)!._object;
return playwrightAPI;
}
module.exports = setupInProcess();

View File

@ -135,6 +135,9 @@ DEPS['src/debug/'] = ['src/utils/', 'src/generated/', 'src/server/**', 'src/debu
DEPS['src/remote/'] = ['src/client/', 'src/debug/', 'src/dispatchers/', 'src/server/', 'src/server/electron/', 'src/trace/'];
DEPS['src/service.ts'] = ['src/remote/'];
// CLI should only use client-side features.
DEPS['src/cli/'] = ['src/client/**', 'src/install/**'];
checkDeps().catch(e => {
console.error(e && e.stack ? e.stack : e);
process.exit(1);