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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { ElementHandle } from '../../dom';
|
|
|
|
import * as js from '../../javascript';
|
|
|
|
import * as types from '../../types';
|
|
|
|
import { ElementHandleChannel, FrameChannel } from '../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-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-06-25 16:05:36 -07:00
|
|
|
export class ElementHandleDispatcher extends JSHandleDispatcher implements ElementHandleChannel {
|
2020-06-26 11:51:47 -07:00
|
|
|
readonly _elementHandle: ElementHandle;
|
2020-06-25 16:05:36 -07:00
|
|
|
|
2020-06-30 21:30:39 -07:00
|
|
|
static createNullable(scope: DispatcherScope, handle: ElementHandle | null): ElementHandleDispatcher | null {
|
2020-06-25 16:05:36 -07:00
|
|
|
if (!handle)
|
|
|
|
return null;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
async ownerFrame(): Promise<FrameChannel | null> {
|
2020-06-30 21:30:39 -07:00
|
|
|
return lookupNullableDispatcher<FrameDispatcher>(await this._elementHandle.ownerFrame());
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async contentFrame(): Promise<FrameChannel | null> {
|
2020-06-30 21:30:39 -07:00
|
|
|
return lookupNullableDispatcher<FrameDispatcher>(await this._elementHandle.contentFrame());
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async getAttribute(params: { name: string }): Promise<string | null> {
|
|
|
|
return this._elementHandle.getAttribute(params.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
async textContent(): Promise<string | null> {
|
|
|
|
return this._elementHandle.textContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
async innerText(): Promise<string> {
|
|
|
|
return this._elementHandle.innerText();
|
|
|
|
}
|
|
|
|
|
|
|
|
async innerHTML(): Promise<string> {
|
|
|
|
return this._elementHandle.innerHTML();
|
|
|
|
}
|
|
|
|
|
|
|
|
async dispatchEvent(params: { type: string, eventInit: Object }) {
|
|
|
|
await this._elementHandle.dispatchEvent(params.type, params.eventInit);
|
|
|
|
}
|
|
|
|
|
2020-07-06 10:04:09 -07:00
|
|
|
async scrollIntoViewIfNeeded(params: types.TimeoutOptions) {
|
|
|
|
await this._elementHandle.scrollIntoViewIfNeeded(params);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-06 10:04:09 -07:00
|
|
|
async hover(params: types.PointerActionOptions & types.PointerActionWaitOptions) {
|
|
|
|
await this._elementHandle.hover(params);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-06 10:04:09 -07:00
|
|
|
async click(params: types.MouseClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions) {
|
|
|
|
await this._elementHandle.click(params);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-06 10:04:09 -07:00
|
|
|
async dblclick(params: types.MouseMultiClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions) {
|
|
|
|
await this._elementHandle.dblclick(params);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-10 15:39:11 -07:00
|
|
|
async selectOption(params: { elements?: ElementHandleChannel[], options?: types.SelectOption[] } & types.NavigatingActionWaitOptions): Promise<string[]> {
|
|
|
|
return this._elementHandle.selectOption(convertSelectOptionValues(params.elements, params.options), params);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-06 10:04:09 -07:00
|
|
|
async fill(params: { value: string } & types.NavigatingActionWaitOptions) {
|
|
|
|
await this._elementHandle.fill(params.value, params);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-06 10:04:09 -07:00
|
|
|
async selectText(params: types.TimeoutOptions) {
|
|
|
|
await this._elementHandle.selectText(params);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-10 15:39:11 -07:00
|
|
|
async setInputFiles(params: { files: { name: string, mimeType: string, buffer: string }[] } & types.NavigatingActionWaitOptions) {
|
|
|
|
await this._elementHandle.setInputFiles(convertInputFiles(params.files), params);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async focus() {
|
|
|
|
await this._elementHandle.focus();
|
|
|
|
}
|
|
|
|
|
2020-07-06 10:04:09 -07:00
|
|
|
async type(params: { text: string } & { delay?: number } & types.NavigatingActionWaitOptions) {
|
|
|
|
await this._elementHandle.type(params.text, params);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-06 10:04:09 -07:00
|
|
|
async press(params: { key: string } & { delay?: number } & types.NavigatingActionWaitOptions) {
|
|
|
|
await this._elementHandle.press(params.key, params);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-06 10:04:09 -07:00
|
|
|
async check(params: types.PointerActionWaitOptions & types.NavigatingActionWaitOptions) {
|
|
|
|
await this._elementHandle.check(params);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-06 10:04:09 -07:00
|
|
|
async uncheck(params: types.PointerActionWaitOptions & types.NavigatingActionWaitOptions) {
|
|
|
|
await this._elementHandle.uncheck(params);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async boundingBox(): Promise<types.Rect | null> {
|
|
|
|
return await this._elementHandle.boundingBox();
|
|
|
|
}
|
|
|
|
|
2020-07-06 10:04:09 -07:00
|
|
|
async screenshot(params: types.ElementScreenshotOptions): Promise<string> {
|
|
|
|
return (await this._elementHandle.screenshot(params)).toString('base64');
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async querySelector(params: { selector: string }): Promise<ElementHandleChannel | null> {
|
2020-06-30 21:30:39 -07:00
|
|
|
const handle = await this._elementHandle.$(params.selector);
|
|
|
|
return handle ? new ElementHandleDispatcher(this._scope, handle) : null;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async querySelectorAll(params: { selector: string }): Promise<ElementHandleChannel[]> {
|
|
|
|
const elements = await this._elementHandle.$$(params.selector);
|
2020-06-30 21:30:39 -07:00
|
|
|
return elements.map(e => new ElementHandleDispatcher(this._scope, e));
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-03 18:04:08 -07:00
|
|
|
async evalOnSelector(params: { selector: string, expression: string, isFunction: boolean, arg: any }): Promise<any> {
|
2020-06-27 11:10:07 -07:00
|
|
|
return serializeResult(await this._elementHandle._$evalExpression(params.selector, params.expression, params.isFunction, parseArgument(params.arg)));
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-03 18:04:08 -07:00
|
|
|
async evalOnSelectorAll(params: { selector: string, expression: string, isFunction: boolean, arg: any }): Promise<any> {
|
2020-06-27 11:10:07 -07:00
|
|
|
return serializeResult(await this._elementHandle._$$evalExpression(params.selector, params.expression, params.isFunction, parseArgument(params.arg)));
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
}
|
2020-06-26 11:51:47 -07:00
|
|
|
|
2020-07-10 15:39:11 -07:00
|
|
|
export function convertSelectOptionValues(elements?: ElementHandleChannel[], options?: types.SelectOption[]): string | ElementHandle | types.SelectOption | string[] | ElementHandle[] | types.SelectOption[] | null {
|
|
|
|
if (elements)
|
|
|
|
return elements.map(v => (v as ElementHandleDispatcher)._elementHandle);
|
|
|
|
if (options)
|
|
|
|
return options;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function convertInputFiles(files: { name: string, mimeType: string, buffer: string }[]): types.FilePayload[] {
|
|
|
|
return files.map(f => ({ name: f.name, mimeType: f.mimeType, buffer: Buffer.from(f.buffer, 'base64') }));
|
2020-06-26 11:51:47 -07:00
|
|
|
}
|