chore: simplify screenshot option handling (#12245)

#9938
This commit is contained in:
Andrey Lushnikov 2022-02-18 22:34:56 -07:00 committed by GitHub
parent a16eaf584a
commit 18fdf927b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 23 deletions

View File

@ -18,7 +18,7 @@ import * as mime from 'mime';
import * as injectedScriptSource from '../generated/injectedScriptSource';
import * as channels from '../protocol/channels';
import { isSessionClosedError } from './protocolError';
import { ScreenshotMaskOption } from './screenshotter';
import { ScreenshotOptions } from './screenshotter';
import * as frames from './frames';
import type { InjectedScript, InjectedScriptPoll, LogEntry, HitTargetInterceptionResult } from './injected/injectedScript';
import { CallMetadata } from './instrumentation';
@ -27,6 +27,7 @@ import { Page } from './page';
import { Progress, ProgressController } from './progress';
import { SelectorInfo } from './selectors';
import * as types from './types';
import { TimeoutOptions } from '../common/types';
type SetInputFilesFiles = channels.ElementHandleSetInputFilesParams['files'];
type ActionName = 'click' | 'hover' | 'dblclick' | 'tap' | 'move and up' | 'move and down';
@ -773,7 +774,7 @@ export class ElementHandle<T extends Node = Node> extends js.JSHandle<T> {
return this._page._delegate.getBoundingBox(this);
}
async screenshot(metadata: CallMetadata, options: types.ElementScreenshotOptions & ScreenshotMaskOption = {}): Promise<Buffer> {
async screenshot(metadata: CallMetadata, options: ScreenshotOptions & TimeoutOptions = {}): Promise<Buffer> {
const controller = new ProgressController(metadata, this);
return controller.run(
progress => this._page._screenshotter.screenshotElement(progress, this, options),

View File

@ -20,7 +20,7 @@ import * as frames from './frames';
import * as input from './input';
import * as js from './javascript';
import * as network from './network';
import { Screenshotter, ScreenshotMaskOption } from './screenshotter';
import { Screenshotter, ScreenshotOptions } from './screenshotter';
import { TimeoutSettings } from '../utils/timeoutSettings';
import * as types from './types';
import { BrowserContext } from './browserContext';
@ -35,6 +35,7 @@ import { SelectorInfo, Selectors } from './selectors';
import { CallMetadata, SdkObject } from './instrumentation';
import { Artifact } from './artifact';
import { ParsedSelector } from './common/selectorParser';
import { TimeoutOptions } from '../common/types';
export interface PageDelegate {
readonly rawMouse: input.RawMouse;
@ -424,7 +425,7 @@ export class Page extends SdkObject {
route.continue();
}
async screenshot(metadata: CallMetadata, options: types.ScreenshotOptions & ScreenshotMaskOption = {}): Promise<Buffer> {
async screenshot(metadata: CallMetadata, options: ScreenshotOptions & TimeoutOptions): Promise<Buffer> {
const controller = new ProgressController(metadata, this);
return controller.run(
progress => this._screenshotter.screenshotPage(progress, options),

View File

@ -16,6 +16,7 @@
*/
import * as dom from './dom';
import { Rect } from '../common/types';
import { helper } from './helper';
import { Page } from './page';
import { Frame } from './frames';
@ -31,8 +32,14 @@ declare global {
}
}
export type ScreenshotMaskOption = {
export type ScreenshotOptions = {
type?: 'png' | 'jpeg',
quality?: number,
omitBackground?: boolean,
disableAnimations?: boolean,
mask?: { frame: Frame, selector: string}[],
fullPage?: boolean,
clip?: Rect,
};
export class Screenshotter {
@ -72,7 +79,7 @@ export class Screenshotter {
return fullPageSize!;
}
async screenshotPage(progress: Progress, options: types.ScreenshotOptions & ScreenshotMaskOption): Promise<Buffer> {
async screenshotPage(progress: Progress, options: ScreenshotOptions): Promise<Buffer> {
const format = validateScreenshotOptions(options);
return this._queue.postTask(async () => {
const { viewportSize } = await this._originalViewportSize(progress);
@ -99,7 +106,7 @@ export class Screenshotter {
});
}
async screenshotElement(progress: Progress, handle: dom.ElementHandle, options: types.ElementScreenshotOptions & ScreenshotMaskOption = {}): Promise<Buffer> {
async screenshotElement(progress: Progress, handle: dom.ElementHandle, options: ScreenshotOptions): Promise<Buffer> {
const format = validateScreenshotOptions(options);
return this._queue.postTask(async () => {
const { viewportSize } = await this._originalViewportSize(progress);
@ -215,7 +222,7 @@ export class Screenshotter {
}));
}
async _maskElements(progress: Progress, options: ScreenshotMaskOption) {
async _maskElements(progress: Progress, options: ScreenshotOptions) {
const framesToParsedSelectors: MultiMap<Frame, ParsedSelector> = new MultiMap();
await Promise.all((options.mask || []).map(async ({ frame, selector }) => {
const pair = await frame.resolveFrameForSelectorNoWait(selector);
@ -230,7 +237,7 @@ export class Screenshotter {
progress.cleanupWhenAborted(() => this._page.hideHighlight());
}
private async _screenshot(progress: Progress, format: 'png' | 'jpeg', documentRect: types.Rect | undefined, viewportRect: types.Rect | undefined, fitsViewport: boolean | undefined, options: types.ElementScreenshotOptions & ScreenshotMaskOption): Promise<Buffer> {
private async _screenshot(progress: Progress, format: 'png' | 'jpeg', documentRect: types.Rect | undefined, viewportRect: types.Rect | undefined, fitsViewport: boolean | undefined, options: ScreenshotOptions): Promise<Buffer> {
if ((options as any).__testHookBeforeScreenshot)
await (options as any).__testHookBeforeScreenshot();
progress.throwIfAborted(); // Screenshotting is expensive - avoid extra work.
@ -287,7 +294,7 @@ function trimClipToSize(clip: types.Rect, size: types.Size): types.Rect {
return result;
}
function validateScreenshotOptions(options: types.ScreenshotOptions): 'png' | 'jpeg' {
function validateScreenshotOptions(options: ScreenshotOptions): 'png' | 'jpeg' {
let format: 'png' | 'jpeg' | null = null;
// options.type takes precedence over inferring the type from options.path
// because it may be a 0-length file with no extension created beforehand (i.e. as a temp file).

View File

@ -15,7 +15,7 @@
* limitations under the License.
*/
import { Size, Point, Rect, TimeoutOptions } from '../common/types';
import { Size, Point, TimeoutOptions } from '../common/types';
export { Size, Point, Rect, Quad, URLMatch, TimeoutOptions } from '../common/types';
export type StrictOptions = {
@ -47,18 +47,6 @@ export type PointerActionWaitOptions = TimeoutOptions & ForceOptions & StrictOpt
trial?: boolean;
};
export type ElementScreenshotOptions = TimeoutOptions & {
type?: 'png' | 'jpeg',
quality?: number,
omitBackground?: boolean,
disableAnimations?: boolean,
};
export type ScreenshotOptions = ElementScreenshotOptions & {
fullPage?: boolean,
clip?: Rect,
};
export type PageScreencastOptions = {
width: number,
height: number,