From 545b3be2ae31da15388ca0b143f49ab66ec9f307 Mon Sep 17 00:00:00 2001 From: Simon Schick Date: Thu, 25 Jul 2019 11:56:39 -0700 Subject: [PATCH] feat(puppeteer): v1.19 (#37084) --- types/puppeteer/index.d.ts | 160 ++++++++++++----------------- types/puppeteer/puppeteer-tests.ts | 20 ++++ 2 files changed, 85 insertions(+), 95 deletions(-) diff --git a/types/puppeteer/index.d.ts b/types/puppeteer/index.d.ts index 38ff6c5b65..c4f25ee54c 100644 --- a/types/puppeteer/index.d.ts +++ b/types/puppeteer/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for puppeteer 1.12 +// Type definitions for puppeteer 1.19 // Project: https://github.com/GoogleChrome/puppeteer#readme // Definitions by: Marvin Hagemeister // Christopher Deutsch @@ -13,6 +13,11 @@ import { EventEmitter } from "events"; import { ChildProcess } from "child_process"; +import * as errors from "./Errors"; +import * as devices from "./DeviceDescriptors"; + +export { errors, devices }; + /** Wraps a DOM element into an ElementHandle instance */ export type WrapElementHandle = X extends Element ? ElementHandle : X; @@ -215,6 +220,31 @@ export interface Evalable { ): Promise>; } +export interface JSEvalable { + /** + * Evaluates a function in the browser context. + * If the function, passed to the frame.evaluate, returns a Promise, then frame.evaluate would wait for the promise to resolve and return its value. + * If the function passed into frame.evaluate returns a non-Serializable value, then frame.evaluate resolves to undefined. + * @param fn Function to be evaluated in browser context + * @param args Arguments to pass to `fn` + */ + evaluate( + pageFunction: T, + ...args: SerializableOrJSHandle[], + ): Promise>; + /** + * The only difference between `evaluate` and `evaluateHandle` is that `evaluateHandle` returns in-page object (`JSHandle`). + * If the function, passed to the `evaluateHandle`, returns a `Promise`, then `evaluateHandle` would wait for the + * promise to resolve and return its value. + * @param fn Function to be evaluated in browser context + * @param args Arguments to pass to `fn` + */ + evaluateHandle( + pageFunction: (...args: any[]) => any, + ...args: SerializableOrJSHandle[], + ): Promise; + } + /** Keyboard provides an api for managing a virtual keyboard. */ export interface Keyboard { /** @@ -375,29 +405,6 @@ export interface ConsoleMessage { type(): ConsoleMessageType; } -export type PageEvents = - | "close" - | "console" - | "dialog" - | "error" - | "frameattached" - | "framedetached" - | "framenavigated" - | "load" - | "pageerror" - | "request" - | "requestfailed" - | "requestfinished" - | "response" - | "workercreated" - | "workerdestroyed"; - -export type BrowserEvents = - | "disconnected" - | "targetchanged" - | "targetcreated" - | "targetdestroyed"; - export interface AuthOptions { username: string; password: string; @@ -742,28 +749,7 @@ export interface Box { * The Worker class represents a WebWorker. * The events workercreated and workerdestroyed are emitted on the page object to signal the worker lifecycle. */ -export interface Worker { - /** - * If the function passed to the `worker.evaluate` returns a Promise, - * then `worker.evaluate` would wait for the promise to resolve and return its value. - * - * If the function passed to the `worker.evaluate` returns a non-Serializable value, - * then `worker.evaluate` resolves to `undefined`. - */ - evaluate( - pageFunction: T, - ...args: SerializableOrJSHandle[], - ): Promise>; - - /** - * The only difference between `worker.evaluate` and `worker.evaluateHandle` is - * that `worker.evaluateHandle` returns in-page object (JSHandle). - */ - evaluateHandle( - pageFunction: (...args: any[]) => T | Promise, - ...args: SerializableOrJSHandle[], - ): Promise; - +export interface Worker extends JSEvalable { executionContext(): Promise; url(): string; @@ -861,15 +847,7 @@ export interface ElementHandle extends JSHandle, Ev } /** The class represents a context for JavaScript execution. */ -export interface ExecutionContext { - evaluate( - fn: F, - ...args: SerializableOrJSHandle[] - ): Promise>; - evaluateHandle( - fn: EvaluateFn, - ...args: SerializableOrJSHandle[] - ): Promise; +export interface ExecutionContext extends JSEvalable { queryObjects(prototypeHandle: JSHandle): JSHandle; } @@ -1141,7 +1119,7 @@ export interface WaitForSelectorOptionsHidden extends WaitForSelectorOptions { hidden: true; } -export interface FrameBase extends Evalable { +export interface FrameBase extends Evalable, JSEvalable { /** * The method queries frame for the selector. * If there's no such element within the frame, the method will resolve to null. @@ -1185,31 +1163,6 @@ export interface FrameBase extends Evalable { */ goto(url: string, options?: DirectNavigationOptions): Promise; - /** - * Evaluates a function in the browser context. - * If the function, passed to the frame.evaluate, returns a Promise, then frame.evaluate would wait for the promise to resolve and return its value. - * If the function passed into frame.evaluate returns a non-Serializable value, then frame.evaluate resolves to undefined. - * @param fn Function to be evaluated in browser context - * @param args Arguments to pass to `fn` - */ - evaluate( - fn: F, - ...args: SerializableOrJSHandle[] - ): Promise>; - - /** - * Evaluates a function in the page context. - * If the function, passed to the page.evaluateHandle, returns a Promise, then page.evaluateHandle - * would wait for the promise to resolve and return its value. - * @param fn The function to be evaluated in the page context. - * @param args The arguments to pass to the `fn`. - * @returns A promise which resolves to return value of `fn`. - */ - evaluateHandle( - fn: EvaluateFn, - ...args: SerializableOrJSHandle[] - ): Promise; - /** This method fetches an element with selector and focuses it. */ focus(selector: string): Promise; @@ -1513,6 +1466,11 @@ export interface SnapshopOptions { * @default true */ interestingOnly?: boolean; + /** + * The root DOM element for the snapshot. + * @default document.body + */ + root?: ElementHandle; } /** @@ -1531,6 +1489,18 @@ export interface Accessibility { snapshot(options?: SnapshopOptions): Promise; } +export interface FileChooser { + /** + * Accept the file chooser request with given paths. + * If some of the filePaths are relative paths, then they are resolved relative to the current working directory. + */ + accept(filePaths: string[]): Promise; + /** Closes the file chooser without selecting any files. */ + cancel(): Promise; + /** Whether file chooser allow for multiple file selection. */ + isMultiple(): boolean; +} + /** Page provides methods to interact with a single tab in Chromium. One Browser instance might have multiple Page instances. */ export interface Page extends EventEmitter, FrameBase { /** @@ -1596,19 +1566,6 @@ export interface Page extends EventEmitter, FrameBase { /** Emulates the media. */ emulateMedia(mediaType: MediaType | null): Promise; - /** - * Evaluates a function in the page context. - * If the function, passed to the page.evaluateHandle, returns a Promise, then page.evaluateHandle - * would wait for the promise to resolve and return its value. - * @param fn The function to be evaluated in the page context. - * @param args The arguments to pass to the `fn`. - * @returns A promise which resolves to return value of `fn`. - */ - evaluateHandle( - fn: EvaluateFn, - ...args: SerializableOrJSHandle[] - ): Promise; - /** * Adds a function which would be invoked in one of the following scenarios: whenever the page is navigated; whenever the child frame is attached or navigated. * The function is invoked after the document was created but before any of its scripts were run. This is useful to amend JavaScript environment, e.g. to seed Math.random. @@ -1805,6 +1762,13 @@ export interface Page extends EventEmitter, FrameBase { options?: Timeoutable ): Promise; + /** + * In non-headless Chromium, this method results in the native file picker dialog not showing up for the user. + * This method is typically coupled with an action that triggers file choosing. + * This must be called before the file chooser is launched. It will not return a currently active file chooser. + */ + waitForFileChooser(options?: Timeoutable): Promise; + /** This method returns all of the dedicated WebWorkers associated with the page. */ workers(): Worker[]; } @@ -1862,6 +1826,9 @@ export interface Browser extends EventEmitter, TargetAwaiter { */ disconnect(): void; + /** Indicates that the browser is connected. */ + isConnected(): boolean; + /** * Returns the default browser context. * The default browser context can not be closed. @@ -2003,7 +1970,7 @@ export interface BrowserContextEventObj { targetdestroyed: Target; } -export type TargetType = "page" | "background_page" | "service_worker" | "browser" | "other"; +export type TargetType = "page" | "background_page" | "shared_worker" | "service_worker" | "browser" | "other"; export interface Target { /** Get the browser the target belongs to. */ @@ -2026,6 +1993,9 @@ export interface Target { /** Returns the target URL. */ url(): string; + + /** If the target is not of type `service_worker` or `shared_worker`, resolves `null`. */ + worker(): Promise; } export interface LaunchOptions extends ChromeArgOptions, BrowserOptions, Timeoutable { diff --git a/types/puppeteer/puppeteer-tests.ts b/types/puppeteer/puppeteer-tests.ts index 90006c77d4..e38cafda6c 100644 --- a/types/puppeteer/puppeteer-tests.ts +++ b/types/puppeteer/puppeteer-tests.ts @@ -9,6 +9,7 @@ import * as Devices from "puppeteer/DeviceDescriptors"; const page = await browser.newPage(); const snap = await page.accessibility.snapshot({ interestingOnly: true, + root: undefined, }); for (const child of snap.children) { console.log(child.name); @@ -121,6 +122,7 @@ puppeteer.launch().then(async browser => { await page.emulateMedia("screen"); await page.emulate(Devices['test']); + await page.emulate(puppeteer.devices['test']); await page.pdf({ path: "page.pdf" }); await page.setRequestInterception(true); @@ -643,3 +645,21 @@ puppeteer.launch().then(async browser => { await browserFetcher.remove(rev); } }); + +(async () => { + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + const url = page.workers()[0].url(); + if (page.target().type() === 'shared_worker') { + const a: number = await (await page.target().worker())!.evaluate(() => 1); + } +}); + +(async () => { + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + const fileChooser = await page.waitForFileChooser({ timeout: 999 }); + await fileChooser.cancel(); + const isMultiple: boolean = fileChooser.isMultiple(); + await fileChooser.accept(['/foo/bar']); +});