Best JavaScript code snippet using playwright-internal
crPage.js
Source:crPage.js
1"use strict";2Object.defineProperty(exports, "__esModule", {3 value: true4});5exports.CRPage = void 0;6var dom = _interopRequireWildcard(require("../dom"));7var _helper = require("../helper");8var _eventsHelper = require("../../utils/eventsHelper");9var network = _interopRequireWildcard(require("../network"));10var _crConnection = require("./crConnection");11var _crExecutionContext = require("./crExecutionContext");12var _crNetworkManager = require("./crNetworkManager");13var _page = require("../page");14var _crProtocolHelper = require("./crProtocolHelper");15var dialog = _interopRequireWildcard(require("../dialog"));16var _path = _interopRequireDefault(require("path"));17var _crInput = require("./crInput");18var _crAccessibility = require("./crAccessibility");19var _crCoverage = require("./crCoverage");20var _crPdf = require("./crPdf");21var _crBrowser = require("./crBrowser");22var _stackTrace = require("../../utils/stackTrace");23var _utils = require("../../utils/utils");24var _videoRecorder = require("./videoRecorder");25var _crDragDrop = require("./crDragDrop");26var _registry = require("../../utils/registry");27function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }28function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }29function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }30/**31 * Copyright 2017 Google Inc. All rights reserved.32 * Modifications copyright (c) Microsoft Corporation.33 *34 * Licensed under the Apache License, Version 2.0 (the "License");35 * you may not use this file except in compliance with the License.36 * You may obtain a copy of the License at37 *38 * http://www.apache.org/licenses/LICENSE-2.039 *40 * Unless required by applicable law or agreed to in writing, software41 * distributed under the License is distributed on an "AS IS" BASIS,42 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.43 * See the License for the specific language governing permissions and44 * limitations under the License.45 */46const UTILITY_WORLD_NAME = '__playwright_utility_world__';47class CRPage {48 // Holds window features for the next popup being opened via window.open,49 // until the popup target arrives. This could be racy if two oopifs50 // simultaneously call window.open with window features: the order51 // of their Page.windowOpen events is not guaranteed to match the order52 // of new popup targets.53 static mainFrameSession(page) {54 const crPage = page._delegate;55 return crPage._mainFrameSession;56 }57 constructor(client, targetId, browserContext, opener, hasUIWindow, isBackgroundPage) {58 this._mainFrameSession = void 0;59 this._sessions = new Map();60 this._page = void 0;61 this.rawMouse = void 0;62 this.rawKeyboard = void 0;63 this.rawTouchscreen = void 0;64 this._targetId = void 0;65 this._opener = void 0;66 this._pdf = void 0;67 this._coverage = void 0;68 this._browserContext = void 0;69 this._pagePromise = void 0;70 this._initializedPage = null;71 this._isBackgroundPage = void 0;72 this._nextWindowOpenPopupFeatures = [];73 this._targetId = targetId;74 this._opener = opener;75 this._isBackgroundPage = isBackgroundPage;76 const dragManager = new _crDragDrop.DragManager(this);77 this.rawKeyboard = new _crInput.RawKeyboardImpl(client, browserContext._browser._isMac, dragManager);78 this.rawMouse = new _crInput.RawMouseImpl(this, client, dragManager);79 this.rawTouchscreen = new _crInput.RawTouchscreenImpl(client);80 this._pdf = new _crPdf.CRPDF(client);81 this._coverage = new _crCoverage.CRCoverage(client);82 this._browserContext = browserContext;83 this._page = new _page.Page(this, browserContext);84 this._mainFrameSession = new FrameSession(this, client, targetId, null);85 this._sessions.set(targetId, this._mainFrameSession);86 client.once(_crConnection.CRSessionEvents.Disconnected, () => this._page._didDisconnect());87 if (opener && !browserContext._options.noDefaultViewport) {88 const features = opener._nextWindowOpenPopupFeatures.shift() || [];89 const viewportSize = _helper.helper.getViewportSizeFromWindowFeatures(features);90 if (viewportSize) this._page._state.emulatedSize = {91 viewport: viewportSize,92 screen: viewportSize93 };94 } // Note: it is important to call |reportAsNew| before resolving pageOrError promise,95 // so that anyone who awaits pageOrError got a ready and reported page.96 this._pagePromise = this._mainFrameSession._initialize(hasUIWindow).then(async r => {97 await this._page.initOpener(this._opener);98 return r;99 }).catch(async e => {100 await this._page.initOpener(this._opener);101 throw e;102 }).then(() => {103 this._initializedPage = this._page;104 this._reportAsNew();105 return this._page;106 }).catch(e => {107 this._reportAsNew(e);108 return e;109 });110 }111 _reportAsNew(error) {112 if (this._isBackgroundPage) {113 if (!error) this._browserContext.emit(_crBrowser.CRBrowserContext.CREvents.BackgroundPage, this._page);114 } else {115 this._page.reportAsNew(error);116 }117 }118 async _forAllFrameSessions(cb) {119 const frameSessions = Array.from(this._sessions.values());120 await Promise.all(frameSessions.map(frameSession => {121 if (frameSession._isMainFrame()) return cb(frameSession);122 return cb(frameSession).catch(e => {123 // Broadcasting a message to the closed iframe shoule be a noop.124 if (e.message && (e.message.includes('Target closed.') || e.message.includes('Session closed.'))) return;125 throw e;126 });127 }));128 }129 _sessionForFrame(frame) {130 // Frame id equals target id.131 while (!this._sessions.has(frame._id)) {132 const parent = frame.parentFrame();133 if (!parent) throw new Error(`Frame has been detached.`);134 frame = parent;135 }136 return this._sessions.get(frame._id);137 }138 _sessionForHandle(handle) {139 const frame = handle._context.frame;140 return this._sessionForFrame(frame);141 }142 willBeginDownload() {143 this._mainFrameSession._willBeginDownload();144 }145 async pageOrError() {146 return this._pagePromise;147 }148 didClose() {149 for (const session of this._sessions.values()) session.dispose();150 this._page._didClose();151 }152 async navigateFrame(frame, url, referrer) {153 return this._sessionForFrame(frame)._navigate(frame, url, referrer);154 }155 async exposeBinding(binding) {156 await this._forAllFrameSessions(frame => frame._initBinding(binding));157 await Promise.all(this._page.frames().map(frame => frame.evaluateExpression(binding.source, false, {}).catch(e => {})));158 }159 async updateExtraHTTPHeaders() {160 await this._forAllFrameSessions(frame => frame._updateExtraHTTPHeaders(false));161 }162 async updateGeolocation() {163 await this._forAllFrameSessions(frame => frame._updateGeolocation(false));164 }165 async updateOffline() {166 await this._forAllFrameSessions(frame => frame._updateOffline(false));167 }168 async updateHttpCredentials() {169 await this._forAllFrameSessions(frame => frame._updateHttpCredentials(false));170 }171 async setEmulatedSize(emulatedSize) {172 (0, _utils.assert)(this._page._state.emulatedSize === emulatedSize);173 await this._mainFrameSession._updateViewport();174 }175 async bringToFront() {176 await this._mainFrameSession._client.send('Page.bringToFront');177 }178 async updateEmulateMedia() {179 await this._forAllFrameSessions(frame => frame._updateEmulateMedia(false));180 }181 async updateRequestInterception() {182 await this._forAllFrameSessions(frame => frame._updateRequestInterception());183 }184 async setFileChooserIntercepted(enabled) {185 await this._forAllFrameSessions(frame => frame._setFileChooserIntercepted(enabled));186 }187 async reload() {188 await this._mainFrameSession._client.send('Page.reload');189 }190 async _go(delta) {191 const history = await this._mainFrameSession._client.send('Page.getNavigationHistory');192 const entry = history.entries[history.currentIndex + delta];193 if (!entry) return false;194 await this._mainFrameSession._client.send('Page.navigateToHistoryEntry', {195 entryId: entry.id196 });197 return true;198 }199 goBack() {200 return this._go(-1);201 }202 goForward() {203 return this._go(+1);204 }205 async evaluateOnNewDocument(source, world = 'main') {206 await this._forAllFrameSessions(frame => frame._evaluateOnNewDocument(source, world));207 }208 async closePage(runBeforeUnload) {209 if (runBeforeUnload) await this._mainFrameSession._client.send('Page.close');else await this._browserContext._browser._closePage(this);210 }211 async setBackgroundColor(color) {212 await this._mainFrameSession._client.send('Emulation.setDefaultBackgroundColorOverride', {213 color214 });215 }216 async takeScreenshot(progress, format, documentRect, viewportRect, quality, fitsViewport) {217 const {218 visualViewport219 } = await this._mainFrameSession._client.send('Page.getLayoutMetrics');220 if (!documentRect) {221 documentRect = {222 x: visualViewport.pageX + viewportRect.x,223 y: visualViewport.pageY + viewportRect.y,224 ..._helper.helper.enclosingIntSize({225 width: viewportRect.width / visualViewport.scale,226 height: viewportRect.height / visualViewport.scale227 })228 };229 } // When taking screenshots with documentRect (based on the page content, not viewport),230 // ignore current page scale.231 const clip = { ...documentRect,232 scale: viewportRect ? visualViewport.scale : 1233 };234 progress.throwIfAborted();235 const result = await this._mainFrameSession._client.send('Page.captureScreenshot', {236 format,237 quality,238 clip,239 captureBeyondViewport: !fitsViewport240 });241 return Buffer.from(result.data, 'base64');242 }243 async getContentFrame(handle) {244 return this._sessionForHandle(handle)._getContentFrame(handle);245 }246 async getOwnerFrame(handle) {247 return this._sessionForHandle(handle)._getOwnerFrame(handle);248 }249 isElementHandle(remoteObject) {250 return remoteObject.subtype === 'node';251 }252 async getBoundingBox(handle) {253 return this._sessionForHandle(handle)._getBoundingBox(handle);254 }255 async scrollRectIntoViewIfNeeded(handle, rect) {256 return this._sessionForHandle(handle)._scrollRectIntoViewIfNeeded(handle, rect);257 }258 async setScreencastOptions(options) {259 if (options) {260 await this._mainFrameSession._startScreencast(this, {261 format: 'jpeg',262 quality: options.quality,263 maxWidth: options.width,264 maxHeight: options.height265 });266 } else {267 await this._mainFrameSession._stopScreencast(this);268 }269 }270 rafCountForStablePosition() {271 return 1;272 }273 async getContentQuads(handle) {274 return this._sessionForHandle(handle)._getContentQuads(handle);275 }276 async setInputFiles(handle, files) {277 await handle.evaluateInUtility(([injected, node, files]) => injected.setInputFiles(node, files), files);278 }279 async adoptElementHandle(handle, to) {280 return this._sessionForHandle(handle)._adoptElementHandle(handle, to);281 }282 async getAccessibilityTree(needle) {283 return (0, _crAccessibility.getAccessibilityTree)(this._mainFrameSession._client, needle);284 }285 async inputActionEpilogue() {286 await this._mainFrameSession._client.send('Page.enable').catch(e => {});287 }288 async pdf(options) {289 return this._pdf.generate(options);290 }291 coverage() {292 return this._coverage;293 }294 async getFrameElement(frame) {295 let parent = frame.parentFrame();296 if (!parent) throw new Error('Frame has been detached.');297 const parentSession = this._sessionForFrame(parent);298 const {299 backendNodeId300 } = await parentSession._client.send('DOM.getFrameOwner', {301 frameId: frame._id302 }).catch(e => {303 if (e instanceof Error && e.message.includes('Frame with the given id was not found.')) (0, _stackTrace.rewriteErrorMessage)(e, 'Frame has been detached.');304 throw e;305 });306 parent = frame.parentFrame();307 if (!parent) throw new Error('Frame has been detached.');308 return parentSession._adoptBackendNodeId(backendNodeId, await parent._mainContext());309 }310}311exports.CRPage = CRPage;312class FrameSession {313 // Marks the oopif session that remote -> local transition has happened in the parent.314 // See Target.detachedFromTarget handler for details.315 constructor(crPage, client, targetId, parentSession) {316 this._client = void 0;317 this._crPage = void 0;318 this._page = void 0;319 this._networkManager = void 0;320 this._contextIdToContext = new Map();321 this._eventListeners = [];322 this._targetId = void 0;323 this._firstNonInitialNavigationCommittedPromise = void 0;324 this._firstNonInitialNavigationCommittedFulfill = () => {};325 this._firstNonInitialNavigationCommittedReject = e => {};326 this._windowId = void 0;327 this._swappedIn = false;328 this._videoRecorder = null;329 this._screencastId = null;330 this._screencastClients = new Set();331 this._client = client;332 this._crPage = crPage;333 this._page = crPage._page;334 this._targetId = targetId;335 this._networkManager = new _crNetworkManager.CRNetworkManager(client, this._page, parentSession ? parentSession._networkManager : null);336 this._firstNonInitialNavigationCommittedPromise = new Promise((f, r) => {337 this._firstNonInitialNavigationCommittedFulfill = f;338 this._firstNonInitialNavigationCommittedReject = r;339 });340 client.once(_crConnection.CRSessionEvents.Disconnected, () => {341 this._firstNonInitialNavigationCommittedReject(new Error('Page closed'));342 });343 }344 _isMainFrame() {345 return this._targetId === this._crPage._targetId;346 }347 _addRendererListeners() {348 this._eventListeners.push(...[_eventsHelper.eventsHelper.addEventListener(this._client, 'Log.entryAdded', event => this._onLogEntryAdded(event)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Page.fileChooserOpened', event => this._onFileChooserOpened(event)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Page.frameAttached', event => this._onFrameAttached(event.frameId, event.parentFrameId)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Page.frameDetached', event => this._onFrameDetached(event.frameId, event.reason)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Page.frameNavigated', event => this._onFrameNavigated(event.frame, false)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Page.frameRequestedNavigation', event => this._onFrameRequestedNavigation(event)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Page.frameStoppedLoading', event => this._onFrameStoppedLoading(event.frameId)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Page.javascriptDialogOpening', event => this._onDialog(event)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Page.navigatedWithinDocument', event => this._onFrameNavigatedWithinDocument(event.frameId, event.url)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Runtime.bindingCalled', event => this._onBindingCalled(event)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Runtime.consoleAPICalled', event => this._onConsoleAPI(event)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Runtime.exceptionThrown', exception => this._handleException(exception.exceptionDetails)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Runtime.executionContextCreated', event => this._onExecutionContextCreated(event.context)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Runtime.executionContextDestroyed', event => this._onExecutionContextDestroyed(event.executionContextId)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Runtime.executionContextsCleared', event => this._onExecutionContextsCleared()), _eventsHelper.eventsHelper.addEventListener(this._client, 'Target.attachedToTarget', event => this._onAttachedToTarget(event)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Target.detachedFromTarget', event => this._onDetachedFromTarget(event))]);349 }350 _addBrowserListeners() {351 this._eventListeners.push(...[_eventsHelper.eventsHelper.addEventListener(this._client, 'Inspector.targetCrashed', event => this._onTargetCrashed()), _eventsHelper.eventsHelper.addEventListener(this._client, 'Page.screencastFrame', event => this._onScreencastFrame(event)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Page.windowOpen', event => this._onWindowOpen(event))]);352 }353 async _initialize(hasUIWindow) {354 if (hasUIWindow && !this._crPage._browserContext._browser.isClank() && !this._crPage._browserContext._options.noDefaultViewport) {355 const {356 windowId357 } = await this._client.send('Browser.getWindowForTarget');358 this._windowId = windowId;359 }360 let screencastOptions;361 if (this._isMainFrame() && this._crPage._browserContext._options.recordVideo && hasUIWindow) {362 const screencastId = (0, _utils.createGuid)();363 const outputFile = _path.default.join(this._crPage._browserContext._options.recordVideo.dir, screencastId + '.webm');364 screencastOptions = { // validateBrowserContextOptions ensures correct video size.365 ...this._crPage._browserContext._options.recordVideo.size,366 outputFile367 };368 await this._crPage._browserContext._ensureVideosPath(); // Note: it is important to start video recorder before sending Page.startScreencast,369 // and it is equally important to send Page.startScreencast before sending Runtime.runIfWaitingForDebugger.370 await this._createVideoRecorder(screencastId, screencastOptions);371 this._crPage.pageOrError().then(p => {372 if (p instanceof Error) this._stopVideoRecording().catch(() => {});373 });374 }375 let lifecycleEventsEnabled;376 if (!this._isMainFrame()) this._addRendererListeners();377 this._addBrowserListeners();378 const promises = [this._client.send('Page.enable'), this._client.send('Page.getFrameTree').then(({379 frameTree380 }) => {381 if (this._isMainFrame()) {382 this._handleFrameTree(frameTree);383 this._addRendererListeners();384 }385 const localFrames = this._isMainFrame() ? this._page.frames() : [this._page._frameManager.frame(this._targetId)];386 for (const frame of localFrames) {387 // Note: frames might be removed before we send these.388 this._client._sendMayFail('Page.createIsolatedWorld', {389 frameId: frame._id,390 grantUniveralAccess: true,391 worldName: UTILITY_WORLD_NAME392 });393 for (const binding of this._crPage._browserContext._pageBindings.values()) frame.evaluateExpression(binding.source, false, undefined).catch(e => {});394 for (const source of this._crPage._browserContext._evaluateOnNewDocumentSources) frame.evaluateExpression(source, false, undefined, 'main').catch(e => {});395 }396 const isInitialEmptyPage = this._isMainFrame() && this._page.mainFrame().url() === ':';397 if (isInitialEmptyPage) {398 // Ignore lifecycle events for the initial empty page. It is never the final page399 // hence we are going to get more lifecycle updates after the actual navigation has400 // started (even if the target url is about:blank).401 lifecycleEventsEnabled.catch(e => {}).then(() => {402 this._eventListeners.push(_eventsHelper.eventsHelper.addEventListener(this._client, 'Page.lifecycleEvent', event => this._onLifecycleEvent(event)));403 });404 } else {405 this._firstNonInitialNavigationCommittedFulfill();406 this._eventListeners.push(_eventsHelper.eventsHelper.addEventListener(this._client, 'Page.lifecycleEvent', event => this._onLifecycleEvent(event)));407 }408 }), this._client.send('Log.enable', {}), lifecycleEventsEnabled = this._client.send('Page.setLifecycleEventsEnabled', {409 enabled: true410 }), this._client.send('Runtime.enable', {}), this._client.send('Page.addScriptToEvaluateOnNewDocument', {411 source: '',412 worldName: UTILITY_WORLD_NAME413 }), this._networkManager.initialize(), this._client.send('Target.setAutoAttach', {414 autoAttach: true,415 waitForDebuggerOnStart: true,416 flatten: true417 })];418 if (this._isMainFrame()) promises.push(this._client.send('Emulation.setFocusEmulationEnabled', {419 enabled: true420 }));421 const options = this._crPage._browserContext._options;422 if (options.bypassCSP) promises.push(this._client.send('Page.setBypassCSP', {423 enabled: true424 }));425 if (options.ignoreHTTPSErrors) promises.push(this._client.send('Security.setIgnoreCertificateErrors', {426 ignore: true427 }));428 if (this._isMainFrame()) promises.push(this._updateViewport());429 if (options.hasTouch) promises.push(this._client.send('Emulation.setTouchEmulationEnabled', {430 enabled: true431 }));432 if (options.javaScriptEnabled === false) promises.push(this._client.send('Emulation.setScriptExecutionDisabled', {433 value: true434 }));435 if (options.userAgent || options.locale) promises.push(this._client.send('Emulation.setUserAgentOverride', {436 userAgent: options.userAgent || '',437 acceptLanguage: options.locale438 }));439 if (options.locale) promises.push(emulateLocale(this._client, options.locale));440 if (options.timezoneId) promises.push(emulateTimezone(this._client, options.timezoneId));441 promises.push(this._updateGeolocation(true));442 promises.push(this._updateExtraHTTPHeaders(true));443 promises.push(this._updateRequestInterception());444 promises.push(this._updateOffline(true));445 promises.push(this._updateHttpCredentials(true));446 promises.push(this._updateEmulateMedia(true));447 for (const binding of this._crPage._page.allBindings()) promises.push(this._initBinding(binding));448 for (const source of this._crPage._browserContext._evaluateOnNewDocumentSources) promises.push(this._evaluateOnNewDocument(source, 'main'));449 for (const source of this._crPage._page._evaluateOnNewDocumentSources) promises.push(this._evaluateOnNewDocument(source, 'main'));450 if (screencastOptions) promises.push(this._startVideoRecording(screencastOptions));451 promises.push(this._client.send('Runtime.runIfWaitingForDebugger'));452 promises.push(this._firstNonInitialNavigationCommittedPromise);453 await Promise.all(promises);454 }455 dispose() {456 _eventsHelper.eventsHelper.removeEventListeners(this._eventListeners);457 this._networkManager.dispose();458 this._crPage._sessions.delete(this._targetId);459 }460 async _navigate(frame, url, referrer) {461 const response = await this._client.send('Page.navigate', {462 url,463 referrer,464 frameId: frame._id465 });466 if (response.errorText) throw new Error(`${response.errorText} at ${url}`);467 return {468 newDocumentId: response.loaderId469 };470 }471 _onLifecycleEvent(event) {472 if (this._eventBelongsToStaleFrame(event.frameId)) return;473 if (event.name === 'load') this._page._frameManager.frameLifecycleEvent(event.frameId, 'load');else if (event.name === 'DOMContentLoaded') this._page._frameManager.frameLifecycleEvent(event.frameId, 'domcontentloaded');474 }475 _onFrameStoppedLoading(frameId) {476 if (this._eventBelongsToStaleFrame(frameId)) return;477 this._page._frameManager.frameStoppedLoading(frameId);478 }479 _handleFrameTree(frameTree) {480 this._onFrameAttached(frameTree.frame.id, frameTree.frame.parentId || null);481 this._onFrameNavigated(frameTree.frame, true);482 if (!frameTree.childFrames) return;483 for (const child of frameTree.childFrames) this._handleFrameTree(child);484 }485 _eventBelongsToStaleFrame(frameId) {486 const frame = this._page._frameManager.frame(frameId); // Subtree may be already gone because some ancestor navigation destroyed the oopif.487 if (!frame) return true; // When frame goes remote, parent process may still send some events488 // related to the local frame before it sends frameDetached.489 // In this case, we already have a new session for this frame, so events490 // in the old session should be ignored.491 const session = this._crPage._sessionForFrame(frame);492 return session && session !== this && !session._swappedIn;493 }494 _onFrameAttached(frameId, parentFrameId) {495 const frameSession = this._crPage._sessions.get(frameId);496 if (frameSession && frameId !== this._targetId) {497 // This is a remote -> local frame transition.498 frameSession._swappedIn = true;499 const frame = this._page._frameManager.frame(frameId); // Frame or even a whole subtree may be already gone, because some ancestor did navigate.500 if (frame) this._page._frameManager.removeChildFramesRecursively(frame);501 return;502 }503 if (parentFrameId && !this._page._frameManager.frame(parentFrameId)) {504 // Parent frame may be gone already because some ancestor frame navigated and505 // destroyed the whole subtree of some oopif, while oopif's process is still sending us events.506 // Be careful to not confuse this with "main frame navigated cross-process" scenario507 // where parentFrameId is null.508 return;509 }510 this._page._frameManager.frameAttached(frameId, parentFrameId);511 }512 _onFrameNavigated(framePayload, initial) {513 if (this._eventBelongsToStaleFrame(framePayload.id)) return;514 this._page._frameManager.frameCommittedNewDocumentNavigation(framePayload.id, framePayload.url + (framePayload.urlFragment || ''), framePayload.name || '', framePayload.loaderId, initial);515 if (!initial) this._firstNonInitialNavigationCommittedFulfill();516 }517 _onFrameRequestedNavigation(payload) {518 if (this._eventBelongsToStaleFrame(payload.frameId)) return;519 if (payload.disposition === 'currentTab') this._page._frameManager.frameRequestedNavigation(payload.frameId);520 }521 _onFrameNavigatedWithinDocument(frameId, url) {522 if (this._eventBelongsToStaleFrame(frameId)) return;523 this._page._frameManager.frameCommittedSameDocumentNavigation(frameId, url);524 }525 _onFrameDetached(frameId, reason) {526 if (this._crPage._sessions.has(frameId)) {527 // This is a local -> remote frame transtion, where528 // Page.frameDetached arrives after Target.attachedToTarget.529 // We've already handled the new target and frame reattach - nothing to do here.530 return;531 }532 if (reason === 'swap') {533 // This is a local -> remote frame transtion, where534 // Page.frameDetached arrives before Target.attachedToTarget.535 // We should keep the frame in the tree, and it will be used for the new target.536 const frame = this._page._frameManager.frame(frameId);537 if (frame) this._page._frameManager.removeChildFramesRecursively(frame);538 return;539 } // Just a regular frame detach.540 this._page._frameManager.frameDetached(frameId);541 }542 _onExecutionContextCreated(contextPayload) {543 const frame = contextPayload.auxData ? this._page._frameManager.frame(contextPayload.auxData.frameId) : null;544 if (!frame || this._eventBelongsToStaleFrame(frame._id)) return;545 const delegate = new _crExecutionContext.CRExecutionContext(this._client, contextPayload);546 let worldName = null;547 if (contextPayload.auxData && !!contextPayload.auxData.isDefault) worldName = 'main';else if (contextPayload.name === UTILITY_WORLD_NAME) worldName = 'utility';548 const context = new dom.FrameExecutionContext(delegate, frame, worldName);549 context[contextDelegateSymbol] = delegate;550 if (worldName) frame._contextCreated(worldName, context);551 this._contextIdToContext.set(contextPayload.id, context);552 }553 _onExecutionContextDestroyed(executionContextId) {554 const context = this._contextIdToContext.get(executionContextId);555 if (!context) return;556 this._contextIdToContext.delete(executionContextId);557 context.frame._contextDestroyed(context);558 }559 _onExecutionContextsCleared() {560 for (const contextId of Array.from(this._contextIdToContext.keys())) this._onExecutionContextDestroyed(contextId);561 }562 _onAttachedToTarget(event) {563 const session = _crConnection.CRConnection.fromSession(this._client).session(event.sessionId);564 if (event.targetInfo.type === 'iframe') {565 // Frame id equals target id.566 const targetId = event.targetInfo.targetId;567 const frame = this._page._frameManager.frame(targetId);568 if (!frame) return; // Subtree may be already gone due to renderer/browser race.569 this._page._frameManager.removeChildFramesRecursively(frame);570 const frameSession = new FrameSession(this._crPage, session, targetId, this);571 this._crPage._sessions.set(targetId, frameSession);572 frameSession._initialize(false).catch(e => e);573 return;574 }575 if (event.targetInfo.type !== 'worker') {576 // Ideally, detaching should resume any target, but there is a bug in the backend.577 session._sendMayFail('Runtime.runIfWaitingForDebugger').then(() => {578 this._client._sendMayFail('Target.detachFromTarget', {579 sessionId: event.sessionId580 });581 });582 return;583 }584 const url = event.targetInfo.url;585 const worker = new _page.Worker(this._page, url);586 this._page._addWorker(event.sessionId, worker);587 session.once('Runtime.executionContextCreated', async event => {588 worker._createExecutionContext(new _crExecutionContext.CRExecutionContext(session, event.context));589 }); // This might fail if the target is closed before we initialize.590 session._sendMayFail('Runtime.enable');591 session._sendMayFail('Network.enable');592 session._sendMayFail('Runtime.runIfWaitingForDebugger');593 session.on('Runtime.consoleAPICalled', event => {594 const args = event.args.map(o => worker._existingExecutionContext.createHandle(o));595 this._page._addConsoleMessage(event.type, args, (0, _crProtocolHelper.toConsoleMessageLocation)(event.stackTrace));596 });597 session.on('Runtime.exceptionThrown', exception => this._page.emit(_page.Page.Events.PageError, (0, _crProtocolHelper.exceptionToError)(exception.exceptionDetails))); // TODO: attribute workers to the right frame.598 this._networkManager.instrumentNetworkEvents(session, this._page._frameManager.frame(this._targetId));599 }600 _onDetachedFromTarget(event) {601 // This might be a worker...602 this._page._removeWorker(event.sessionId); // ... or an oopif.603 const childFrameSession = this._crPage._sessions.get(event.targetId);604 if (!childFrameSession) return; // Usually, we get frameAttached in this session first and mark child as swappedIn.605 if (childFrameSession._swappedIn) {606 childFrameSession.dispose();607 return;608 } // However, sometimes we get detachedFromTarget before frameAttached.609 // In this case we don't know wheter this is a remote frame detach,610 // or just a remote -> local transition. In the latter case, frameAttached611 // is already inflight, so let's make a safe roundtrip to ensure it arrives.612 this._client.send('Page.enable').catch(e => null).then(() => {613 // Child was not swapped in - that means frameAttached did not happen and614 // this is remote detach rather than remote -> local swap.615 if (!childFrameSession._swappedIn) this._page._frameManager.frameDetached(event.targetId);616 childFrameSession.dispose();617 });618 }619 _onWindowOpen(event) {620 this._crPage._nextWindowOpenPopupFeatures.push(event.windowFeatures);621 }622 async _onConsoleAPI(event) {623 if (event.executionContextId === 0) {624 // DevTools protocol stores the last 1000 console messages. These625 // messages are always reported even for removed execution contexts. In626 // this case, they are marked with executionContextId = 0 and are627 // reported upon enabling Runtime agent.628 //629 // Ignore these messages since:630 // - there's no execution context we can use to operate with message631 // arguments632 // - these messages are reported before Playwright clients can subscribe633 // to the 'console'634 // page event.635 //636 // @see https://github.com/GoogleChrome/puppeteer/issues/3865637 return;638 }639 const context = this._contextIdToContext.get(event.executionContextId);640 if (!context) return;641 const values = event.args.map(arg => context.createHandle(arg));642 this._page._addConsoleMessage(event.type, values, (0, _crProtocolHelper.toConsoleMessageLocation)(event.stackTrace));643 }644 async _initBinding(binding) {645 await Promise.all([this._client.send('Runtime.addBinding', {646 name: binding.name647 }), this._client.send('Page.addScriptToEvaluateOnNewDocument', {648 source: binding.source649 })]);650 }651 async _onBindingCalled(event) {652 const pageOrError = await this._crPage.pageOrError();653 if (!(pageOrError instanceof Error)) {654 const context = this._contextIdToContext.get(event.executionContextId);655 if (context) await this._page._onBindingCalled(event.payload, context);656 }657 }658 _onDialog(event) {659 if (!this._page._frameManager.frame(this._targetId)) return; // Our frame/subtree may be gone already.660 this._page.emit(_page.Page.Events.Dialog, new dialog.Dialog(this._page, event.type, event.message, async (accept, promptText) => {661 await this._client.send('Page.handleJavaScriptDialog', {662 accept,663 promptText664 });665 }, event.defaultPrompt));666 }667 _handleException(exceptionDetails) {668 this._page.firePageError((0, _crProtocolHelper.exceptionToError)(exceptionDetails));669 }670 async _onTargetCrashed() {671 this._client._markAsCrashed();672 this._page._didCrash();673 }674 _onLogEntryAdded(event) {675 const {676 level,677 text,678 args,679 source,680 url,681 lineNumber682 } = event.entry;683 if (args) args.map(arg => (0, _crProtocolHelper.releaseObject)(this._client, arg.objectId));684 if (source !== 'worker') {685 const location = {686 url: url || '',687 lineNumber: lineNumber || 0,688 columnNumber: 0689 };690 this._page._addConsoleMessage(level, [], location, text);691 }692 }693 async _onFileChooserOpened(event) {694 const frame = this._page._frameManager.frame(event.frameId);695 if (!frame) return;696 let handle;697 try {698 const utilityContext = await frame._utilityContext();699 handle = await this._adoptBackendNodeId(event.backendNodeId, utilityContext);700 } catch (e) {701 // During async processing, frame/context may go away. We should not throw.702 return;703 }704 await this._page._onFileChooserOpened(handle);705 }706 _willBeginDownload() {707 const originPage = this._crPage._initializedPage;708 if (!originPage) {709 // Resume the page creation with an error. The page will automatically close right710 // after the download begins.711 this._firstNonInitialNavigationCommittedReject(new Error('Starting new page download'));712 }713 }714 _onScreencastFrame(payload) {715 this._page.throttleScreencastFrameAck(() => {716 this._client.send('Page.screencastFrameAck', {717 sessionId: payload.sessionId718 }).catch(() => {});719 });720 const buffer = Buffer.from(payload.data, 'base64');721 this._page.emit(_page.Page.Events.ScreencastFrame, {722 buffer,723 timestamp: payload.metadata.timestamp,724 width: payload.metadata.deviceWidth,725 height: payload.metadata.deviceHeight726 });727 }728 async _createVideoRecorder(screencastId, options) {729 (0, _utils.assert)(!this._screencastId);730 const ffmpegPath = _registry.registry.findExecutable('ffmpeg').executablePathOrDie(this._page._browserContext._browser.options.sdkLanguage);731 this._videoRecorder = await _videoRecorder.VideoRecorder.launch(this._crPage._page, ffmpegPath, options);732 this._screencastId = screencastId;733 }734 async _startVideoRecording(options) {735 const screencastId = this._screencastId;736 (0, _utils.assert)(screencastId);737 this._page.once(_page.Page.Events.Close, () => this._stopVideoRecording().catch(() => {}));738 const gotFirstFrame = new Promise(f => this._client.once('Page.screencastFrame', f));739 await this._startScreencast(this._videoRecorder, {740 format: 'jpeg',741 quality: 90,742 maxWidth: options.width,743 maxHeight: options.height744 }); // Wait for the first frame before reporting video to the client.745 gotFirstFrame.then(() => {746 this._crPage._browserContext._browser._videoStarted(this._crPage._browserContext, screencastId, options.outputFile, this._crPage.pageOrError());747 });748 }749 async _stopVideoRecording() {750 if (!this._screencastId) return;751 const screencastId = this._screencastId;752 this._screencastId = null;753 const recorder = this._videoRecorder;754 this._videoRecorder = null;755 await this._stopScreencast(recorder);756 await recorder.stop().catch(() => {}); // Keep the video artifact in the map utntil encoding is fully finished, if the context757 // starts closing before the video is fully written to disk it will wait for it.758 const video = this._crPage._browserContext._browser._takeVideo(screencastId);759 video === null || video === void 0 ? void 0 : video.reportFinished();760 }761 async _startScreencast(client, options = {}) {762 this._screencastClients.add(client);763 if (this._screencastClients.size === 1) await this._client.send('Page.startScreencast', options);764 }765 async _stopScreencast(client) {766 this._screencastClients.delete(client);767 if (!this._screencastClients.size) await this._client._sendMayFail('Page.stopScreencast');768 }769 async _updateExtraHTTPHeaders(initial) {770 const headers = network.mergeHeaders([this._crPage._browserContext._options.extraHTTPHeaders, this._page._state.extraHTTPHeaders]);771 if (!initial || headers.length) await this._client.send('Network.setExtraHTTPHeaders', {772 headers: (0, _utils.headersArrayToObject)(headers, false773 /* lowerCase */774 )775 });776 }777 async _updateGeolocation(initial) {778 const geolocation = this._crPage._browserContext._options.geolocation;779 if (!initial || geolocation) await this._client.send('Emulation.setGeolocationOverride', geolocation || {});780 }781 async _updateOffline(initial) {782 const offline = !!this._crPage._browserContext._options.offline;783 if (!initial || offline) await this._networkManager.setOffline(offline);784 }785 async _updateHttpCredentials(initial) {786 const credentials = this._crPage._browserContext._options.httpCredentials || null;787 if (!initial || credentials) await this._networkManager.authenticate(credentials);788 }789 async _updateViewport() {790 if (this._crPage._browserContext._browser.isClank()) return;791 (0, _utils.assert)(this._isMainFrame());792 const options = this._crPage._browserContext._options;793 const emulatedSize = this._page._state.emulatedSize;794 if (emulatedSize === null) return;795 const viewportSize = emulatedSize.viewport;796 const screenSize = emulatedSize.screen;797 const isLandscape = viewportSize.width > viewportSize.height;798 const promises = [this._client.send('Emulation.setDeviceMetricsOverride', {799 mobile: !!options.isMobile,800 width: viewportSize.width,801 height: viewportSize.height,802 screenWidth: screenSize.width,803 screenHeight: screenSize.height,804 deviceScaleFactor: options.deviceScaleFactor || 1,805 screenOrientation: isLandscape ? {806 angle: 90,807 type: 'landscapePrimary'808 } : {809 angle: 0,810 type: 'portraitPrimary'811 }812 })];813 if (this._windowId) {814 let insets = {815 width: 0,816 height: 0817 };818 if (this._crPage._browserContext._browser.options.headful) {819 // TODO: popup windows have their own insets.820 insets = {821 width: 24,822 height: 88823 };824 if (process.platform === 'win32') insets = {825 width: 16,826 height: 88827 };else if (process.platform === 'linux') insets = {828 width: 8,829 height: 85830 };else if (process.platform === 'darwin') insets = {831 width: 2,832 height: 80833 };834 if (this._crPage._browserContext.isPersistentContext()) {835 // FIXME: Chrome bug: OOPIF router is confused when hit target is836 // outside browser window.837 // Account for the infobar here to work around the bug.838 insets.height += 46;839 }840 }841 promises.push(this.setWindowBounds({842 width: viewportSize.width + insets.width,843 height: viewportSize.height + insets.height844 }));845 }846 await Promise.all(promises);847 }848 async windowBounds() {849 const {850 bounds851 } = await this._client.send('Browser.getWindowBounds', {852 windowId: this._windowId853 });854 return bounds;855 }856 async setWindowBounds(bounds) {857 return await this._client.send('Browser.setWindowBounds', {858 windowId: this._windowId,859 bounds860 });861 }862 async _updateEmulateMedia(initial) {863 if (this._crPage._browserContext._browser.isClank()) return;864 const colorScheme = this._page._state.colorScheme === null ? '' : this._page._state.colorScheme;865 const reducedMotion = this._page._state.reducedMotion === null ? '' : this._page._state.reducedMotion;866 const forcedColors = this._page._state.forcedColors === null ? '' : this._page._state.forcedColors;867 const features = [{868 name: 'prefers-color-scheme',869 value: colorScheme870 }, {871 name: 'prefers-reduced-motion',872 value: reducedMotion873 }, {874 name: 'forced-colors',875 value: forcedColors876 }]; // Empty string disables the override.877 await this._client.send('Emulation.setEmulatedMedia', {878 media: this._page._state.mediaType || '',879 features880 });881 }882 async _updateRequestInterception() {883 await this._networkManager.setRequestInterception(this._page._needsRequestInterception());884 }885 async _setFileChooserIntercepted(enabled) {886 await this._client.send('Page.setInterceptFileChooserDialog', {887 enabled888 }).catch(e => {}); // target can be closed.889 }890 async _evaluateOnNewDocument(source, world) {891 const worldName = world === 'utility' ? UTILITY_WORLD_NAME : undefined;892 await this._client.send('Page.addScriptToEvaluateOnNewDocument', {893 source,894 worldName895 });896 }897 async _getContentFrame(handle) {898 const nodeInfo = await this._client.send('DOM.describeNode', {899 objectId: handle._objectId900 });901 if (!nodeInfo || typeof nodeInfo.node.frameId !== 'string') return null;902 return this._page._frameManager.frame(nodeInfo.node.frameId);903 }904 async _getOwnerFrame(handle) {905 // document.documentElement has frameId of the owner frame.906 const documentElement = await handle.evaluateHandle(node => {907 const doc = node;908 if (doc.documentElement && doc.documentElement.ownerDocument === doc) return doc.documentElement;909 return node.ownerDocument ? node.ownerDocument.documentElement : null;910 });911 if (!documentElement) return null;912 if (!documentElement._objectId) return null;913 const nodeInfo = await this._client.send('DOM.describeNode', {914 objectId: documentElement._objectId915 });916 const frameId = nodeInfo && typeof nodeInfo.node.frameId === 'string' ? nodeInfo.node.frameId : null;917 documentElement.dispose();918 return frameId;919 }920 async _getBoundingBox(handle) {921 const result = await this._client._sendMayFail('DOM.getBoxModel', {922 objectId: handle._objectId923 });924 if (!result) return null;925 const quad = result.model.border;926 const x = Math.min(quad[0], quad[2], quad[4], quad[6]);927 const y = Math.min(quad[1], quad[3], quad[5], quad[7]);928 const width = Math.max(quad[0], quad[2], quad[4], quad[6]) - x;929 const height = Math.max(quad[1], quad[3], quad[5], quad[7]) - y;930 const position = await this._framePosition();931 if (!position) return null;932 return {933 x: x + position.x,934 y: y + position.y,935 width,936 height937 };938 }939 async _framePosition() {940 const frame = this._page._frameManager.frame(this._targetId);941 if (!frame) return null;942 if (frame === this._page.mainFrame()) return {943 x: 0,944 y: 0945 };946 const element = await frame.frameElement();947 const box = await element.boundingBox();948 return box;949 }950 async _scrollRectIntoViewIfNeeded(handle, rect) {951 return await this._client.send('DOM.scrollIntoViewIfNeeded', {952 objectId: handle._objectId,953 rect954 }).then(() => 'done').catch(e => {955 if (e instanceof Error && e.message.includes('Node does not have a layout object')) return 'error:notvisible';956 if (e instanceof Error && e.message.includes('Node is detached from document')) return 'error:notconnected';957 throw e;958 });959 }960 async _getContentQuads(handle) {961 const result = await this._client._sendMayFail('DOM.getContentQuads', {962 objectId: handle._objectId963 });964 if (!result) return null;965 const position = await this._framePosition();966 if (!position) return null;967 return result.quads.map(quad => [{968 x: quad[0] + position.x,969 y: quad[1] + position.y970 }, {971 x: quad[2] + position.x,972 y: quad[3] + position.y973 }, {974 x: quad[4] + position.x,975 y: quad[5] + position.y976 }, {977 x: quad[6] + position.x,978 y: quad[7] + position.y979 }]);980 }981 async _adoptElementHandle(handle, to) {982 const nodeInfo = await this._client.send('DOM.describeNode', {983 objectId: handle._objectId984 });985 return this._adoptBackendNodeId(nodeInfo.node.backendNodeId, to);986 }987 async _adoptBackendNodeId(backendNodeId, to) {988 const result = await this._client._sendMayFail('DOM.resolveNode', {989 backendNodeId,990 executionContextId: to[contextDelegateSymbol]._contextId991 });992 if (!result || result.object.subtype === 'null') throw new Error(dom.kUnableToAdoptErrorMessage);993 return to.createHandle(result.object).asElement();994 }995}996async function emulateLocale(session, locale) {997 try {998 await session.send('Emulation.setLocaleOverride', {999 locale1000 });1001 } catch (exception) {1002 // All pages in the same renderer share locale. All such pages belong to the same1003 // context and if locale is overridden for one of them its value is the same as1004 // we are trying to set so it's not a problem.1005 if (exception.message.includes('Another locale override is already in effect')) return;1006 throw exception;1007 }1008}1009async function emulateTimezone(session, timezoneId) {1010 try {1011 await session.send('Emulation.setTimezoneOverride', {1012 timezoneId: timezoneId1013 });1014 } catch (exception) {1015 if (exception.message.includes('Timezone override is already in effect')) return;1016 if (exception.message.includes('Invalid timezone')) throw new Error(`Invalid timezone ID: ${timezoneId}`);1017 throw exception;1018 }1019}...
buildExpected.js
Source:buildExpected.js
1import Expected from '../classes/Expected'2import TypeError from '../classes/TypeError'3import anyToName from './anyToName'4import anyToString from './anyToString'5import fnToSignatureString from './fnToSignatureString'6import functionToParameterNames from './functionToParameterNames'7import sourceToString from './sourceToString'8const prefixNot = (not, expectation) => `${not ? 'not.' : ''}${expectation}`9const targetToString = (target) => `${target.type}:${anyToString(target.value)}`10const toBeEmpty = (next, not = false) => () =>11 next(12 new Expected({13 data: {},14 exceptionToError: (exception) =>15 new TypeError(16 `${sourceToString(exception.source)} expected ${17 not ? 'at least one Parameter' : 'no Parameters'18 }.`19 ),20 expectation: prefixNot(not, 'toBeEmpty')21 })22 )23const toBeInstanceOf = (next, not = false) => (_class) =>24 next(25 new Expected({26 data: {27 class: _class28 },29 exceptionToError: (exception, expected) =>30 new TypeError(31 `${sourceToString(exception.source)} expected ${targetToString(32 exception.target33 )} to ${not ? 'NOT ' : ''}be an instance of ${anyToName(34 expected.data.class35 )}. Instead was given ${targetToString(exception.target)}.`36 )37 })38 )39const toBeOfMinLength = (next, not = false) => (length) =>40 next(41 new Expected({42 data: {43 length44 },45 exceptionToError: (exception, expected) =>46 new TypeError(47 `${sourceToString(exception.source)} expected ${targetToString(48 exception.target49 )} ${not ? 'NOT ' : ''}to be of minimum length ${50 expected.data.length51 }.`52 ),53 expectation: prefixNot(not, 'toBeOfMinLength')54 })55 )56const toHaveParameters = (next, not = false) => () =>57 next(58 new Expected({59 exceptionToError: (exception) => {60 let sentence61 if (exception.source === exception.target.value) {62 sentence = `${sourceToString(exception.source)} was`63 } else {64 sentence = `${sourceToString(65 exception.source66 )} expected ${targetToString(exception.target)}`67 }68 return new TypeError(69 `${sentence} ${not ? 'NOT ' : ''}to have Parameters.`70 )71 },72 expectation: prefixNot(not, 'toHaveParameters')73 })74 )75const toHaveParametersOrDispatcher = (next, not = false) => () =>76 next(77 new Expected({78 exceptionToError: (exception) => {79 let sentence80 if (exception.source === exception.target.value) {81 sentence = `${sourceToString(exception.source)} was`82 } else {83 sentence = `${sourceToString(84 exception.source85 )} expected ${targetToString(exception.target)}`86 }87 return new TypeError(88 `${sentence} ${not ? 'NOT ' : ''}to have Parameters or a Dispatcher.`89 )90 },91 expectation: prefixNot(not, 'toHaveParametersOrDispatcher')92 })93 )94const toMatchDispatcher = (next, not = false) => (dispatcher) => {95 if (!dispatcher) {96 throw new Error('Expected Dispatcher')97 }98 return next(99 new Expected({100 data: {101 dispatcher102 },103 exceptionToError: (exception, expected) => {104 const allFns = expected.data.dispatcher.getAllDispatchableFns()105 if (allFns.isEmpty()) {106 return new TypeError(107 `${sourceToString(exception.source)} expected ${targetToString(108 exception.target109 )} ${110 not ? 'NOT ' : ''111 }to dispatch to at least one Fn. It's possible that no Fns have been defiend for this `112 )113 }114 return new TypeError(115 `${sourceToString(exception.source)} expected ${targetToString(116 exception.target117 )} ${118 not ? 'NOT ' : ''119 }to match one of the following method signatures.\n${allFns120 .map((fn) => `* ${fnToSignatureString(fn)}`)121 .join('\n')}`122 )123 },124 expectation: prefixNot(not, 'toMatchDispatcher')125 })126 )127}128const toMatchParameter = (next, not = false) => (parameter) =>129 next(130 new Expected({131 data: {132 parameter133 },134 exceptionToError: (exception, expected) =>135 new TypeError(136 `${sourceToString(exception.source)} expected ${targetToString(137 exception.target138 )} for Parameter ${anyToName(expected.data.parameter)} to ${139 not ? 'NOT ' : ''140 }be a ${expected.data.parameter.type}. Instead was given ${141 exception.target.value142 }.`143 ),144 expectation: prefixNot(not, 'toMatchParameter')145 })146 )147const toMatchRegex = (next, not = false) => (regex) =>148 next(149 new Expected({150 data: {151 regex152 },153 exceptionToError: (exception, expected) => {154 const parameterName = functionToParameterNames(exception.source)[155 exception.target.index156 ]157 return new TypeError(158 `${sourceToString(exception.source)} expected ${targetToString(159 exception.target160 )} for Parameter '${parameterName}' ${161 not ? 'NOT ' : ''162 }to match regex ${expected.data.regex}.`163 )164 },165 expectation: prefixNot(not, 'toMatchRegex')166 })167 )168const toMatchReturns = (next, not = false) => (returns) =>169 next(170 new Expected({171 data: {172 returns173 },174 exceptionToError: (exception, expected) =>175 new TypeError(176 `${sourceToString(exception.source)} expected ${targetToString(177 exception.target178 )} value to ${not ? 'NOT ' : ''}return a ${anyToName(179 expected.data.returns180 )}. Instead was given ${targetToString(exception.target)}.`181 ),182 expectation: prefixNot(not, 'toMatchReturns')183 })184 )185const toSatisfyProtocol = (next, not = false) => (protocol) =>186 next(187 new Expected({188 data: {189 protocol190 },191 exceptionToError: (exception, expected) =>192 new TypeError(193 `${sourceToString(exception.source)} expected ${targetToString(194 exception.target195 )} to ${not ? 'NOT ' : ''}satisfy the Protocol ${anyToName(196 expected.data.protocol197 )}. Instead was given ${targetToString(exception.target)}.`198 )199 })200 )201const buildExpected = (type, next) => {202 switch (type) {203 case 'Argument':204 return {205 not: {206 toBeInstanceOf: toBeInstanceOf(next, true),207 toMatchParameter: toMatchParameter(next, true),208 toMatchRegex: toMatchRegex(next, true),209 toSatisfyProtocol: toSatisfyProtocol(next, true)210 },211 toBeInstanceOf: toBeInstanceOf(next),212 toMatchParameter: toMatchParameter(next),213 toMatchRegex: toMatchRegex(next),214 toSatisfyProtocol: toSatisfyProtocol(next)215 }216 case 'Arguments':217 return {218 not: {219 toBeEmpty: toBeEmpty(next, true),220 toBeOfMinLength: toBeOfMinLength(next, true),221 toMatchDispatcher: toMatchDispatcher(next, true)222 },223 toBeEmpty: toBeEmpty(next),224 toBeOfMinLength: toBeOfMinLength(next),225 toMatchDispatcher: toMatchDispatcher(next)226 }227 case 'Fn':228 return {229 not: {230 toHaveParameters: toHaveParameters(next, true),231 toHaveParametersOrDispatcher: toHaveParametersOrDispatcher(next, true)232 },233 toHaveParameters: toHaveParameters(next),234 toHaveParametersOrDispatcher: toHaveParametersOrDispatcher(next)235 }236 case 'Returned':237 return {238 not: {239 toMatchReturns: toMatchReturns(next, true)240 },241 toMatchReturns: toMatchReturns(next)242 }243 case 'This':244 return {245 not: {246 toBeInstanceOf: toBeInstanceOf(next, true)247 },248 toBeInstanceOf: toBeInstanceOf(next)249 }250 }251 throw new Error(`Unhandled type '${type}'in buildExpected`)252}...
crProtocolHelper.js
Source:crProtocolHelper.js
...78 lineNumber: 0,79 columnNumber: 080 };81}82function exceptionToError(exceptionDetails) {83 const messageWithStack = getExceptionMessage(exceptionDetails);84 const lines = messageWithStack.split('\n');85 const firstStackTraceLine = lines.findIndex(line => line.startsWith(' at'));86 let messageWithName = '';87 let stack = '';88 if (firstStackTraceLine === -1) {89 messageWithName = messageWithStack;90 } else {91 messageWithName = lines.slice(0, firstStackTraceLine).join('\n');92 stack = messageWithStack;93 }94 const {95 name,96 message...
Expected.test.js
Source:Expected.test.js
1import Exception from './Exception'2import Expected from './Expected'3import anyToStringTag from '../util/anyToStringTag'4describe('js:Expected', () => {5 describe('constructor', () => {6 test('correctly constructs the Type instance', () => {7 const instance = new Expected({8 data: {},9 exceptionToError: () => {},10 expectation: 'foo'11 })12 expect(instance).toBeInstanceOf(Expected)13 expect(instance).toMatchObject({14 data: {},15 expectation: 'foo'16 })17 })18 })19 describe('Symbol.toStringTag', () => {20 test('Returns back a custom StringTag', () => {21 const instance = new Expected({22 data: {},23 exceptionToError: () => {},24 expectation: 'foo'25 })26 expect(anyToStringTag(instance)).toBe('Expected')27 })28 })29 describe('toError', () => {30 test('Returns an Error from the given exception', () => {31 const error = new Error('foo')32 const exceptionToError = jest.fn(() => error)33 const expected = new Expected({34 data: {},35 exceptionToError,36 expectation: 'foo'37 })38 const source = function () {}39 const target = {40 index: 0,41 type: 'Argument',42 value: 'test'43 }44 const exception = new Exception({ expected, source, target })45 const result = expected.toError(exception)46 expect(result).toBe(error)47 expect(exceptionToError).toHaveBeenCalledWith(exception, expected)48 })49 })...
Exception.test.js
Source:Exception.test.js
1import Exception from './Exception'2import Expected from './Expected'3describe('js:Exception', () => {4 describe('constructor', () => {5 test('correctly constructs the Type instance', () => {6 const source = function () {}7 const target = {8 index: 0,9 type: 'Argument',10 value: 'test'11 }12 const expected = new Expected({13 data: {},14 exceptionToError: () => {},15 expectation: 'foo'16 })17 const instance = new Exception({ expected, source, target })18 expect(instance).toBeInstanceOf(Exception)19 expect(instance).toMatchObject({20 expected,21 source,22 target,23 type: 'Expected:Argument:foo'24 })25 })26 })27 describe('toError', () => {28 test('calls the toError method of the expected', () => {29 const source = function () {}30 const target = {31 index: 0,32 type: 'Argument',33 value: 'test'34 }35 const expected = {36 toError: jest.fn(() => new Error('test'))37 }38 const instance = new Exception({ expected, source, target })39 const result = instance.toError()40 expect(result).toBeInstanceOf(Error)41 expect(result.message).toBe('test')42 })43 })...
anyIsException.test.js
Source:anyIsException.test.js
1import { values } from '../../test'2import Exception from '../classes/Exception'3import Expected from '../classes/Expected'4import anyIsException from './anyIsException'5import arrayDifference from './arrayDifference'6describe('anyIsException', () => {7 test('returns true for an Exception', () => {8 const source = function () {}9 const target = {10 index: 0,11 type: 'Argument',12 value: 'test'13 }14 const expected = new Expected({15 data: {},16 exceptionToError: () => {},17 expectation: 'foo'18 })19 expect(anyIsException(new Exception({ expected, source, target }))).toBe(20 true21 )22 })23 test('returns false for all other values', () => {24 expect(anyIsException).toHaveReturnedFalsyForValues(25 arrayDifference(values())26 )27 })...
Expected.js
Source:Expected.js
...13 get [TO_STRING_TAG]() {14 return 'Expected'15 }16 toError(exception) {17 return this.exceptionToError(exception, this)18 }19}...
Using AI Code Generation
1const { exceptionToError } = require('playwright/lib/utils/utils');2const { chromium } = require('playwright');3async function main() {4 try {5 const browser = await chromium.launch();6 const context = await browser.newContext();7 const page = await context.newPage();8 await page.waitForSelector('test');9 } catch (e) {10 console.log(exceptionToError(e));11 }12}13main();
Using AI Code Generation
1const { exceptionToError } = require('playwright-core/lib/utils/utils');2try {3 throw new Error('test');4} catch (e) {5 console.log(exceptionToError(e));6}7 at Object.<anonymous> (/home/shubham/Workspace/playwright/test.js:5:9)8 at Module._compile (internal/modules/cjs/loader.js:1063:30)9 at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)10 at Module.load (internal/modules/cjs/loader.js:928:32)11 at Function.Module._load (internal/modules/cjs/loader.js:769:14)12 at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
Using AI Code Generation
1const { InternalError } = require('playwright/lib/internal/errors');2const err = new InternalError('test');3console.log(InternalError.exceptionToError(err));4const { TimeoutError } = require('playwright/lib/internal/errors');5const err = new TimeoutError('test');6console.log(TimeoutError.exceptionToError(err));7const { PageError } = require('playwright/lib/internal/errors');8const err = new PageError('test');9console.log(PageError.exceptionToError(err));10const { BrowserError } = require('playwright/lib/internal/errors');11const err = new BrowserError('test');12console.log(BrowserError.exceptionToError(err));13const { NetworkError } = require('playwright/lib/internal/errors');14const err = new NetworkError('test');15console.log(NetworkError.exceptionToError(err));16const { ProtocolError } = require('playwright/lib/internal/errors');17const err = new ProtocolError('test');18console.log(ProtocolError.exceptionToError(err));19const { Error } = require('playwright/lib/internal/errors');20const err = new Error('test');21console.log(Error.exceptionToError(err));22const { Error } = require('playwright/lib/internal/errors');23const err = new Error('test');24console.log(Error.exceptionToError(err));25const { Error } = require('playwright/lib/internal/errors');26const err = new Error('test');27console.log(Error.exceptionToError(err));28const { Error } = require('playwright/lib/internal/errors');29const err = new Error('test');30console.log(Error.exceptionToError(err));31const { Error } = require('playwright/lib/internal/errors');32const err = new Error('test');33console.log(Error.exceptionToError(err));34const { Error } = require('play
Using AI Code Generation
1const { exceptionToError } = require('playwright/lib/server/utils/stackTrace');2const err = new Error('test error');3const internalErr = new InternalError('test internal error', err);4console.log(exceptionToError(internalErr));5const { exceptionToError } = require('playwright/lib/server/utils/stackTrace');6const err = new Error('test error');7const internalErr = new Error('test internal error');8console.log(exceptionToError(internalErr));9const { exceptionToError } = require('playwright/lib/server/utils/stackTrace');10const err = new Error('test error');11const internalErr = new Error('test internal error');12console.log(exceptionToError(internalErr));13const { exceptionToError } = require('playwright/lib/server/utils/stackTrace');14const err = new Error('test error');15const internalErr = new Error('test internal error');16console.log(exceptionToError(internalErr));17const { exceptionToError } = require('playwright/lib/server/utils/stackTrace');18const err = new Error('test error');19const internalErr = new Error('test internal error');20console.log(exceptionToError(internalErr));21const { exceptionToError } = require('playwright/lib/server/utils/stackTrace');22const err = new Error('test error');23const internalErr = new Error('test internal error');24console.log(exceptionToError(internalErr));25const { exceptionToError } = require('playwright/lib/server/utils/stackTrace');26const err = new Error('test error');27const internalErr = new Error('test internal error');28console.log(exceptionToError(internalErr));29const { exceptionToError } = require('playwright/lib/server/utils/stackTrace');30const err = new Error('test error');31const internalErr = new Error('test internal error');32console.log(exceptionToError(internalErr));33const { exceptionToError } = require('playwright/lib/server/utils/stackTrace');
Using AI Code Generation
1const { InternalError } = require('playwright/lib/utils/stackTrace');2let error = new InternalError('Error message');3let err = error.exceptionToError();4console.log(err);5const { InternalError } = require('playwright/lib/utils/stackTrace');6let error = new InternalError('Error message');7let err = error.exceptionToError();8console.log(err);9const { InternalError } = require('playwright/lib/utils/stackTrace');10let error = new InternalError('Error message');11let err = error.exceptionToError();12console.log(err);13const { InternalError } = require('playwright/lib/utils/stackTrace');14let error = new InternalError('Error message');15let err = error.exceptionToError();16console.log(err);17const { InternalError } = require('playwright/lib/utils/stackTrace');18let error = new InternalError('Error message');19let err = error.exceptionToError();20console.log(err);21const { InternalError } = require('playwright/lib/utils/stackTrace');22let error = new InternalError('Error message');23let err = error.exceptionToError();24console.log(err);25const { InternalError } = require('playwright/lib/utils/stackTrace');26let error = new InternalError('Error message');27let err = error.exceptionToError();28console.log(err);29const { InternalError } = require('playwright/lib/utils/stackTrace');30let error = new InternalError('Error message');31let err = error.exceptionToError();32console.log(err);33const { InternalError } = require('playwright/lib/utils/stackTrace');34let error = new InternalError('Error
Using AI Code Generation
1const { InternalError } = require('playwright/lib/server/errors');2const err = new InternalError('test');3const err2 = new Error('test2');4const err3 = new InternalError(err2);5console.log(err);6console.log(err2);7console.log(err3);8console.log(InternalError.exceptionToError(err));9console.log(InternalError.exceptionToError(err2));10console.log(InternalError.exceptionToError(err3));11 at Object.<anonymous> (test.js:6:13)12 at Module._compile (internal/modules/cjs/loader.js:999:30)13 at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)14 at Module.load (internal/modules/cjs/loader.js:863:32)15 at Function.Module._load (internal/modules/cjs/loader.js:708:14)16 at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)17 at Object.<anonymous> (test.js:7:13)18 at Module._compile (internal/modules/cjs/loader.js:999:30)19 at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)20 at Module.load (internal/modules/cjs/loader.js:863:32)21 at Function.Module._load (internal/modules/cjs/loader.js:708:14)22 at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)23 at Object.<anonymous> (test.js:8:13)24 at Module._compile (internal/modules/cjs/loader.js:999:30)25 at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)26 at Module.load (internal/modules/cjs/loader.js:863:32)27 at Function.Module._load (internal/modules/cjs/loader.js:708:14)28 at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)29 at Object.<anonymous> (test.js:11:13)30 at Module._compile (internal
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!