Skip to content

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

feat(test-runner): add {testFileBaseName} token to snapshotPathTemplate (#40466)

Devendra Reddy Pennabadicd3a044ebf23
diff --git a/docs/src/api/params.md b/docs/src/api/params.md
index df6b543ead..dc86942aa6 100644
--- a/docs/src/api/params.md
+++ b/docs/src/api/params.md
@@ -1934,6 +1934,8 @@ The list of supported tokens:
* Value: `/home/playwright/tests` (absolute path since `testDir` is resolved relative to directory with config)
* `{testFileDir}` - Directories in relative path from `testDir` to **test file**.
* Value: `page`
+* `{testFileBaseName}` - Test file name without the last extension.
+ * Value: `page-click.spec`
* `{testFileName}` - Test file name with extension.
* Value: `page-click.spec.ts`
* `{testFilePath}` - Relative path from `testDir` to **test file**.
diff --git a/packages/playwright/src/worker/testInfo.ts b/packages/playwright/src/worker/testInfo.ts
index c85c7bca8c..43003ee9e7 100644
--- a/packages/playwright/src/worker/testInfo.ts
+++ b/packages/playwright/src/worker/testInfo.ts
@@ -625,6 +625,7 @@ export class TestInfoImpl implements TestInfo {
.replace(/\{(.)?platform\}/g, '$1' + process.platform)
.replace(/\{(.)?projectName\}/g, projectNamePathSegment ? '$1' + projectNamePathSegment : '')
.replace(/\{(.)?testName\}/g, '$1' + this._fsSanitizedTestName())
+ .replace(/\{(.)?testFileBaseName\}/g, '$1' + parsedRelativeTestFilePath.name)
.replace(/\{(.)?testFileName\}/g, '$1' + parsedRelativeTestFilePath.base)
.replace(/\{(.)?testFilePath\}/g, '$1' + relativeTestFilePath)
.replace(/\{(.)?arg\}/g, '$1' + nameArgument)
diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.ts
index d0b1557c0b..f68d89e0a7 100644
--- a/packages/playwright/types/test.d.ts
+++ b/packages/playwright/types/test.d.ts
@@ -529,6 +529,8 @@ interface TestProject<TestArgs = {}, WorkerArgs = {}> {
* config)
* - `{testFileDir}` - Directories in relative path from `testDir` to **test file**.
* - Value: `page`
+ * - `{testFileBaseName}` - Test file name without the last extension.
+ * - Value: `page-click.spec`
* - `{testFileName}` - Test file name with extension.
* - Value: `page-click.spec.ts`
* - `{testFilePath}` - Relative path from `testDir` to **test file**.
@@ -1770,6 +1772,8 @@ interface TestConfig<TestArgs = {}, WorkerArgs = {}> {
* config)
* - `{testFileDir}` - Directories in relative path from `testDir` to **test file**.
* - Value: `page`
+ * - `{testFileBaseName}` - Test file name without the last extension.
+ * - Value: `page-click.spec`
* - `{testFileName}` - Test file name with extension.
* - Value: `page-click.spec.ts`
* - `{testFilePath}` - Relative path from `testDir` to **test file**.
diff --git a/tests/playwright-test/snapshot-path-template.spec.ts b/tests/playwright-test/snapshot-path-template.spec.ts
index f1224b9563..b454473cc3 100644
--- a/tests/playwright-test/snapshot-path-template.spec.ts
+++ b/tests/playwright-test/snapshot-path-template.spec.ts
@@ -79,6 +79,9 @@ test('tokens should expand property', async ({ runInlineTest }, testInfo) => {
}, {
name: 'testFileName',
snapshotPathTemplate: '{testFileName}',
+ }, {
+ name: 'testFileBaseName',
+ snapshotPathTemplate: '{testFileBaseName}',
}, {
name: 'snapshotDir',
snapshotDir: './a-snapshot-dir',
@@ -101,6 +104,7 @@ test('tokens should expand property', async ({ runInlineTest }, testInfo) => {
expect.soft(snapshotPath['testFileDir']).toBe(path.join('a', 'b', 'c'));
expect.soft(snapshotPath['testFilePath']).toBe(path.join('a', 'b', 'c', 'd.spec.ts'));
expect.soft(snapshotPath['testFileName']).toBe('d.spec.ts');
+ expect.soft(snapshotPath['testFileBaseName']).toBe('d.spec');
expect.soft(snapshotPath['snapshotDir']).toBe('a-snapshot-dir.png');
expect.soft(snapshotPath['snapshotSuffix']).toBe('-' + process.platform);
expect.soft(snapshotPath['testName']).toBe('suite-test-should-work');