/** * 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. */ import * as fs from 'fs'; import * as util from 'util'; import * as types from '../types'; import {ElementHandle, JSHandle} from './JSHandle'; import { ExecutionContext } from './ExecutionContext'; import { WaitTaskParams, WaitTask, waitForSelectorOrXPath } from '../waitTask'; 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>; _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; if (context) { this._contextResolveCallback.call(null, context); this._contextResolveCallback = null; for (const waitTask of this._waitTasks) waitTask.rerun(context); } 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 { 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.'); } evaluateHandle: types.EvaluateHandle = async (pageFunction, ...args) => { const context = await this.executionContext(); return context.evaluateHandle(pageFunction, ...args as any); } evaluate: types.Evaluate = async (pageFunction, ...args) => { const context = await this.executionContext(); return context.evaluate(pageFunction, ...args as any); } async $(selector: string): Promise { 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> { const document = await this._document(); return document.$x(expression); } $eval: types.$Eval = async (selector, pageFunction, ...args) => { const document = await this._document(); return document.$eval(selector, pageFunction, ...args); } $$eval: types.$$Eval = async (selector, pageFunction, ...args) => { const document = await this._document(); return document.$$eval(selector, pageFunction, ...args); } async $$(selector: string): Promise> { const document = await this._document(); return document.$$(selector); } async content(): Promise { 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 { 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 { 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 { 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 { 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 { 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 { 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(); } async waitForXPath(xpath: string, options: { visible?: boolean, hidden?: boolean, timeout?: number } = {}): Promise { 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(); } waitForFunction(pageFunction: Function | string, options: { polling?: string | number; timeout?: number; } | undefined = {}, ...args): Promise { const { polling = 'raf', timeout = this._timeoutSettings.timeout(), } = options; const params: WaitTaskParams = { predicateBody: pageFunction, title: 'function', polling, timeout, args }; return this._scheduleWaitTask(params); } async title(): Promise { return this.evaluate(() => document.title); } private _scheduleWaitTask(params: WaitTaskParams): Promise { const task = new WaitTask(params, () => this._waitTasks.delete(task)); this._waitTasks.add(task); if (this._context) task.rerun(this._context); return task.promise; } }