/** * 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 type InjectedScript from '../../server/injected/injectedScript'; import { generateSelector } from './selectorGenerator'; export class ConsoleAPI { private _injectedScript: InjectedScript; constructor(injectedScript: InjectedScript) { this._injectedScript = injectedScript; if ((window as any).playwright) return; (window as any).playwright = { $: (selector: string) => this._querySelector(selector), $$: (selector: string) => this._querySelectorAll(selector), inspect: (selector: string) => this._inspect(selector), selector: (element: Element) => this._selector(element), }; } private _querySelector(selector: string): (Element | undefined) { if (typeof selector !== 'string') throw new Error(`Usage: playwright.query('Playwright >> selector').`); const parsed = this._injectedScript.parseSelector(selector); return this._injectedScript.querySelector(parsed, document); } private _querySelectorAll(selector: string): Element[] { if (typeof selector !== 'string') throw new Error(`Usage: playwright.$$('Playwright >> selector').`); const parsed = this._injectedScript.parseSelector(selector); return this._injectedScript.querySelectorAll(parsed, document); } private _inspect(selector: string) { if (typeof (window as any).inspect !== 'function') return; if (typeof selector !== 'string') throw new Error(`Usage: playwright.inspect('Playwright >> selector').`); (window as any).inspect(this._querySelector(selector)); } private _selector(element: Element) { if (!(element instanceof Element)) throw new Error(`Usage: playwright.selector(element).`); return generateSelector(this._injectedScript, element).selector; } } export default ConsoleAPI;