2020-06-25 16:05:36 -07: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 14:48:03 -07:00
|
|
|
import { ElementHandle } from '../server/dom';
|
|
|
|
import * as js from '../server/javascript';
|
|
|
|
import * as channels from '../protocol/channels';
|
2020-06-30 22:21:17 -07:00
|
|
|
import { DispatcherScope, lookupNullableDispatcher } from './dispatcher';
|
2020-06-27 11:10:07 -07:00
|
|
|
import { JSHandleDispatcher, serializeResult, parseArgument } from './jsHandleDispatcher';
|
|
|
|
import { FrameDispatcher } from './frameDispatcher';
|
2020-09-17 09:32:54 -07:00
|
|
|
import { runAction } from '../server/browserContext';
|
2020-06-25 16:05:36 -07:00
|
|
|
|
2020-06-30 21:30:39 -07:00
|
|
|
export function createHandle(scope: DispatcherScope, handle: js.JSHandle): JSHandleDispatcher {
|
2020-06-30 10:55:11 -07:00
|
|
|
return handle.asElement() ? new ElementHandleDispatcher(scope, handle.asElement()!) : new JSHandleDispatcher(scope, handle);
|
|
|
|
}
|
|
|
|
|
2020-08-20 11:25:33 -07:00
|
|
|
export class ElementHandleDispatcher extends JSHandleDispatcher implements channels.ElementHandleChannel {
|
2020-06-26 11:51:47 -07:00
|
|
|
readonly _elementHandle: ElementHandle;
|
2020-06-25 16:05:36 -07:00
|
|
|
|
2020-07-20 17:38:06 -07:00
|
|
|
static createNullable(scope: DispatcherScope, handle: ElementHandle | null): ElementHandleDispatcher | undefined {
|
2020-06-25 16:05:36 -07:00
|
|
|
if (!handle)
|
2020-07-20 17:38:06 -07:00
|
|
|
return undefined;
|
2020-06-30 21:30:39 -07:00
|
|
|
return new ElementHandleDispatcher(scope, handle);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(scope: DispatcherScope, elementHandle: ElementHandle) {
|
2020-06-26 12:28:27 -07:00
|
|
|
super(scope, elementHandle);
|
2020-06-25 16:05:36 -07:00
|
|
|
this._elementHandle = elementHandle;
|
|
|
|
}
|
|
|
|
|
2020-08-20 11:25:33 -07:00
|
|
|
async ownerFrame(): Promise<channels.ElementHandleOwnerFrameResult> {
|
2020-07-14 18:26:50 -07:00
|
|
|
return { frame: lookupNullableDispatcher<FrameDispatcher>(await this._elementHandle.ownerFrame()) };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:25:33 -07:00
|
|
|
async contentFrame(): Promise<channels.ElementHandleContentFrameResult> {
|
2020-07-14 18:26:50 -07:00
|
|
|
return { frame: lookupNullableDispatcher<FrameDispatcher>(await this._elementHandle.contentFrame()) };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:25:33 -07:00
|
|
|
async getAttribute(params: channels.ElementHandleGetAttributeParams): Promise<channels.ElementHandleGetAttributeResult> {
|
2020-07-20 17:38:06 -07:00
|
|
|
const value = await this._elementHandle.getAttribute(params.name);
|
|
|
|
return { value: value === null ? undefined : value };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:25:33 -07:00
|
|
|
async textContent(): Promise<channels.ElementHandleTextContentResult> {
|
2020-07-20 17:38:06 -07:00
|
|
|
const value = await this._elementHandle.textContent();
|
|
|
|
return { value: value === null ? undefined : value };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:25:33 -07:00
|
|
|
async innerText(): Promise<channels.ElementHandleInnerTextResult> {
|
2020-07-14 18:26:50 -07:00
|
|
|
return { value: await this._elementHandle.innerText() };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:25:33 -07:00
|
|
|
async innerHTML(): Promise<channels.ElementHandleInnerHTMLResult> {
|
2020-07-14 18:26:50 -07:00
|
|
|
return { value: await this._elementHandle.innerHTML() };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:25:33 -07:00
|
|
|
async dispatchEvent(params: channels.ElementHandleDispatchEventParams): Promise<void> {
|
2020-07-17 09:53:13 -07:00
|
|
|
await this._elementHandle.dispatchEvent(params.type, parseArgument(params.eventInit));
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:25:33 -07:00
|
|
|
async scrollIntoViewIfNeeded(params: channels.ElementHandleScrollIntoViewIfNeededParams): Promise<void> {
|
2020-07-06 10:04:09 -07:00
|
|
|
await this._elementHandle.scrollIntoViewIfNeeded(params);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-09-11 13:28:24 -07:00
|
|
|
async hover(params: channels.ElementHandleHoverParams, metadata?: channels.Metadata): Promise<void> {
|
2020-09-17 09:32:54 -07:00
|
|
|
return runAction(async controller => {
|
|
|
|
return await this._elementHandle.hover(controller, params);
|
|
|
|
}, { ...metadata, type: 'hover', target: this._elementHandle, page: this._elementHandle._page });
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-09-10 21:42:09 -07:00
|
|
|
async click(params: channels.ElementHandleClickParams, metadata?: channels.Metadata): Promise<void> {
|
2020-09-17 09:32:54 -07:00
|
|
|
return runAction(async controller => {
|
|
|
|
return await this._elementHandle.click(controller, params);
|
|
|
|
}, { ...metadata, type: 'click', target: this._elementHandle, page: this._elementHandle._page });
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-09-11 13:28:24 -07:00
|
|
|
async dblclick(params: channels.ElementHandleDblclickParams, metadata?: channels.Metadata): Promise<void> {
|
2020-09-17 09:32:54 -07:00
|
|
|
return runAction(async controller => {
|
|
|
|
return await this._elementHandle.dblclick(controller, params);
|
|
|
|
}, { ...metadata, type: 'dblclick', target: this._elementHandle, page: this._elementHandle._page });
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-10-19 10:07:33 -07:00
|
|
|
async tap(params: channels.ElementHandleTapParams, metadata?: channels.Metadata): Promise<void> {
|
|
|
|
return runAction(async controller => {
|
|
|
|
return await this._elementHandle.tap(controller, params);
|
|
|
|
}, { ...metadata, type: 'tap', target: this._elementHandle, page: this._elementHandle._page });
|
|
|
|
}
|
|
|
|
|
2020-09-11 13:28:24 -07:00
|
|
|
async selectOption(params: channels.ElementHandleSelectOptionParams, metadata?: channels.Metadata): Promise<channels.ElementHandleSelectOptionResult> {
|
2020-09-17 09:32:54 -07:00
|
|
|
return runAction(async controller => {
|
2020-09-11 13:28:24 -07:00
|
|
|
const elements = (params.elements || []).map(e => (e as ElementHandleDispatcher)._elementHandle);
|
2020-09-17 09:32:54 -07:00
|
|
|
return { values: await this._elementHandle.selectOption(controller, elements, params.options || [], params) };
|
|
|
|
}, { ...metadata, type: 'selectOption', target: this._elementHandle, page: this._elementHandle._page });
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-09-10 21:42:09 -07:00
|
|
|
async fill(params: channels.ElementHandleFillParams, metadata?: channels.Metadata): Promise<void> {
|
2020-09-17 09:32:54 -07:00
|
|
|
return runAction(async controller => {
|
|
|
|
return await this._elementHandle.fill(controller, params.value, params);
|
|
|
|
}, { ...metadata, type: 'fill', value: params.value, target: this._elementHandle, page: this._elementHandle._page });
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:25:33 -07:00
|
|
|
async selectText(params: channels.ElementHandleSelectTextParams): Promise<void> {
|
2020-07-06 10:04:09 -07:00
|
|
|
await this._elementHandle.selectText(params);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-09-11 13:28:24 -07:00
|
|
|
async setInputFiles(params: channels.ElementHandleSetInputFilesParams, metadata?: channels.Metadata): Promise<void> {
|
2020-09-17 09:32:54 -07:00
|
|
|
return runAction(async controller => {
|
|
|
|
return await this._elementHandle.setInputFiles(controller, params.files, params);
|
|
|
|
}, { ...metadata, type: 'setInputFiles', target: this._elementHandle, page: this._elementHandle._page });
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:25:33 -07:00
|
|
|
async focus(): Promise<void> {
|
2020-06-25 16:05:36 -07:00
|
|
|
await this._elementHandle.focus();
|
|
|
|
}
|
|
|
|
|
2020-09-11 13:28:24 -07:00
|
|
|
async type(params: channels.ElementHandleTypeParams, metadata?: channels.Metadata): Promise<void> {
|
2020-09-17 09:32:54 -07:00
|
|
|
return runAction(async controller => {
|
|
|
|
return await this._elementHandle.type(controller, params.text, params);
|
|
|
|
}, { ...metadata, type: 'type', value: params.text, target: this._elementHandle, page: this._elementHandle._page });
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-09-11 13:28:24 -07:00
|
|
|
async press(params: channels.ElementHandlePressParams, metadata?: channels.Metadata): Promise<void> {
|
2020-09-17 09:32:54 -07:00
|
|
|
return runAction(async controller => {
|
|
|
|
return await this._elementHandle.press(controller, params.key, params);
|
|
|
|
}, { ...metadata, type: 'press', value: params.key, target: this._elementHandle, page: this._elementHandle._page });
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-09-11 13:28:24 -07:00
|
|
|
async check(params: channels.ElementHandleCheckParams, metadata?: channels.Metadata): Promise<void> {
|
2020-09-17 09:32:54 -07:00
|
|
|
return runAction(async controller => {
|
|
|
|
return await this._elementHandle.check(controller, params);
|
|
|
|
}, { ...metadata, type: 'check', target: this._elementHandle, page: this._elementHandle._page });
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-09-11 13:28:24 -07:00
|
|
|
async uncheck(params: channels.ElementHandleUncheckParams, metadata?: channels.Metadata): Promise<void> {
|
2020-09-17 09:32:54 -07:00
|
|
|
return runAction(async controller => {
|
|
|
|
return await this._elementHandle.uncheck(controller, params);
|
|
|
|
}, { ...metadata, type: 'uncheck', target: this._elementHandle, page: this._elementHandle._page });
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:25:33 -07:00
|
|
|
async boundingBox(): Promise<channels.ElementHandleBoundingBoxResult> {
|
2020-07-20 17:38:06 -07:00
|
|
|
const value = await this._elementHandle.boundingBox();
|
|
|
|
return { value: value || undefined };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:25:33 -07:00
|
|
|
async screenshot(params: channels.ElementHandleScreenshotParams): Promise<channels.ElementHandleScreenshotResult> {
|
2020-07-14 18:26:50 -07:00
|
|
|
return { binary: (await this._elementHandle.screenshot(params)).toString('base64') };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:25:33 -07:00
|
|
|
async querySelector(params: channels.ElementHandleQuerySelectorParams): Promise<channels.ElementHandleQuerySelectorResult> {
|
2020-06-30 21:30:39 -07:00
|
|
|
const handle = await this._elementHandle.$(params.selector);
|
2020-07-20 17:38:06 -07:00
|
|
|
return { element: handle ? new ElementHandleDispatcher(this._scope, handle) : undefined };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:25:33 -07:00
|
|
|
async querySelectorAll(params: channels.ElementHandleQuerySelectorAllParams): Promise<channels.ElementHandleQuerySelectorAllResult> {
|
2020-06-25 16:05:36 -07:00
|
|
|
const elements = await this._elementHandle.$$(params.selector);
|
2020-07-14 18:26:50 -07:00
|
|
|
return { elements: elements.map(e => new ElementHandleDispatcher(this._scope, e)) };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:25:33 -07:00
|
|
|
async evalOnSelector(params: channels.ElementHandleEvalOnSelectorParams): Promise<channels.ElementHandleEvalOnSelectorResult> {
|
2020-07-14 18:26:50 -07:00
|
|
|
return { value: serializeResult(await this._elementHandle._$evalExpression(params.selector, params.expression, params.isFunction, parseArgument(params.arg))) };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:25:33 -07:00
|
|
|
async evalOnSelectorAll(params: channels.ElementHandleEvalOnSelectorAllParams): Promise<channels.ElementHandleEvalOnSelectorAllResult> {
|
2020-07-14 18:26:50 -07:00
|
|
|
return { value: serializeResult(await this._elementHandle._$$evalExpression(params.selector, params.expression, params.isFunction, parseArgument(params.arg))) };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
2020-08-14 14:47:24 -07:00
|
|
|
|
2020-08-20 11:25:33 -07:00
|
|
|
async waitForElementState(params: channels.ElementHandleWaitForElementStateParams): Promise<void> {
|
2020-08-17 16:22:34 -07:00
|
|
|
await this._elementHandle.waitForElementState(params.state, params);
|
|
|
|
}
|
|
|
|
|
2020-08-20 11:25:33 -07:00
|
|
|
async waitForSelector(params: channels.ElementHandleWaitForSelectorParams): Promise<channels.ElementHandleWaitForSelectorResult> {
|
2020-08-14 14:47:24 -07:00
|
|
|
return { element: ElementHandleDispatcher.createNullable(this._scope, await this._elementHandle.waitForSelector(params.selector, params)) };
|
|
|
|
}
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|