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: extract ConsoleMessage and FileChooser into its own module #5856

Merged
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
58 changes: 58 additions & 0 deletions src/ConsoleMessage.ts
@@ -0,0 +1,58 @@
/**
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { JSHandle } from './JSHandle';

interface ConsoleMessageLocation {
url?: string;
lineNumber?: number;
columnNumber?: number;
}

export class ConsoleMessage {
_type: string;
_text: string;
_args: JSHandle[];
_location: ConsoleMessageLocation;

constructor(
type: string,
text: string,
args: JSHandle[],
location: ConsoleMessageLocation = {}
) {
this._type = type;
this._text = text;
this._args = args;
this._location = location;
}

type(): string {
return this._type;
}

text(): string {
return this._text;
}

args(): JSHandle[] {
return this._args;
}

location(): ConsoleMessageLocation {
return this._location;
}
}
57 changes: 57 additions & 0 deletions src/FileChooser.ts
@@ -0,0 +1,57 @@
/**
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { CDPSession } from './Connection';
import { ElementHandle } from './JSHandle';
import { assert } from './helper';

export class FileChooser {
_client: CDPSession;
_element: ElementHandle;
_multiple: boolean;
_handled = false;

constructor(
client: CDPSession,
element: ElementHandle,
event: Protocol.Page.fileChooserOpenedPayload
) {
this._client = client;
this._element = element;
this._multiple = event.mode !== 'selectSingle';
}

isMultiple(): boolean {
return this._multiple;
}

async accept(filePaths: string[]): Promise<void> {
assert(
!this._handled,
'Cannot accept FileChooser which is already handled!'
);
this._handled = true;
await this._element.uploadFile(...filePaths);
}

async cancel(): Promise<void> {
assert(
!this._handled,
'Cannot cancel FileChooser which is already handled!'
);
this._handled = true;
}
}
12 changes: 1 addition & 11 deletions src/LifecycleWatcher.ts
Expand Up @@ -19,17 +19,7 @@ import { Events } from './Events';
import { TimeoutError } from './Errors';
import { FrameManager, Frame } from './FrameManager';
import { Request, Response } from './NetworkManager';

export type PuppeteerLifeCycleEvent =
| 'load'
| 'domcontentloaded'
| 'networkidle0'
| 'networkidle2';
type ProtocolLifeCycleEvent =
| 'load'
| 'DOMContentLoaded'
| 'networkIdle'
| 'networkAlmostIdle';
import { PuppeteerLifeCycleEvent, ProtocolLifeCycleEvent } from './types'

const puppeteerToProtocolLifecycle = new Map<
PuppeteerLifeCycleEvent,
Expand Down
157 changes: 6 additions & 151 deletions src/Page.ts
Expand Up @@ -38,81 +38,15 @@ import {
} from './NetworkManager';
import { Accessibility } from './Accessibility';
import { TimeoutSettings } from './TimeoutSettings';
import { PuppeteerLifeCycleEvent } from './LifecycleWatcher';
import { FileChooser } from './FileChooser';
import { ConsoleMessage } from './ConsoleMessage'
import {
ScreenshotOptions, PaperFormat, Metrics, WaitForOptions, MediaFeature,
ScreenshotClip, PDFOptions
} from './types'

const writeFileAsync = helper.promisify(fs.writeFile);

interface Metrics {
Timestamp?: number;
Documents?: number;
Frames?: number;
JSEventListeners?: number;
Nodes?: number;
LayoutCount?: number;
RecalcStyleCount?: number;
LayoutDuration?: number;
RecalcStyleDuration?: number;
ScriptDuration?: number;
TaskDuration?: number;
JSHeapUsedSize?: number;
JSHeapTotalSize?: number;
}

interface WaitForOptions {
timeout?: number;
waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[];
}

interface MediaFeature {
name: string;
value: string;
}

interface ScreenshotClip {
x: number;
y: number;
width: number;
height: number;
}

interface ScreenshotOptions {
type?: 'png' | 'jpeg';
path?: string;
fullPage?: boolean;
clip?: ScreenshotClip;
quality?: number;
omitBackground?: boolean;
encoding?: string;
}

interface PDFMargin {
top?: string | number;
bottom?: string | number;
left?: string | number;
right?: string | number;
}

interface PDFOptions {
scale?: number;
displayHeaderFooter?: boolean;
headerTemplate?: string;
footerTemplate?: string;
printBackground?: boolean;
landscape?: boolean;
pageRanges?: string;
format?: string;
width?: string | number;
height?: string | number;
preferCSSPageSize?: boolean;
margin?: PDFMargin;
path?: string;
}

interface PaperFormat {
width: number;
height: number;
}

const paperFormats: Record<string, PaperFormat> = {
letter: { width: 8.5, height: 11 },
legal: { width: 8.5, height: 14 },
Expand Down Expand Up @@ -1319,82 +1253,3 @@ function convertPrintParameterToInches(
}
return pixels / 96;
}

interface ConsoleMessageLocation {
url?: string;
lineNumber?: number;
columnNumber?: number;
}

export class ConsoleMessage {
_type: string;
_text: string;
_args: JSHandle[];
_location: ConsoleMessageLocation;

constructor(
type: string,
text: string,
args: JSHandle[],
location: ConsoleMessageLocation = {}
) {
this._type = type;
this._text = text;
this._args = args;
this._location = location;
}

type(): string {
return this._type;
}

text(): string {
return this._text;
}

args(): JSHandle[] {
return this._args;
}

location(): ConsoleMessageLocation {
return this._location;
}
}

export class FileChooser {
_client: CDPSession;
_element: ElementHandle;
_multiple: boolean;
_handled = false;

constructor(
client: CDPSession,
element: ElementHandle,
event: Protocol.Page.fileChooserOpenedPayload
) {
this._client = client;
this._element = element;
this._multiple = event.mode !== 'selectSingle';
}

isMultiple(): boolean {
return this._multiple;
}

async accept(filePaths: string[]): Promise<void> {
assert(
!this._handled,
'Cannot accept FileChooser which is already handled!'
);
this._handled = true;
await this._element.uploadFile(...filePaths);
}

async cancel(): Promise<void> {
assert(
!this._handled,
'Cannot cancel FileChooser which is already handled!'
);
this._handled = true;
}
}