This thread is trying to answer question "Has anyone encountered differing screenshot dimensions between headless and headful modes when taking full-page screenshots, and is there a solution?"
Yes, it's described here: https://github.com/microsoft/playwright/issues/12683
if you need to take a screenshot of particular component that's just not fully visible, then there's a good workaround:
Then(
'framework verified screenshot of scrollable element with testId {string}',
async ({ page, $testInfo }, locator: string): Promise<void> => {
const currentViewport = page.viewportSize();
if (!currentViewport) {
throw new Error('Current viewport size is not set.');
}
const pixelAmountRenderedOffscreen = await page.evaluate((locator) => {
const content = document.querySelector(`[data-testid='${locator}']`);
if (!content) {
throw new Error(`Element with data-testId '${locator}' not found.`);
}
return content.clientHeight;
}, locator);
await page.setViewportSize({
width: currentViewport.width,
height: currentViewport.height + pixelAmountRenderedOffscreen,
});
try {
await expect(page.getByTestId(locator)).toHaveScreenshot(`${$testInfo.title}.png`, {
threshold: defaultThresholds.threshold,
maxDiffPixelRatio: defaultThresholds.maxDiffPixelRatio,
timeout: 15000,
});
} catch (error) {
const pixelDifferenceMatch = error.message.match(errorMessageRegex);
if (pixelDifferenceMatch) {
throw new Error(pixelDifferenceMatch[1]);
} else {
throw error;
}
}
}
);
BUT if that element is really huge you might run into performance issues.
now it looks like:
Error: 508 pixels (ratio 0.11 of all image pixels) are different.
was
Call log:
- page._expectScreenshot with timeout 5000ms
- verifying given screenshot expectation
- waiting for getByTestId('select-wrapper')
- locator resolved to <div data-testid="select-wrapper">…</div>
- taking element screenshot
- disabled all CSS animations
- waiting for element to be visible and stable
- element is visible and stable
- 508 pixels (ratio 0.11 of all image pixels) are different.
- waiting 100ms before taking screenshot
- waiting for getByTestId('select-wrapper')
- locator resolved to <div data-testid="select-wrapper">…</div>
- taking element screenshot
- disabled all CSS animations
- waiting for element to be visible and stable
- element is visible and stable
- captured a stable screenshot
- 508 pixels (ratio 0.11 of all image pixels) are different.
in case of missing locator in both cases it looks the same:
Call log:
- page._expectScreenshot with timeout 5000ms
- verifying given screenshot expectation
- waiting for getByTestId('select-wrapper1')
- Timeout 5000ms exceeded.
Call log:
- page._expectScreenshot with timeout 5000ms
- verifying given screenshot expectation
- waiting for getByTestId('select-wrapper1')
- Timeout 5000ms exceeded.
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].