diff --git a/lib/ElementHandle.js b/lib/ElementHandle.js index aabdbb6d646..49e7a8db5da 100644 --- a/lib/ElementHandle.js +++ b/lib/ElementHandle.js @@ -144,12 +144,16 @@ class ElementHandle extends JSHandle { */ async screenshot(options = {}) { await this._scrollIntoViewIfNeeded(); + const { layoutViewport: { pageX, pageY } } = await this._client.send('Page.getLayoutMetrics'); + const boundingBox = await this.boundingBox(); if (!boundingBox) throw new Error('Node is not visible'); - + const clip = Object.assign({}, boundingBox); + clip.x += pageX; + clip.y += pageY; return await this._page.screenshot(Object.assign({}, { - clip: boundingBox + clip }, options)); } diff --git a/test/golden/screenshot-element-scrolled-into-view.png b/test/golden/screenshot-element-scrolled-into-view.png new file mode 100644 index 00000000000..917dd48188d Binary files /dev/null and b/test/golden/screenshot-element-scrolled-into-view.png differ diff --git a/test/test.js b/test/test.js index bc66777e46c..ac4db6c686f 100644 --- a/test/test.js +++ b/test/test.js @@ -1796,6 +1796,29 @@ describe('Page', function() { const screenshot = await elementHandle.screenshot(); expect(screenshot).toBeGolden('screenshot-element-padding-border.png'); })); + it('should scroll element into view', SX(async function() { + await page.setViewport({width: 500, height: 500}); + await page.setContent(` + something above + +
+ + `); + const elementHandle = await page.$('div.to-screenshot'); + const screenshot = await elementHandle.screenshot(); + expect(screenshot).toBeGolden('screenshot-element-scrolled-into-view.png'); + })); it('should work with a rotated element', SX(async function() { await page.setViewport({width: 500, height: 500}); await page.setContent(`