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}...crProtocolHelper.js
Source:crProtocolHelper.js  
...67    handle68  });69  return Buffer.concat(bufs);70}71function toConsoleMessageLocation(stackTrace) {72  return stackTrace && stackTrace.callFrames.length ? {73    url: stackTrace.callFrames[0].url,74    lineNumber: stackTrace.callFrames[0].lineNumber,75    columnNumber: stackTrace.callFrames[0].columnNumber76  } : {77    url: '',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'));...Using AI Code Generation
1const { toConsoleMessageLocation } = require('playwright/lib/server/inspector/inspector');2const { ConsoleMessage } = require('playwright/lib/server/chromium/crConsole');3const { helper } = require('playwright/lib/server/helper');4const message = new ConsoleMessage(helper.debugProtocolLogger(), {5  args: [{ type: 'string', value: 'hello' }],6  location: {7  },8});9console.log(toConsoleMessageLocation(message));Using AI Code Generation
1const { toConsoleMessageLocation } = require('playwright-core/lib/server/inspector/inspector');2const { ConsoleMessage } = require('playwright-core/lib/server/chromium/crConsole');3const { ConsoleMessageLocation } = require('playwright-core/lib/server/common/consoleMessage');4const message = new ConsoleMessage('log', 'test message', [], location);5console.log(message.location().url());6console.log(message.location().lineNumber());7console.log(message.location().columnNumber());8const { toConsoleMessageLocation } = require('playwright-core/lib/server/inspector/inspector');9const { ConsoleMessage } = require('playwright-core/lib/server/chromium/crConsole');10const { ConsoleMessageLocation } = require('playwright-core/lib/server/common/consoleMessage');11const message = new ConsoleMessage('log', 'test message', [], location);12console.log(message.location().url());13console.log(message.location().lineNumber());14console.log(message.location().columnNumber());15const { toConsoleMessageLocation } = require('playwright-core/lib/server/inspector/inspector');16const { ConsoleMessage } = require('playwright-core/lib/server/chromium/crConsole');17const { ConsoleMessageLocation } = require('playwright-core/lib/server/common/consoleMessage');18const message = new ConsoleMessage('log', 'test message', [], location);19console.log(message.location().url());20console.log(message.location().lineNumber());21console.log(message.location().columnNumber());Using AI Code Generation
1const { InternalLogger } = require('playwright/lib/utils/logger');2InternalLogger.prototype.toConsoleMessageLocation = function (location) {3  if (!location)4    return '';5  return `${location.url}:${location.lineNumber}:${location.columnNumber}`;6};7const { ConsoleLogger } = require('playwright/lib/utils/logger');8ConsoleLogger.prototype.toConsoleMessageLocation = function (location) {9  if (!location)10    return '';11  return `${location.url}:${location.lineNumber}:${location.columnNumber}`;12};13const { chromium } = require('playwright');14(async () => {15  const browser = await chromium.launch();16  const context = await browser.newContext();17  const page = await context.newPage();18  await page.screenshot({ path: `example.png` });19  await browser.close();20})();21const { chromium } = require('playwright');22(async () => {23  const browser = await chromium.launch();24  const context = await browser.newContext();25  const page = await context.newPage();26  await page.screenshot({ path: `example.png` });27  await browser.close();28})();Using AI Code Generation
1const { toConsoleMessageLocation } = require('playwright/lib/server/chromium/crConsole');2const consoleMessage = {3  location: {4  },5};6const location = toConsoleMessageLocation(consoleMessage);7console.log(location);8const { toConsoleMessageLocation } = require('playwright/lib/server/chromium/crConsole');9const consoleMessage = {10  location: {11  },12};13const location = toConsoleMessageLocation(consoleMessage);14console.log(location);15const { toConsoleMessageLocation } = require('playwright/lib/server/chromium/crConsole');16const consoleMessage = {17  location: {18  },19};20const location = toConsoleMessageLocation(consoleMessage);21console.log(location);22const { toConsoleMessageLocation } = require('playwright/lib/server/chromium/crConsole');23const consoleMessage = {24  location: {25  },26};27const location = toConsoleMessageLocation(consoleMessage);28console.log(location);29const { toConsoleMessageLocation } = requireUsing AI Code Generation
1const { chromium, firefox, webkit } = require('playwright');2const { InternalLogger } = require('playwright/lib/server/logger');3const logger = new InternalLogger('test');4logger.toConsoleMessageLocation = (message, location) => {5    return `${message} at ${location.lineNumber}:${location.columnNumber}`;6};7(async () => {8    const browser = await chromium.launch();9    const page = await browser.newPage();10    await browser.close();11})();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!!
