Skip to content

[Bug]: toHaveScreenshot method does not take maxDiffPixels or maxDiffPixelRatio inputs into consideration when there are visual differences and these thresholds are higher than the differences. #31592

@PramodKumarYadav

Description

@PramodKumarYadav

Version

1.45.0

Steps to reproduce

NOTE: I have the setup working on a customer project so I dont have a generically accessible code to reproduce this at the moment but am sure the same issue can be verified if we run tests in a headed (VSCode plugin -> Settings -> Show Browser) or headless mode on any UI application. If this is not the case, do let me know and I will try to create a reproducible example and share.

For now, here are the steps and issue.

  1. Run tests only for a particular input field without giving any options in headless mode.
  2. Command I use to run: npx playwright test --grep=user-mgmt-search --project=ux-tests. With this the below test passes:
test("user-mgmt-search", async ({ page, userManagementPage, }) => {
  await userManagementPage.goTo();
  const searchUserInput = page.getByPlaceholder("Start typing to search");
  await expect(searchUserInput).toHaveScreenshot({});
});
2 passed (9.2s)
::notice title=🎭 Playwright Run Summary::  2 passed (9.2s)
  1. Now run the same test in headed mode by going to VSCode plugin -> Settings -> Show Browser option.
  2. This time, run test from clicking the button from UI and verify results.
  3. Test throws an error - Expected an image 537px by 47px, received 529px by 47px. 98 pixels (ratio 0.01 of all image pixels) are different.
  4. Now go to test and change pixel thresholds above the ones shown above. i.e. more than 98 pixels or more than 0.01. I will set both for this particular test.
test("user-mgmt-search", async ({ page, userManagementPage, }) => {
  await userManagementPage.goTo();
  const searchUserInput = page.getByPlaceholder("Start typing to search");
  await expect(searchUserInput).toHaveScreenshot({
    maxDiffPixels: 100, // maximum number of different pixels
    maxDiffPixelRatio: 0.1, // maximum ratio of different pixels
    threshold: 0.5 // color comparison threshold (0-1)
  });
});
  1. Run test again from UI.

Expected behavior

Since the ratios are higher than the differences, I expect the tests to pass.

Actual behavior

Even if I make these ratios much higher - even to their max limit, the test still fails with below error.
- Expected an image 537px by 47px, received 529px by 47px.

Additional context

Due to this reason I am not able to use the visual comparison tests for isolated fields in our tests. Kindly let me know if I am doing something wrong here.

Environment

System:
    OS: macOS 14.0
    CPU: (8) arm64 Apple M2
    Memory: 60.63 MB / 16.00 GB
  Binaries:
    Node: 21.7.1 - /opt/homebrew/bin/node
    Yarn: 1.22.22 - /opt/homebrew/bin/yarn
    npm: 10.8.1 - /opt/homebrew/bin/npm
  Languages:
    Bash: 3.2.57 - /bin/bash
  npmPackages:
    @playwright/test: ^1.45.0 => 1.45.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions