Rayrun
← Back to Discord Forum

Tests are flaky due to Image dimension change

Hello,

I'm facing an issue where certain visual comparison tests are flaky due to a 1-pixel change in image dimensions.

The error message is as follows: "Expected an image size of 118px by 47px, but received 117px by 47px."

Any assistance would be greatly appreciated.

This thread is trying to answer question "How can I resolve the issue of flaky visual comparison tests due to a 1-pixel change in image dimensions?"

5 replies

Are you using "css" or "device" pixels?

Hi, css

Well, I tried all the options that screenshot api in PW provides, there is no in-build solution for your situation.

My suggestion is, either figure out why the image is different size and fix it, or you can capture screenshot and diff it yourself. This might help https://playwright.dev/docs/screenshots#capture-into-buffer

I would most definitely want PW to fail my TC if pixels do not match.

image.png
image.png

Thank you

Answer

Handling Flaky Visual Comparison Tests in Playwright

Flaky visual comparison tests can be a pain, especially when they're due to a 1-pixel change in image dimensions. But don't worry, there are several ways to tackle this.

Adjust the Threshold Value

The threshold value represents the acceptable perceived color difference between pixels in compared images. By default, Playwright uses a threshold value of 0.2. You can customize this threshold by passing the threshold option to the toMatchSnapshot() or toHaveScreenshot() method.

import { test } from '@playwright/test';

test('should match screenshot', async ({ page }) => {
  await page.goto('https://ray.run');
  expect(await page.screenshot()).toMatchSnapshot('screenshot.png', { threshold: 0.3 });
});

Modify the maxDiffPixels Option

This option specifies an acceptable amount of pixels that could be different between compared images. By default, it is unset, but you can set a specific value for maxDiffPixels.

import { test } from '@playwright/test';

test('should match screenshot', async ({ page }) => {
  await page.goto('https://ray.run');
  expect(await page.screenshot()).toMatchSnapshot('screenshot.png', { maxDiffPixels: 10 });
});

Adjust the maxDiffPixelRatio Option

This option represents an acceptable ratio of differing pixels relative to the total number of pixels in an image. This allows you to define how much variation in pixel differences should be tolerated.

import { test } from '@playwright/test';

test('should match screenshot', async ({ page }) => {
  await page.goto('https://ray.run');
  expect(await page.screenshot()).toMatchSnapshot('screenshot.png', { maxDiffPixelRatio: 0.05 });
});

Remember, these options should be used with caution and tailored according to your specific requirements. For more tips on handling flaky tests, check out this blog post.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.