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
feat(cli): make run-driver work (#4836)
Dmitry Gozman068d8612a7ec
diff --git a/src/cli/cli.ts b/src/cli/cli.tsindex 76a643cb43..b84e9a7433 100755--- a/src/cli/cli.ts+++ b/src/cli/cli.ts@@ -22,7 +22,6 @@ 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';import * as consoleApiSource from '../generated/consoleApiSource';import { OutputMultiplexer, TerminalOutput, FileOutput } from './codegen/outputs';import { CodeGenerator, CodeGeneratorOutput } from './codegen/codeGenerator';@@ -30,6 +29,7 @@ import { JavaScriptLanguageGenerator, LanguageGenerator } from './codegen/languaimport { PythonLanguageGenerator } from './codegen/languages/python';import { CSharpLanguageGenerator } from './codegen/languages/csharp';import { RecorderController } from './codegen/recorderController';+import { runServer, printApiJson, installBrowsers } from './driver';import type { Browser, BrowserContext, Page, BrowserType, BrowserContextOptions, LaunchOptions } from '../..';import * as playwright from '../..';@@ -130,19 +130,18 @@ 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) => {+ installBrowsers().catch((e: any) => {console.log(`Failed to install browsers\n${e}`);process.exit(1);});});-program.parse(process.argv);+if (process.argv[2] === 'run-driver')+ runServer();+else if (process.argv[2] === 'print-api-json')+ printApiJson();+else+ program.parse(process.argv);type Options = {diff --git a/src/driver.ts b/src/cli/driver.tssimilarity index 51%rename from src/driver.tsrename to src/cli/driver.tsindex ad190a133e..2d58b43c9f 100644--- a/src/driver.ts+++ b/src/cli/driver.ts@@ -14,33 +14,26 @@* limitations under the License.*/-import * as fs from 'fs';-import * as util from 'util';-import { installDebugController } from './debug/debugController';-import { DispatcherConnection } from './dispatchers/dispatcher';-import { PlaywrightDispatcher } from './dispatchers/playwrightDispatcher';-import { installBrowsersWithProgressBar } from './install/installer';-import { Transport } from './protocol/transport';-import { Playwright } from './server/playwright';-import { gracefullyCloseAll } from './server/processLauncher';-import { installHarTracer } from './trace/harTracer';-import { installTracer } from './trace/tracer';---const readFileAsync = util.promisify(fs.readFile);-const writeFileAsync = util.promisify(fs.writeFile);+/* eslint-disable no-console */-export async function copyPrintDeps(destination: string) {- const content = await readFileAsync(require.resolve('../bin/PrintDeps.exe'));- await writeFileAsync(destination, content);-}+import * as fs from 'fs';+import * as path from 'path';+import { installDebugController } from '../debug/debugController';+import { DispatcherConnection } from '../dispatchers/dispatcher';+import { PlaywrightDispatcher } from '../dispatchers/playwrightDispatcher';+import { installBrowsersWithProgressBar } from '../install/installer';+import { Transport } from '../protocol/transport';+import { Playwright } from '../server/playwright';+import { gracefullyCloseAll } from '../server/processLauncher';+import { installHarTracer } from '../trace/harTracer';+import { installTracer } from '../trace/tracer';-export async function installWithProgressBar(location: string) {- await installBrowsersWithProgressBar(location);+export function printApiJson() {+ console.log(JSON.stringify(require('../../api.json')));}-export async function apiJson(): Promise<string> {- return (await readFileAsync(require.resolve('../docs/api.json'))).toString();+export function printProtocol() {+ console.log(fs.readFileSync(path.join(__dirname, '..', '..', 'protocol.yml'), 'utf8'));}export function runServer() {@@ -62,9 +55,16 @@ export function runServer() {process.exit(0);};- const playwright = new Playwright(__dirname, require('../browsers.json')['browsers']);+ const playwright = new Playwright(__dirname, require('../../browsers.json')['browsers']);new PlaywrightDispatcher(dispatcherConnection.rootDispatcher(), playwright);}-if (process.argv[2] === 'serve')- runServer();+export async function installBrowsers() {+ 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);+ }+ await installBrowsersWithProgressBar(browsersJsonDir);+}diff --git a/test/fixtures.ts b/test/fixtures.tsindex 1c784a0854..7c520072c3 100644--- a/test/fixtures.ts+++ b/test/fixtures.ts@@ -108,7 +108,7 @@ fixtures.playwright.override(async ({ browserName, testWorkerIndex, platform, moif (mode === 'driver') {require('../lib/utils/utils').setUnderTest();const connection = new Connection();- const spawnedProcess = childProcess.fork(path.join(__dirname, '..', 'lib', 'driver.js'), ['serve'], {+ const spawnedProcess = childProcess.fork(path.join(__dirname, '..', 'lib', 'cli', 'cli.js'), ['run-driver'], {stdio: 'pipe',detached: true,});diff --git a/utils/check_deps.js b/utils/check_deps.jsindex f0c1c27f25..fdfece79f4 100644--- a/utils/check_deps.js+++ b/utils/check_deps.js@@ -123,7 +123,7 @@ DEPS['src/server/android/'] = [...DEPS['src/server/'], 'src/server/chromium/', 'DEPS['src/server/electron/'] = [...DEPS['src/server/'], 'src/server/chromium/'];DEPS['src/server/playwright.ts'] = [...DEPS['src/server/'], 'src/server/chromium/', 'src/server/webkit/', 'src/server/firefox/', 'src/server/android/', 'src/server/electron/'];-DEPS['src/driver.ts'] = DEPS['src/inprocess.ts'] = DEPS['src/browserServerImpl.ts'] = ['src/**'];+DEPS['src/cli/driver.ts'] = DEPS['src/inprocess.ts'] = DEPS['src/browserServerImpl.ts'] = ['src/**'];// Tracing is a client/server plugin, nothing should depend on it.DEPS['src/trace/'] = ['src/utils/', 'src/client/**', 'src/server/**'];