Best JavaScript code snippet using puppeteer
FrameManager.js
Source:FrameManager.js
...99 return error;100 }101 }102 }103 async waitForFrameNavigation(frame, options = {}) {104 assertNoLegacyNavigationOptions(options);105 const { waitUntil = ['load'], timeout = this._timeoutSettings.navigationTimeout(), } = options;106 const watcher = new LifecycleWatcher_1.LifecycleWatcher(this, frame, waitUntil, timeout);107 const error = await Promise.race([108 watcher.timeoutOrTerminationPromise(),109 watcher.sameDocumentNavigationPromise(),110 watcher.newDocumentNavigationPromise(),111 ]);112 watcher.dispose();113 if (error)114 throw error;115 return watcher.navigationResponse();116 }117 _onLifecycleEvent(event) {118 const frame = this._frames.get(event.frameId);119 if (!frame)120 return;121 frame._onLifecycleEvent(event.loaderId, event.name);122 this.emit(Events_1.Events.FrameManager.LifecycleEvent, frame);123 }124 _onFrameStoppedLoading(frameId) {125 const frame = this._frames.get(frameId);126 if (!frame)127 return;128 frame._onLoadingStopped();129 this.emit(Events_1.Events.FrameManager.LifecycleEvent, frame);130 }131 _handleFrameTree(frameTree) {132 if (frameTree.frame.parentId)133 this._onFrameAttached(frameTree.frame.id, frameTree.frame.parentId);134 this._onFrameNavigated(frameTree.frame);135 if (!frameTree.childFrames)136 return;137 for (const child of frameTree.childFrames)138 this._handleFrameTree(child);139 }140 page() {141 return this._page;142 }143 mainFrame() {144 return this._mainFrame;145 }146 frames() {147 return Array.from(this._frames.values());148 }149 frame(frameId) {150 return this._frames.get(frameId) || null;151 }152 _onFrameAttached(frameId, parentFrameId) {153 if (this._frames.has(frameId))154 return;155 helper_1.assert(parentFrameId);156 const parentFrame = this._frames.get(parentFrameId);157 const frame = new Frame(this, this._client, parentFrame, frameId);158 this._frames.set(frame._id, frame);159 this.emit(Events_1.Events.FrameManager.FrameAttached, frame);160 }161 _onFrameNavigated(framePayload) {162 const isMainFrame = !framePayload.parentId;163 let frame = isMainFrame164 ? this._mainFrame165 : this._frames.get(framePayload.id);166 helper_1.assert(isMainFrame || frame, 'We either navigate top level or have old version of the navigated frame');167 // Detach all child frames first.168 if (frame) {169 for (const child of frame.childFrames())170 this._removeFramesRecursively(child);171 }172 // Update or create main frame.173 if (isMainFrame) {174 if (frame) {175 // Update frame id to retain frame identity on cross-process navigation.176 this._frames.delete(frame._id);177 frame._id = framePayload.id;178 }179 else {180 // Initial main frame navigation.181 frame = new Frame(this, this._client, null, framePayload.id);182 }183 this._frames.set(framePayload.id, frame);184 this._mainFrame = frame;185 }186 // Update frame payload.187 frame._navigated(framePayload);188 this.emit(Events_1.Events.FrameManager.FrameNavigated, frame);189 }190 async _ensureIsolatedWorld(name) {191 if (this._isolatedWorlds.has(name))192 return;193 this._isolatedWorlds.add(name);194 await this._client.send('Page.addScriptToEvaluateOnNewDocument', {195 source: `//# sourceURL=${ExecutionContext_1.EVALUATION_SCRIPT_URL}`,196 worldName: name,197 }),198 await Promise.all(this.frames().map((frame) => this._client199 .send('Page.createIsolatedWorld', {200 frameId: frame._id,201 grantUniveralAccess: true,202 worldName: name,203 })204 .catch(helper_1.debugError))); // frames might be removed before we send this205 }206 _onFrameNavigatedWithinDocument(frameId, url) {207 const frame = this._frames.get(frameId);208 if (!frame)209 return;210 frame._navigatedWithinDocument(url);211 this.emit(Events_1.Events.FrameManager.FrameNavigatedWithinDocument, frame);212 this.emit(Events_1.Events.FrameManager.FrameNavigated, frame);213 }214 _onFrameDetached(frameId) {215 const frame = this._frames.get(frameId);216 if (frame)217 this._removeFramesRecursively(frame);218 }219 _onExecutionContextCreated(contextPayload) {220 const auxData = contextPayload.auxData;221 const frameId = auxData ? auxData.frameId : null;222 const frame = this._frames.get(frameId) || null;223 let world = null;224 if (frame) {225 if (contextPayload.auxData && !!contextPayload.auxData['isDefault']) {226 world = frame._mainWorld;227 }228 else if (contextPayload.name === UTILITY_WORLD_NAME &&229 !frame._secondaryWorld._hasContext()) {230 // In case of multiple sessions to the same target, there's a race between231 // connections so we might end up creating multiple isolated worlds.232 // We can use either.233 world = frame._secondaryWorld;234 }235 }236 if (contextPayload.auxData && contextPayload.auxData['type'] === 'isolated')237 this._isolatedWorlds.add(contextPayload.name);238 const context = new ExecutionContext_1.ExecutionContext(this._client, contextPayload, world);239 if (world)240 world._setContext(context);241 this._contextIdToContext.set(contextPayload.id, context);242 }243 /**244 * @param {number} executionContextId245 */246 _onExecutionContextDestroyed(executionContextId) {247 const context = this._contextIdToContext.get(executionContextId);248 if (!context)249 return;250 this._contextIdToContext.delete(executionContextId);251 if (context._world)252 context._world._setContext(null);253 }254 _onExecutionContextsCleared() {255 for (const context of this._contextIdToContext.values()) {256 if (context._world)257 context._world._setContext(null);258 }259 this._contextIdToContext.clear();260 }261 executionContextById(contextId) {262 const context = this._contextIdToContext.get(contextId);263 helper_1.assert(context, 'INTERNAL ERROR: missing context with id = ' + contextId);264 return context;265 }266 _removeFramesRecursively(frame) {267 for (const child of frame.childFrames())268 this._removeFramesRecursively(child);269 frame._detach();270 this._frames.delete(frame._id);271 this.emit(Events_1.Events.FrameManager.FrameDetached, frame);272 }273}274exports.FrameManager = FrameManager;275class Frame {276 constructor(frameManager, client, parentFrame, frameId) {277 this._url = '';278 this._detached = false;279 this._loaderId = '';280 this._lifecycleEvents = new Set();281 this._frameManager = frameManager;282 this._client = client;283 this._parentFrame = parentFrame;284 this._url = '';285 this._id = frameId;286 this._detached = false;287 this._loaderId = '';288 this._mainWorld = new DOMWorld_1.DOMWorld(frameManager, this, frameManager._timeoutSettings);289 this._secondaryWorld = new DOMWorld_1.DOMWorld(frameManager, this, frameManager._timeoutSettings);290 this._childFrames = new Set();291 if (this._parentFrame)292 this._parentFrame._childFrames.add(this);293 }294 async goto(url, options) {295 return await this._frameManager.navigateFrame(this, url, options);296 }297 async waitForNavigation(options) {298 return await this._frameManager.waitForFrameNavigation(this, options);299 }300 executionContext() {301 return this._mainWorld.executionContext();302 }303 async evaluateHandle(pageFunction, ...args) {304 return this._mainWorld.evaluateHandle(pageFunction, ...args);305 }306 async evaluate(pageFunction, ...args) {307 return this._mainWorld.evaluate(pageFunction, ...args);308 }309 async $(selector) {310 return this._mainWorld.$(selector);311 }312 async $x(expression) {...
Frame.js
Source:Frame.js
...207 * @param {!{timeout?: number, waitUntil?: string|Array<string>}} [options]208 * @return {Promise<Response>}209 */210 waitForNavigation (options) {211 return this._frameManager.waitForFrameNavigation(this, options)212 }213 /**214 * @return {?Promise<ExecutionContext>}215 */216 executionContext () {217 return this._mainWorld.executionContext()218 }219 /**220 * @param {Function|string} pageFunction221 * @param {...*} args222 * @return {Promise<JSHandle>}223 */224 evaluateHandle (pageFunction, ...args) {225 return this._mainWorld.evaluateHandle(pageFunction, ...args)...
Using AI Code Generation
1const puppeteer = require('puppeteer');2(async () => {3 const browser = await puppeteer.launch({4 });5 const page = await browser.newPage();6 await page.waitForNavigation();7 await page.type('input[name="q"]', 'Puppeteer');8 await page.waitForSelector('input[name="btnK"]');9 await page.click('input[name="btnK"]');10 await page.waitForNavigation();11 await page.screenshot({path: 'google.png'});12 await browser.close();13})();14const puppeteer = require('puppeteer');15(async () => {16 const browser = await puppeteer.launch({17 });18 const page = await browser.newPage();19 await page.waitForNavigation();20 await page.type('input[name="q"]', 'Puppeteer');21 await page.waitForSelector('input[name="btnK"]');22 await page.click('input[name="btnK"]');23 await page.waitForNavigation();24 await page.screenshot({path: 'google.png'});25 await browser.close();26})();27const puppeteer = require('puppeteer');28(async () => {29 const browser = await puppeteer.launch({30 });31 const page = await browser.newPage();32 await page.waitForNavigation();33 await page.type('input[name="q"]', 'Puppeteer');34 await page.waitForSelector('input[name="btnK"]');35 await page.click('input[name="btnK"]');36 await page.waitForNavigation();37 await page.screenshot({path: 'google.png'});38 await browser.close();39})();40const puppeteer = require('puppeteer');41(async () => {42 const browser = await puppeteer.launch({43 });44 const page = await browser.newPage();45 await page.waitForNavigation();46 await page.type('input[name="q"]', 'Puppeteer');
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!