playwright/src/firefox/DOMWorld.ts

288 lines
9.3 KiB
TypeScript
Raw Normal View History

2019-11-26 01:42:37 +01:00
/**
* Copyright 2019 Google Inc. All rights reserved.
* Modifications 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.
*/
2019-11-19 03:18:28 +01:00
import * as fs from 'fs';
import * as util from 'util';
2019-11-26 01:42:37 +01:00
import * as types from '../types';
2019-11-19 03:18:28 +01:00
import {ElementHandle, JSHandle} from './JSHandle';
import { ExecutionContext } from './ExecutionContext';
import { WaitTaskParams, WaitTask, waitForSelectorOrXPath } from '../waitTask';
2019-11-19 03:18:28 +01:00
const readFileAsync = util.promisify(fs.readFile);
export class DOMWorld {
_frame: any;
_timeoutSettings: any;
_documentPromise: any;
_contextPromise: any;
_contextResolveCallback: any;
private _context: ExecutionContext | null;
_waitTasks: Set<WaitTask<JSHandle>>;
2019-11-19 03:18:28 +01:00
_detached: boolean;
constructor(frame, timeoutSettings) {
this._frame = frame;
this._timeoutSettings = timeoutSettings;
this._documentPromise = null;
this._contextPromise;
this._contextResolveCallback = null;
this._setContext(null);
this._waitTasks = new Set();
this._detached = false;
}
frame() {
return this._frame;
}
_setContext(context: ExecutionContext) {
this._context = context;
2019-11-19 03:18:28 +01:00
if (context) {
this._contextResolveCallback.call(null, context);
this._contextResolveCallback = null;
for (const waitTask of this._waitTasks)
waitTask.rerun(context);
2019-11-19 03:18:28 +01:00
} else {
this._documentPromise = null;
this._contextPromise = new Promise(fulfill => {
this._contextResolveCallback = fulfill;
});
}
}
_detach() {
this._detached = true;
for (const waitTask of this._waitTasks)
waitTask.terminate(new Error('waitForFunction failed: frame got detached.'));
}
async executionContext(): Promise<ExecutionContext> {
2019-11-19 03:18:28 +01:00
if (this._detached)
throw new Error(`Execution Context is not available in detached frame "${this.url()}" (are you trying to evaluate?)`);
return this._contextPromise;
}
url() {
throw new Error('Method not implemented.');
}
2019-11-26 01:42:37 +01:00
evaluateHandle: types.EvaluateHandle<JSHandle> = async (pageFunction, ...args) => {
2019-11-19 03:18:28 +01:00
const context = await this.executionContext();
2019-11-26 01:42:37 +01:00
return context.evaluateHandle(pageFunction, ...args as any);
2019-11-19 03:18:28 +01:00
}
2019-11-26 01:42:37 +01:00
evaluate: types.Evaluate<JSHandle> = async (pageFunction, ...args) => {
2019-11-19 03:18:28 +01:00
const context = await this.executionContext();
2019-11-26 01:42:37 +01:00
return context.evaluate(pageFunction, ...args as any);
2019-11-19 03:18:28 +01:00
}
async $(selector: string): Promise<ElementHandle | null> {
const document = await this._document();
return document.$(selector);
}
_document() {
if (!this._documentPromise)
this._documentPromise = this.evaluateHandle('document').then(handle => handle.asElement());
return this._documentPromise;
}
async $x(expression: string): Promise<Array<ElementHandle>> {
const document = await this._document();
return document.$x(expression);
}
2019-11-26 01:42:37 +01:00
$eval: types.$Eval<JSHandle> = async (selector, pageFunction, ...args) => {
2019-11-19 03:18:28 +01:00
const document = await this._document();
return document.$eval(selector, pageFunction, ...args);
}
2019-11-26 01:42:37 +01:00
$$eval: types.$$Eval<JSHandle> = async (selector, pageFunction, ...args) => {
2019-11-19 03:18:28 +01:00
const document = await this._document();
return document.$$eval(selector, pageFunction, ...args);
}
async $$(selector: string): Promise<Array<ElementHandle>> {
const document = await this._document();
return document.$$(selector);
}
async content(): Promise<string> {
return await this.evaluate(() => {
let retVal = '';
if (document.doctype)
retVal = new XMLSerializer().serializeToString(document.doctype);
if (document.documentElement)
retVal += document.documentElement.outerHTML;
return retVal;
});
}
async setContent(html: string) {
await this.evaluate(html => {
document.open();
document.write(html);
document.close();
}, html);
}
async addScriptTag(options: { content?: string; path?: string; type?: string; url?: string; }): Promise<ElementHandle> {
if (typeof options.url === 'string') {
const url = options.url;
try {
return (await this.evaluateHandle(addScriptUrl, url, options.type)).asElement();
} catch (error) {
throw new Error(`Loading script from ${url} failed`);
}
}
if (typeof options.path === 'string') {
let contents = await readFileAsync(options.path, 'utf8');
contents += '//# sourceURL=' + options.path.replace(/\n/g, '');
return (await this.evaluateHandle(addScriptContent, contents, options.type)).asElement();
}
if (typeof options.content === 'string')
return (await this.evaluateHandle(addScriptContent, options.content, options.type)).asElement();
throw new Error('Provide an object with a `url`, `path` or `content` property');
async function addScriptUrl(url: string, type: string): Promise<HTMLElement> {
const script = document.createElement('script');
script.src = url;
if (type)
script.type = type;
const promise = new Promise((res, rej) => {
script.onload = res;
script.onerror = rej;
});
document.head.appendChild(script);
await promise;
return script;
}
function addScriptContent(content: string, type: string = 'text/javascript'): HTMLElement {
const script = document.createElement('script');
script.type = type;
script.text = content;
let error = null;
script.onerror = e => error = e;
document.head.appendChild(script);
if (error)
throw error;
return script;
}
}
async addStyleTag(options: { content?: string; path?: string; url?: string; }): Promise<ElementHandle> {
if (typeof options.url === 'string') {
const url = options.url;
try {
return (await this.evaluateHandle(addStyleUrl, url)).asElement();
} catch (error) {
throw new Error(`Loading style from ${url} failed`);
}
}
if (typeof options.path === 'string') {
let contents = await readFileAsync(options.path, 'utf8');
contents += '/*# sourceURL=' + options.path.replace(/\n/g, '') + '*/';
return (await this.evaluateHandle(addStyleContent, contents)).asElement();
}
if (typeof options.content === 'string')
return (await this.evaluateHandle(addStyleContent, options.content)).asElement();
throw new Error('Provide an object with a `url`, `path` or `content` property');
async function addStyleUrl(url: string): Promise<HTMLElement> {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = url;
const promise = new Promise((res, rej) => {
link.onload = res;
link.onerror = rej;
});
document.head.appendChild(link);
await promise;
return link;
}
async function addStyleContent(content: string): Promise<HTMLElement> {
const style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode(content));
const promise = new Promise((res, rej) => {
style.onload = res;
style.onerror = rej;
});
document.head.appendChild(style);
await promise;
return style;
}
}
async waitForSelector(selector: string, options: { visible?: boolean; hidden?: boolean; timeout?: number; } | undefined): Promise<ElementHandle | null> {
const params = waitForSelectorOrXPath(selector, false /* isXPath */, { timeout: this._timeoutSettings.timeout(), ...options });
const handle = await this._scheduleWaitTask(params);
if (!handle.asElement()) {
await handle.dispose();
return null;
}
return handle.asElement();
2019-11-19 03:18:28 +01:00
}
async waitForXPath(xpath: string, options: { visible?: boolean, hidden?: boolean, timeout?: number } = {}): Promise<ElementHandle | null> {
const params = waitForSelectorOrXPath(xpath, true /* isXPath */, { timeout: this._timeoutSettings.timeout(), ...options });
const handle = await this._scheduleWaitTask(params);
if (!handle.asElement()) {
await handle.dispose();
return null;
}
return handle.asElement();
2019-11-19 03:18:28 +01:00
}
waitForFunction(pageFunction: Function | string, options: { polling?: string | number; timeout?: number; } | undefined = {}, ...args): Promise<JSHandle> {
const {
polling = 'raf',
timeout = this._timeoutSettings.timeout(),
} = options;
const params: WaitTaskParams = {
predicateBody: pageFunction,
title: 'function',
polling,
timeout,
args
};
return this._scheduleWaitTask(params);
2019-11-19 03:18:28 +01:00
}
async title(): Promise<string> {
return this.evaluate(() => document.title);
}
private _scheduleWaitTask(params: WaitTaskParams): Promise<JSHandle> {
const task = new WaitTask(params, () => this._waitTasks.delete(task));
this._waitTasks.add(task);
if (this._context)
task.rerun(this._context);
return task.promise;
2019-11-19 03:18:28 +01:00
}
}