Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat: Allow configuring last-run output file location (#40806)

DJ Mountney561bb37ae81f
diff --git a/docs/src/running-tests-js.md b/docs/src/running-tests-js.md
index 32223fdd0e..0925ef7db8 100644
--- a/docs/src/running-tests-js.md
+++ b/docs/src/running-tests-js.md
@@ -91,6 +91,11 @@ To run only the tests that failed in the last test run, first run your tests and
npx playwright test --last-failed
```
+Playwright stores the list of failed tests from the previous run in `<outputDir>/.last-run.json` (see [`property: TestConfig.outputDir`](./test-configuration.md)). To use a different file path, pass `--last-failed-file=<path>` or set `PLAYWRIGHT_LAST_RUN_OUTPUT_FILE`.
+
+```bash
+npx playwright test --last-failed --last-failed-file=.cache/last-run-shard-1.json
+```
### Run tests in VS Code
diff --git a/docs/src/test-cli-js.md b/docs/src/test-cli-js.md
index 979fd99b17..c6c744b242 100644
--- a/docs/src/test-cli-js.md
+++ b/docs/src/test-cli-js.md
@@ -91,6 +91,7 @@ npx playwright test --ui
| `--ignore-snapshots` | Ignore screenshot and snapshot expectations. |
| `-j <workers>` or `--workers <workers>` | Number of concurrent workers or percentage of logical CPU cores, use 1 to run in a single worker (default: 50%). |
| `--last-failed` | Only re-run the failures. |
+| `--last-failed-file <file>` | Override the default last-run JSON path for `--last-failed` (default: `<outputDir>/.last-run.json`). Same as `PLAYWRIGHT_LAST_RUN_OUTPUT_FILE` environment variable. |
| `--list` | Collect all the tests and report them, but do not run. |
| `--max-failures <N>` or `-x` | Stop after the first `N` failures. Passing `-x` stops after the first failure. |
| `--no-deps` | Do not run project dependencies. |
diff --git a/packages/playwright/src/cli/testActions.ts b/packages/playwright/src/cli/testActions.ts
index c97ed6dcf8..c4bc09e2df 100644
--- a/packages/playwright/src/cli/testActions.ts
+++ b/packages/playwright/src/cli/testActions.ts
@@ -40,6 +40,7 @@ export async function runTests(args: string[], opts: { [key: string]: any }) {
projectFilter: opts.project || undefined,
passWithNoTests: !!opts.passWithNoTests,
lastFailed: !!opts.lastFailed,
+ lastFailedFile: opts.lastFailedFile,
testList: opts.testList ? path.resolve(process.cwd(), opts.testList) : undefined,
testListInvert: opts.testListInvert ? path.resolve(process.cwd(), opts.testListInvert) : undefined,
shardWeights: resolveShardWeightsOption(),
diff --git a/packages/playwright/src/program.ts b/packages/playwright/src/program.ts
index 1366d686d0..f4df8b0d7c 100644
--- a/packages/playwright/src/program.ts
+++ b/packages/playwright/src/program.ts
@@ -200,6 +200,7 @@ const testOptions: [string, { description: string, choices?: string[], preset?:
['--headed', { description: `Run tests in headed browsers (default: headless)` }],
['--ignore-snapshots', { description: `Ignore screenshot and snapshot expectations` }],
['--last-failed', { description: `Only re-run the failures` }],
+ ['--last-failed-file <file>', { description: `Override the default path for the last-run JSON file used with --last-failed (default: <outputDir>/.last-run.json). Same as PLAYWRIGHT_LAST_RUN_OUTPUT_FILE environment variable.` }],
['--list', { description: `Collect all the tests and report them, but do not run` }],
['--max-failures <N>', { description: `Stop after the first N failures` }],
['--no-deps', { description: `Do not run project dependencies` }],
diff --git a/packages/playwright/src/runner/lastRun.ts b/packages/playwright/src/runner/lastRun.ts
index 62ddf75b21..a4081da782 100644
--- a/packages/playwright/src/runner/lastRun.ts
+++ b/packages/playwright/src/runner/lastRun.ts
@@ -31,11 +31,16 @@ export class LastRunReporter implements ReporterV2 {
private _suite: Suite | undefined;
private _listMode: boolean;
- constructor(filteredProjects: commonConfig.FullProjectInternal[], listMode?: boolean) {
+ constructor(filteredProjects: commonConfig.FullProjectInternal[], listMode?: boolean, lastFailedFileOverride?: string) {
this._listMode = !!listMode;
- const [project] = filteredProjects;
- if (project)
- this._lastRunFile = path.join(project.project.outputDir, '.last-run.json');
+ const override = lastFailedFileOverride ?? process.env.PLAYWRIGHT_LAST_RUN_OUTPUT_FILE;
+ if (override) {
+ this._lastRunFile = path.resolve(process.cwd(), override);
+ } else {
+ const [project] = filteredProjects;
+ if (project)
+ this._lastRunFile = path.join(project.project.outputDir, '.last-run.json');
+ }
}
async filterLastFailed(): Promise<string[]> {
diff --git a/packages/playwright/src/runner/tasks.ts b/packages/playwright/src/runner/tasks.ts
index 6edd4e887c..c2a3a9b5dd 100644
--- a/packages/playwright/src/runner/tasks.ts
+++ b/packages/playwright/src/runner/tasks.ts
@@ -64,6 +64,7 @@ export type TestRunOptions = {
listMode?: boolean;
passWithNoTests?: boolean;
lastFailed?: boolean;
+ lastFailedFile?: string;
testList?: string;
testListInvert?: string;
lastFailedTestIds?: string[];
diff --git a/packages/playwright/src/runner/testRunner.ts b/packages/playwright/src/runner/testRunner.ts
index c7267c5264..0d40a9ac13 100644
--- a/packages/playwright/src/runner/testRunner.ts
+++ b/packages/playwright/src/runner/testRunner.ts
@@ -446,7 +446,7 @@ export async function runAllTestsWithConfig(config: FullConfigInternal, options:
const filteredProjects = filterProjects(config.projects, options.projectFilter);
const reporters = await createReporters(config, options.listMode ? 'list' : 'test', undefined, options);
- const lastRun = new LastRunReporter(filteredProjects, options.listMode);
+ const lastRun = new LastRunReporter(filteredProjects, options.listMode, options.lastFailedFile);
if (options.lastFailed) {
const lastFailedTestIds = await lastRun.filterLastFailed();
if (lastFailedTestIds.length)
diff --git a/tests/playwright-test/runner.spec.ts b/tests/playwright-test/runner.spec.ts
index 1d91cb9d8d..ea0ed51291 100644
--- a/tests/playwright-test/runner.spec.ts
+++ b/tests/playwright-test/runner.spec.ts
@@ -878,3 +878,79 @@ test('should run last failed tests in a shard', async ({ runInlineTest }) => {
expect(result2.output).not.toContain('b.spec.js:3:11 › pass-b');
expect(result2.output).toContain('b.spec.js:4:11 › fail-b');
});
+
+test('should run last failed tests in a shard with PLAYWRIGHT_LAST_RUN_OUTPUT_FILE', async ({ runInlineTest }, testInfo) => {
+ const customRel = '.cache/shard-2-last-run.json';
+ const customAbs = path.join(testInfo.outputPath(), customRel);
+ const defaultLastRun = path.join(testInfo.outputPath(), 'test-results', '.last-run.json');
+ const env = { PLAYWRIGHT_LAST_RUN_OUTPUT_FILE: customRel };
+ const workspace = {
+ 'a.spec.js': `
+ import { test, expect } from '@playwright/test';
+ test('pass-a', async () => {});
+ test('fail-a', async () => {
+ expect(1).toBe(2);
+ });
+ `,
+ 'b.spec.js': `
+ import { test, expect } from '@playwright/test';
+ test('pass-b', async () => {});
+ test('fail-b', async () => {
+ expect(1).toBe(2);
+ });
+ `,
+ };
+ const result1 = await runInlineTest(workspace, { shard: '2/2' }, env);
+ expect(result1.exitCode).toBe(1);
+ expect(result1.passed).toBe(1);
+ expect(result1.failed).toBe(1);
+ expect(fs.existsSync(customAbs)).toBe(true);
+ expect(fs.existsSync(defaultLastRun)).toBe(false);
+ expect(result1.output).toContain('b.spec.js:3:11 › pass-b');
+ expect(result1.output).toContain('b.spec.js:4:11 › fail-b');
+
+ const result2 = await runInlineTest(workspace, { shard: '2/2' }, env, { additionalArgs: ['--last-failed'] });
+ expect(result2.exitCode).toBe(1);
+ expect(result2.passed).toBe(0);
+ expect(result2.failed).toBe(1);
+ expect(result2.output).not.toContain('b.spec.js:3:11 › pass-b');
+ expect(result2.output).toContain('b.spec.js:4:11 › fail-b');
+});
+
+test('should run last failed tests in a shard with --last-failed-file', async ({ runInlineTest }, testInfo) => {
+ const customRel = '.cache/shard-2-cli-last-run.json';
+ const customAbs = path.join(testInfo.outputPath(), customRel);
+ const defaultLastRun = path.join(testInfo.outputPath(), 'test-results', '.last-run.json');
+ const lastRunArgs = ['--last-failed', `--last-failed-file=${customRel}`];
+ const workspace = {
+ 'a.spec.js': `
+ import { test, expect } from '@playwright/test';
+ test('pass-a', async () => {});
+ test('fail-a', async () => {
+ expect(1).toBe(2);
+ });
+ `,
+ 'b.spec.js': `
+ import { test, expect } from '@playwright/test';
+ test('pass-b', async () => {});
+ test('fail-b', async () => {
+ expect(1).toBe(2);
+ });
+ `,
+ };
+ const result1 = await runInlineTest(workspace, { shard: '2/2' }, {}, { additionalArgs: lastRunArgs });
+ expect(result1.exitCode).toBe(1);
+ expect(result1.passed).toBe(1);
+ expect(result1.failed).toBe(1);
+ expect(fs.existsSync(customAbs)).toBe(true);
+ expect(fs.existsSync(defaultLastRun)).toBe(false);
+ expect(result1.output).toContain('b.spec.js:3:11 › pass-b');
+ expect(result1.output).toContain('b.spec.js:4:11 › fail-b');
+
+ const result2 = await runInlineTest(workspace, { shard: '2/2' }, {}, { additionalArgs: lastRunArgs });
+ expect(result2.exitCode).toBe(1);
+ expect(result2.passed).toBe(0);
+ expect(result2.failed).toBe(1);
+ expect(result2.output).not.toContain('b.spec.js:3:11 › pass-b');
+ expect(result2.output).toContain('b.spec.js:4:11 › fail-b');
+});