@@ -13,6 +13,8 @@ const {
1313 getFileInfoFromPath,
1414 extractFileNameWithoutExtension,
1515 plural,
16+ sleep,
17+ waitForConditionTrue,
1618} = require ( './utils.js' ) ;
1719const consts = require ( './consts.js' ) ;
1820const { Options } = require ( './options.js' ) ;
@@ -203,7 +205,7 @@ async function runAllCommands(loaded, logs, options, browser) {
203205 page = await browser . newPage ( options , currentFile , logs ) ;
204206 } catch ( e ) {
205207 // try again after waiting a bit first to avoid "Session with given id not found" error...
206- await new Promise ( r => setTimeout ( r , 100 ) ) ;
208+ await sleep ( 100 ) ;
207209 page = await browser . newPage ( options , currentFile , logs ) ;
208210 }
209211 await browser . emulate ( options , currentFile , page , logs ) ;
@@ -419,14 +421,19 @@ async function runAllCommands(loaded, logs, options, browser) {
419421 }
420422 if ( shouldWait ) {
421423 // We wait a bit between each command to be sure the browser can follow.
422- await new Promise ( r => setTimeout ( r , 50 ) ) ;
424+ await sleep ( 50 ) ;
423425 }
424426 // If the URL changed, we wait for the document to be fully loaded before running other
425427 // commands.
426428 const url = page . url ( ) ;
427429 if ( url !== current_url ) {
428430 current_url = url ;
429- await page . waitForFunction ( 'document.readyState === "complete"' ) ;
431+ if ( ! await waitForConditionTrue ( pages , async ( ) => {
432+ return await page . evaluate ( ( ) => 'return document.readyState === "complete"' ) ;
433+ } ) ) {
434+ logs . error ( currentFile , `\`${ url } \` never finished loading` ) ;
435+ break command_loop;
436+ }
430437 }
431438 if ( checkJsErrors ( ) || checkRequestErrors ( ) ) {
432439 break command_loop;
0 commit comments