mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
fix: support clip in ElementHandle.screenshot (#12115)
This commit is contained in:
parent
93e9acc410
commit
b096ffaa03
@ -68,7 +68,7 @@ clip
|
|||||||
|
|
||||||
</td><td>
|
</td><td>
|
||||||
|
|
||||||
Specifies the region of the page to clip.
|
Specifies the region of the page/element to clip.
|
||||||
|
|
||||||
</td><td>
|
</td><td>
|
||||||
|
|
||||||
|
@ -1236,9 +1236,9 @@ export abstract class ElementHandle<
|
|||||||
this: ElementHandle<Element>,
|
this: ElementHandle<Element>,
|
||||||
options: Readonly<ElementScreenshotOptions> = {}
|
options: Readonly<ElementScreenshotOptions> = {}
|
||||||
): Promise<string | Buffer> {
|
): Promise<string | Buffer> {
|
||||||
const {scrollIntoView = true} = options;
|
const {scrollIntoView = true, clip} = options;
|
||||||
|
|
||||||
let clip = await this.#nonEmptyVisibleBoundingBox();
|
let elementClip = await this.#nonEmptyVisibleBoundingBox();
|
||||||
|
|
||||||
const page = this.frame.page();
|
const page = this.frame.page();
|
||||||
|
|
||||||
@ -1247,7 +1247,7 @@ export abstract class ElementHandle<
|
|||||||
await this.scrollIntoViewIfNeeded();
|
await this.scrollIntoViewIfNeeded();
|
||||||
|
|
||||||
// We measure again just in case.
|
// We measure again just in case.
|
||||||
clip = await this.#nonEmptyVisibleBoundingBox();
|
elementClip = await this.#nonEmptyVisibleBoundingBox();
|
||||||
}
|
}
|
||||||
|
|
||||||
const [pageLeft, pageTop] = await this.evaluate(() => {
|
const [pageLeft, pageTop] = await this.evaluate(() => {
|
||||||
@ -1259,10 +1259,16 @@ export abstract class ElementHandle<
|
|||||||
window.visualViewport.pageTop,
|
window.visualViewport.pageTop,
|
||||||
] as const;
|
] as const;
|
||||||
});
|
});
|
||||||
clip.x += pageLeft;
|
elementClip.x += pageLeft;
|
||||||
clip.y += pageTop;
|
elementClip.y += pageTop;
|
||||||
|
if (clip) {
|
||||||
|
elementClip.x += clip.x;
|
||||||
|
elementClip.y += clip.y;
|
||||||
|
elementClip.height = clip.height;
|
||||||
|
elementClip.width = clip.width;
|
||||||
|
}
|
||||||
|
|
||||||
return await page.screenshot({...options, clip});
|
return await page.screenshot({...options, clip: elementClip});
|
||||||
}
|
}
|
||||||
|
|
||||||
async #nonEmptyVisibleBoundingBox() {
|
async #nonEmptyVisibleBoundingBox() {
|
||||||
|
@ -274,7 +274,7 @@ export interface ScreenshotOptions {
|
|||||||
*/
|
*/
|
||||||
path?: string;
|
path?: string;
|
||||||
/**
|
/**
|
||||||
* Specifies the region of the page to clip.
|
* Specifies the region of the page/element to clip.
|
||||||
*/
|
*/
|
||||||
clip?: ScreenshotClip;
|
clip?: ScreenshotClip;
|
||||||
/**
|
/**
|
||||||
|
BIN
test/golden-chrome/screenshot-element-clip.png
Normal file
BIN
test/golden-chrome/screenshot-element-clip.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 104 B |
BIN
test/golden-firefox/screenshot-element-clip.png
Normal file
BIN
test/golden-firefox/screenshot-element-clip.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 104 B |
@ -393,6 +393,33 @@ describe('Screenshots', function () {
|
|||||||
|
|
||||||
await context.close();
|
await context.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should use element clip', async () => {
|
||||||
|
const {page} = await getTestState();
|
||||||
|
|
||||||
|
await page.setViewport({width: 500, height: 500});
|
||||||
|
await page.setContent(`
|
||||||
|
something above
|
||||||
|
<style>div {
|
||||||
|
border: 2px solid blue;
|
||||||
|
background: green;
|
||||||
|
width: 50px;
|
||||||
|
height: 50px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<div></div>
|
||||||
|
`);
|
||||||
|
using elementHandle = (await page.$('div'))!;
|
||||||
|
const screenshot = await elementHandle.screenshot({
|
||||||
|
clip: {
|
||||||
|
x: 10,
|
||||||
|
y: 10,
|
||||||
|
width: 20,
|
||||||
|
height: 20,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(screenshot).toBeGolden('screenshot-element-clip.png');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Cdp', () => {
|
describe('Cdp', () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user