diff --git a/src/ConsoleMessage.ts b/src/ConsoleMessage.ts new file mode 100644 index 0000000000000..1e19b18c45820 --- /dev/null +++ b/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; + } +} diff --git a/src/FileChooser.ts b/src/FileChooser.ts new file mode 100644 index 0000000000000..547cd4d58e1a8 --- /dev/null +++ b/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 { + assert( + !this._handled, + 'Cannot accept FileChooser which is already handled!' + ); + this._handled = true; + await this._element.uploadFile(...filePaths); + } + + async cancel(): Promise { + assert( + !this._handled, + 'Cannot cancel FileChooser which is already handled!' + ); + this._handled = true; + } +} diff --git a/src/Page.ts b/src/Page.ts index c7ed0e70bd19a..ea608a2c51d9c 100644 --- a/src/Page.ts +++ b/src/Page.ts @@ -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 { - assert( - !this._handled, - 'Cannot accept FileChooser which is already handled!' - ); - this._handled = true; - await this._element.uploadFile(...filePaths); - } - - async cancel(): Promise { - assert( - !this._handled, - 'Cannot cancel FileChooser which is already handled!' - ); - this._handled = true; - } -} diff --git a/src/api.ts b/src/api.ts index 3eafe8c3076dd..637f2f67c58fe 100644 --- a/src/api.ts +++ b/src/api.ts @@ -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,