Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate src/Page.js to TypeScript #5809

Merged
merged 4 commits into from May 5, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
mathiasbynens marked this conversation as resolved.
Show resolved Hide resolved
* 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