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-08-24 15:51:51 +02:00
|
|
|
import { Frame, NavigationEvent } from '../../server/frames';
|
2020-08-22 03:46:11 +02:00
|
|
|
import * as channels from '../../protocol/channels';
|
2020-07-01 07:21:17 +02:00
|
|
|
import { Dispatcher, DispatcherScope, lookupNullableDispatcher, existingDispatcher } from './dispatcher';
|
2020-08-19 02:32:11 +02:00
|
|
|
import { ElementHandleDispatcher, createHandle } from './elementHandlerDispatcher';
|
2020-06-27 20:10:07 +02:00
|
|
|
import { parseArgument, serializeResult } from './jsHandleDispatcher';
|
2020-07-16 03:48:19 +02:00
|
|
|
import { ResponseDispatcher, RequestDispatcher } from './networkDispatchers';
|
2020-06-26 01:05:36 +02:00
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
export class FrameDispatcher extends Dispatcher<Frame, channels.FrameInitializer> implements channels.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-07-22 19:37:21 +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-08-23 00:46:42 +02:00
|
|
|
frame.on(Frame.Events.AddLifecycle, lifecycleEvent => {
|
|
|
|
|
this._dispatchEvent('loadstate', { add: lifecycleEvent });
|
2020-07-14 01:03:24 +02:00
|
|
|
});
|
2020-08-23 00:46:42 +02:00
|
|
|
frame.on(Frame.Events.RemoveLifecycle, lifecycleEvent => {
|
|
|
|
|
this._dispatchEvent('loadstate', { remove: lifecycleEvent });
|
2020-07-14 01:03:24 +02:00
|
|
|
});
|
2020-08-22 01:26:33 +02:00
|
|
|
frame.on(Frame.Events.Navigation, (event: NavigationEvent) => {
|
2020-07-16 03:48:19 +02:00
|
|
|
const params = { url: event.url, name: event.name, error: event.error ? event.error.message : undefined };
|
|
|
|
|
if (event.newDocument)
|
|
|
|
|
(params as any).newDocument = { request: RequestDispatcher.fromNullable(this._scope, event.newDocument.request || null) };
|
|
|
|
|
this._dispatchEvent('navigated', params);
|
|
|
|
|
});
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async goto(params: channels.FrameGotoParams): Promise<channels.FrameGotoResult> {
|
2020-07-15 23:04:39 +02:00
|
|
|
return { response: lookupNullableDispatcher<ResponseDispatcher>(await this._frame.goto(params.url, params)) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async frameElement(): Promise<channels.FrameFrameElementResult> {
|
2020-07-15 03:26:50 +02:00
|
|
|
return { element: new ElementHandleDispatcher(this._scope, await this._frame.frameElement()) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async evaluateExpression(params: channels.FrameEvaluateExpressionParams): Promise<channels.FrameEvaluateExpressionResult> {
|
2020-07-15 23:04:39 +02:00
|
|
|
return { value: serializeResult(await this._frame._evaluateExpression(params.expression, params.isFunction, parseArgument(params.arg))) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async evaluateExpressionHandle(params: channels.FrameEvaluateExpressionHandleParams): Promise<channels.FrameEvaluateExpressionHandleResult> {
|
2020-07-15 23:04:39 +02:00
|
|
|
return { handle: createHandle(this._scope, await this._frame._evaluateExpressionHandle(params.expression, params.isFunction, parseArgument(params.arg))) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async waitForSelector(params: channels.FrameWaitForSelectorParams): Promise<channels.FrameWaitForSelectorResult> {
|
2020-07-15 23:04:39 +02:00
|
|
|
return { element: ElementHandleDispatcher.createNullable(this._scope, await this._frame.waitForSelector(params.selector, params)) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async dispatchEvent(params: channels.FrameDispatchEventParams): Promise<void> {
|
2020-07-15 23:04:39 +02:00
|
|
|
return this._frame.dispatchEvent(params.selector, params.type, parseArgument(params.eventInit), params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async evalOnSelector(params: channels.FrameEvalOnSelectorParams): Promise<channels.FrameEvalOnSelectorResult> {
|
2020-07-15 23:04:39 +02:00
|
|
|
return { value: serializeResult(await this._frame._$evalExpression(params.selector, params.expression, params.isFunction, parseArgument(params.arg))) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async evalOnSelectorAll(params: channels.FrameEvalOnSelectorAllParams): Promise<channels.FrameEvalOnSelectorAllResult> {
|
2020-07-15 23:04:39 +02:00
|
|
|
return { value: serializeResult(await this._frame._$$evalExpression(params.selector, params.expression, params.isFunction, parseArgument(params.arg))) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async querySelector(params: channels.FrameQuerySelectorParams): Promise<channels.FrameQuerySelectorResult> {
|
2020-07-15 23:04:39 +02:00
|
|
|
return { element: ElementHandleDispatcher.createNullable(this._scope, await this._frame.$(params.selector)) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async querySelectorAll(params: channels.FrameQuerySelectorAllParams): Promise<channels.FrameQuerySelectorAllResult> {
|
2020-07-15 23:04:39 +02:00
|
|
|
const elements = await this._frame.$$(params.selector);
|
2020-07-15 03:26:50 +02:00
|
|
|
return { elements: elements.map(e => new ElementHandleDispatcher(this._scope, e)) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async content(): Promise<channels.FrameContentResult> {
|
2020-07-15 03:26:50 +02:00
|
|
|
return { value: await this._frame.content() };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async setContent(params: channels.FrameSetContentParams): Promise<void> {
|
2020-07-15 23:04:39 +02:00
|
|
|
await this._frame.setContent(params.html, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async addScriptTag(params: channels.FrameAddScriptTagParams): Promise<channels.FrameAddScriptTagResult> {
|
2020-07-15 23:04:39 +02:00
|
|
|
return { element: new ElementHandleDispatcher(this._scope, await this._frame.addScriptTag(params)) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async addStyleTag(params: channels.FrameAddStyleTagParams): Promise<channels.FrameAddStyleTagResult> {
|
2020-07-15 23:04:39 +02:00
|
|
|
return { element: new ElementHandleDispatcher(this._scope, await this._frame.addStyleTag(params)) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async click(params: channels.FrameClickParams): Promise<void> {
|
2020-07-15 23:04:39 +02:00
|
|
|
await this._frame.click(params.selector, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async dblclick(params: channels.FrameDblclickParams): Promise<void> {
|
2020-07-15 23:04:39 +02:00
|
|
|
await this._frame.dblclick(params.selector, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async fill(params: channels.FrameFillParams): Promise<void> {
|
2020-07-15 23:04:39 +02:00
|
|
|
await this._frame.fill(params.selector, params.value, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async focus(params: channels.FrameFocusParams): Promise<void> {
|
2020-07-15 23:04:39 +02:00
|
|
|
await this._frame.focus(params.selector, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async textContent(params: channels.FrameTextContentParams): Promise<channels.FrameTextContentResult> {
|
2020-07-21 02:38:06 +02:00
|
|
|
const value = await this._frame.textContent(params.selector, params);
|
|
|
|
|
return { value: value === null ? undefined : value };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async innerText(params: channels.FrameInnerTextParams): Promise<channels.FrameInnerTextResult> {
|
2020-07-15 23:04:39 +02:00
|
|
|
return { value: await this._frame.innerText(params.selector, params) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async innerHTML(params: channels.FrameInnerHTMLParams): Promise<channels.FrameInnerHTMLResult> {
|
2020-07-15 23:04:39 +02:00
|
|
|
return { value: await this._frame.innerHTML(params.selector, params) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async getAttribute(params: channels.FrameGetAttributeParams): Promise<channels.FrameGetAttributeResult> {
|
2020-07-21 02:38:06 +02:00
|
|
|
const value = await this._frame.getAttribute(params.selector, params.name, params);
|
|
|
|
|
return { value: value === null ? undefined : value };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async hover(params: channels.FrameHoverParams): Promise<void> {
|
2020-07-15 23:04:39 +02:00
|
|
|
await this._frame.hover(params.selector, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async selectOption(params: channels.FrameSelectOptionParams): Promise<channels.FrameSelectOptionResult> {
|
2020-08-19 01:44:17 +02:00
|
|
|
const elements = (params.elements || []).map(e => (e as ElementHandleDispatcher)._elementHandle);
|
|
|
|
|
return { values: await this._frame.selectOption(params.selector, elements, params.options || [], params) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async setInputFiles(params: channels.FrameSetInputFilesParams): Promise<void> {
|
2020-08-19 02:32:11 +02:00
|
|
|
await this._frame.setInputFiles(params.selector, params.files, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async type(params: channels.FrameTypeParams): Promise<void> {
|
2020-07-15 23:04:39 +02:00
|
|
|
await this._frame.type(params.selector, params.text, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async press(params: channels.FramePressParams): Promise<void> {
|
2020-07-15 23:04:39 +02:00
|
|
|
await this._frame.press(params.selector, params.key, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async check(params: channels.FrameCheckParams): Promise<void> {
|
2020-07-15 23:04:39 +02:00
|
|
|
await this._frame.check(params.selector, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async uncheck(params: channels.FrameUncheckParams): Promise<void> {
|
2020-07-15 23:04:39 +02:00
|
|
|
await this._frame.uncheck(params.selector, params);
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async waitForFunction(params: channels.FrameWaitForFunctionParams): Promise<channels.FrameWaitForFunctionResult> {
|
2020-08-22 16:07:13 +02:00
|
|
|
return { handle: createHandle(this._scope, await this._frame._waitForFunctionExpression(params.expression, params.isFunction, parseArgument(params.arg), params)) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async title(): Promise<channels.FrameTitleResult> {
|
2020-07-15 03:26:50 +02:00
|
|
|
return { value: await this._frame.title() };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
}
|