cypress-image-diff icon indicating copy to clipboard operation
cypress-image-diff copied to clipboard

Normalize screen pixel densities

Open jkjustjoshing opened this issue 2 years ago • 6 comments

When I collect snapshots using cypress run, the screenshots collected are collected at a default pixel density. However, when I collect snapshots using cypress open on my MacBook, the screenshots collected are at double pixel density.

Is there a way to standardize this? Otherwise, when I run my tests using cypress open they always fail because the comparison images are twice as wide as the baseline images that were generated with cypress run.

jkjustjoshing avatar Sep 20 '22 16:09 jkjustjoshing

I am looking for this too because we are using MacBooks at my company but I would integrate it in the CI too.

Lyokolux avatar Sep 22 '22 12:09 Lyokolux

Here is a visual regression plugin that does it with the resizeDevicePixelRatio key in the configuration: https://github.com/meinaart/cypress-plugin-snapshots#make-changes-to-default-configuration

Lyokolux avatar Sep 22 '22 12:09 Lyokolux

Thanks @Lyokolux , the logic there is pretty simple: https://github.com/meinaart/cypress-plugin-snapshots/blob/master/src/tasks/matchImageSnapshot.js#L32. I'll try it out.

nmkataoka avatar Sep 23 '22 16:09 nmkataoka

You can set scale factor for Chrome in launch options:

// cypress.config.js
setupNodeEvents(on, config) {
  getCompareSnapshotsPlugin(on, config)
  
  on('before:browser:launch', (browser, launchOptions) => {
    if (browser.name === 'chrome') {
      // force screen to be non-retina
      launchOptions.args.push('--force-device-scale-factor=1')
    }

    return launchOptions
  })
}

BatyrSeven avatar Oct 14 '22 07:10 BatyrSeven

You can set scale factor for Chrome in launch options:

// cypress.config.js
setupNodeEvents(on, config) {
  getCompareSnapshotsPlugin(on, config)
  
  on('before:browser:launch', (browser, launchOptions) => {
    if (browser.name === 'chrome') {
      // force screen to be non-retina
      launchOptions.args.push('--force-device-scale-factor=1')
    }

    return launchOptions
  })
}

This is great workaround, but it does not provide a solution for every browsers. I checked for Firefox and didn't find a solution. Maybe we can add this workaround on the README after the warning though: it is possible, but only on chrome.

Lyokolux avatar Oct 14 '22 08:10 Lyokolux

I am facing the same issue. Set scale factor for Chrome in launch option doesn't work well for me. Force resolution size following create double size (2000*1320) when I do cypress open and cypress run.

{
  "viewportWidth": 1000, // Default value: 1280
  "viewportHeight": 660 // Default value: 720
}

When I run in CI I can get correct resolution (1000*660). Does anyone figure out a good solution? I am hoping there could be some optional parameters we can set for screenshot size e.g. { capture: "viewport" } in this plugin.

yuyan68 avatar Oct 18 '22 13:10 yuyan68

You can set scale factor for Chrome in launch options:

// cypress.config.js
setupNodeEvents(on, config) {
  getCompareSnapshotsPlugin(on, config)
  
  on('before:browser:launch', (browser, launchOptions) => {
    if (browser.name === 'chrome') {
      // force screen to be non-retina
      launchOptions.args.push('--force-device-scale-factor=1')
    }

    return launchOptions
  })
}

Really good workaround, it's worked in my case! Thanks!

admapop avatar Nov 28 '22 09:11 admapop