How to use _ensureIsolatedWorld method in Puppeteer

Best JavaScript code snippet using puppeteer

FrameManager.js

Source:FrameManager.js Github

copy

Full Screen

...51 const { frameTree } = result[1];52 this._handleFrameTree(frameTree);53 await Promise.all([54 this._client.send('Page.setLifecycleEventsEnabled', { enabled: true }),55 this._client.send('Runtime.enable', {}).then(() => this._ensureIsolatedWorld(UTILITY_WORLD_NAME)),56 this._networkManager.initialize(),57 ]);58 }59 networkManager() {60 return this._networkManager;61 }62 async navigateFrame(frame, url, options = {}) {63 assertNoLegacyNavigationOptions(options);64 const { referer = this._networkManager.extraHTTPHeaders()['referer'], waitUntil = ['load'], timeout = this._timeoutSettings.navigationTimeout(), } = options;65 const watcher = new LifecycleWatcher_1.LifecycleWatcher(this, frame, waitUntil, timeout);66 let ensureNewDocumentNavigation = false;67 let error = await Promise.race([68 navigate(this._client, url, referer, frame._id),69 watcher.timeoutOrTerminationPromise(),70 ]);71 if (!error) {72 error = await Promise.race([73 watcher.timeoutOrTerminationPromise(),74 ensureNewDocumentNavigation ? watcher.newDocumentNavigationPromise() : watcher.sameDocumentNavigationPromise(),75 ]);76 }77 watcher.dispose();78 if (error)79 throw error;80 return watcher.navigationResponse();81 async function navigate(client, url, referrer, frameId) {82 try {83 const response = await client.send('Page.navigate', { url, referrer, frameId });84 ensureNewDocumentNavigation = !!response.loaderId;85 return response.errorText ? new Error(`${response.errorText} at ${url}`) : null;86 }87 catch (error) {88 return error;89 }90 }91 }92 async waitForFrameNavigation(frame, options = {}) {93 assertNoLegacyNavigationOptions(options);94 const { waitUntil = ['load'], timeout = this._timeoutSettings.navigationTimeout(), } = options;95 const watcher = new LifecycleWatcher_1.LifecycleWatcher(this, frame, waitUntil, timeout);96 const error = await Promise.race([97 watcher.timeoutOrTerminationPromise(),98 watcher.sameDocumentNavigationPromise(),99 watcher.newDocumentNavigationPromise()100 ]);101 watcher.dispose();102 if (error)103 throw error;104 return watcher.navigationResponse();105 }106 _onLifecycleEvent(event) {107 const frame = this._frames.get(event.frameId);108 if (!frame)109 return;110 frame._onLifecycleEvent(event.loaderId, event.name);111 this.emit(Events_1.Events.FrameManager.LifecycleEvent, frame);112 }113 _onFrameStoppedLoading(frameId) {114 const frame = this._frames.get(frameId);115 if (!frame)116 return;117 frame._onLoadingStopped();118 this.emit(Events_1.Events.FrameManager.LifecycleEvent, frame);119 }120 _handleFrameTree(frameTree) {121 if (frameTree.frame.parentId)122 this._onFrameAttached(frameTree.frame.id, frameTree.frame.parentId);123 this._onFrameNavigated(frameTree.frame);124 if (!frameTree.childFrames)125 return;126 for (const child of frameTree.childFrames)127 this._handleFrameTree(child);128 }129 page() {130 return this._page;131 }132 mainFrame() {133 return this._mainFrame;134 }135 frames() {136 return Array.from(this._frames.values());137 }138 frame(frameId) {139 return this._frames.get(frameId) || null;140 }141 _onFrameAttached(frameId, parentFrameId) {142 if (this._frames.has(frameId))143 return;144 helper_1.assert(parentFrameId);145 const parentFrame = this._frames.get(parentFrameId);146 const frame = new Frame(this, this._client, parentFrame, frameId);147 this._frames.set(frame._id, frame);148 this.emit(Events_1.Events.FrameManager.FrameAttached, frame);149 }150 _onFrameNavigated(framePayload) {151 const isMainFrame = !framePayload.parentId;152 let frame = isMainFrame ? this._mainFrame : this._frames.get(framePayload.id);153 helper_1.assert(isMainFrame || frame, 'We either navigate top level or have old version of the navigated frame');154 // Detach all child frames first.155 if (frame) {156 for (const child of frame.childFrames())157 this._removeFramesRecursively(child);158 }159 // Update or create main frame.160 if (isMainFrame) {161 if (frame) {162 // Update frame id to retain frame identity on cross-process navigation.163 this._frames.delete(frame._id);164 frame._id = framePayload.id;165 }166 else {167 // Initial main frame navigation.168 frame = new Frame(this, this._client, null, framePayload.id);169 }170 this._frames.set(framePayload.id, frame);171 this._mainFrame = frame;172 }173 // Update frame payload.174 frame._navigated(framePayload);175 this.emit(Events_1.Events.FrameManager.FrameNavigated, frame);176 }177 async _ensureIsolatedWorld(name) {178 if (this._isolatedWorlds.has(name))179 return;180 this._isolatedWorlds.add(name);181 await this._client.send('Page.addScriptToEvaluateOnNewDocument', {182 source: `//# sourceURL=${ExecutionContext_1.EVALUATION_SCRIPT_URL}`,183 worldName: name,184 }),185 await Promise.all(this.frames().map(frame => this._client.send('Page.createIsolatedWorld', {186 frameId: frame._id,187 grantUniveralAccess: true,188 worldName: name,189 }).catch(helper_1.debugError))); // frames might be removed before we send this190 }191 _onFrameNavigatedWithinDocument(frameId, url) {...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Puppeteer automation tests on LambdaTest cloud grid

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

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful