chore: extract ConsoleMessage and FileChooser into its own module (#5856)

* chore: extract `ConsoleMessage` and `FileChooser` into its own module
This commit is contained in:
Christian Bromann 2020-05-13 12:30:29 +02:00 committed by GitHub
parent 0aba6dfddf
commit 69c38fc2d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 116 additions and 82 deletions

58
src/ConsoleMessage.ts Normal file
View File

@ -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
src/FileChooser.ts Normal file
View File

@ -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;
}
}

View File

@ -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);
@ -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);
}
@ -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;
}
}

View File

@ -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,