Skip to content

Commit 80f6c8f

Browse files
Merge pull request #273 from GuillaumeGomez/reduce-puppeteer-failures
Reduce puppeteer failures
2 parents 6b8dc63 + 61374e3 commit 80f6c8f

File tree

3 files changed

+54
-16
lines changed

3 files changed

+54
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ The list of fields of the `Options` class is the following:
177177
* `generateImages`: if provided, it'll generate test images and won't run comparison tests
178178
* `imageFolder`: path of the folder where screenshots are and where they are generated (`testFolder` value by default)
179179
* `noHeadless`: disable headless mode
180-
* `noScreenshot`: disable screenshots generation and comparison at the end of the scripts
180+
* `noScreenshotComparison`: disable screenshots generation and comparison at the end of the scripts
181181
* `onPageCreatedCallback`: callback which is called when a new puppeteer page is created. It provides the puppeteer `page` and the test name as arguments.
182182
* `pauseOnError`: will pause indefinitely if an error occurs.
183183
* `permissions`: list of permissions to enable (you can see the full list by running with `--show-permissions`)

src/index.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -435,20 +435,25 @@ async function innerRunTests(logs, options) {
435435
return [logs.logs, failures];
436436
}
437437

438-
const browser = await utils.loadPuppeteer(options);
439-
for (let i = 0; i < loaded.length; ++i) {
440-
const ret = await runCommand(loaded[i], logs, options, browser);
441-
if (ret !== Status.Ok) {
442-
failures += 1;
438+
try {
439+
const browser = await utils.loadPuppeteer(options);
440+
for (let i = 0; i < loaded.length; ++i) {
441+
const ret = await runCommand(loaded[i], logs, options, browser);
442+
if (ret !== Status.Ok) {
443+
failures += 1;
444+
}
443445
}
444-
}
445-
await browser.close();
446+
await browser.close();
446447

447-
logs.append(
448-
'\n<= doc-ui tests done: ' + (total - failures) + ' succeeded, ' + failures + ' failed');
448+
logs.append(`\n<= doc-ui tests done: ${total - failures} succeeded, ${failures} failed`);
449449

450-
if (logs.showLogs === true) {
451-
logs.append('');
450+
if (logs.showLogs === true) {
451+
logs.append('');
452+
}
453+
} catch (error) {
454+
logs.append(`An exception occured: ${error.message}\n== STACKTRACE ==\n` +
455+
`${new Error().stack}`);
456+
return [logs.logs, 1];
452457
}
453458

454459
return [logs.logs, failures];
@@ -475,13 +480,13 @@ async function innerRunTestCode(testName, content, options, showLogs, checkTestF
475480
if (ret !== Status.Ok) {
476481
return [logs.logs, 1];
477482
}
478-
479-
return [logs.logs, 0];
480483
} catch (error) {
481484
logs.append(`An exception occured: ${error.message}\n== STACKTRACE ==\n` +
482485
`${new Error().stack}`);
483486
return [logs.logs, 1];
484487
}
488+
489+
return [logs.logs, 0];
485490
}
486491

487492
async function runTestCode(testName, content, options = new Options(), showLogs = false) {

src/puppeteer-wrapper.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ function buildPuppeteerOptions(options) {
1818
return puppeteer_options;
1919
}
2020

21+
function check_if_known_error(error) {
22+
const KNOWN_ERRORS = [
23+
'Failed to launch the browser process!',
24+
'read ECONNRESET',
25+
];
26+
27+
for (const known_error of KNOWN_ERRORS) {
28+
if (error.message.indexOf(known_error) !== -1) {
29+
return true;
30+
}
31+
}
32+
return false;
33+
}
34+
2135
class PuppeteerWrapper {
2236
constructor() {
2337
this.puppeteer = require('puppeteer');
@@ -26,7 +40,24 @@ class PuppeteerWrapper {
2640
}
2741

2842
async init(options) {
29-
this.browser = await this.puppeteer.launch(buildPuppeteerOptions(options));
43+
let i;
44+
let last_error;
45+
46+
for (i = 0; i < 3; ++i) {
47+
try {
48+
this.browser = await this.puppeteer.launch(buildPuppeteerOptions(options));
49+
} catch (error) {
50+
if (!check_if_known_error(error)) {
51+
throw error;
52+
}
53+
last_error = error;
54+
continue;
55+
}
56+
break;
57+
}
58+
if (i === 3) {
59+
throw last_error;
60+
}
3061
if (options.incognito === true) {
3162
this.context = await this.browser.createIncognitoBrowserContext();
3263
}
@@ -49,7 +80,9 @@ class PuppeteerWrapper {
4980
await this.context.close();
5081
this.context = null;
5182
}
52-
await this.browser.close();
83+
if (this.browser !== null) {
84+
await this.browser.close();
85+
}
5386
}
5487

5588
async overridePermissions(url, permissions) {

0 commit comments

Comments
 (0)