Skip to content

Commit

Permalink
add docker test command
Browse files Browse the repository at this point in the history
  • Loading branch information
aslushnikov committed Sep 6, 2022
1 parent 1070f4e commit 3b06d19
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
1 change: 1 addition & 0 deletions packages/playwright-core/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ if (!process.env.PW_LANG_NAME) {
}

require(playwrightTestPackagePath).addTestCommands(program);
require(playwrightTestPackagePath).addDockerTestCommand(dockerCommand);
} else {
{
const command = program.command('test').allowUnknownOption(true);
Expand Down
38 changes: 20 additions & 18 deletions packages/playwright-test/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ import { baseFullConfig, defaultTimeout, fileIsModule } from './loader';
import type { TraceMode } from './types';

export function addTestCommands(program: Command) {
addTestCommand(program);
addTestCommand(program, false /* isDocker */);
addShowReportCommand(program);
addListFilesCommand(program);
}

function addTestCommand(program: Command) {
export function addDockerTestCommand(subCommand: Command) {
addTestCommand(subCommand, true /* isDocker */);
}

function addTestCommand(program: Command, isDocker: boolean) {
const command = program.command('test [test-filter...]');
command.description('Run tests with Playwright Test');
command.option('--browser <browser>', `Browser to use for tests, one of "all", "chromium", "firefox" or "webkit" (default: "chromium")`);
Expand All @@ -53,7 +57,6 @@ function addTestCommand(program: Command) {
command.option('--output <dir>', `Folder for output artifacts (default: "test-results")`);
command.option('--pass-with-no-tests', `Makes test run succeed even if no tests were found`);
command.option('--quiet', `Suppress stdio`);
command.option('--docker', `Launch tests inside docker container`);
command.option('--repeat-each <N>', `Run each test N times (default: 1)`);
command.option('--reporter <reporter>', `Reporter to use, comma-separated, can be ${builtInReporters.map(name => `"${name}"`).join(', ')} (default: "${baseFullConfig.reporter[0]}")`);
command.option('--retries <retries>', `Maximum retry count for flaky tests, zero for no retries (default: no retries)`);
Expand All @@ -65,6 +68,16 @@ function addTestCommand(program: Command) {
command.option('-x', `Stop after the first failure`);
command.action(async (args, opts) => {
try {
if (isDocker) {
const wsEndpoint = await docker.wsEndpoint();
if (!wsEndpoint)
throw new Error(`Playwright docker container is not running; run "npx playwright docker start" first`);
process.env.PW_TEST_CONNECT_WS_ENDPOINT = await docker.wsEndpoint();
process.env.PW_TEST_CONNECT_HEADERS = JSON.stringify({
'x-playwright-proxy': '*',
});
process.env.PW_TEST_IS_DOCKER = '1';
}
await runTests(args, opts);
} catch (e) {
console.error(e);
Expand All @@ -76,10 +89,10 @@ Arguments [test-filter...]:
Pass arguments to filter test files. Each argument is treated as a regular expression.
Examples:
$ npx playwright test my.spec.ts
$ npx playwright test some.spec.ts:42
$ npx playwright test --headed
$ npx playwright test --browser=webkit`);
$ npx playwright${isDocker ? ' docker ' : ' '}test my.spec.ts
$ npx playwright${isDocker ? ' docker ' : ' '}test some.spec.ts:42
$ npx playwright${isDocker ? ' docker ' : ' '}test --headed
$ npx playwright${isDocker ? ' docker ' : ' '}test --browser=webkit`);
}

function addListFilesCommand(program: Command) {
Expand Down Expand Up @@ -127,17 +140,6 @@ async function runTests(args: string[], opts: { [key: string]: any }) {
});
}

if (opts.docker) {
const wsEndpoint = await docker.wsEndpoint();
if (!wsEndpoint)
throw new Error(`Playwright docker container is not running; run "npx playwright docker start" first`);
process.env.PW_TEST_CONNECT_WS_ENDPOINT = await docker.wsEndpoint();
process.env.PW_TEST_CONNECT_HEADERS = JSON.stringify({
'x-playwright-proxy': '*',
});
process.env.PW_TEST_IS_DOCKER = '1';
}

if (opts.headed || opts.debug)
overrides.use = { headless: false };
if (opts.debug) {
Expand Down

0 comments on commit 3b06d19

Please sign in to comment.