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: taking a screenshot with null viewport should be possible #8680

Merged
merged 1 commit into from Jul 21, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/common/ElementHandle.ts
Expand Up @@ -785,11 +785,11 @@ export class ElementHandle<
assert(boundingBox, 'Node is either not visible or not an HTMLElement');

const viewport = this.#page.viewport();
assert(viewport);

if (
boundingBox.width > viewport.width ||
boundingBox.height > viewport.height
viewport &&
(boundingBox.width > viewport.width ||
boundingBox.height > viewport.height)
) {
const newViewport = {
width: Math.max(viewport.width, Math.ceil(boundingBox.width)),
Expand Down Expand Up @@ -826,7 +826,7 @@ export class ElementHandle<
)
);

if (needsViewportReset) {
if (needsViewportReset && viewport) {
await this.#page.setViewport(viewport);
}

Expand Down
22 changes: 22 additions & 0 deletions test/src/screenshot.spec.ts
Expand Up @@ -21,6 +21,7 @@ import {
setupTestPageAndContextHooks,
itFailsFirefox,
itHeadfulOnly,
itChromeOnly,
} from './mocha-utils.js';

describe('Screenshots', function () {
Expand Down Expand Up @@ -214,6 +215,27 @@ describe('Screenshots', function () {
const screenshot = await elementHandle.screenshot();
expect(screenshot).toBeGolden('screenshot-element-bounding-box.png');
});
itChromeOnly('should work with a null viewport', async () => {
const {defaultBrowserOptions, puppeteer, server} = getTestState();

const browser = await puppeteer.launch({
...defaultBrowserOptions,
defaultViewport: null,
});

try {
const page = await browser.newPage();
await page.goto(server.PREFIX + '/grid.html');
await page.evaluate(() => {
return window.scrollBy(50, 100);
});
const elementHandle = (await page.$('.box:nth-of-type(3)'))!;
const screenshot = await elementHandle.screenshot();
expect(screenshot).toBeTruthy();
} finally {
await browser.close();
}
});
it('should take into account padding and border', async () => {
const {page} = getTestState();

Expand Down