Skip to content

Commit

Permalink
chore: extract ConsoleMessage and FileChooser into its own module (
Browse files Browse the repository at this point in the history
…#5856)

* chore: extract `ConsoleMessage` and `FileChooser` into its own module
  • Loading branch information
christian-bromann committed May 13, 2020
1 parent 0aba6df commit 69c38fc
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 82 deletions.
58 changes: 58 additions & 0 deletions src/ConsoleMessage.ts
@@ -0,0 +1,58 @@
/**
* Copyright 2020 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 {
private _type: string;
private _text: string;
private _args: JSHandle[];
private _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;
}
}
53 changes: 53 additions & 0 deletions src/FileChooser.ts
@@ -0,0 +1,53 @@
/**
* Copyright 2020 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 { ElementHandle } from './JSHandle';
import { assert } from './helper';

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

constructor(
element: ElementHandle,
event: Protocol.Page.fileChooserOpenedPayload
) {
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;
}
}
83 changes: 3 additions & 80 deletions src/Page.ts
Expand Up @@ -38,6 +38,8 @@ import {
} from './NetworkManager';
import { Accessibility } from './Accessibility';
import { TimeoutSettings } from './TimeoutSettings';
import { FileChooser } from './FileChooser';
import { ConsoleMessage } from './ConsoleMessage';
import { PuppeteerLifeCycleEvent } from './LifecycleWatcher';

const writeFileAsync = helper.promisify(fs.writeFile);
Expand Down Expand Up @@ -292,7 +294,7 @@ export class Page extends EventEmitter {
const element = await context._adoptBackendNodeId(event.backendNodeId);
const interceptors = Array.from(this._fileChooserInterceptors);
this._fileChooserInterceptors.clear();
const fileChooser = new FileChooser(this._client, element, event);
const fileChooser = new FileChooser(element, event);
for (const interceptor of interceptors) interceptor.call(null, fileChooser);
}

Expand Down Expand Up @@ -1319,82 +1321,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;
}
}
4 changes: 2 additions & 2 deletions src/api.ts
Expand Up @@ -24,12 +24,12 @@ module.exports = {
BrowserContext: require('./Browser').BrowserContext,
BrowserFetcher: require('./BrowserFetcher').BrowserFetcher,
CDPSession: require('./Connection').CDPSession,
ConsoleMessage: require('./Page').ConsoleMessage,
ConsoleMessage: require('./ConsoleMessage').ConsoleMessage,
Coverage: require('./Coverage').Coverage,
Dialog: require('./Dialog').Dialog,
ElementHandle: require('./JSHandle').ElementHandle,
ExecutionContext: require('./ExecutionContext').ExecutionContext,
FileChooser: require('./Page').FileChooser,
FileChooser: require('./FileChooser').FileChooser,
Frame: require('./FrameManager').Frame,
JSHandle: require('./JSHandle').JSHandle,
Keyboard: require('./Input').Keyboard,
Expand Down

0 comments on commit 69c38fc

Please sign in to comment.