feat: add fromSurface option to page.screenshot (#8496)
This commit is contained in:
parent
329195011f
commit
79e11985ba
@ -2211,6 +2211,7 @@ Shortcut for [page.mainFrame().executionContext().queryObjects(prototypeHandle)]
|
|||||||
- `omitBackground` <[boolean]> Hides default white background and allows capturing screenshots with transparency. Defaults to `false`.
|
- `omitBackground` <[boolean]> Hides default white background and allows capturing screenshots with transparency. Defaults to `false`.
|
||||||
- `encoding` <[string]> The encoding of the image, can be either `base64` or `binary`. Defaults to `binary`.
|
- `encoding` <[string]> The encoding of the image, can be either `base64` or `binary`. Defaults to `binary`.
|
||||||
- `captureBeyondViewport` <[boolean]> When true, captures screenshot [beyond the viewport](https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-captureScreenshot). When false, falls back to old behaviour, and cuts the screenshot by the viewport size. Defaults to `true`.
|
- `captureBeyondViewport` <[boolean]> When true, captures screenshot [beyond the viewport](https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-captureScreenshot). When false, falls back to old behaviour, and cuts the screenshot by the viewport size. Defaults to `true`.
|
||||||
|
- `fromSurface` <[boolean]> When true, captures screenshot [from the surface rather than the view](https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-captureScreenshot). When false, works only in headful mode and ignores page viewport (but not browser window's bounds). Defaults to `true`.
|
||||||
- returns: <[Promise]<[string]|[Buffer]>> Promise which resolves to buffer or a base64 string (depending on the value of `encoding`) with captured screenshot.
|
- returns: <[Promise]<[string]|[Buffer]>> Promise which resolves to buffer or a base64 string (depending on the value of `encoding`) with captured screenshot.
|
||||||
|
|
||||||
> **NOTE** Screenshots take at least 1/6 second on OS X. See https://crbug.com/741689 for discussion.
|
> **NOTE** Screenshots take at least 1/6 second on OS X. See https://crbug.com/741689 for discussion.
|
||||||
|
@ -197,10 +197,15 @@ export interface ScreenshotOptions {
|
|||||||
*/
|
*/
|
||||||
encoding?: 'base64' | 'binary';
|
encoding?: 'base64' | 'binary';
|
||||||
/**
|
/**
|
||||||
* If you need a screenshot bigger than the Viewport
|
* Capture the screenshot beyond the viewport.
|
||||||
* @defaultValue true
|
* @defaultValue true
|
||||||
*/
|
*/
|
||||||
captureBeyondViewport?: boolean;
|
captureBeyondViewport?: boolean;
|
||||||
|
/**
|
||||||
|
* Capture the screenshot from the surface, rather than the view.
|
||||||
|
* @defaultValue true
|
||||||
|
*/
|
||||||
|
fromSurface?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2759,7 +2764,7 @@ export class Page extends EventEmitter {
|
|||||||
* applicable to `png` images.
|
* applicable to `png` images.
|
||||||
*
|
*
|
||||||
* - `fullPage` : When true, takes a screenshot of the full
|
* - `fullPage` : When true, takes a screenshot of the full
|
||||||
* scrollable page. Defaults to `false`
|
* scrollable page. Defaults to `false`.
|
||||||
*
|
*
|
||||||
* - `clip` : An object which specifies clipping region of the page.
|
* - `clip` : An object which specifies clipping region of the page.
|
||||||
* Should have the following fields:<br/>
|
* Should have the following fields:<br/>
|
||||||
@ -2769,11 +2774,21 @@ export class Page extends EventEmitter {
|
|||||||
* - `height` : height of clipping area.
|
* - `height` : height of clipping area.
|
||||||
*
|
*
|
||||||
* - `omitBackground` : Hides default white background and allows
|
* - `omitBackground` : Hides default white background and allows
|
||||||
* capturing screenshots with transparency. Defaults to `false`
|
* capturing screenshots with transparency. Defaults to `false`.
|
||||||
*
|
*
|
||||||
* - `encoding` : The encoding of the image, can be either base64 or
|
* - `encoding` : The encoding of the image, can be either base64 or
|
||||||
* binary. Defaults to `binary`.
|
* binary. Defaults to `binary`.
|
||||||
*
|
*
|
||||||
|
* - `captureBeyondViewport` : When true, captures screenshot
|
||||||
|
* {@link https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-captureScreenshot
|
||||||
|
* | beyond the viewport}. When false, falls back to old behaviour,
|
||||||
|
* and cuts the screenshot by the viewport size. Defaults to `true`.
|
||||||
|
*
|
||||||
|
* - `fromSurface` : When true, captures screenshot
|
||||||
|
* {@link https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-captureScreenshot
|
||||||
|
* | from the surface rather than the view}. When false, works only in
|
||||||
|
* headful mode and ignores page viewport (but not browser window's
|
||||||
|
* bounds). Defaults to `true`.
|
||||||
*
|
*
|
||||||
* NOTE: Screenshots take at least 1/6 second on OS X. See
|
* NOTE: Screenshots take at least 1/6 second on OS X. See
|
||||||
* {@link https://crbug.com/741689} for discussion.
|
* {@link https://crbug.com/741689} for discussion.
|
||||||
@ -2881,9 +2896,14 @@ export class Page extends EventEmitter {
|
|||||||
targetId: this.#target._targetId,
|
targetId: this.#target._targetId,
|
||||||
});
|
});
|
||||||
let clip = options.clip ? processClip(options.clip) : undefined;
|
let clip = options.clip ? processClip(options.clip) : undefined;
|
||||||
let {captureBeyondViewport = true} = options;
|
const captureBeyondViewport =
|
||||||
captureBeyondViewport =
|
typeof options.captureBeyondViewport === 'boolean'
|
||||||
typeof captureBeyondViewport === 'boolean' ? captureBeyondViewport : true;
|
? options.captureBeyondViewport
|
||||||
|
: true;
|
||||||
|
const fromSurface =
|
||||||
|
typeof options.fromSurface === 'boolean'
|
||||||
|
? options.fromSurface
|
||||||
|
: undefined;
|
||||||
|
|
||||||
if (options.fullPage) {
|
if (options.fullPage) {
|
||||||
const metrics = await this.#client.send('Page.getLayoutMetrics');
|
const metrics = await this.#client.send('Page.getLayoutMetrics');
|
||||||
@ -2923,6 +2943,7 @@ export class Page extends EventEmitter {
|
|||||||
quality: options.quality,
|
quality: options.quality,
|
||||||
clip,
|
clip,
|
||||||
captureBeyondViewport,
|
captureBeyondViewport,
|
||||||
|
fromSurface,
|
||||||
});
|
});
|
||||||
if (shouldSetDefaultBackground) {
|
if (shouldSetDefaultBackground) {
|
||||||
await this.#resetDefaultBackgroundColor();
|
await this.#resetDefaultBackgroundColor();
|
||||||
|
@ -185,6 +185,17 @@ export const itHeadlessOnly = (
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const itHeadfulOnly = (
|
||||||
|
description: string,
|
||||||
|
body: Mocha.Func
|
||||||
|
): Mocha.Test => {
|
||||||
|
if (isChrome && isHeadless === false) {
|
||||||
|
return it(description, body);
|
||||||
|
} else {
|
||||||
|
return xit(description, body);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export const itFirefoxOnly = (
|
export const itFirefoxOnly = (
|
||||||
description: string,
|
description: string,
|
||||||
body: Mocha.Func
|
body: Mocha.Func
|
||||||
|
@ -20,6 +20,7 @@ import {
|
|||||||
setupTestBrowserHooks,
|
setupTestBrowserHooks,
|
||||||
setupTestPageAndContextHooks,
|
setupTestPageAndContextHooks,
|
||||||
itFailsFirefox,
|
itFailsFirefox,
|
||||||
|
itHeadfulOnly,
|
||||||
} from './mocha-utils.js';
|
} from './mocha-utils.js';
|
||||||
|
|
||||||
describe('Screenshots', function () {
|
describe('Screenshots', function () {
|
||||||
@ -188,6 +189,16 @@ describe('Screenshots', function () {
|
|||||||
'screenshot-sanity.png'
|
'screenshot-sanity.png'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
itHeadfulOnly('should work in "fromSurface: false" mode', async () => {
|
||||||
|
const {page, server} = getTestState();
|
||||||
|
|
||||||
|
await page.setViewport({width: 500, height: 500});
|
||||||
|
await page.goto(server.PREFIX + '/grid.html');
|
||||||
|
const screenshot = await page.screenshot({
|
||||||
|
fromSurface: false,
|
||||||
|
});
|
||||||
|
expect(screenshot).toBeDefined(); // toBeGolden('screenshot-fromsurface-false.png');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('ElementHandle.screenshot', function () {
|
describe('ElementHandle.screenshot', function () {
|
||||||
|
Loading…
Reference in New Issue
Block a user