Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const {
getFileInfoFromPath,
extractFileNameWithoutExtension,
plural,
sleep,
waitForConditionTrue,
} = require('./utils.js');
const consts = require('./consts.js');
const { Options } = require('./options.js');
Expand Down Expand Up @@ -203,7 +205,7 @@ async function runAllCommands(loaded, logs, options, browser) {
page = await browser.newPage(options, currentFile, logs);
} catch (e) {
// try again after waiting a bit first to avoid "Session with given id not found" error...
await new Promise(r => setTimeout(r, 100));
await sleep(100);
page = await browser.newPage(options, currentFile, logs);
}
await browser.emulate(options, currentFile, page, logs);
Expand Down Expand Up @@ -419,14 +421,19 @@ async function runAllCommands(loaded, logs, options, browser) {
}
if (shouldWait) {
// We wait a bit between each command to be sure the browser can follow.
await new Promise(r => setTimeout(r, 50));
await sleep(50);
}
// If the URL changed, we wait for the document to be fully loaded before running other
// commands.
const url = page.url();
if (url !== current_url) {
current_url = url;
await page.waitForFunction('document.readyState === "complete"');
if (!await waitForConditionTrue(pages, async() => {
return await page.evaluate(() => 'return document.readyState === "complete"');
})) {
logs.error(currentFile, `\`${url}\` never finished loading`);
break command_loop;
}
}
if (checkJsErrors() || checkRequestErrors()) {
break command_loop;
Expand Down
26 changes: 26 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,30 @@ function getFileInfo(context_parser, line = null, isExact = true) {
return getFileInfoFromPath(context_parser.getCurrentFile(), line, isExact);
}

async function sleep(ms) {
await new Promise(r => setTimeout(r, ms));
}

async function waitForConditionTrue(pages, callback) {
const timeLimit = pages[0].getDefaultTimeout();
const timeAdd = 50;
let allTime = 0;

// eslint-disable-next-line no-constant-condition
while (true) {
if (await callback()) {
return true;
}
if (timeLimit === 0) {
continue;
}
allTime += timeAdd;
if (allTime >= timeLimit) {
return false;
}
}
}

module.exports = {
'addSlash': addSlash,
'getCurrentDir': getCurrentDir,
Expand All @@ -218,5 +242,7 @@ module.exports = {
'hasError': hasError,
'getFileInfo': getFileInfo,
'getFileInfoFromPath': getFileInfoFromPath,
'sleep': sleep,
'waitForConditionTrue': waitForConditionTrue,
'ALLOWED_EMULATE_MEDIA_FEATURES_KEYS': ALLOWED_EMULATE_MEDIA_FEATURES_KEYS,
};