Best JavaScript code snippet using playwright-internal
ffBrowser.js
Source:ffBrowser.js
...39 if (options.persistent) {40 browser._defaultContext = new FFBrowserContext(browser, undefined, options.persistent);41 promises.push(browser._defaultContext._initialize());42 }43 if (options.proxy) promises.push(browser._connection.send('Browser.setBrowserProxy', toJugglerProxyOptions(options.proxy)));44 await Promise.all(promises);45 return browser;46 }47 constructor(connection, options) {48 super(options);49 this._connection = void 0;50 this._ffPages = void 0;51 this._contexts = void 0;52 this._version = '';53 this._userAgent = '';54 this._connection = connection;55 this._ffPages = new Map();56 this._contexts = new Map();57 this._connection.on(_ffConnection.ConnectionEvents.Disconnected, () => this._onDisconnect());58 this._connection.on('Browser.attachedToTarget', this._onAttachedToTarget.bind(this));59 this._connection.on('Browser.detachedFromTarget', this._onDetachedFromTarget.bind(this));60 this._connection.on('Browser.downloadCreated', this._onDownloadCreated.bind(this));61 this._connection.on('Browser.downloadFinished', this._onDownloadFinished.bind(this));62 this._connection.on('Browser.videoRecordingFinished', this._onVideoRecordingFinished.bind(this));63 }64 async _initVersion() {65 const result = await this._connection.send('Browser.getInfo');66 this._version = result.version.substring(result.version.indexOf('/') + 1);67 this._userAgent = result.userAgent;68 }69 isConnected() {70 return !this._connection._closed;71 }72 async newContext(options) {73 (0, _browserContext.validateBrowserContextOptions)(options, this.options);74 if (options.isMobile) throw new Error('options.isMobile is not supported in Firefox');75 const {76 browserContextId77 } = await this._connection.send('Browser.createBrowserContext', {78 removeOnDetach: true79 });80 const context = new FFBrowserContext(this, browserContextId, options);81 await context._initialize();82 this._contexts.set(browserContextId, context);83 return context;84 }85 contexts() {86 return Array.from(this._contexts.values());87 }88 version() {89 return this._version;90 }91 userAgent() {92 return this._userAgent;93 }94 _onDetachedFromTarget(payload) {95 const ffPage = this._ffPages.get(payload.targetId);96 this._ffPages.delete(payload.targetId);97 ffPage.didClose();98 }99 _onAttachedToTarget(payload) {100 const {101 targetId,102 browserContextId,103 openerId,104 type105 } = payload.targetInfo;106 (0, _utils.assert)(type === 'page');107 const context = browserContextId ? this._contexts.get(browserContextId) : this._defaultContext;108 (0, _utils.assert)(context, `Unknown context id:${browserContextId}, _defaultContext: ${this._defaultContext}`);109 const session = this._connection.createSession(payload.sessionId);110 const opener = openerId ? this._ffPages.get(openerId) : null;111 const ffPage = new _ffPage.FFPage(session, context, opener);112 this._ffPages.set(targetId, ffPage);113 }114 _onDownloadCreated(payload) {115 const ffPage = this._ffPages.get(payload.pageTargetId);116 (0, _utils.assert)(ffPage);117 if (!ffPage) return;118 let originPage = ffPage._initializedPage; // If it's a new window download, report it on the opener page.119 if (!originPage) {120 // Resume the page creation with an error. The page will automatically close right121 // after the download begins.122 ffPage._markAsError(new Error('Starting new page download'));123 if (ffPage._opener) originPage = ffPage._opener._initializedPage;124 }125 if (!originPage) return;126 this._downloadCreated(originPage, payload.uuid, payload.url, payload.suggestedFileName);127 }128 _onDownloadFinished(payload) {129 const error = payload.canceled ? 'canceled' : payload.error;130 this._downloadFinished(payload.uuid, error);131 }132 _onVideoRecordingFinished(payload) {133 var _this$_takeVideo;134 (_this$_takeVideo = this._takeVideo(payload.screencastId)) === null || _this$_takeVideo === void 0 ? void 0 : _this$_takeVideo.reportFinished();135 }136 _onDisconnect() {137 for (const video of this._idToVideo.values()) video.artifact.reportFinished(_errors.kBrowserClosedError);138 this._idToVideo.clear();139 this._didClose();140 }141}142exports.FFBrowser = FFBrowser;143class FFBrowserContext extends _browserContext.BrowserContext {144 constructor(browser, browserContextId, options) {145 super(browser, options, browserContextId);146 }147 async _initialize() {148 (0, _utils.assert)(!this._ffPages().length);149 const browserContextId = this._browserContextId;150 const promises = [super._initialize()];151 promises.push(this._browser._connection.send('Browser.setDownloadOptions', {152 browserContextId,153 downloadOptions: {154 behavior: this._options.acceptDownloads ? 'saveToDisk' : 'cancel',155 downloadsDir: this._browser.options.downloadsPath156 }157 }));158 if (this._options.viewport) {159 const viewport = {160 viewportSize: {161 width: this._options.viewport.width,162 height: this._options.viewport.height163 },164 deviceScaleFactor: this._options.deviceScaleFactor || 1165 };166 promises.push(this._browser._connection.send('Browser.setDefaultViewport', {167 browserContextId,168 viewport169 }));170 }171 if (this._options.hasTouch) promises.push(this._browser._connection.send('Browser.setTouchOverride', {172 browserContextId,173 hasTouch: true174 }));175 if (this._options.userAgent) promises.push(this._browser._connection.send('Browser.setUserAgentOverride', {176 browserContextId,177 userAgent: this._options.userAgent178 }));179 if (this._options.bypassCSP) promises.push(this._browser._connection.send('Browser.setBypassCSP', {180 browserContextId,181 bypassCSP: true182 }));183 if (this._options.ignoreHTTPSErrors) promises.push(this._browser._connection.send('Browser.setIgnoreHTTPSErrors', {184 browserContextId,185 ignoreHTTPSErrors: true186 }));187 if (this._options.javaScriptEnabled === false) promises.push(this._browser._connection.send('Browser.setJavaScriptDisabled', {188 browserContextId,189 javaScriptDisabled: true190 }));191 if (this._options.locale) promises.push(this._browser._connection.send('Browser.setLocaleOverride', {192 browserContextId,193 locale: this._options.locale194 }));195 if (this._options.timezoneId) promises.push(this._browser._connection.send('Browser.setTimezoneOverride', {196 browserContextId,197 timezoneId: this._options.timezoneId198 }));199 if (this._options.permissions) promises.push(this.grantPermissions(this._options.permissions));200 if (this._options.extraHTTPHeaders || this._options.locale) promises.push(this.setExtraHTTPHeaders(this._options.extraHTTPHeaders || []));201 if (this._options.httpCredentials) promises.push(this.setHTTPCredentials(this._options.httpCredentials));202 if (this._options.geolocation) promises.push(this.setGeolocation(this._options.geolocation));203 if (this._options.offline) promises.push(this.setOffline(this._options.offline));204 promises.push(this._browser._connection.send('Browser.setColorScheme', {205 browserContextId,206 colorScheme: this._options.colorScheme !== undefined ? this._options.colorScheme : 'light'207 }));208 // [Replay] - Browser.setReducedMotion and Browser.setReducedMotion are not supported209 // promises.push(this._browser._connection.send('Browser.setReducedMotion', {210 // browserContextId,211 // reducedMotion: this._options.reducedMotion !== undefined ? this._options.reducedMotion : 'no-preference'212 // }));213 // promises.push(this._browser._connection.send('Browser.setReducedMotion', {214 // browserContextId,215 // forcedColors: this._options.forcedColors !== undefined ? this._options.forcedColors : 'none'216 // }));217 if (this._options.recordVideo) {218 promises.push(this._ensureVideosPath().then(() => {219 return this._browser._connection.send('Browser.setVideoRecordingOptions', {220 // validateBrowserContextOptions ensures correct video size.221 options: { ...this._options.recordVideo.size,222 dir: this._options.recordVideo.dir223 },224 browserContextId: this._browserContextId225 });226 }));227 }228 if (this._options.proxy) {229 promises.push(this._browser._connection.send('Browser.setContextProxy', {230 browserContextId: this._browserContextId,231 ...toJugglerProxyOptions(this._options.proxy)232 }));233 }234 await Promise.all(promises);235 }236 _ffPages() {237 return Array.from(this._browser._ffPages.values()).filter(ffPage => ffPage._browserContext === this);238 }239 pages() {240 return this._ffPages().map(ffPage => ffPage._initializedPage).filter(pageOrNull => !!pageOrNull);241 }242 async newPageDelegate() {243 (0, _browserContext.assertBrowserContextIsNotOwned)(this);244 const {245 targetId246 } = await this._browser._connection.send('Browser.newPage', {247 browserContextId: this._browserContextId248 }).catch(e => {249 if (e.message.includes('Failed to override timezone')) throw new Error(`Invalid timezone ID: ${this._options.timezoneId}`);250 throw e;251 });252 return this._browser._ffPages.get(targetId);253 }254 async _doCookies(urls) {255 const {256 cookies257 } = await this._browser._connection.send('Browser.getCookies', {258 browserContextId: this._browserContextId259 });260 return network.filterCookies(cookies.map(c => {261 const copy = { ...c262 };263 delete copy.size;264 delete copy.session;265 return copy;266 }), urls);267 }268 async addCookies(cookies) {269 const cc = network.rewriteCookies(cookies).map(c => ({ ...c,270 expires: c.expires && c.expires !== -1 ? c.expires : undefined271 }));272 await this._browser._connection.send('Browser.setCookies', {273 browserContextId: this._browserContextId,274 cookies: cc275 });276 }277 async clearCookies() {278 await this._browser._connection.send('Browser.clearCookies', {279 browserContextId: this._browserContextId280 });281 }282 async _doGrantPermissions(origin, permissions) {283 const webPermissionToProtocol = new Map([['geolocation', 'geo'], ['persistent-storage', 'persistent-storage'], ['push', 'push'], ['notifications', 'desktop-notification']]);284 const filtered = permissions.map(permission => {285 const protocolPermission = webPermissionToProtocol.get(permission);286 if (!protocolPermission) throw new Error('Unknown permission: ' + permission);287 return protocolPermission;288 });289 await this._browser._connection.send('Browser.grantPermissions', {290 origin: origin,291 browserContextId: this._browserContextId,292 permissions: filtered293 });294 }295 async _doClearPermissions() {296 await this._browser._connection.send('Browser.resetPermissions', {297 browserContextId: this._browserContextId298 });299 }300 async setGeolocation(geolocation) {301 (0, _browserContext.verifyGeolocation)(geolocation);302 this._options.geolocation = geolocation;303 await this._browser._connection.send('Browser.setGeolocationOverride', {304 browserContextId: this._browserContextId,305 geolocation: geolocation || null306 });307 }308 async setExtraHTTPHeaders(headers) {309 this._options.extraHTTPHeaders = headers;310 let allHeaders = this._options.extraHTTPHeaders;311 if (this._options.locale) allHeaders = network.mergeHeaders([allHeaders, network.singleHeader('Accept-Language', this._options.locale)]);312 await this._browser._connection.send('Browser.setExtraHTTPHeaders', {313 browserContextId: this._browserContextId,314 headers: allHeaders315 });316 }317 async setOffline(offline) {318 this._options.offline = offline;319 await this._browser._connection.send('Browser.setOnlineOverride', {320 browserContextId: this._browserContextId,321 override: offline ? 'offline' : 'online'322 });323 }324 async _doSetHTTPCredentials(httpCredentials) {325 this._options.httpCredentials = httpCredentials;326 await this._browser._connection.send('Browser.setHTTPCredentials', {327 browserContextId: this._browserContextId,328 credentials: httpCredentials || null329 });330 }331 async _doAddInitScript(source) {332 await this._browser._connection.send('Browser.addScriptToEvaluateOnNewDocument', {333 browserContextId: this._browserContextId,334 script: source335 });336 }337 async _doExposeBinding(binding) {338 await this._browser._connection.send('Browser.addBinding', {339 browserContextId: this._browserContextId,340 name: binding.name,341 script: binding.source342 });343 }344 async _doUpdateRequestInterception() {345 await this._browser._connection.send('Browser.setRequestInterception', {346 browserContextId: this._browserContextId,347 enabled: !!this._requestInterceptor348 });349 }350 _onClosePersistent() {}351 async _doClose() {352 (0, _utils.assert)(this._browserContextId);353 await this._browser._connection.send('Browser.removeBrowserContext', {354 browserContextId: this._browserContextId355 });356 this._browser._contexts.delete(this._browserContextId);357 }358 async _doCancelDownload(uuid) {359 await this._browser._connection.send('Browser.cancelDownload', {360 uuid361 });362 }363}364exports.FFBrowserContext = FFBrowserContext;365function toJugglerProxyOptions(proxy) {366 const proxyServer = new URL(proxy.server);367 let port = parseInt(proxyServer.port, 10);368 let type = 'http';369 if (proxyServer.protocol === 'socks5:') type = 'socks';else if (proxyServer.protocol === 'socks4:') type = 'socks4';else if (proxyServer.protocol === 'https:') type = 'https';370 if (proxyServer.port === '') {371 if (proxyServer.protocol === 'http:') port = 80;else if (proxyServer.protocol === 'https:') port = 443;372 }373 return {374 type,375 bypass: proxy.bypass ? proxy.bypass.split(',').map(domain => domain.trim()) : [],376 host: proxyServer.hostname,377 port,378 username: proxy.username,379 password: proxy.password...
Using AI Code Generation
1const { toJugglerProxyOptions } = require('playwright-core/lib/utils/utils');2const { toJugglerProxyOptions } = require('playwright/lib/utils/utils');3const { toJugglerProxyOptions } = require('puppeteer/lib/cjs/puppeteer/common/Connection');4const { toJugglerProxyOptions } = require('puppeteer-core/lib/cjs/puppeteer/common/Connection');5const { toJugglerProxyOptions } = require('puppeteer-firefox/lib/cjs/puppeteer/common/Connection');6const { toJugglerProxyOptions } = require('puppeteer-firefox-core/lib/cjs/puppeteer/common/Connection');7const { toJugglerProxyOptions } = require('puppeteer-chromium/lib/cjs/puppeteer/common/Connection');8const { toJugglerProxyOptions } = require('puppeteer-chromium-core/lib/cjs/puppeteer/common/Connection');9const { toJugglerProxyOptions } = require('puppeteer-chromium-webkit/lib/cjs/puppeteer/common/Connection');10const { toJugglerProxyOptions } = require('puppeteer-chromium-webkit-core/lib/cjs/puppeteer/common/Connection');11const { toJugglerProxyOptions } = require('puppeteer-webkit/lib/cjs/puppeteer/common/Connection');12const { toJugglerProxyOptions } = require('puppeteer-webkit-core
Using AI Code Generation
1const { toJugglerProxyOptions } = require('playwright/lib/utils/utils');2const { chromium } = require('playwright');3(async () => {4 const browser = await chromium.launch();5 const page = await browser.newPage();6 await page.goto('
Using AI Code Generation
1const { toJugglerProxyOptions } = require('playwright/lib/internal/utils');2const { chromium } = require('playwright');3(async () => {4 const browser = await chromium.launch();5 const context = await browser.newContext();6 const page = await context.newPage();7 const options = toJugglerProxyOptions({8 });9 await page.route('**', route => route.fulfill(options));10 await page.screenshot({ path: 'example.png' });11 await browser.close();12})();13const { toJugglerProxyOptions } = require('playwright/lib/internal/utils');14const { chromium } = require('playwright');15(async () => {16 const browser = await chromium.launch();17 const context = await browser.newContext();18 const page = await context.newPage();19 const options = toJugglerProxyOptions({20 });21 await page.route('**', route => route.fulfill(options));22 await page.screenshot({ path: 'example.png' });23 await browser.close();24})();25const { toJugglerProxyOptions } = require('playwright/lib/internal/utils');26const { chromium } = require('playwright');27(async () => {28 const browser = await chromium.launch();29 const context = await browser.newContext();30 const page = await context.newPage();31 const options = toJugglerProxyOptions({32 });33 await page.route('**', route => route.fulfill(options));34 await page.screenshot({ path: 'example.png' });35 await browser.close();36})();37const { toJugglerProxyOptions } = require('playwright/lib/internal/utils');38const { chromium } = require('playwright');39(async () => {
Using AI Code Generation
1const { toJugglerProxyOptions } = require('playwright/lib/utils/utils');2const proxyOptions = toJugglerProxyOptions({3});4console.log(proxyOptions);5const { toJugglerProxyOptions } = require('playwright/lib/utils/utils');6const proxyOptions = toJugglerProxyOptions({7});8console.log(proxyOptions);9const { toJugglerProxyOptions } = require('playwright/lib/utils/utils');10const proxyOptions = toJugglerProxyOptions({11});12console.log(proxyOptions);13const { toJugglerProxyOptions } = require('playwright/lib/utils/utils');14const proxyOptions = toJugglerProxyOptions({15});16console.log(proxyOptions);17const { toJugglerProxyOptions } = require('playwright/lib/utils/utils');18const proxyOptions = toJugglerProxyOptions({19});20console.log(proxyOptions);21const { toJugglerProxyOptions } = require('playwright/lib/utils/utils');22const proxyOptions = toJugglerProxyOptions({23});24console.log(proxyOptions);25const { toJugglerProxyOptions } = require('playwright/lib/utils/utils');26const proxyOptions = toJugglerProxyOptions({27});28console.log(proxyOptions);29const { toJugglerProxy
Using AI Code Generation
1const { toJugglerProxyOptions } = require('playwright-core/lib/utils/utils');2console.log(proxyOptions);3const { toJugglerProxyOptions } = require('playwright/lib/utils/utils');4console.log(proxyOptions);5const { toJugglerProxyOptions } = require('puppeteer/lib/cjs/puppeteer/common/LifecycleWatcher.js');6console.log(proxyOptions);7const { toJugglerProxyOptions } = require('puppeteer-core/lib/cjs/puppeteer/common/LifecycleWatcher.js');8console.log(proxyOptions);9const { toJugglerProxyOptions } = require('puppeteer-firefox/lib/cjs/puppeteer/common/LifecycleWatcher.js');10console.log(proxyOptions);11const { toJugglerProxyOptions } = require('puppeteer-firefox-core/lib/cjs/puppeteer/common/LifecycleWatcher.js');12console.log(proxyOptions);13const { toJugglerProxyOptions } = require('puppeteer-chromium/lib/cjs/puppeteer/common/LifecycleWatcher.js');14console.log(proxyOptions);15const { toJugglerProxyOptions } = require
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!!