How to use newBrowserCDPSession method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

browserDispatcher.js

Source: browserDispatcher.js Github

copy
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.ConnectedBrowserDispatcher = exports.BrowserDispatcher = void 0;
7
8var _browser = require("../server/browser");
9
10var _browserContextDispatcher = require("./browserContextDispatcher");
11
12var _cdpSessionDispatcher = require("./cdpSessionDispatcher");
13
14var _dispatcher = require("./dispatcher");
15
16var _instrumentation = require("../server/instrumentation");
17
18var _browserContext = require("../server/browserContext");
19
20var _selectors = require("../server/selectors");
21
22/**
23 * Copyright (c) Microsoft Corporation.
24 *
25 * Licensed under the Apache License, Version 2.0 (the 'License");
26 * you may not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
36 */
37class BrowserDispatcher extends _dispatcher.Dispatcher {
38  constructor(scope, browser) {
39    super(scope, browser, 'Browser', {
40      version: browser.version(),
41      name: browser.options.name
42    }, true);
43    this._type_Browser = true;
44    browser.on(_browser.Browser.Events.Disconnected, () => this._didClose());
45  }
46
47  _didClose() {
48    this._dispatchEvent('close');
49
50    this._dispose();
51  }
52
53  async newContext(params, metadata) {
54    const context = await this._object.newContext(params);
55    if (params.storageState) await context.setStorageState(metadata, params.storageState);
56    return {
57      context: new _browserContextDispatcher.BrowserContextDispatcher(this._scope, context)
58    };
59  }
60
61  async close() {
62    await this._object.close();
63  }
64
65  async killForTests() {
66    await this._object.killForTests();
67  }
68
69  async newBrowserCDPSession() {
70    if (!this._object.options.isChromium) throw new Error(`CDP session is only available in Chromium`);
71    const crBrowser = this._object;
72    return {
73      session: new _cdpSessionDispatcher.CDPSessionDispatcher(this._scope, await crBrowser.newBrowserCDPSession())
74    };
75  }
76
77  async startTracing(params) {
78    if (!this._object.options.isChromium) throw new Error(`Tracing is only available in Chromium`);
79    const crBrowser = this._object;
80    await crBrowser.startTracing(params.page ? params.page._object : undefined, params);
81  }
82
83  async stopTracing() {
84    if (!this._object.options.isChromium) throw new Error(`Tracing is only available in Chromium`);
85    const crBrowser = this._object;
86    const buffer = await crBrowser.stopTracing();
87    return {
88      binary: buffer.toString('base64')
89    };
90  }
91
92} // This class implements multiplexing browser dispatchers over a single Browser instance.
93
94
95exports.BrowserDispatcher = BrowserDispatcher;
96
97class ConnectedBrowserDispatcher extends _dispatcher.Dispatcher {
98  constructor(scope, browser) {
99    super(scope, browser, 'Browser', {
100      version: browser.version(),
101      name: browser.options.name
102    }, true); // When we have a remotely-connected browser, each client gets a fresh Selector instance,
103    // so that two clients do not interfere between each other.
104
105    this._type_Browser = true;
106    this._contexts = new Set();
107    this.selectors = void 0;
108    this.selectors = new _selectors.Selectors();
109  }
110
111  async newContext(params, metadata) {
112    if (params.recordVideo) params.recordVideo.dir = this._object.options.artifactsDir;
113    const context = await this._object.newContext(params);
114
115    this._contexts.add(context);
116
117    context._setSelectors(this.selectors);
118
119    context.on(_browserContext.BrowserContext.Events.Close, () => this._contexts.delete(context));
120    if (params.storageState) await context.setStorageState(metadata, params.storageState);
121    return {
122      context: new _browserContextDispatcher.BrowserContextDispatcher(this._scope, context)
123    };
124  }
125
126  async close() {// Client should not send us Browser.close.
127  }
128
129  async killForTests() {// Client should not send us Browser.killForTests.
130  }
131
132  async newBrowserCDPSession() {
133    if (!this._object.options.isChromium) throw new Error(`CDP session is only available in Chromium`);
134    const crBrowser = this._object;
135    return {
136      session: new _cdpSessionDispatcher.CDPSessionDispatcher(this._scope, await crBrowser.newBrowserCDPSession())
137    };
138  }
139
140  async startTracing(params) {
141    if (!this._object.options.isChromium) throw new Error(`Tracing is only available in Chromium`);
142    const crBrowser = this._object;
143    await crBrowser.startTracing(params.page ? params.page._object : undefined, params);
144  }
145
146  async stopTracing() {
147    if (!this._object.options.isChromium) throw new Error(`Tracing is only available in Chromium`);
148    const crBrowser = this._object;
149    const buffer = await crBrowser.stopTracing();
150    return {
151      binary: buffer.toString('base64')
152    };
153  }
154
155  async cleanupContexts() {
156    await Promise.all(Array.from(this._contexts).map(context => context.close((0, _instrumentation.internalCallMetadata)())));
157  }
158
159}
160
161exports.ConnectedBrowserDispatcher = ConnectedBrowserDispatcher;
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)