Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] a toMatchScreenshot() for comparing two different screenshots #18937

Open
NVolcz opened this issue Nov 19, 2022 · 9 comments
Open

[Feature] a toMatchScreenshot() for comparing two different screenshots #18937

NVolcz opened this issue Nov 19, 2022 · 9 comments

Comments

@NVolcz
Copy link

NVolcz commented Nov 19, 2022

We currently have expect(screenshot).toMatchSnapshot(name[, options]) which is nice when comparing against at snapshot but it doesn't work when comparing a screenshot against a reference website which is useful for doing visual regression to see what has changed. I propose that we add a toMatchScreenshot(screenshot[, options]) that could be used for something like this:

page.goto("production.example.com")
const ref = await page.screenshot({
  animations: "disabled",
  fullPage: true,
  scale: "css"
});

page.goto("localhost:8080")
const test = await page.screenshot({
  animations: "disabled",
  fullPage: true,
  scale: "css"
});

expect(test, "should  match reference").toMatchScreenshot(ref, {
  maxDiffPixelRatio: 0,
  maxDiffPixels: 0,
  threshold: 0
});
@dgozman
Copy link
Contributor

dgozman commented Nov 21, 2022

@NVolcz I think we can probably do something like expect(image1).toMatchImage(image2) for this usecase. See also #18897.

In your particular case, I can recommend to produce screenshots once in production environment, commit them to your version control, and then compare localhost screenshots to these expectations. This way, you'll have a stable set of screenshots to test against. I hope this helps.

@NVolcz
Copy link
Author

NVolcz commented Nov 22, 2022

expect(image1).toMatchImage(image2) would be awesome!
I implemented something like what you are suggesting but handling snapshots is troublesome and doesn't seem worth it in my case.

@thenbe
Copy link

thenbe commented Nov 24, 2022

@dgozman

Would you consider implementing this in a way that would support the following usecase?

test('MyButton has no side effects', async ({ page }) => {
	await page.goto('/');

	const before = await page.getByTestId('my-element').screenshot();

	await page.getByRole('button', { name: 'MyButton' }).click();

	const after = await page.getByTestId('my-element').screenshot();

        // ensure my-element still looks the same
	await expect(before).toMatchImage(after);
});

Specifically:

  • Compare a screenshot taken at the beginning of the test with one taken at the end.
  • Without saving them or having to handle paths, etc.
  • While benefiting from the 'visual image diff widget' in the test report which makes it easy to find out why a particular test failed. Like the videos, the images can be retained-on-failure, and discarded on success.

@dgozman
Copy link
Contributor

dgozman commented Nov 24, 2022

@ambiguous48 Yes, so far I think that toMatchImage() could support both buffers and file paths.

@NikkTod
Copy link

NikkTod commented Dec 14, 2022

Hello @aslushnikov, is there a chance we see that feature in 1.29?

@aslushnikov
Copy link
Collaborator

@NikkTod no, it's not coming to 1.29. Will be moved to 1.30 instead

@elesh90
Copy link

elesh90 commented Apr 3, 2023

Does this feature work in version 1.32 by now?

@AleksandrovAlex
Copy link

It will be very helpful to have this feature implemented because it opens a wide range of new options. Plus the new masking trick ... Can't wait.

@danilovxp
Copy link

Hello @aslushnikov,

Is there any lucky news about this feature? 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants