Best JavaScript code snippet using puppeteer
FrameManager.js
Source:FrameManager.js
...147 const {148 timeout = this._timeoutSettings.navigationTimeout(),149 waitUntil = ['load'],150 } = options;151 const normalizedWaitUntil = normalizeWaitUntil(waitUntil);152 const timeoutError = new TimeoutError('Navigation timeout of ' + timeout + ' ms exceeded');153 let timeoutCallback;154 const timeoutPromise = new Promise(resolve => timeoutCallback = resolve.bind(null, timeoutError));155 const timeoutId = timeout ? setTimeout(timeoutCallback, timeout) : null;156 const nextNavigationDog = new NextNavigationWatchdog(this._session, this);157 const error1 = await Promise.race([158 nextNavigationDog.promise(),159 timeoutPromise,160 ]);161 nextNavigationDog.dispose();162 // If timeout happened first - throw.163 if (error1) {164 clearTimeout(timeoutId);165 throw error1;166 }167 const {navigationId, url} = nextNavigationDog.navigation();168 if (!navigationId) {169 // Same document navigation happened.170 clearTimeout(timeoutId);171 return null;172 }173 const watchDog = new NavigationWatchdog(this._session, this, this._networkManager, navigationId, url, normalizedWaitUntil);174 const error = await Promise.race([175 timeoutPromise,176 watchDog.promise(),177 ]);178 watchDog.dispose();179 clearTimeout(timeoutId);180 if (error)181 throw error;182 return watchDog.navigationResponse();183 }184 /**185 * @param {string} url186 * @param {!{timeout?: number, waitUntil?: string|!Array<string>}} options187 */188 async goto(url, options = {}) {189 const {190 timeout = this._timeoutSettings.navigationTimeout(),191 waitUntil = ['load'],192 referer,193 } = options;194 const normalizedWaitUntil = normalizeWaitUntil(waitUntil);195 const {navigationId} = await this._session.send('Page.navigate', {196 frameId: this._frameId,197 referer,198 url,199 });200 if (!navigationId)201 return;202 const timeoutError = new TimeoutError('Navigation timeout of ' + timeout + ' ms exceeded');203 let timeoutCallback;204 const timeoutPromise = new Promise(resolve => timeoutCallback = resolve.bind(null, timeoutError));205 const timeoutId = timeout ? setTimeout(timeoutCallback, timeout) : null;206 const watchDog = new NavigationWatchdog(this._session, this, this._networkManager, navigationId, url, normalizedWaitUntil);207 const error = await Promise.race([208 timeoutPromise,209 watchDog.promise(),210 ]);211 watchDog.dispose();212 clearTimeout(timeoutId);213 if (error)214 throw error;215 return watchDog.navigationResponse();216 }217 /**218 * @param {string} selector219 * @param {!{delay?: number, button?: string, clickCount?: number}=} options220 */221 async click(selector, options = {}) {222 return this._mainWorld.click(selector, options);223 }224 /**225 * @param {string} selector226 */227 async tap(selector) {228 return this._mainWorld.tap(selector);229 }230 /**231 * @param {string} selector232 * @param {string} text233 * @param {{delay: (number|undefined)}=} options234 */235 async type(selector, text, options) {236 return this._mainWorld.type(selector, text, options);237 }238 /**239 * @param {string} selector240 */241 async focus(selector) {242 return this._mainWorld.focus(selector);243 }244 /**245 * @param {string} selector246 */247 async hover(selector) {248 return this._mainWorld.hover(selector);249 }250 _detach() {251 this._parentFrame._children.delete(this);252 this._parentFrame = null;253 this._detached = true;254 this._mainWorld._detach();255 }256 _navigated(url, name, navigationId) {257 this._url = url;258 this._name = name;259 this._lastCommittedNavigationId = navigationId;260 this._firedEvents.clear();261 }262 /**263 * @param {string} selector264 * @param {!Array<string>} values265 * @return {!Promise<!Array<string>>}266 */267 select(selector, ...values) {268 return this._mainWorld.select(selector, ...values);269 }270 /**271 * @param {(string|number|Function)} selectorOrFunctionOrTimeout272 * @param {!{polling?: string|number, timeout?: number, visible?: boolean, hidden?: boolean}=} options273 * @param {!Array<*>} args274 * @return {!Promise<!JSHandle>}275 */276 waitFor(selectorOrFunctionOrTimeout, options, ...args) {277 const xPathPattern = '//';278 if (helper.isString(selectorOrFunctionOrTimeout)) {279 const string = /** @type {string} */ (selectorOrFunctionOrTimeout);280 if (string.startsWith(xPathPattern))281 return this.waitForXPath(string, options);282 return this.waitForSelector(string, options);283 }284 if (helper.isNumber(selectorOrFunctionOrTimeout))285 return new Promise(fulfill => setTimeout(fulfill, /** @type {number} */ (selectorOrFunctionOrTimeout)));286 if (typeof selectorOrFunctionOrTimeout === 'function')287 return this.waitForFunction(selectorOrFunctionOrTimeout, options, ...args);288 return Promise.reject(new Error('Unsupported target type: ' + (typeof selectorOrFunctionOrTimeout)));289 }290 /**291 * @param {Function|string} pageFunction292 * @param {!{polling?: string|number, timeout?: number}=} options293 * @return {!Promise<!JSHandle>}294 */295 waitForFunction(pageFunction, options = {}, ...args) {296 return this._mainWorld.waitForFunction(pageFunction, options, ...args);297 }298 /**299 * @param {string} selector300 * @param {!{timeout?: number, visible?: boolean, hidden?: boolean}=} options301 * @return {!Promise<!ElementHandle>}302 */303 waitForSelector(selector, options) {304 return this._mainWorld.waitForSelector(selector, options);305 }306 /**307 * @param {string} xpath308 * @param {!{timeout?: number, visible?: boolean, hidden?: boolean}=} options309 * @return {!Promise<!ElementHandle>}310 */311 waitForXPath(xpath, options) {312 return this._mainWorld.waitForXPath(xpath, options);313 }314 /**315 * @return {!Promise<String>}316 */317 async content() {318 return this._mainWorld.content();319 }320 /**321 * @param {string} html322 */323 async setContent(html) {324 return this._mainWorld.setContent(html);325 }326 async evaluate(pageFunction, ...args) {327 return this._mainWorld.evaluate(pageFunction, ...args);328 }329 /**330 * @param {string} selector331 * @return {!Promise<?ElementHandle>}332 */333 async $(selector) {334 return this._mainWorld.$(selector);335 }336 /**337 * @param {string} selector338 * @return {!Promise<!Array<!ElementHandle>>}339 */340 async $$(selector) {341 return this._mainWorld.$$(selector);342 }343 /**344 * @param {string} selector345 * @param {Function|String} pageFunction346 * @param {!Array<*>} args347 * @return {!Promise<(!Object|undefined)>}348 */349 async $eval(selector, pageFunction, ...args) {350 return this._mainWorld.$eval(selector, pageFunction, ...args);351 }352 /**353 * @param {string} selector354 * @param {Function|String} pageFunction355 * @param {!Array<*>} args356 * @return {!Promise<(!Object|undefined)>}357 */358 async $$eval(selector, pageFunction, ...args) {359 return this._mainWorld.$$eval(selector, pageFunction, ...args);360 }361 /**362 * @param {string} expression363 * @return {!Promise<!Array<!ElementHandle>>}364 */365 async $x(expression) {366 return this._mainWorld.$x(expression);367 }368 async evaluateHandle(pageFunction, ...args) {369 return this._mainWorld.evaluateHandle(pageFunction, ...args);370 }371 /**372 * @param {!{content?: string, path?: string, type?: string, url?: string}} options373 * @return {!Promise<!ElementHandle>}374 */375 async addScriptTag(options) {376 return this._mainWorld.addScriptTag(options);377 }378 /**379 * @param {!{content?: string, path?: string, url?: string}} options380 * @return {!Promise<!ElementHandle>}381 */382 async addStyleTag(options) {383 return this._mainWorld.addStyleTag(options);384 }385 /**386 * @return {!Promise<string>}387 */388 async title() {389 return this._mainWorld.title();390 }391 name() {392 return this._name;393 }394 isDetached() {395 return this._detached;396 }397 childFrames() {398 return Array.from(this._children);399 }400 url() {401 return this._url;402 }403 parentFrame() {404 return this._parentFrame;405 }406}407function normalizeWaitUntil(waitUntil) {408 if (!Array.isArray(waitUntil))409 waitUntil = [waitUntil];410 for (const condition of waitUntil) {411 if (condition !== 'load' && condition !== 'domcontentloaded')412 throw new Error('Unknown waitUntil condition: ' + condition);413 }414 return waitUntil;415}...
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!!