From 2f74644cb8576c86a27e137f300f761378489bd8 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Sun, 14 May 2017 22:17:57 -0700 Subject: [PATCH] Puppeteer's Page.navigate should not throw with invalid URL. This patch makes puppeteer's Page.navigate to just return 'false' when given an invalid URL. --- lib/Page.js | 6 +++++- test/test.js | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/Page.js b/lib/Page.js index e528af2a..00974e11 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -260,7 +260,11 @@ class Page extends EventEmitter { var interstitialPromise = new Promise(fulfill => this._client.once('Security.certificateError', fulfill)).then(() => false); var referrer = this._extraHeaders.referer; // Await for the command to throw exception in case of illegal arguments. - await this._client.send('Page.navigate', {url, referrer}); + try { + await this._client.send('Page.navigate', {url, referrer}); + } catch (e) { + return false; + } return await Promise.race([loadPromise, interstitialPromise]); } diff --git a/test/test.js b/test/test.js index ef4cbf8d..283906fa 100644 --- a/test/test.js +++ b/test/test.js @@ -40,6 +40,17 @@ describe('Puppeteer', function() { expect(msgs).toEqual(['Message!']); })); + describe('Page.navigate', function() { + it('should fail when navigating to bad url', SX(async function() { + var success = await page.navigate('asdfasdf'); + expect(success).toBe(false); + })); + it('should succeed when navigating to good url', SX(async function() { + var success = await page.navigate(EMPTY_PAGE); + expect(success).toBe(true); + })); + }); + describe('Page.setInPageCallback', function() { it('should work', SX(async function() { await page.setInPageCallback('callController', function(a, b) {