/** * Copyright 2018 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const utils = require('./utils'); const {TimeoutError} = utils.requireRoot('Errors'); module.exports.addTests = function({testRunner, expect}) { const {describe, xdescribe, fdescribe} = testRunner; const {it, fit, xit} = testRunner; const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; describe('Page.goto', function() { it('should navigate to about:blank', async({page, server}) => { const response = await page.goto('about:blank'); expect(response).toBe(null); }); it('should return response when page changes its URL after load', async({page, server}) => { const response = await page.goto(server.PREFIX + '/historyapi.html'); expect(response.status()).toBe(200); }); it('should work with subframes return 204', async({page, server}) => { server.setRoute('/frames/frame.html', (req, res) => { res.statusCode = 204; res.end(); }); await page.goto(server.PREFIX + '/frames/one-frame.html'); }); it('should fail when server returns 204', async({page, server}) => { server.setRoute('/empty.html', (req, res) => { res.statusCode = 204; res.end(); }); let error = null; await page.goto(server.EMPTY_PAGE).catch(e => error = e); expect(error).not.toBe(null); expect(error.message).toContain('net::ERR_ABORTED'); }); it('should navigate to empty page with domcontentloaded', async({page, server}) => { const response = await page.goto(server.EMPTY_PAGE, {waitUntil: 'domcontentloaded'}); expect(response.status()).toBe(200); expect(response.securityDetails()).toBe(null); }); it('should work when page calls history API in beforeunload', async({page, server}) => { await page.goto(server.EMPTY_PAGE); await page.evaluate(() => { window.addEventListener('beforeunload', () => history.replaceState(null, 'initial', window.location.href), false); }); const response = await page.goto(server.PREFIX + '/grid.html'); expect(response.status()).toBe(200); }); it('should navigate to empty page with networkidle0', async({page, server}) => { const response = await page.goto(server.EMPTY_PAGE, {waitUntil: 'networkidle0'}); expect(response.status()).toBe(200); }); it('should navigate to empty page with networkidle2', async({page, server}) => { const response = await page.goto(server.EMPTY_PAGE, {waitUntil: 'networkidle2'}); expect(response.status()).toBe(200); }); it('should fail when navigating to bad url', async({page, server}) => { let error = null; await page.goto('asdfasdf').catch(e => error = e); expect(error.message).toContain('Cannot navigate to invalid URL'); }); it('should fail when navigating to bad SSL', async({page, httpsServer}) => { // Make sure that network events do not emit 'undefined'. // @see https://crbug.com/750469 page.on('request', request => expect(request).toBeTruthy()); page.on('requestfinished', request => expect(request).toBeTruthy()); page.on('requestfailed', request => expect(request).toBeTruthy()); let error = null; await page.goto(httpsServer.EMPTY_PAGE).catch(e => error = e); expect(error.message).toContain('net::ERR_CERT_AUTHORITY_INVALID'); }); it('should fail when navigating to bad SSL after redirects', async({page, server, httpsServer}) => { server.setRedirect('/redirect/1.html', '/redirect/2.html'); server.setRedirect('/redirect/2.html', '/empty.html'); let error = null; await page.goto(httpsServer.PREFIX + '/redirect/1.html').catch(e => error = e); expect(error.message).toContain('net::ERR_CERT_AUTHORITY_INVALID'); }); it('should throw if networkidle is passed as an option', async({page, server}) => { let error = null; await page.goto(server.EMPTY_PAGE, {waitUntil: 'networkidle'}).catch(err => error = err); expect(error.message).toContain('"networkidle" option is no longer supported'); }); it('should fail when main resources failed to load', async({page, server}) => { let error = null; await page.goto('http://localhost:44123/non-existing-url').catch(e => error = e); expect(error.message).toContain('net::ERR_CONNECTION_REFUSED'); }); it('should fail when exceeding maximum navigation timeout', async({page, server}) => { // Hang for request to the empty.html server.setRoute('/empty.html', (req, res) => { }); let error = null; await page.goto(server.PREFIX + '/empty.html', {timeout: 1}).catch(e => error = e); expect(error.message).toContain('Navigation Timeout Exceeded: 1ms'); expect(error).toBeInstanceOf(TimeoutError); }); it('should fail when exceeding default maximum navigation timeout', async({page, server}) => { // Hang for request to the empty.html server.setRoute('/empty.html', (req, res) => { }); let error = null; page.setDefaultNavigationTimeout(1); await page.goto(server.PREFIX + '/empty.html').catch(e => error = e); expect(error.message).toContain('Navigation Timeout Exceeded: 1ms'); expect(error).toBeInstanceOf(TimeoutError); }); it('should disable timeout when its set to 0', async({page, server}) => { let error = null; let loaded = false; page.once('load', () => loaded = true); await page.goto(server.PREFIX + '/grid.html', {timeout: 0, waitUntil: ['load']}).catch(e => error = e); expect(error).toBe(null); expect(loaded).toBe(true); }); it('should work when navigating to valid url', async({page, server}) => { const response = await page.goto(server.EMPTY_PAGE); expect(response.ok()).toBe(true); }); it('should work when navigating to data url', async({page, server}) => { const response = await page.goto('data:text/html,hello'); expect(response.ok()).toBe(true); }); it('should work when navigating to 404', async({page, server}) => { const response = await page.goto(server.PREFIX + '/not-found'); expect(response.ok()).toBe(false); expect(response.status()).toBe(404); }); it('should return last response in redirect chain', async({page, server}) => { server.setRedirect('/redirect/1.html', '/redirect/2.html'); server.setRedirect('/redirect/2.html', '/redirect/3.html'); server.setRedirect('/redirect/3.html', server.EMPTY_PAGE); const response = await page.goto(server.PREFIX + '/redirect/1.html'); expect(response.ok()).toBe(true); expect(response.url()).toBe(server.EMPTY_PAGE); }); it('should wait for network idle to succeed navigation', async({page, server}) => { let responses = []; // Hold on to a bunch of requests without answering. server.setRoute('/fetch-request-a.js', (req, res) => responses.push(res)); server.setRoute('/fetch-request-b.js', (req, res) => responses.push(res)); server.setRoute('/fetch-request-c.js', (req, res) => responses.push(res)); server.setRoute('/fetch-request-d.js', (req, res) => responses.push(res)); const initialFetchResourcesRequested = Promise.all([ server.waitForRequest('/fetch-request-a.js'), server.waitForRequest('/fetch-request-b.js'), server.waitForRequest('/fetch-request-c.js'), ]); const secondFetchResourceRequested = server.waitForRequest('/fetch-request-d.js'); // Navigate to a page which loads immediately and then does a bunch of // requests via javascript's fetch method. const navigationPromise = page.goto(server.PREFIX + '/networkidle.html', { waitUntil: 'networkidle0', }); // Track when the navigation gets completed. let navigationFinished = false; navigationPromise.then(() => navigationFinished = true); // Wait for the page's 'load' event. await new Promise(fulfill => page.once('load', fulfill)); expect(navigationFinished).toBe(false); // Wait for the initial three resources to be requested. await initialFetchResourcesRequested; // Expect navigation still to be not finished. expect(navigationFinished).toBe(false); // Respond to initial requests. for (const response of responses) { response.statusCode = 404; response.end(`File not found`); } // Reset responses array responses = []; // Wait for the second round to be requested. await secondFetchResourceRequested; // Expect navigation still to be not finished. expect(navigationFinished).toBe(false); // Respond to requests. for (const response of responses) { response.statusCode = 404; response.end(`File not found`); } const response = await navigationPromise; // Expect navigation to succeed. expect(response.ok()).toBe(true); }); it('should not leak listeners during navigation', async({page, server}) => { let warning = null; const warningHandler = w => warning = w; process.on('warning', warningHandler); for (let i = 0; i < 20; ++i) await page.goto(server.EMPTY_PAGE); process.removeListener('warning', warningHandler); expect(warning).toBe(null); }); it('should not leak listeners during bad navigation', async({page, server}) => { let warning = null; const warningHandler = w => warning = w; process.on('warning', warningHandler); for (let i = 0; i < 20; ++i) await page.goto('asdf').catch(e => {/* swallow navigation error */}); process.removeListener('warning', warningHandler); expect(warning).toBe(null); }); it('should not leak listeners during navigation of 11 pages', async({page, context, server}) => { let warning = null; const warningHandler = w => warning = w; process.on('warning', warningHandler); await Promise.all([...Array(20)].map(async() => { const page = await context.newPage(); await page.goto(server.EMPTY_PAGE); await page.close(); })); process.removeListener('warning', warningHandler); expect(warning).toBe(null); }); it('should navigate to dataURL and fire dataURL requests', async({page, server}) => { const requests = []; page.on('request', request => requests.push(request)); const dataURL = 'data:text/html,