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

fix(testing): use viewport for Puppeteer screenshot clip dimensions #5359

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/testing/puppeteer/puppeteer-screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,6 @@ export async function pageCompareScreenshot(
});
});

const screenshotOpts = createPuppeteerScreenshotOptions(opts);
const screenshotBuf = await page.screenshot(screenshotOpts);
const pixelmatchThreshold =
typeof opts.pixelmatchThreshold === 'number' ? opts.pixelmatchThreshold : screenshotBuildData.pixelmatchThreshold;

let width = emulateConfig.viewport.width;
let height = emulateConfig.viewport.height;

Expand All @@ -123,6 +118,15 @@ export async function pageCompareScreenshot(
}
}

// The width and height passed into this function will be the dimensions of the generated image
// This is _not_ guaranteed to be the viewport dimensions specified in the emulate config. If clip
// options were provided this comparison function, the width and height will be set to those clip dimensions.
// Otherwise, it will default to the emulate config viewport dimensions.
const screenshotOpts = createPuppeteerScreenshotOptions(opts, { width, height });
const screenshotBuf = await page.screenshot(screenshotOpts);
const pixelmatchThreshold =
typeof opts.pixelmatchThreshold === 'number' ? opts.pixelmatchThreshold : screenshotBuildData.pixelmatchThreshold;

const results = await compareScreenshot(
emulateConfig,
screenshotBuildData,
Expand All @@ -137,7 +141,10 @@ export async function pageCompareScreenshot(
return results;
}

function createPuppeteerScreenshotOptions(opts: ScreenshotOptions) {
export function createPuppeteerScreenshotOptions(
opts: ScreenshotOptions,
{ width, height }: { width: number; height: number },
) {
const puppeteerOpts: puppeteer.ScreenshotOptions = {
type: 'png',
fullPage: opts.fullPage,
Expand All @@ -152,6 +159,13 @@ function createPuppeteerScreenshotOptions(opts: ScreenshotOptions) {
width: opts.clip.width,
height: opts.clip.height,
};
} else {
puppeteerOpts.clip = {
x: 0,
y: 0,
width,
height,
};
}

return puppeteerOpts;
Expand Down
37 changes: 37 additions & 0 deletions src/testing/puppeteer/test/puppeteer-screenshot.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { createPuppeteerScreenshotOptions } from '../puppeteer-screenshot';

describe('Puppeteer Screenshot', () => {
describe('createPuppeteerScreenshotOptions', () => {
it('should use the viewport width and height by default', () => {
const options = createPuppeteerScreenshotOptions({}, { width: 800, height: 600 });

expect(options.clip).toEqual({
x: 0,
y: 0,
width: 800,
height: 600,
});
});

it('should use clip options if provided', () => {
const options = createPuppeteerScreenshotOptions(
{
clip: {
x: 10,
y: 20,
width: 100,
height: 200,
},
},
{ width: 800, height: 600 },
);

expect(options.clip).toEqual({
x: 10,
y: 20,
width: 100,
height: 200,
});
});
});
});
Loading