question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Diff between screenshots is not present in the report

See original GitHub issue

Context:

  • Playwright Version: 1.23.0
  • Operating System: Mac
  • Node.js version: 16.14.0
  • Browser: Chromium

Code Snippet

Playwright config:

const config: PlaywrightTestConfig = {
  testDir: 'tests',
  fullyParallel: true,
  reporter: process.env.CI ? [['github'], ['html']] : [['list'], ['html', { open: 'on-failure' }]],
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 1 : 0,
  use: {
    headless: true,
    viewport: { width: 1280, height: 720 },
    screenshot: 'only-on-failure',
    video: 'retain-on-failure',
    trace: 'retain-on-failure',
  },
  expect: {
    toHaveScreenshot: { 
      threshold: 0.2,
      maxDiffPixelRatio: 0.05
     },
  },
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },
  ],
};
export default config;

Method that compares screenshots:

async matchesScreenshot(
    locator: Locator,
    options?: LocatorScreenshotOptions,
  ) {
    expect(locator).toHaveScreenshot({timeout: 5000, ...options});
  }

Describe the bug

I’m doing visual regression testing. I want to see the difference between screenshots in the report. However, the difference is not present there. Also, in the test-results folder the difference is missing too.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dgozmancommented, Aug 1, 2022

@muhammad-saleh Thank you for the explanation, this makes sense. So, this is happening because you use full-page screenshots. I’ll reopen this issue for now.

0reactions
anetrebskycommented, Nov 14, 2022

If it helps someone, I have prepared an NPM package patch using https://www.npmjs.com/package/patch-package, that ignores difference in height but still requires the same width of snapshots.

diff --git a/node_modules/@playwright/test/node_modules/playwright-core/lib/utils/comparators.js b/node_modules/@playwright/test/node_modules/playwright-core/lib/utils/comparators.js
index a0a36b1..eaad391 100644
--- a/node_modules/@playwright/test/node_modules/playwright-core/lib/utils/comparators.js
+++ b/node_modules/@playwright/test/node_modules/playwright-core/lib/utils/comparators.js
@@ -10,6 +10,7 @@ var _utilsBundle = require("../utilsBundle");
 var _pixelmatch = _interopRequireDefault(require("../third_party/pixelmatch"));
 
 var _diff_match_patch = require("../third_party/diff_match_patch");
+const exp = require("constants");
 
 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
@@ -55,31 +56,48 @@ function compareImages(mimeType, actualBuffer, expectedBuffer, options = {}) {
   if (!actualBuffer || !(actualBuffer instanceof Buffer)) return {
     errorMessage: 'Actual result should be a Buffer.'
   };
-  const actual = mimeType === 'image/png' ? _utilsBundle.PNG.sync.read(actualBuffer) : _utilsBundle.jpegjs.decode(actualBuffer, {
+  var actual = mimeType === 'image/png' ? _utilsBundle.PNG.sync.read(actualBuffer) : _utilsBundle.jpegjs.decode(actualBuffer, {
     maxMemoryUsageInMB: JPEG_JS_MAX_BUFFER_SIZE_IN_MB
   });
-  const expected = mimeType === 'image/png' ? _utilsBundle.PNG.sync.read(expectedBuffer) : _utilsBundle.jpegjs.decode(expectedBuffer, {
+  var expected = mimeType === 'image/png' ? _utilsBundle.PNG.sync.read(expectedBuffer) : _utilsBundle.jpegjs.decode(expectedBuffer, {
     maxMemoryUsageInMB: JPEG_JS_MAX_BUFFER_SIZE_IN_MB
   });
 
-  if (expected.width !== actual.width || expected.height !== actual.height) {
-    return {
-      errorMessage: `Expected an image ${expected.width}px by ${expected.height}px, received ${actual.width}px by ${actual.height}px. `
-    };
+  if (expected.width !== actual.width) {
+     return {
+       errorMessage: `Expected an image ${expected.width}px by ${expected.height}px, received ${actual.width}px by ${actual.height}px. `
+     };
+   }
+
+  var expectedImageData = expected.data;
+  var actualImageData = actual.data;
+  var imageWidth = expected.width;
+  var imageHeight = Math.max(expected.height, actual.height);
+
+  if (actualImageData.length < expectedImageData.length) {
+    var tmp = Buffer.alloc(expectedImageData.length);
+    actualImageData.copy(tmp)
+    actualImageData = tmp;
+  }
+  else if (expectedImageData.length < actualImageData.length) {
+    var tmp = Buffer.alloc(actualImageData.length);
+    expectedImageData.copy(tmp)
+    expectedImageData = tmp;
   }
 
   const diff = new _utilsBundle.PNG({
-    width: expected.width,
-    height: expected.height
+    width: imageWidth,
+    height: imageHeight
   });
-  const count = (0, _pixelmatch.default)(expected.data, actual.data, diff.data, expected.width, expected.height, {
+  
+  const count = (0, _pixelmatch.default)(expectedImageData, actualImageData, diff.data, imageWidth, imageHeight, {
     threshold: (_options$threshold = options.threshold) !== null && _options$threshold !== void 0 ? _options$threshold : 0.2
   });
   const maxDiffPixels1 = options.maxDiffPixels;
-  const maxDiffPixels2 = options.maxDiffPixelRatio !== undefined ? expected.width * expected.height * options.maxDiffPixelRatio : undefined;
+  const maxDiffPixels2 = options.maxDiffPixelRatio !== undefined ? imageWidth * imageHeight * options.maxDiffPixelRatio : undefined;
   let maxDiffPixels;
   if (maxDiffPixels1 !== undefined && maxDiffPixels2 !== undefined) maxDiffPixels = Math.min(maxDiffPixels1, maxDiffPixels2);else maxDiffPixels = (_ref = maxDiffPixels1 !== null && maxDiffPixels1 !== void 0 ? maxDiffPixels1 : maxDiffPixels2) !== null && _ref !== void 0 ? _ref : 0;
-  const ratio = Math.ceil(count / (expected.width * expected.height) * 100) / 100;
+  const ratio = Math.ceil(count / (imageWidth * imageHeight) * 100) / 100;
   return count > maxDiffPixels ? {
     errorMessage: `${count} pixels (ratio ${ratio.toFixed(2)} of all image pixels) are different`,
     diff: _utilsBundle.PNG.sync.write(diff)
Read more comments on GitHub >

github_iconTop Results From Across the Web

ExtentReports - screenshot not in the report - broken image
I've figured out that a solution is to store the images in the same directory where the report gets generated, give the image...
Read more >
It says: "There are still screenshot uploads in progress. ...
This message means there are screenshot uploads that were started but never finished. Normally if an upload fails, App Store Connect will report...
Read more >
found this bug (screenshot attached) but don't kno...
Hello @Faisal Tahir ,. Welcome to the community,. The above screen shot is not an issue it basically telling the format. Or please...
Read more >
Why do you need screenshots and logs to be attached to a ...
However, past experience has shown that discovered bugs are not immediately rectified. ... Difference between Use Case and Test Case.
Read more >
Every Report.Info contains a screenshot on failure when ...
The Report.Info lines then don't contain any screenshots. Only the Error Line does, as expected. It also makes no difference if i use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found