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

Option to create snapshots in "light-mode" and/or "dark-mode" / prefers-color-scheme #1229

Open
t3easy opened this issue Sep 14, 2020 · 3 comments

Comments

@t3easy
Copy link

t3easy commented Sep 14, 2020

A lot of websites have styles for light and dark mode.
@media (prefers-color-scheme: dark) {}
It would be useful to add a config per scenario to get screenshots in light/default, dark or both.
Puppeteer offers page.emulateMedia.

puppeteer/puppeteer#5012
https://github.com/tomayac/dark-mode-screenshot/blob/master/dark-mode-screenshot.js#L42

@garris
Copy link
Owner

garris commented Sep 15, 2020

This is actually pretty easy to do. Here is an example.

In your config you could add an emulateDark to your viewport props...

  "viewports": [
    {
      "label": "phone",
      "emulateDark": false,
      "width": 320,
      "height": 480
    },
    {
      "label": "phone_dark",
      "emulateDark": true, 
      "width": 320,
      "height": 480
    }
  ],

Then customize the onReady script to look at the emulateDark prop and do the thing...

module.exports = async (page, scenario, vp) => {
  console.log('SCENARIO > ' + scenario.label);
  await require('./clickAndHoverHelper')(page, scenario);

  // Example: changing behavior based on viewport properties
  if (!!vp.emulateDark) {
    await page.emulateMediaFeatures([{
    name: 'prefers-color-scheme', value: 'dark' }]);
  }
};

@t3easy
Copy link
Author

t3easy commented Sep 18, 2020

Thanks for your fast response!
I've added it to my demo project
t3easy/BackstopJS-Demo@b7c5eb7

@Dok11
Copy link

Dok11 commented Dec 6, 2022

But it doesn work if you want to set prefers-contrast: more because:

Puppeteer encountered an error while running scenario "Home Page"
Error: Unsupported media feature: prefers-contrast

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

No branches or pull requests

3 participants