question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add option to page.goto() to retry on ERR_CONNECTION_REFUSED

See original GitHub issue

I start servers and run Puppeteer tests at roughly the same time. When tests first attempt page.goto(), it fails with ERR_CONNECTION_REFUSED because server hasn’t finished initializing. So I wrote manual retries for page.goto().

It would be nice if there was an option to page.goto() to implement the retry logic itself. This would be the equivalent of hitting refresh in the browser, until page loads.

I would think this would be a common request, but it appears it’s not. I’m interested to hear how people automate their deployment such that server initialization is guaranteed to be done before Puppeteer tests are run.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
Xamplecommented, Jan 13, 2019

@curran nice snippet! for people trying to type it in TS:

// retry.ts
export const retry: <T>(fn: () => Promise<T>, ms: number) => Promise<T> = <T>(fn: () => Promise<T>, ms: number) => new Promise<T>((resolve) => {
    fn()
        .then(resolve)
        .catch(() => {
            setTimeout(() => {
                console.log('retrying...');
                retry(fn, ms).then(resolve);
            }, ms);
        });
});

Just because it was cumbersome to do it…

4reactions
currancommented, Jul 6, 2018

Retying outside the Puppeteer API makes sense. Looks like not required as part of Puppeteer.

Here’s the solution I’m using:

const retry = (fn, ms) => new Promise(resolve => { 
  fn()
    .then(resolve)
    .catch(() => {
      setTimeout(() => {
        console.log('retrying...');
        retry(fn, ms).then(resolve);
      }, ms);
    })
});

Here’s the code that calls it (inspired by UI testing with Puppeteer and Mocha):

it('should open page', async () => {
  browser = await puppeteer.launch({ headless: false });
  page = await browser.newPage();
  const response = await retry(() => page.goto('http://localhost:3000'), 1000);
  assert.equal(response.status(), 200);
});

This works well for waiting for the server to spin up. Just like I would do when manually testing. Refresh, refresh, refresh, boom!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix the ERR_CONNECTION_REFUSED Error in Chrome
Typically this involves the following steps: Check to see whether the page itself has gone down. Restart your router. Clear your browser's cache ......
Read more >
ERR_CONNECTION_REFUSED: What It Is and 11 Ways to Fix It
The ERR_CONNECTION_REFUSED error message appears on Google Chrome when the browser's attempt to connect to a web page is refused.
Read more >
Best Practice for retrying page.goto, page.waitForNavigation ...
I would recommend this rather simple approach: async function retry(promiseFactory, retryCount) { try { return await promiseFactory(); } ...
Read more >
How to Fix the ERR_CONNECTION_REFUSED Error in Chrome
Solution 1: Check the Website Status · Solution 2: Restart Your Router · Solution 3: Clear the Browser Cache · Solution 4: Check...
Read more >
Can't connect to node HTTP server running on localhost
newPage() const siteUrl = 'http://localhost:9000' // process.env. ... siteUrl) await page.goto(siteUrl, {waitUntil: 'networkidle2'}) const ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found