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

Feature: Add "suggested" widths based on media queries #228

Open
Auspicus opened this issue Apr 20, 2019 · 0 comments
Open

Feature: Add "suggested" widths based on media queries #228

Auspicus opened this issue Apr 20, 2019 · 0 comments

Comments

@Auspicus
Copy link

I noticed that Chrome Dev Tools now has a feature to show all media query widths for a page. It might be helpful to some users of Percy to have the it automatically detect these media query widths and take screenshots at these dimensions rather than providing manual widths.

https://github.com/chromium/chromium/blob/2ca8c5037021c9d2ecc00b787d58a31ed8fc8bcb/third_party/blink/renderer/devtools/front_end/emulation/MediaQueryInspector.js

The following code spits out the breakpoints from https://www.google.com

const puppeteer = require('puppeteer');

const run = async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://www.google.com');
  const client = await page.target().createCDPSession();
  await client.send('DOM.enable');
  await client.send('CSS.enable');
  const mediaQueries = await client.send('CSS.getMediaQueries');
  let breakpoints = [];
  mediaQueries.medias.forEach(mediaQuery => {
    mediaQuery.mediaList.forEach(item => {
      item.expressions.forEach(expression => {
        if (['max-width', 'min-width'].indexOf(expression.feature) !== -1) {
          breakpoints.push({
            type: expression.feature,
            value: expression.value,
            unit: expression.unit
          });
        }
      });
    });
  });

  console.group('Breakpoints');
  breakpoints = breakpoints.sort((a, b) => a.value - b.value);
  console.log(breakpoints);
  console.groupEnd();

  // Using these Percy could determine where to take screenshots.
  // For example:
  const bpMinWidth = breakpoints.filter(bp => bp.type === 'min-width');
  const bpMaxWidth = breakpoints.filter(bp => bp.type === 'max-width');
  let firstMaxWidth = bpMaxWidth[0];
  let lastMinWidth = bpMinWidth[0];

  console.log(`Need to test below ${firstMaxWidth.value}${firstMaxWidth.unit}`);
  bpMaxWidth.forEach((bp, index, arr) => {
    if (index === 0) {
      return;
    }

    const previousBp = arr[index - 1];
    console.log(`Need to test between ${previousBp.value}${previousBp.unit} and ${bp.value}${bp.unit}`);
  });
  console.log(`Need to test above ${lastMinWidth.value}${lastMinWidth.unit}`);

  await browser.close();
};

run();

/*
Breakpoints
  [ { type: 'max-width', value: 400, unit: 'px' },
    { type: 'max-width', value: 580, unit: 'px' },
    { type: 'min-width', value: 1222, unit: 'px' } ]
Need to test below 400px
Need to test between 400px and 580px
Need to test above 1222px
*/

Screen Shot 2019-04-20 at 4 23 15 PM

@Robdel12 Robdel12 transferred this issue from percy/percy-js Jun 5, 2019
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

1 participant