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-17 16:22:34 -07:00
|
|
|
import { ElementHandleChannel, JSHandleInitializer, ElementHandleScrollIntoViewIfNeededOptions, ElementHandleHoverOptions, ElementHandleClickOptions, ElementHandleDblclickOptions, ElementHandleFillOptions, ElementHandleSetInputFilesOptions, ElementHandlePressOptions, ElementHandleCheckOptions, ElementHandleUncheckOptions, ElementHandleScreenshotOptions, ElementHandleTypeOptions, ElementHandleSelectTextOptions, ElementHandleWaitForSelectorOptions, ElementHandleWaitForElementStateOptions } from '../channels';
|
2020-06-25 16:05:36 -07:00
|
|
|
import { Frame } from './frame';
|
2020-06-27 11:10:07 -07:00
|
|
|
import { FuncOn, JSHandle, serializeArgument, parseResult } from './jsHandle';
|
2020-07-10 15:11:47 -07:00
|
|
|
import { ChannelOwner } from './channelOwner';
|
2020-07-10 15:39:11 -07:00
|
|
|
import { helper, assert } from '../../helper';
|
2020-07-28 15:33:38 -07:00
|
|
|
import { normalizeFilePayloads } from '../../converters';
|
2020-07-29 17:26:59 -07:00
|
|
|
import { SelectOption, FilePayload, Rect, SelectOptionOptions } from './types';
|
2020-06-25 16:05:36 -07:00
|
|
|
|
|
|
|
export class ElementHandle<T extends Node = Node> extends JSHandle<T> {
|
2020-06-25 18:01:18 -07:00
|
|
|
readonly _elementChannel: ElementHandleChannel;
|
2020-06-25 16:05:36 -07:00
|
|
|
|
|
|
|
static from(handle: ElementHandleChannel): ElementHandle {
|
2020-07-01 18:36:09 -07:00
|
|
|
return (handle as any)._object;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-20 17:38:06 -07:00
|
|
|
static fromNullable(handle: ElementHandleChannel | undefined): ElementHandle | null {
|
2020-06-25 16:05:36 -07:00
|
|
|
return handle ? ElementHandle.from(handle) : null;
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:00:10 -07:00
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: JSHandleInitializer) {
|
|
|
|
super(parent, type, guid, initializer);
|
2020-07-01 13:55:29 -07:00
|
|
|
this._elementChannel = this._channel as ElementHandleChannel;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
asElement(): ElementHandle<T> | null {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
async ownerFrame(): Promise<Frame | null> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.ownerFrame', async () => {
|
|
|
|
return Frame.fromNullable((await this._elementChannel.ownerFrame()).frame);
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async contentFrame(): Promise<Frame | null> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.contentFrame', async () => {
|
|
|
|
return Frame.fromNullable((await this._elementChannel.contentFrame()).frame);
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async getAttribute(name: string): Promise<string | null> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.getAttribute', async () => {
|
2020-07-20 17:38:06 -07:00
|
|
|
const value = (await this._elementChannel.getAttribute({ name })).value;
|
|
|
|
return value === undefined ? null : value;
|
2020-07-15 14:04:39 -07:00
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async textContent(): Promise<string | null> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.textContent', async () => {
|
2020-07-20 17:38:06 -07:00
|
|
|
const value = (await this._elementChannel.textContent()).value;
|
|
|
|
return value === undefined ? null : value;
|
2020-07-15 14:04:39 -07:00
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async innerText(): Promise<string> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.innerText', async () => {
|
|
|
|
return (await this._elementChannel.innerText()).value;
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async innerHTML(): Promise<string> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.innerHTML', async () => {
|
|
|
|
return (await this._elementChannel.innerHTML()).value;
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async dispatchEvent(type: string, eventInit: Object = {}) {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.dispatchEvent', async () => {
|
2020-07-17 09:53:13 -07:00
|
|
|
await this._elementChannel.dispatchEvent({ type, eventInit: serializeArgument(eventInit) });
|
2020-07-15 14:04:39 -07:00
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
async scrollIntoViewIfNeeded(options: ElementHandleScrollIntoViewIfNeededOptions = {}) {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.scrollIntoViewIfNeeded', async () => {
|
|
|
|
await this._elementChannel.scrollIntoViewIfNeeded(options);
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
async hover(options: ElementHandleHoverOptions = {}): Promise<void> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.hover', async () => {
|
|
|
|
await this._elementChannel.hover(options);
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
async click(options: ElementHandleClickOptions = {}): Promise<void> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.click', async () => {
|
|
|
|
return await this._elementChannel.click(options);
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
async dblclick(options: ElementHandleDblclickOptions = {}): Promise<void> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.dblclick', async () => {
|
|
|
|
return await this._elementChannel.dblclick(options);
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
async selectOption(values: string | ElementHandle | SelectOption | string[] | ElementHandle[] | SelectOption[] | null, options: SelectOptionOptions = {}): Promise<string[]> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.selectOption', async () => {
|
|
|
|
const result = await this._elementChannel.selectOption({ ...convertSelectOptionValues(values), ...options });
|
|
|
|
return result.values;
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
async fill(value: string, options: ElementHandleFillOptions = {}): Promise<void> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.fill', async () => {
|
|
|
|
return await this._elementChannel.fill({ value, ...options });
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-31 17:00:36 -07:00
|
|
|
async selectText(options: ElementHandleSelectTextOptions = {}): Promise<void> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.selectText', async () => {
|
|
|
|
await this._elementChannel.selectText(options);
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
async setInputFiles(files: string | FilePayload | string[] | FilePayload[], options: ElementHandleSetInputFilesOptions = {}) {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.setInputFiles', async () => {
|
|
|
|
await this._elementChannel.setInputFiles({ files: await convertInputFiles(files), ...options });
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async focus(): Promise<void> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.focus', async () => {
|
|
|
|
await this._elementChannel.focus();
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
async type(text: string, options: ElementHandleTypeOptions = {}): Promise<void> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.type', async () => {
|
|
|
|
await this._elementChannel.type({ text, ...options });
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
async press(key: string, options: ElementHandlePressOptions = {}): Promise<void> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.press', async () => {
|
|
|
|
await this._elementChannel.press({ key, ...options });
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
async check(options: ElementHandleCheckOptions = {}) {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.check', async () => {
|
|
|
|
return await this._elementChannel.check(options);
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
async uncheck(options: ElementHandleUncheckOptions = {}) {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.uncheck', async () => {
|
|
|
|
return await this._elementChannel.uncheck(options);
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
async boundingBox(): Promise<Rect | null> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.boundingBox', async () => {
|
2020-07-20 17:38:06 -07:00
|
|
|
const value = (await this._elementChannel.boundingBox()).value;
|
|
|
|
return value === undefined ? null : value;
|
2020-07-15 14:04:39 -07:00
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
async screenshot(options: ElementHandleScreenshotOptions = {}): Promise<Buffer> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.screenshot', async () => {
|
|
|
|
return Buffer.from((await this._elementChannel.screenshot(options)).binary, 'base64');
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async $(selector: string): Promise<ElementHandle<Element> | null> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.$', async () => {
|
|
|
|
return ElementHandle.fromNullable((await this._elementChannel.querySelector({ selector })).element) as ElementHandle<Element> | null;
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async $$(selector: string): Promise<ElementHandle<Element>[]> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.$$', async () => {
|
|
|
|
const result = await this._elementChannel.querySelectorAll({ selector });
|
|
|
|
return result.elements.map(h => ElementHandle.from(h) as ElementHandle<Element>);
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async $eval<R, Arg>(selector: string, pageFunction: FuncOn<Element, Arg, R>, arg: Arg): Promise<R>;
|
|
|
|
async $eval<R>(selector: string, pageFunction: FuncOn<Element, void, R>, arg?: any): Promise<R>;
|
|
|
|
async $eval<R, Arg>(selector: string, pageFunction: FuncOn<Element, Arg, R>, arg: Arg): Promise<R> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.$eval', async () => {
|
|
|
|
const result = await this._elementChannel.evalOnSelector({ selector, expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) });
|
|
|
|
return parseResult(result.value);
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async $$eval<R, Arg>(selector: string, pageFunction: FuncOn<Element[], Arg, R>, arg: Arg): Promise<R>;
|
|
|
|
async $$eval<R>(selector: string, pageFunction: FuncOn<Element[], void, R>, arg?: any): Promise<R>;
|
|
|
|
async $$eval<R, Arg>(selector: string, pageFunction: FuncOn<Element[], Arg, R>, arg: Arg): Promise<R> {
|
2020-07-15 14:04:39 -07:00
|
|
|
return this._wrapApiCall('elementHandle.$$eval', async () => {
|
|
|
|
const result = await this._elementChannel.evalOnSelectorAll({ selector, expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) });
|
|
|
|
return parseResult(result.value);
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
2020-08-14 14:47:24 -07:00
|
|
|
|
2020-08-17 16:22:34 -07:00
|
|
|
async waitForElementState(state: 'visible' | 'hidden' | 'stable' | 'enabled', options: ElementHandleWaitForElementStateOptions = {}): Promise<void> {
|
|
|
|
return this._wrapApiCall('elementHandle.waitForElementState', async () => {
|
|
|
|
return await this._elementChannel.waitForElementState({ state, ...options });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-14 14:47:24 -07:00
|
|
|
async waitForSelector(selector: string, options: ElementHandleWaitForSelectorOptions = {}): Promise<ElementHandle<Element> | null> {
|
|
|
|
return this._wrapApiCall('elementHandle.waitForSelector', async () => {
|
|
|
|
const result = await this._elementChannel.waitForSelector({ selector, ...options });
|
|
|
|
return ElementHandle.fromNullable(result.element) as ElementHandle<Element> | null;
|
|
|
|
});
|
|
|
|
}
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
2020-06-26 11:51:47 -07:00
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
export function convertSelectOptionValues(values: string | ElementHandle | SelectOption | string[] | ElementHandle[] | SelectOption[] | null): { elements?: ElementHandleChannel[], options?: SelectOption[] } {
|
2020-07-10 15:39:11 -07:00
|
|
|
if (!values)
|
|
|
|
return {};
|
|
|
|
if (!Array.isArray(values))
|
|
|
|
values = [ values as any ];
|
|
|
|
if (!values.length)
|
|
|
|
return {};
|
2020-07-21 15:25:31 -07:00
|
|
|
for (let i = 0; i < values.length; i++)
|
|
|
|
assert(values[i] !== null, `options[${i}]: expected object, got null`);
|
2020-07-10 15:39:11 -07:00
|
|
|
|
|
|
|
if (values[0] instanceof ElementHandle)
|
|
|
|
return { elements: (values as ElementHandle[]).map((v: ElementHandle) => v._elementChannel) };
|
|
|
|
if (helper.isString(values[0]))
|
|
|
|
return { options: (values as string[]).map(value => ({ value })) };
|
2020-07-29 17:26:59 -07:00
|
|
|
return { options: values as SelectOption[] };
|
2020-07-10 15:39:11 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
export async function convertInputFiles(files: string | FilePayload | string[] | FilePayload[]): Promise<{ name: string, mimeType: string, buffer: string }[]> {
|
2020-07-10 15:39:11 -07:00
|
|
|
const filePayloads = await normalizeFilePayloads(files);
|
|
|
|
return filePayloads.map(f => ({ name: f.name, mimeType: f.mimeType, buffer: f.buffer.toString('base64') }));
|
2020-06-26 11:51:47 -07:00
|
|
|
}
|