2020-06-26 01:05:36 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-07-14 01:03:24 +02:00
|
|
|
import { Frame, kAddLifecycleEvent, kRemoveLifecycleEvent } from '../../frames';
|
2020-06-26 01:05:36 +02:00
|
|
|
import * as types from '../../types';
|
2020-07-06 19:04:09 +02:00
|
|
|
import { ElementHandleChannel, FrameChannel, FrameInitializer, JSHandleChannel, ResponseChannel, PageAttribution } from '../channels';
|
2020-07-01 07:21:17 +02:00
|
|
|
import { Dispatcher, DispatcherScope, lookupNullableDispatcher, existingDispatcher } from './dispatcher';
|
2020-07-11 00:39:11 +02:00
|
|
|
import { convertSelectOptionValues, ElementHandleDispatcher, createHandle, convertInputFiles } from './elementHandlerDispatcher';
|
2020-06-27 20:10:07 +02:00
|
|
|
import { parseArgument, serializeResult } from './jsHandleDispatcher';
|
2020-06-26 01:05:36 +02:00
|
|
|
import { ResponseDispatcher } from './networkDispatchers';
|
|
|
|
|
|
2020-06-27 02:24:21 +02:00
|
|
|
export class FrameDispatcher extends Dispatcher<Frame, FrameInitializer> implements FrameChannel {
|
2020-06-26 01:05:36 +02:00
|
|
|
private _frame: Frame;
|
|
|
|
|
|
|
|
|
|
static from(scope: DispatcherScope, frame: Frame): FrameDispatcher {
|
2020-07-01 06:30:39 +02:00
|
|
|
const result = existingDispatcher<FrameDispatcher>(frame);
|
|
|
|
|
return result || new FrameDispatcher(scope, frame);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-01 06:30:39 +02:00
|
|
|
private constructor(scope: DispatcherScope, frame: Frame) {
|
2020-06-26 21:28:27 +02:00
|
|
|
super(scope, frame, 'frame', {
|
2020-06-26 01:05:36 +02:00
|
|
|
url: frame.url(),
|
|
|
|
|
name: frame.name(),
|
2020-07-14 01:03:24 +02:00
|
|
|
parentFrame: lookupNullableDispatcher<FrameDispatcher>(frame.parentFrame()),
|
|
|
|
|
loadStates: Array.from(frame._subtreeLifecycleEvents),
|
2020-06-26 01:05:36 +02:00
|
|
|
});
|
2020-06-26 21:28:27 +02:00
|
|
|
this._frame = frame;
|
2020-07-14 01:03:24 +02:00
|
|
|
frame._eventEmitter.on(kAddLifecycleEvent, (event: types.LifecycleEvent) => {
|
|
|
|
|
this._dispatchEvent('loadstate', { add: event });
|
|
|
|
|
});
|
|
|
|
|
frame._eventEmitter.on(kRemoveLifecycleEvent, (event: types.LifecycleEvent) => {
|
|
|
|
|
this._dispatchEvent('loadstate', { remove: event });
|
|
|
|
|
});
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async goto(params: { url: string } & types.GotoOptions & PageAttribution): Promise<ResponseChannel | null> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
return lookupNullableDispatcher<ResponseDispatcher>(await target.goto(params.url, params));
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async waitForNavigation(params: types.WaitForNavigationOptions & PageAttribution): Promise<ResponseChannel | null> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
return lookupNullableDispatcher<ResponseDispatcher>(await target.waitForNavigation(params));
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async frameElement(): Promise<ElementHandleChannel> {
|
2020-07-01 06:30:39 +02:00
|
|
|
return new ElementHandleDispatcher(this._scope, await this._frame.frameElement());
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async evaluateExpression(params: { expression: string, isFunction: boolean, arg: any } & PageAttribution): Promise<any> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
|
|
|
|
return serializeResult(await target._evaluateExpression(params.expression, params.isFunction, parseArgument(params.arg)));
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async evaluateExpressionHandle(params: { expression: string, isFunction: boolean, arg: any } & PageAttribution): Promise<JSHandleChannel> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-01 06:30:39 +02:00
|
|
|
return createHandle(this._scope, await target._evaluateExpressionHandle(params.expression, params.isFunction, parseArgument(params.arg)));
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async waitForSelector(params: { selector: string } & types.WaitForElementOptions & PageAttribution): Promise<ElementHandleChannel | null> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
return ElementHandleDispatcher.createNullable(this._scope, await target.waitForSelector(params.selector, params));
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async dispatchEvent(params: { selector: string, type: string, eventInit: any } & types.TimeoutOptions & PageAttribution): Promise<void> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
return target.dispatchEvent(params.selector, params.type, parseArgument(params.eventInit), params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async evalOnSelector(params: { selector: string, expression: string, isFunction: boolean, arg: any } & PageAttribution): Promise<any> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
|
|
|
|
return serializeResult(await target._$evalExpression(params.selector, params.expression, params.isFunction, parseArgument(params.arg)));
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async evalOnSelectorAll(params: { selector: string, expression: string, isFunction: boolean, arg: any } & PageAttribution): Promise<any> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
|
|
|
|
return serializeResult(await target._$$evalExpression(params.selector, params.expression, params.isFunction, parseArgument(params.arg)));
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async querySelector(params: { selector: string } & PageAttribution): Promise<ElementHandleChannel | null> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-01 06:30:39 +02:00
|
|
|
return ElementHandleDispatcher.createNullable(this._scope, await target.$(params.selector));
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async querySelectorAll(params: { selector: string } & PageAttribution): Promise<ElementHandleChannel[]> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
|
|
|
|
const elements = await target.$$(params.selector);
|
2020-07-01 06:30:39 +02:00
|
|
|
return elements.map(e => new ElementHandleDispatcher(this._scope, e));
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async content(): Promise<string> {
|
|
|
|
|
return await this._frame.content();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async setContent(params: { html: string } & types.NavigateOptions & PageAttribution): Promise<void> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
await target.setContent(params.html, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async addScriptTag(params: { url?: string | undefined, path?: string | undefined, content?: string | undefined, type?: string | undefined } & PageAttribution): Promise<ElementHandleChannel> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
return new ElementHandleDispatcher(this._scope, await target.addScriptTag(params));
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async addStyleTag(params: { url?: string | undefined, path?: string | undefined, content?: string | undefined } & PageAttribution): Promise<ElementHandleChannel> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
return new ElementHandleDispatcher(this._scope, await target.addStyleTag(params));
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async click(params: { selector: string } & types.PointerActionOptions & types.MouseClickOptions & types.TimeoutOptions & { force?: boolean } & { noWaitAfter?: boolean } & PageAttribution): Promise<void> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
await target.click(params.selector, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async dblclick(params: { selector: string } & types.PointerActionOptions & types.MouseMultiClickOptions & types.TimeoutOptions & { force?: boolean } & PageAttribution): Promise<void> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
await target.dblclick(params.selector, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async fill(params: { selector: string, value: string } & types.NavigatingActionWaitOptions & PageAttribution): Promise<void> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
await target.fill(params.selector, params.value, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async focus(params: { selector: string } & types.TimeoutOptions & PageAttribution): Promise<void> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
await target.focus(params.selector, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async textContent(params: { selector: string } & types.TimeoutOptions & PageAttribution): Promise<string | null> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
return await target.textContent(params.selector, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async innerText(params: { selector: string } & types.TimeoutOptions & PageAttribution): Promise<string> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
return await target.innerText(params.selector, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async innerHTML(params: { selector: string } & types.TimeoutOptions & PageAttribution): Promise<string> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
return await target.innerHTML(params.selector, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async getAttribute(params: { selector: string, name: string } & types.TimeoutOptions & PageAttribution): Promise<string | null> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
return await target.getAttribute(params.selector, params.name, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async hover(params: { selector: string } & types.PointerActionOptions & types.TimeoutOptions & { force?: boolean } & PageAttribution): Promise<void> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
await target.hover(params.selector, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-11 00:39:11 +02:00
|
|
|
async selectOption(params: { selector: string, elements?: ElementHandleChannel[], options?: types.SelectOption[] } & types.NavigatingActionWaitOptions & PageAttribution): Promise<string[]> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-11 00:39:11 +02:00
|
|
|
return target.selectOption(params.selector, convertSelectOptionValues(params.elements, params.options), params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async setInputFiles(params: { selector: string, files: { name: string, mimeType: string, buffer: string }[] } & types.NavigatingActionWaitOptions & PageAttribution): Promise<void> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-11 00:39:11 +02:00
|
|
|
await target.setInputFiles(params.selector, convertInputFiles(params.files), params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async type(params: { selector: string, text: string } & { delay?: number | undefined } & types.TimeoutOptions & { noWaitAfter?: boolean } & PageAttribution): Promise<void> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
await target.type(params.selector, params.text, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async press(params: { selector: string, key: string } & { delay?: number | undefined } & types.TimeoutOptions & { noWaitAfter?: boolean } & PageAttribution): Promise<void> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
await target.press(params.selector, params.key, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async check(params: { selector: string } & types.TimeoutOptions & { force?: boolean } & { noWaitAfter?: boolean } & PageAttribution): Promise<void> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
await target.check(params.selector, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async uncheck(params: { selector: string } & types.TimeoutOptions & { force?: boolean } & { noWaitAfter?: boolean } & PageAttribution): Promise<void> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
await target.uncheck(params.selector, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:04:09 +02:00
|
|
|
async waitForFunction(params: { expression: string, isFunction: boolean, arg: any } & types.WaitForFunctionOptions & PageAttribution): Promise<JSHandleChannel> {
|
2020-06-30 03:58:09 +02:00
|
|
|
const target = params.isPage ? this._frame._page : this._frame;
|
2020-07-06 19:04:09 +02:00
|
|
|
return createHandle(this._scope, await target._waitForFunctionExpression(params.expression, params.isFunction, parseArgument(params.arg), params));
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async title(): Promise<string> {
|
|
|
|
|
return await this._frame.title();
|
|
|
|
|
}
|
|
|
|
|
}
|