Skip to content

Commit

Permalink
chore: migrate src/Page.js to TypeScript (#5809)
Browse files Browse the repository at this point in the history
* chore: migrate src/Page.js to TypeScript

The final one! This is a huge file and needs to be split up and tidied,
but for now I've left all the definitions in place and converted types
accordingly.

There's some additional tidying we can do now every `src` file is TS,
but I'll leave that for another PR to avoid this one getting any bigger.

Co-authored-by: Mathias Bynens <mathias@qiwi.be>
  • Loading branch information
jackfranklin and mathiasbynens committed May 5, 2020
1 parent eed7d94 commit de4f08d
Show file tree
Hide file tree
Showing 9 changed files with 354 additions and 689 deletions.
14 changes: 6 additions & 8 deletions src/Browser.ts
Expand Up @@ -20,6 +20,7 @@ import * as EventEmitter from 'events';
import {TaskQueue} from './TaskQueue';
import {Events} from './Events';
import {Connection} from './Connection';
import {Page} from './Page';
import {ChildProcess} from 'child_process';

type BrowserCloseCallback = () => Promise<void> | void;
Expand Down Expand Up @@ -137,11 +138,11 @@ export class Browser extends EventEmitter {
return this._connection.url();
}

async newPage(): Promise<Puppeteer.Page> {
async newPage(): Promise<Page> {
return this._defaultContext.newPage();
}

async _createPageInContext(contextId?: string): Promise<Puppeteer.Page> {
async _createPageInContext(contextId?: string): Promise<Page> {
const {targetId} = await this._connection.send('Target.createTarget', {url: 'about:blank', browserContextId: contextId || undefined});
const target = await this._targets.get(targetId);
assert(await target._initializedPromise, 'Failed to create target for page');
Expand Down Expand Up @@ -188,10 +189,7 @@ export class Browser extends EventEmitter {
}
}

/**
* @return {!Promise<!Array<!Puppeteer.Page>>}
*/
async pages(): Promise<Puppeteer.Page[]> {
async pages(): Promise<Page[]> {
const contextPages = await Promise.all(this.browserContexts().map(context => context.pages()));
// Flatten array.
return contextPages.reduce((acc, x) => acc.concat(x), []);
Expand Down Expand Up @@ -245,7 +243,7 @@ export class BrowserContext extends EventEmitter {
return this._browser.waitForTarget(target => target.browserContext() === this && predicate(target), options);
}

async pages(): Promise<Puppeteer.Page[]> {
async pages(): Promise<Page[]> {
const pages = await Promise.all(
this.targets()
.filter(target => target.type() === 'page')
Expand Down Expand Up @@ -292,7 +290,7 @@ export class BrowserContext extends EventEmitter {
await this._connection.send('Browser.resetPermissions', {browserContextId: this._id || undefined});
}

newPage(): Promise<Puppeteer.Page> {
newPage(): Promise<Page> {
return this._browser._createPageInContext(this._id);
}

Expand Down
9 changes: 5 additions & 4 deletions src/Dialog.ts
Expand Up @@ -17,14 +17,17 @@
import {assert} from './helper';
import {CDPSession} from './Connection';

enum DialogType {
/* TODO(jacktfranklin): protocol.d.ts defines this
* so let's ditch this and avoid the duplication
*/
export enum DialogType {
Alert = 'alert',
BeforeUnload = 'beforeunload',
Confirm = 'confirm',
Prompt = 'prompt'
}

class Dialog {
export class Dialog {
static Type = DialogType;

private _client: CDPSession;
Expand Down Expand Up @@ -69,5 +72,3 @@ class Dialog {
});
}
}

export = {Dialog};
7 changes: 4 additions & 3 deletions src/FrameManager.ts
Expand Up @@ -25,20 +25,21 @@ import {TimeoutSettings} from './TimeoutSettings';
import {CDPSession} from './Connection';
import {JSHandle, ElementHandle} from './JSHandle';
import {MouseButtonInput} from './Input';
import {Page} from './Page';

const UTILITY_WORLD_NAME = '__puppeteer_utility_world__';

export class FrameManager extends EventEmitter {
_client: CDPSession;
_page: Puppeteer.Page;
_page: Page;
_networkManager: NetworkManager;
_timeoutSettings: TimeoutSettings;
_frames = new Map<string, Frame>();
_contextIdToContext = new Map<number, ExecutionContext>();
_isolatedWorlds = new Set<string>();
_mainFrame: Frame;

constructor(client: CDPSession, page: Puppeteer.Page, ignoreHTTPSErrors: boolean, timeoutSettings: TimeoutSettings) {
constructor(client: CDPSession, page: Page, ignoreHTTPSErrors: boolean, timeoutSettings: TimeoutSettings) {
super();
this._client = client;
this._page = page;
Expand Down Expand Up @@ -155,7 +156,7 @@ export class FrameManager extends EventEmitter {
this._handleFrameTree(child);
}

page(): Puppeteer.Page {
page(): Page {
return this._page;
}

Expand Down
26 changes: 3 additions & 23 deletions src/JSHandle.ts
Expand Up @@ -16,6 +16,7 @@

import {helper, assert, debugError} from './helper';
import {ExecutionContext} from './ExecutionContext';
import {Page} from './Page';
import {CDPSession} from './Connection';
import {KeyInput} from './USKeyboardLayout';
import {FrameManager, Frame} from './FrameManager';
Expand Down Expand Up @@ -124,16 +125,9 @@ export class JSHandle {
}

export class ElementHandle extends JSHandle {
_page: Puppeteer.Page;
_page: Page;
_frameManager: FrameManager;
/**
* @param {!ExecutionContext} context
* @param {!CDPSession} client
* @param {!Protocol.Runtime.RemoteObject} remoteObject
* @param {!Puppeteer.Page} page
* @param {!FrameManager} frameManager
*/
constructor(context: ExecutionContext, client: CDPSession, remoteObject: Protocol.Runtime.RemoteObject, page: Puppeteer.Page, frameManager: FrameManager) {
constructor(context: ExecutionContext, client: CDPSession, remoteObject: Protocol.Runtime.RemoteObject, page: Page, frameManager: FrameManager) {
super(context, client, remoteObject);
this._client = client;
this._remoteObject = remoteObject;
Expand Down Expand Up @@ -231,12 +225,6 @@ export class ElementHandle extends JSHandle {
];
}

/**
* @param {!Array<{x: number, y: number}>} quad
* @param {number} width
* @param {number} height
* @return {!Array<{x: number, y: number}>}
*/
_intersectQuadWithViewport(quad: Array<{x: number; y: number}>, width: number, height: number): Array<{x: number; y: number}> {
return quad.map(point => ({
x: Math.min(Math.max(point.x, 0), width),
Expand All @@ -256,10 +244,6 @@ export class ElementHandle extends JSHandle {
await this._page.mouse.click(x, y, options);
}

/**
* @param {!Array<string>} values
* @return {!Promise<!Array<string>>}
*/
async select(...values: string[]): Promise<string[]> {
for (const value of values)
assert(helper.isString(value), 'Values must be strings. Found value "' + value + '" of type "' + (typeof value) + '"');
Expand Down Expand Up @@ -439,10 +423,6 @@ export class ElementHandle extends JSHandle {
return null;
}

/**
* @param {string} selector
* @return {!Promise<!Array<!ElementHandle>>}
*/
async $$(selector: string): Promise<ElementHandle[]> {
const defaultHandler = (element: Element, selector: string) => element.querySelectorAll(selector);
const {updatedSelector, queryHandler} = getQueryHandlerAndSelector(selector, defaultHandler);
Expand Down
2 changes: 1 addition & 1 deletion src/NetworkManager.ts
Expand Up @@ -19,7 +19,7 @@ import {Events} from './Events';
import {CDPSession} from './Connection';
import {FrameManager, Frame} from './FrameManager';

interface Credentials {
export interface Credentials {
username: string;
password: string;
}
Expand Down

0 comments on commit de4f08d

Please sign in to comment.