Last active
May 31, 2018 07:42
-
-
Save Bjorn-Eric-Abr/e12b0e4a445127af685ee5be66641c84 to your computer and use it in GitHub Desktop.
Puppeteer – Screenshot array of URLs and handling timeout (unhandledRejection)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @name Puppeteer – Screenshot array of URLs and handling timeout (unhandledRejection) | |
| * | |
| * @desc Takes a screenshot of every URL in array and handles timeout | |
| * (Times out on purpose) | |
| * | |
| */ | |
| const puppeteer = require( 'puppeteer' ); | |
| let urlsToShoot = [ 'https://www.google.se', 'https://github.com/' ] | |
| let url = '' | |
| process.on( "unhandledRejection", ( reason, p ) => { | |
| console.error( "Failed to take screenshot of", url, "Unhandled Rejection at: Promise", p, "reason:", reason); | |
| // Do your handling here | |
| shoot() | |
| } ); | |
| async function render( url ) { | |
| let name = url.replace( 'https:\/\/', '' ).replace( /\//g, '-' ).replace( /\./g, '-' ).replace( /--+$/, '' ); | |
| const browser = await puppeteer.launch( { | |
| args: [ '--no-sandbox' ] | |
| } ); | |
| const page = await browser.newPage() | |
| await page.setViewport( { | |
| width: 1280, | |
| height: 800 | |
| } ) | |
| await page.goto( url, { | |
| waitUntil: "networkidle2", | |
| timeout: 40 | |
| } ) | |
| await page.screenshot( { | |
| path: `${name}.png`, | |
| fullPage: true | |
| } ) | |
| await browser.close() | |
| } | |
| async function shoot() { | |
| if ( urlsToShoot.length > 0 ) { | |
| url = urlsToShoot.pop(); | |
| console.log( `Taking screenshot of ${url}` ) | |
| await render( url ) | |
| await shoot() | |
| } else { | |
| console.log( 'Done and done!' ) | |
| process.exit(); | |
| } | |
| } | |
| shoot(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment