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(test runner): add project.default option (#42750)
Pavel Feldmanf42448423652
diff --git a/docs/src/test-api/class-testproject.md b/docs/src/test-api/class-testproject.mdindex ce55beb069..59dce2f16f 100644--- a/docs/src/test-api/class-testproject.md+++ b/docs/src/test-api/class-testproject.md@@ -86,6 +86,38 @@ export default defineConfig({});```+## property: TestProject.default+* since: v1.64+- type: ?<[boolean]>++Whether the project runs when no `--project` command line option is passed. Defaults to `true`.++To run a project with `default: false`, select it with the `--project` command line option.++A project with `default: false` still runs when another running project lists it in [`property: TestProject.dependencies`] or [`property: TestProject.teardown`].++**Usage**++```js title="playwright.config.ts"+import { defineConfig } from '@playwright/test';++export default defineConfig({+ projects: [+ {+ name: 'chromium',+ use: devices['Desktop Chrome'],+ },+ {+ name: 'slow',+ testDir: './slow-tests',+ default: false,+ },+ ],+});+```++Now `npx playwright test` only runs `chromium`, while `npx playwright test --project=slow` runs `slow`.+## property: TestProject.expect* since: v1.10- type: ?<[Object]>diff --git a/packages/playwright/src/common/config.ts b/packages/playwright/src/common/config.tsindex a237ccd041..bb6f1b2bc4 100644--- a/packages/playwright/src/common/config.ts+++ b/packages/playwright/src/common/config.ts@@ -164,6 +164,7 @@ export class FullProjectInternal {readonly respectGitIgnore: boolean;readonly snapshotPathTemplate: string | undefined;readonly workers: number | undefined;+ readonly default: boolean;id = '';deps: FullProjectInternal[] = [];teardown: FullProjectInternal | undefined;@@ -209,6 +210,7 @@ export class FullProjectInternal {this.workers = projectConfig.workers ? resolveWorkers(projectConfig.workers) : undefined;if (configCLIOverrides.debug && this.workers)this.workers = 1;+ this.default = projectConfig.default ?? true;}}diff --git a/packages/playwright/src/runner/projectUtils.ts b/packages/playwright/src/runner/projectUtils.tsindex 60be67d93e..72ace25e42 100644--- a/packages/playwright/src/runner/projectUtils.ts+++ b/packages/playwright/src/runner/projectUtils.ts@@ -33,9 +33,9 @@ function wildcardPatternToRegExp(pattern: string): RegExp {return new RegExp('^' + pattern.split('*').map(escapeRegExp).join('.*') + '$', 'ig');}-export function filterProjects(projects: commonConfig.FullProjectInternal[], projectNames?: string[]): commonConfig.FullProjectInternal[] {+export function filterProjects(projects: commonConfig.FullProjectInternal[], projectNames?: string[], includeNonDefault?: boolean): commonConfig.FullProjectInternal[] {if (!projectNames)- return [...projects];+ return projects.filter(project => includeNonDefault || project.default);const projectNamesToFind = new Set<string>();const unmatchedProjectNames = new Map<string, string>();diff --git a/packages/playwright/src/runner/tasks.ts b/packages/playwright/src/runner/tasks.tsindex d42800427f..17ea0785a4 100644--- a/packages/playwright/src/runner/tasks.ts+++ b/packages/playwright/src/runner/tasks.ts@@ -59,6 +59,7 @@ export type TestRunOptions = {grepInvert?: string;onlyChanged?: string;projectFilter?: string[];+ includeNonDefaultProjects?: boolean;listMode?: boolean;passWithNoTests?: boolean;lastFailed?: boolean;@@ -98,7 +99,7 @@ export class TestRun {this.config = config;this.options = options ?? {};this.reporter = reporter;- this.filteredProjects = filterProjects(config.projects, this.options.projectFilter);+ this.filteredProjects = filterProjects(config.projects, this.options.projectFilter, this.options.includeNonDefaultProjects);config.config.filteredProjects = this.filteredProjects.map(p => p.project);}diff --git a/packages/playwright/src/runner/testRunner.ts b/packages/playwright/src/runner/testRunner.tsindex 5bef691fc9..df44df98a1 100644--- a/packages/playwright/src/runner/testRunner.ts+++ b/packages/playwright/src/runner/testRunner.ts@@ -203,7 +203,8 @@ export class TestRunner extends EventEmitter<TestRunnerEventMap> {if (!config)return { status: 'failed' };- const options: TestRunOptions = { projectFilter: projects?.length ? projects : undefined };+ // Non-default projects are listed so that they can be selected by name.+ const options: TestRunOptions = { projectFilter: projects?.length ? projects : undefined, includeNonDefaultProjects: true };const status = await runTasks(new TestRun(config, reporter, options), [createListFilesTask(),createReportBeginTask(),@@ -242,6 +243,8 @@ export class TestRunner extends EventEmitter<TestRunnerEventMap> {grep: params.grep,grepInvert: params.grepInvert,projectFilter: params.projects?.length ? params.projects : undefined,+ // Non-default projects are listed so that they can be selected by name.+ includeNonDefaultProjects: true,onlyChanged: params.onlyChanged ? 'HEAD' : undefined,listMode: true,};diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.tsindex 07effc78c1..c458761d89 100644--- a/packages/playwright/types/test.d.ts+++ b/packages/playwright/types/test.d.ts@@ -135,6 +135,40 @@ interface TestProject<TestArgs = {}, WorkerArgs = {}> {* all projects.*/use?: UseOptions<TestArgs, WorkerArgs>;+ /**+ * Whether the project runs when no `--project` command line option is passed. Defaults to `true`.+ *+ * To run a project with `default: false`, select it with the `--project` command line option.+ *+ * A project with `default: false` still runs when another running project lists it in+ * [testProject.dependencies](https://playwright.dev/docs/api/class-testproject#test-project-dependencies) or+ * [testProject.teardown](https://playwright.dev/docs/api/class-testproject#test-project-teardown).+ *+ * **Usage**+ *+ * ```js+ * // playwright.config.ts+ * import { defineConfig } from '@playwright/test';+ *+ * export default defineConfig({+ * projects: [+ * {+ * name: 'chromium',+ * use: devices['Desktop Chrome'],+ * },+ * {+ * name: 'slow',+ * testDir: './slow-tests',+ * default: false,+ * },+ * ],+ * });+ * ```+ *+ * Now `npx playwright test` only runs `chromium`, while `npx playwright test --project=slow` runs `slow`.+ */+ default?: boolean;+/*** List of projects that need to run before any test in this project runs. Dependencies can be useful for configuring* the global setup actions in a way that every action is in a form of a test. Passing `--no-deps` argument ignoresdiff --git a/tests/playwright-test/config.spec.ts b/tests/playwright-test/config.spec.tsindex 24a709c51d..1b1d10286e 100644--- a/tests/playwright-test/config.spec.ts+++ b/tests/playwright-test/config.spec.ts@@ -338,6 +338,70 @@ test('should filter by project wildcard and exact name', async ({ runInlineTestexpect(new Set(result.outputLines)).toEqual(new Set(['first', 'fooBar', 'prefix']));});+test('should not run non-default project by default', async ({ runInlineTest }) => {+ const result = await runInlineTest({+ 'playwright.config.js': `+ module.exports = {+ projects: [+ { name: 'default' },+ { name: 'non-default', default: false },+ ]+ };+ `,+ 'a.test.js': `+ const { test } = require('@playwright/test');+ test('one', async ({}) => {+ console.log('%%' + test.info().project.name);+ });+ `+ });+ expect(result.exitCode).toBe(0);+ expect(result.outputLines).toEqual(['default']);+});++test('should run non-default project when selected by name', async ({ runInlineTest }) => {+ const result = await runInlineTest({+ 'playwright.config.js': `+ module.exports = {+ projects: [+ { name: 'default' },+ { name: 'non-default', default: false },+ ]+ };+ `,+ 'a.test.js': `+ const { test } = require('@playwright/test');+ test('one', async ({}) => {+ console.log('%%' + test.info().project.name);+ });+ `+ }, { '--project': ['Non-Default'] });+ expect(result.exitCode).toBe(0);+ expect(result.outputLines).toEqual(['non-default']);+});++test('should match non-default project by wildcard', async ({ runInlineTest }) => {+ const result = await runInlineTest({+ 'playwright.config.js': `+ module.exports = {+ projects: [+ { name: 'foo-default' },+ { name: 'foo-non-default', default: false },+ { name: 'bar-non-default', default: false },+ ]+ };+ `,+ 'a.test.js': `+ const { test } = require('@playwright/test');+ test('one', async ({}) => {+ console.log('%%' + test.info().project.name);+ });+ `+ }, { '--project': ['foo-*'] });+ expect(result.exitCode).toBe(0);+ expect(new Set(result.outputLines)).toEqual(new Set(['foo-default', 'foo-non-default']));+});+test('should print nice error when project is unknown', async ({ runInlineTest }) => {const { output, exitCode } = await runInlineTest({'playwright.config.ts': `diff --git a/tests/playwright-test/deps.spec.ts b/tests/playwright-test/deps.spec.tsindex 26417bd7bc..e1900f9e1a 100644--- a/tests/playwright-test/deps.spec.ts+++ b/tests/playwright-test/deps.spec.ts@@ -452,6 +452,29 @@ test('should run project with teardown', async ({ runInlineTest }) => {expect(result.outputLines).toEqual(['A', 'B']);});+test('should run non-default dependency and teardown', async ({ runInlineTest }) => {+ const result = await runInlineTest({+ 'playwright.config.ts': `+ module.exports = {+ projects: [+ { name: 'setup', teardown: 'teardown', default: false },+ { name: 'teardown', default: false },+ { name: 'A', dependencies: ['setup'] },+ { name: 'B', default: false },+ ],+ };`,+ 'test.spec.ts': `+ import { test, expect } from '@playwright/test';+ test('test', async ({}, testInfo) => {+ console.log('\\n%%' + testInfo.project.name);+ });+ `,+ }, { workers: 1 });+ expect(result.exitCode).toBe(0);+ expect(result.passed).toBe(3);+ expect(result.outputLines).toEqual(['setup', 'A', 'teardown']);+});+test('should run teardown after dependents', async ({ runInlineTest }) => {const result = await runInlineTest({'playwright.config.ts': `diff --git a/tests/playwright-test/test-server.spec.ts b/tests/playwright-test/test-server.spec.tsindex 5cc9544da8..9febdbe66a 100644--- a/tests/playwright-test/test-server.spec.ts+++ b/tests/playwright-test/test-server.spec.ts@@ -150,6 +150,28 @@ test('should list tests with testIdAttribute', async ({ startTestServer, writeFiexpect(onProject.use.testIdAttribute).toBe('testId');});+test('should list non-default projects', async ({ startTestServer, writeFiles }) => {+ await writeFiles({+ 'a.test.ts': `+ import { test } from '@playwright/test';+ test('foo', () => {});+ `,+ 'playwright.config.ts': `+ module.exports = {+ projects: [+ { name: 'default' },+ { name: 'non-default', default: false },+ ]+ };+ `,+ });++ const testServerConnection = await startTestServer();+ const events = await testServerConnection.listTests({});+ const projectNames = events.report.filter(e => e.method === 'onProject').map(e => e.params.project.name);+ expect(projectNames).toEqual(['default', 'non-default']);+});+test('stdio interception', async ({ startTestServer, writeFiles }) => {const testServerConnection = await startTestServer();await testServerConnection.initialize({ interceptStdio: true });