From 9aa321f04b1dd2eaf2d2bd757ae658680f660a49 Mon Sep 17 00:00:00 2001 From: Simon Schick Date: Fri, 5 Oct 2018 20:08:28 +0200 Subject: [PATCH] feat(puppeteer): update to 1.9 (#29456) --- types/puppeteer/index.d.ts | 110 +++++++++++++++++++---------- types/puppeteer/puppeteer-tests.ts | 8 +++ 2 files changed, 80 insertions(+), 38 deletions(-) diff --git a/types/puppeteer/index.d.ts b/types/puppeteer/index.d.ts index a7faa7f569..3ff4efbe4b 100644 --- a/types/puppeteer/index.d.ts +++ b/types/puppeteer/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for puppeteer 1.8 +// Type definitions for puppeteer 1.9 // Project: https://github.com/GoogleChrome/puppeteer#readme // Definitions by: Marvin Hagemeister // Christopher Deutsch @@ -291,6 +291,8 @@ export interface TracingStartOptions { categories?: string[]; } +export type DialogType = "alert" | "beforeunload" | "confirm" | "prompt"; + /** Dialog objects are dispatched by page via the 'dialog' event. */ export interface Dialog { /** @@ -309,18 +311,35 @@ export interface Dialog { message(): string; /** The dialog type. Dialog's type, can be one of `alert`, `beforeunload`, `confirm` or `prompt`. */ - type(): "alert" | "beforeunload" | "confirm" | "prompt"; + type(): DialogType; } +export type ConsoleMessageType = "log" + | "debug" + | "info" + | "error" + | "warning" + | "dir" + | "dirxml" + | "table" + | "trace" + | "clear" + | "startGroup" + | "startGroupCollapsed" + | "endGroup" + | "assert" + | "profile" + | "profileEnd" + | "count" + | "timeEnd"; + /** ConsoleMessage objects are dispatched by page via the 'console' event. */ export interface ConsoleMessage { /** The message arguments. */ args(): JSHandle[]; /** The message text. */ text(): string; - type(): 'log' | 'debug' | 'info' | 'error' | 'warning' | 'dir' | 'dirxml' | 'table' | - 'trace' | 'clear' | 'startGroup' | 'startGroupCollapsed' | 'endGroup' | 'assert' | - 'profile' | 'profileEnd' | 'count' | 'timeEnd'; + type(): ConsoleMessageType; } export type PageEvents = @@ -365,6 +384,8 @@ export interface ClickOptions { delay?: number; } +export type SameSiteSetting = "Strict" | "Lax"; + /** Represents a browser cookie. */ export interface Cookie { /** The cookie name. */ @@ -384,7 +405,7 @@ export interface Cookie { /** The cookie secure flag. */ secure: boolean; /** The cookie same site definition. */ - sameSite: "Strict" | "Lax"; + sameSite: SameSiteSetting; } export interface DeleteCookie { @@ -416,7 +437,7 @@ export interface SetCookie { /** The cookie secure flag. */ secure?: boolean; /** The cookie same site definition. */ - sameSite?: "Strict" | "Lax"; + sameSite?: SameSiteSetting; } export interface Viewport { @@ -1018,6 +1039,8 @@ export interface RemoteInfo { export interface Response { /** Promise which resolves to a buffer with response body. */ buffer(): Promise; + /** A Frame that initiated this response, or null if navigating to error pages. */ + frame(): Frame | null; /** True if the response was served from either the browser's disk cache or memory cache. */ fromCache(): boolean; /** True if the response was served by a service worker. */ @@ -1103,6 +1126,13 @@ export interface FrameBase extends Evalable { /** Gets the full HTML contents of the page, including the doctype. */ content(): Promise; + /** + * Navigates to a URL. + * @param url URL to navigate page to. The url should include scheme, e.g. `https://` + * @param options The navigation parameters. + */ + 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. @@ -1176,17 +1206,27 @@ export interface FrameBase extends Evalable { * Shortcut for waitForSelector and waitForXPath */ waitFor(selector: string, options?: WaitForSelectorOptions): Promise; + /** * Shortcut for waitForFunction. */ waitFor(selector: ((...args: any[]) => any) | string, options?: WaitForSelectorOptions, ...args: any[]): Promise; + /** + * Allows waiting for various conditions. + */ waitForFunction( fn: string | ((...args: any[]) => any), options?: PageFnOptions, ...args: any[] ): Promise; + /** + * Wait for the page navigation occur. + * @param options The navigation parameters. + */ + waitForNavigation(options?: NavigationOptions): Promise; + waitForSelector( selector: string, options?: WaitForSelectorOptions, @@ -1280,6 +1320,8 @@ export interface GeoOptions { accuracy?: number; } +export type MediaType = "screen" | "print"; + /** 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 { /** @@ -1338,7 +1380,7 @@ export interface Page extends EventEmitter, FrameBase { emulate(options: EmulateOptions): Promise; /** Emulates the media. */ - emulateMedia(mediaType: 'screen' | 'print' | null): Promise; + emulateMedia(mediaType: MediaType | null): Promise; /** * Evaluates a function in the page context. @@ -1388,13 +1430,6 @@ export interface Page extends EventEmitter, FrameBase { */ goForward(options?: NavigationOptions): Promise; - /** - * Navigates to a URL. - * @param url URL to navigate page to. The url should include scheme, e.g. `https://` - * @param options The navigation parameters. - */ - goto(url: string, options?: DirectNavigationOptions): Promise; - /** Returns the virtual keyboard. */ keyboard: Keyboard; @@ -1536,12 +1571,6 @@ export interface Page extends EventEmitter, FrameBase { /** Gets the page viewport. */ viewport(): Viewport; - /** - * Wait for the page navigation occur. - * @param options The navigation parameters. - */ - waitForNavigation(options?: NavigationOptions): Promise; - waitForRequest( urlOrPredicate: string | ((req: Request) => boolean), options?: { timeout?: number } @@ -1620,6 +1649,9 @@ export interface Browser extends EventEmitter { /** Spawned browser process. Returns `null` if the browser instance was created with `puppeteer.connect` method */ process(): ChildProcess; + /** A target associated with the browser. */ + target(): Target; + /** Promise which resolves to an array of all active targets. */ targets(): Promise; @@ -1651,22 +1683,22 @@ export interface BrowserEventObj { } export type Permission = - 'geolocation' | - 'midi' | - 'midi-sysex' | - 'notifications' | - 'push' | - 'camera' | - 'microphone' | - 'background-sync' | - 'ambient-light-sensor' | - 'accelerometer' | - 'gyroscope' | - 'magnetometer' | - 'accessibility-events' | - 'clipboard-read' | - 'clipboard-write' | - 'payment-handler'; + "geolocation" | + "midi" | + "midi-sysex" | + "notifications" | + "push" | + "camera" | + "microphone" | + "background-sync" | + "ambient-light-sensor" | + "accelerometer" | + "gyroscope" | + "magnetometer" | + "accessibility-events" | + "clipboard-read" | + "clipboard-write" | + "payment-handler"; /** * BrowserContexts provide a way to operate multiple independent browser sessions. @@ -1743,6 +1775,8 @@ export interface BrowserContextEventObj { targetdestroyed: Target; } +export type TargetType = "page" | "background_page" | "service_worker" | "browser" | "other"; + export interface Target { /** Get the browser the target belongs to. */ browser(): Browser; @@ -1760,7 +1794,7 @@ export interface Target { page(): Promise; /** Identifies what kind of target this is. */ - type(): "page" | "background_page" | "service_worker" | "browser" | "other"; + type(): TargetType; /** Returns the target URL. */ url(): string; diff --git a/types/puppeteer/puppeteer-tests.ts b/types/puppeteer/puppeteer-tests.ts index 84d1c12487..d5ee6982ef 100644 --- a/types/puppeteer/puppeteer-tests.ts +++ b/types/puppeteer/puppeteer-tests.ts @@ -19,6 +19,9 @@ import { TimeoutError } from "puppeteer/Errors"; await page.goto("https://news.ycombinator.com", { waitUntil: "networkidle0" }); await page.pdf({ path: "hn.pdf", format: "A4" }); + const frame = page.frames()[0]; + await frame.goto('/'); + browser.close(); })(); @@ -440,6 +443,11 @@ puppeteer.launch().then(async browser => { await page.waitFor((stuff: string) => !!document.querySelector(stuff), { hidden: true, }, 'asd'); + + const frame: puppeteer.Frame = page.frames()[0]; + await frame.waitFor((stuff: string) => !!document.querySelector(stuff), { + hidden: true, + }, 'asd'); })(); // Permission tests