Skip to content

feat(playwright-test): introduce snapshotPathTemplate configuration#18568

Merged
aslushnikov merged 13 commits into
microsoft:mainfrom
aslushnikov:snapshot-path-template
Nov 9, 2022
Merged

feat(playwright-test): introduce snapshotPathTemplate configuration#18568
aslushnikov merged 13 commits into
microsoft:mainfrom
aslushnikov:snapshot-path-template

Conversation

@aslushnikov
Copy link
Copy Markdown
Contributor

@aslushnikov aslushnikov commented Nov 4, 2022

This configuration option allows to set a string with template
values for precise control over snapshot path location.

An example of snapshotPathTemplate usage:

// playwright.config.ts
// Notice the `testDir` configuration!
export default {
  testDir: './tests',
  snapshotPathTemplate: './__screenshots__/{platform}/{projectName}/{testFilePath}/{arg}{ext}',
}

Currently supported "magic tokens" inside the snapshotPathTemplate
are:

  • {testDir} - project's testDir
  • {snapshotDir} - project's snapshotDir
  • {platform} - process.platform
  • {projectName} - Project's sanitized name
  • {testFileDir} - Directories in relative path from testDir to test
    file path (e.g. page/ in the example below)
  • {testFileName} - Test file name (with extension) (e.g.
    page-click.spec.ts in the example below)
  • {testFilePath} - Relative path from testDir to test file path
    (e.g. page/page-click.spec.ts in the example below)
  • {ext} - snapshot extension (with dots)
  • {arg} - joined snapshot name parts, without extension (e.g. foo/bar/baz in the example below)
  • {snapshotSuffix} - testInfo.snapshotSuffix value.

Consider the following file structure:

playwright.config.ts
tests/
└── page/
    └── page-click.spec.ts

The following page-click.spec.ts:

// page-click.spec.ts
import { test, expect } from '@playwright/test';

test('should work', async ({ page }) => {
  await expect(page).toHaveScreenshot(['foo', 'bar', 'baz.png']);
});

Fixes #7792

Comment thread packages/playwright-test/src/testInfo.ts Outdated
Comment thread packages/playwright-test/src/testInfo.ts Outdated
Comment thread packages/playwright-test/src/testInfo.ts Outdated
Comment thread packages/playwright-test/src/testInfo.ts Outdated
Comment thread tests/playwright-test/reporter-html.spec.ts Outdated
Comment thread tests/playwright-test/to-have-screenshot.spec.ts
This configuration option allows to set a string with template
values for precise control over snapshot path location.

An example of `snapshotPathTemplate` usage:

```ts
// playwright.config.ts
// Notice the `testDir` configuration!
export default {
  testDir: './tests',
  snapshotPathTemplate: './__screenshots__/{platform}/{projectName}/{testFilePath}/{snapshotName}',
}
```

Currently supported "magic tokens" inside the `snapshotPathTemplate`
are:

- `{testDir}` - project's `testDir`
- `{snapshotDir}` - project's `snapshotDir`
- `{platform}` - `process.platform`
- `{projectName}` - Project's sanitized name
- `{testFileDir}` - Directories in relative path from `testDir` to test
  file path (e.g. `page/` in the example below)
- `{testFileName}` - Test file name (with extension) (e.g.
  `page-click.spec.ts` in the example below)
- `{testFilePath}` - Relative path from `testDir` to test file path
  (e.g. `page/page-click.spec.ts` in the example below)
- `{snapshotNameDir}` - Relative snapshot name directory (e.g.
  `foo/bar/` in the example below)
- `{snapshotNameBase}` - Snapshot name base (e.g. `baz` in the example
  below)
- `{snapshotNameExt}` - Snapshot name extension including `.` (e.g.
  `.png` in the example below)
- `{snapshotName}` - Snapshot name (e.g. `baz.png` in the example below)
- `{snapshotSuffix}` - `testInfo.snapshotSuffix` value.

Consider the following file structure:

```
playwright.config.ts
tests/
└── page/
    └── page-click.spec.ts
```

The following `page-click.spec.ts`:

```ts
// page-click.spec.ts
import { test, expect } from '@playwright/test';

test('should work', async ({ page }) => {
  await expect(page).toHaveScreenshot('foo', 'bar', 'baz.png');
});
```
@aslushnikov aslushnikov force-pushed the snapshot-path-template branch from 5f98729 to 47bec45 Compare November 9, 2022 17:18
Comment thread docs/src/api/params.md
Comment thread docs/src/api/params.md
Comment thread docs/src/api/params.md
Comment thread docs/src/api/params.md
Comment thread docs/src/api/params.md
Comment thread packages/playwright-test/src/loader.ts
Comment thread packages/playwright-test/src/loader.ts
Comment thread packages/playwright-test/src/testInfo.ts
Comment thread packages/playwright-test/src/testInfo.ts
Comment thread utils/generate_types/overrides-test.d.ts
@aslushnikov aslushnikov merged commit 6d491f9 into microsoft:main Nov 9, 2022
@aslushnikov aslushnikov deleted the snapshot-path-template branch November 9, 2022 23:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Can I somehow change the directory for saving screenshots?

2 participants