feat: add maskColor option to the toHaveScreenshot method (#23555)

This commit is contained in:
Andrey Lushnikov 2023-06-06 17:15:55 -07:00 committed by GitHub
parent 7579572688
commit 0f4090472c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 66 additions and 9 deletions

View file

@ -1441,6 +1441,9 @@ Snapshot name.
### option: LocatorAssertions.toHaveScreenshot#1.mask = %%-screenshot-option-mask-%% ### option: LocatorAssertions.toHaveScreenshot#1.mask = %%-screenshot-option-mask-%%
* since: v1.23 * since: v1.23
### option: LocatorAssertions.toHaveScreenshot#1.maskColor = %%-screenshot-option-mask-color-%%
* since: v1.35
### option: LocatorAssertions.toHaveScreenshot#1.omitBackground = %%-screenshot-option-omit-background-%% ### option: LocatorAssertions.toHaveScreenshot#1.omitBackground = %%-screenshot-option-omit-background-%%
* since: v1.23 * since: v1.23
@ -1484,6 +1487,9 @@ Note that screenshot assertions only work with Playwright test runner.
### option: LocatorAssertions.toHaveScreenshot#2.mask = %%-screenshot-option-mask-%% ### option: LocatorAssertions.toHaveScreenshot#2.mask = %%-screenshot-option-mask-%%
* since: v1.23 * since: v1.23
### option: LocatorAssertions.toHaveScreenshot#2.maskColor = %%-screenshot-option-mask-color-%%
* since: v1.35
### option: LocatorAssertions.toHaveScreenshot#2.omitBackground = %%-screenshot-option-omit-background-%% ### option: LocatorAssertions.toHaveScreenshot#2.omitBackground = %%-screenshot-option-omit-background-%%
* since: v1.23 * since: v1.23

View file

@ -158,6 +158,9 @@ Snapshot name.
### option: PageAssertions.toHaveScreenshot#1.mask = %%-screenshot-option-mask-%% ### option: PageAssertions.toHaveScreenshot#1.mask = %%-screenshot-option-mask-%%
* since: v1.23 * since: v1.23
### option: PageAssertions.toHaveScreenshot#1.maskColor = %%-screenshot-option-mask-color-%%
* since: v1.35
### option: PageAssertions.toHaveScreenshot#1.omitBackground = %%-screenshot-option-omit-background-%% ### option: PageAssertions.toHaveScreenshot#1.omitBackground = %%-screenshot-option-omit-background-%%
* since: v1.23 * since: v1.23
@ -206,6 +209,9 @@ Note that screenshot assertions only work with Playwright test runner.
### option: PageAssertions.toHaveScreenshot#2.mask = %%-screenshot-option-mask-%% ### option: PageAssertions.toHaveScreenshot#2.mask = %%-screenshot-option-mask-%%
* since: v1.23 * since: v1.23
### option: PageAssertions.toHaveScreenshot#2.maskColor = %%-screenshot-option-mask-color-%%
* since: v1.35
### option: PageAssertions.toHaveScreenshot#2.omitBackground = %%-screenshot-option-omit-background-%% ### option: PageAssertions.toHaveScreenshot#2.omitBackground = %%-screenshot-option-omit-background-%%
* since: v1.23 * since: v1.23

View file

@ -1090,10 +1090,10 @@ Specify screenshot type, defaults to `png`.
- `mask` <[Array]<[Locator]>> - `mask` <[Array]<[Locator]>>
Specify locators that should be masked when the screenshot is taken. Masked elements will be overlaid with Specify locators that should be masked when the screenshot is taken. Masked elements will be overlaid with
a pink box `#FF00FF` that completely covers its bounding box. a pink box `#FF00FF` (customized by [`option: maskColor`]) that completely covers its bounding box.
## screenshot-option-mask-color ## screenshot-option-mask-color
* since: v1.34 * since: v1.35
- `maskColor` <[string]> - `maskColor` <[string]>
Specify the color of the overlay box for masked elements, in [CSS color format](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value). Default color is pink `#FF00FF`. Specify the color of the overlay box for masked elements, in [CSS color format](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value). Default color is pink `#FF00FF`.

View file

@ -9834,7 +9834,7 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {
/** /**
* Specify locators that should be masked when the screenshot is taken. Masked elements will be overlaid with a pink * Specify locators that should be masked when the screenshot is taken. Masked elements will be overlaid with a pink
* box `#FF00FF` that completely covers its bounding box. * box `#FF00FF` (customized by `maskColor`) that completely covers its bounding box.
*/ */
mask?: Array<Locator>; mask?: Array<Locator>;
@ -19660,7 +19660,7 @@ export interface LocatorScreenshotOptions {
/** /**
* Specify locators that should be masked when the screenshot is taken. Masked elements will be overlaid with a pink * Specify locators that should be masked when the screenshot is taken. Masked elements will be overlaid with a pink
* box `#FF00FF` that completely covers its bounding box. * box `#FF00FF` (customized by `maskColor`) that completely covers its bounding box.
*/ */
mask?: Array<Locator>; mask?: Array<Locator>;
@ -19853,7 +19853,7 @@ export interface PageScreenshotOptions {
/** /**
* Specify locators that should be masked when the screenshot is taken. Masked elements will be overlaid with a pink * Specify locators that should be masked when the screenshot is taken. Masked elements will be overlaid with a pink
* box `#FF00FF` that completely covers its bounding box. * box `#FF00FF` (customized by `maskColor`) that completely covers its bounding box.
*/ */
mask?: Array<Locator>; mask?: Array<Locator>;

View file

@ -335,6 +335,7 @@ export async function toHaveScreenshot(
caret: config?.caret ?? 'hide', caret: config?.caret ?? 'hide',
...helper.allOptions, ...helper.allOptions,
mask: (helper.allOptions.mask || []) as LocatorEx[], mask: (helper.allOptions.mask || []) as LocatorEx[],
maskColor: helper.allOptions.maskColor,
name: undefined, name: undefined,
threshold: undefined, threshold: undefined,
maxDiffPixels: undefined, maxDiffPixels: undefined,

View file

@ -5337,10 +5337,16 @@ interface LocatorAssertions {
/** /**
* Specify locators that should be masked when the screenshot is taken. Masked elements will be overlaid with a pink * Specify locators that should be masked when the screenshot is taken. Masked elements will be overlaid with a pink
* box `#FF00FF` that completely covers its bounding box. * box `#FF00FF` (customized by `maskColor`) that completely covers its bounding box.
*/ */
mask?: Array<Locator>; mask?: Array<Locator>;
/**
* Specify the color of the overlay box for masked elements, in
* [CSS color format](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value). Default color is pink `#FF00FF`.
*/
maskColor?: string;
/** /**
* An acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1`. Default is * An acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1`. Default is
* configurable with `TestConfig.expect`. Unset by default. * configurable with `TestConfig.expect`. Unset by default.
@ -5414,10 +5420,16 @@ interface LocatorAssertions {
/** /**
* Specify locators that should be masked when the screenshot is taken. Masked elements will be overlaid with a pink * Specify locators that should be masked when the screenshot is taken. Masked elements will be overlaid with a pink
* box `#FF00FF` that completely covers its bounding box. * box `#FF00FF` (customized by `maskColor`) that completely covers its bounding box.
*/ */
mask?: Array<Locator>; mask?: Array<Locator>;
/**
* Specify the color of the overlay box for masked elements, in
* [CSS color format](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value). Default color is pink `#FF00FF`.
*/
maskColor?: string;
/** /**
* An acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1`. Default is * An acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1`. Default is
* configurable with `TestConfig.expect`. Unset by default. * configurable with `TestConfig.expect`. Unset by default.
@ -5667,10 +5679,16 @@ interface PageAssertions {
/** /**
* Specify locators that should be masked when the screenshot is taken. Masked elements will be overlaid with a pink * Specify locators that should be masked when the screenshot is taken. Masked elements will be overlaid with a pink
* box `#FF00FF` that completely covers its bounding box. * box `#FF00FF` (customized by `maskColor`) that completely covers its bounding box.
*/ */
mask?: Array<Locator>; mask?: Array<Locator>;
/**
* Specify the color of the overlay box for masked elements, in
* [CSS color format](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value). Default color is pink `#FF00FF`.
*/
maskColor?: string;
/** /**
* An acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1`. Default is * An acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1`. Default is
* configurable with `TestConfig.expect`. Unset by default. * configurable with `TestConfig.expect`. Unset by default.
@ -5774,10 +5792,16 @@ interface PageAssertions {
/** /**
* Specify locators that should be masked when the screenshot is taken. Masked elements will be overlaid with a pink * Specify locators that should be masked when the screenshot is taken. Masked elements will be overlaid with a pink
* box `#FF00FF` that completely covers its bounding box. * box `#FF00FF` (customized by `maskColor`) that completely covers its bounding box.
*/ */
mask?: Array<Locator>; mask?: Array<Locator>;
/**
* Specify the color of the overlay box for masked elements, in
* [CSS color format](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value). Default color is pink `#FF00FF`.
*/
maskColor?: string;
/** /**
* An acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1`. Default is * An acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1`. Default is
* configurable with `TestConfig.expect`. Unset by default. * configurable with `TestConfig.expect`. Unset by default.

View file

@ -1195,6 +1195,26 @@ test('should throw pretty error if expected PNG file is not a PNG', async ({ run
expect(result.output).toContain('could not decode image as JPEG.'); expect(result.output).toContain('could not decode image as JPEG.');
}); });
test('should support maskColor option', async ({ runInlineTest }) => {
const result = await runInlineTest({
...playwrightConfig({
snapshotPathTemplate: '__screenshots__/{testFilePath}/{arg}{ext}',
}),
'__screenshots__/a.spec.js/snapshot.png': createImage(IMG_WIDTH, IMG_HEIGHT, 0, 255, 0),
'a.spec.js': `
const { test, expect } = require('@playwright/test');
test('png', async ({ page }) => {
await page.setContent('<style> html,body { padding: 0; margin: 0; }</style>');
await expect(page).toHaveScreenshot('snapshot.png', {
mask: [page.locator('body')],
maskColor: '#00FF00',
});
});
`,
});
expect(result.exitCode).toBe(0);
});
function playwrightConfig(obj: any) { function playwrightConfig(obj: any) {
return { return {
'playwright.config.js': ` 'playwright.config.js': `