2018-03-16 22:33:31 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 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 fs = require('fs');
|
|
|
|
const os = require('os');
|
|
|
|
const path = require('path');
|
|
|
|
const {helper} = require('../lib/helper');
|
2018-06-20 20:52:36 +00:00
|
|
|
const rmAsync = helper.promisify(require('rimraf'));
|
2018-03-16 22:33:31 +00:00
|
|
|
const mkdtempAsync = helper.promisify(fs.mkdtemp);
|
|
|
|
const readFileAsync = helper.promisify(fs.readFile);
|
2018-04-10 21:11:59 +00:00
|
|
|
const statAsync = helper.promisify(fs.stat);
|
2018-03-16 22:33:31 +00:00
|
|
|
const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-');
|
2018-03-20 03:00:12 +00:00
|
|
|
const utils = require('./utils');
|
2018-08-09 23:51:12 +00:00
|
|
|
const puppeteer = utils.requireRoot('index');
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-08-09 23:51:12 +00:00
|
|
|
module.exports.addTests = function({testRunner, expect, defaultBrowserOptions}) {
|
2018-03-20 03:00:12 +00:00
|
|
|
const {describe, xdescribe, fdescribe} = testRunner;
|
|
|
|
const {it, fit, xit} = testRunner;
|
|
|
|
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
|
2018-03-16 22:33:31 +00:00
|
|
|
|
|
|
|
describe('Puppeteer', function() {
|
|
|
|
describe('BrowserFetcher', function() {
|
|
|
|
it('should download and extract linux binary', async({server}) => {
|
|
|
|
const downloadsFolder = await mkdtempAsync(TMP_FOLDER);
|
|
|
|
const browserFetcher = puppeteer.createBrowserFetcher({
|
|
|
|
platform: 'linux',
|
|
|
|
path: downloadsFolder,
|
|
|
|
host: server.PREFIX
|
|
|
|
});
|
|
|
|
let revisionInfo = browserFetcher.revisionInfo('123456');
|
|
|
|
server.setRoute(revisionInfo.url.substring(server.PREFIX.length), (req, res) => {
|
|
|
|
server.serveFile(req, res, '/chromium-linux.zip');
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(revisionInfo.local).toBe(false);
|
|
|
|
expect(browserFetcher.platform()).toBe('linux');
|
|
|
|
expect(await browserFetcher.canDownload('100000')).toBe(false);
|
|
|
|
expect(await browserFetcher.canDownload('123456')).toBe(true);
|
|
|
|
|
|
|
|
revisionInfo = await browserFetcher.download('123456');
|
|
|
|
expect(revisionInfo.local).toBe(true);
|
|
|
|
expect(await readFileAsync(revisionInfo.executablePath, 'utf8')).toBe('LINUX BINARY\n');
|
2018-04-10 21:11:59 +00:00
|
|
|
const expectedPermissions = os.platform() === 'win32' ? 0666 : 0755;
|
|
|
|
expect((await statAsync(revisionInfo.executablePath)).mode & 0777).toBe(expectedPermissions);
|
2018-03-16 22:33:31 +00:00
|
|
|
expect(await browserFetcher.localRevisions()).toEqual(['123456']);
|
|
|
|
await browserFetcher.remove('123456');
|
|
|
|
expect(await browserFetcher.localRevisions()).toEqual([]);
|
2018-06-20 20:52:36 +00:00
|
|
|
await rmAsync(downloadsFolder);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
|
|
|
});
|
2018-09-14 18:44:54 +00:00
|
|
|
describe('Browser.disconnect', function() {
|
|
|
|
it('should reject navigation when browser closes', async({server}) => {
|
|
|
|
server.setRoute('/one-style.css', () => {});
|
|
|
|
const browser = await puppeteer.launch(defaultBrowserOptions);
|
|
|
|
const remote = await puppeteer.connect({browserWSEndpoint: browser.wsEndpoint()});
|
|
|
|
const page = await remote.newPage();
|
|
|
|
const navigationPromise = page.goto(server.PREFIX + '/one-style.html', {timeout: 60000}).catch(e => e);
|
|
|
|
await server.waitForRequest('/one-style.css');
|
|
|
|
await remote.disconnect();
|
|
|
|
const error = await navigationPromise;
|
|
|
|
expect(error.message).toBe('Navigation failed because browser has disconnected!');
|
|
|
|
await browser.close();
|
|
|
|
});
|
|
|
|
it('should reject waitForSelector when browser closes', async({server}) => {
|
|
|
|
server.setRoute('/empty.html', () => {});
|
|
|
|
const browser = await puppeteer.launch(defaultBrowserOptions);
|
|
|
|
const remote = await puppeteer.connect({browserWSEndpoint: browser.wsEndpoint()});
|
|
|
|
const page = await remote.newPage();
|
|
|
|
const watchdog = page.waitForSelector('div', {timeout: 60000}).catch(e => e);
|
|
|
|
await remote.disconnect();
|
|
|
|
const error = await watchdog;
|
|
|
|
expect(error.message).toBe('Protocol error (Runtime.callFunctionOn): Session closed. Most likely the page has been closed.');
|
|
|
|
await browser.close();
|
|
|
|
});
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
describe('Puppeteer.launch', function() {
|
|
|
|
it('should reject all promises when browser is closed', async() => {
|
|
|
|
const browser = await puppeteer.launch(defaultBrowserOptions);
|
|
|
|
const page = await browser.newPage();
|
|
|
|
let error = null;
|
|
|
|
const neverResolves = page.evaluate(() => new Promise(r => {})).catch(e => error = e);
|
|
|
|
await browser.close();
|
|
|
|
await neverResolves;
|
|
|
|
expect(error.message).toContain('Protocol error');
|
|
|
|
});
|
|
|
|
it('should reject if executable path is invalid', async({server}) => {
|
|
|
|
let waitError = null;
|
|
|
|
const options = Object.assign({}, defaultBrowserOptions, {executablePath: 'random-invalid-path'});
|
|
|
|
await puppeteer.launch(options).catch(e => waitError = e);
|
|
|
|
expect(waitError.message.startsWith('Failed to launch chrome! spawn random-invalid-path ENOENT')).toBe(true);
|
|
|
|
});
|
|
|
|
it('userDataDir option', async({server}) => {
|
|
|
|
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
|
|
|
const options = Object.assign({userDataDir}, defaultBrowserOptions);
|
|
|
|
const browser = await puppeteer.launch(options);
|
2018-05-17 21:49:03 +00:00
|
|
|
// Open a page to make sure its functional.
|
|
|
|
await browser.newPage();
|
2018-03-16 22:33:31 +00:00
|
|
|
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
|
|
|
await browser.close();
|
|
|
|
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
2018-06-20 20:52:36 +00:00
|
|
|
// This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
|
|
|
|
await rmAsync(userDataDir).catch(e => {});
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
|
|
|
it('userDataDir argument', async({server}) => {
|
|
|
|
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
|
|
|
const options = Object.assign({}, defaultBrowserOptions);
|
2018-11-13 07:26:16 +00:00
|
|
|
options.args = [`--user-data-dir=${userDataDir}`].concat(options.args || []);
|
2018-03-16 22:33:31 +00:00
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
|
|
|
await browser.close();
|
|
|
|
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
2018-06-20 20:52:36 +00:00
|
|
|
// This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
|
|
|
|
await rmAsync(userDataDir).catch(e => {});
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
|
|
|
it('userDataDir option should restore state', async({server}) => {
|
|
|
|
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
|
|
|
const options = Object.assign({userDataDir}, defaultBrowserOptions);
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
const page = await browser.newPage();
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.evaluate(() => localStorage.hey = 'hello');
|
|
|
|
await browser.close();
|
|
|
|
|
|
|
|
const browser2 = await puppeteer.launch(options);
|
|
|
|
const page2 = await browser2.newPage();
|
|
|
|
await page2.goto(server.EMPTY_PAGE);
|
|
|
|
expect(await page2.evaluate(() => localStorage.hey)).toBe('hello');
|
|
|
|
await browser2.close();
|
2018-06-20 20:52:36 +00:00
|
|
|
// This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
|
|
|
|
await rmAsync(userDataDir).catch(e => {});
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-07 04:53:25 +00:00
|
|
|
it('userDataDir option should restore cookies', async({server}) => {
|
2018-03-16 22:33:31 +00:00
|
|
|
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
|
|
|
const options = Object.assign({userDataDir}, defaultBrowserOptions);
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
const page = await browser.newPage();
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.evaluate(() => document.cookie = 'doSomethingOnlyOnce=true; expires=Fri, 31 Dec 9999 23:59:59 GMT');
|
|
|
|
await browser.close();
|
|
|
|
|
|
|
|
const browser2 = await puppeteer.launch(options);
|
|
|
|
const page2 = await browser2.newPage();
|
|
|
|
await page2.goto(server.EMPTY_PAGE);
|
|
|
|
expect(await page2.evaluate(() => document.cookie)).toBe('doSomethingOnlyOnce=true');
|
|
|
|
await browser2.close();
|
2018-06-20 20:52:36 +00:00
|
|
|
// This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
|
|
|
|
await rmAsync(userDataDir).catch(e => {});
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
|
|
|
it('should return the default chrome arguments', async() => {
|
2018-08-07 20:22:04 +00:00
|
|
|
expect(puppeteer.defaultArgs()).toContain('--no-first-run');
|
|
|
|
expect(puppeteer.defaultArgs()).toContain('--headless');
|
|
|
|
expect(puppeteer.defaultArgs({headless: false})).not.toContain('--headless');
|
|
|
|
expect(puppeteer.defaultArgs({userDataDir: 'foo'})).toContain('--user-data-dir=foo');
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
|
|
|
it('should dump browser process stderr', async({server}) => {
|
|
|
|
const dumpioTextToLog = 'MAGIC_DUMPIO_TEST';
|
|
|
|
let dumpioData = '';
|
|
|
|
const {spawn} = require('child_process');
|
2018-05-29 22:45:03 +00:00
|
|
|
const options = Object.assign({}, defaultBrowserOptions, {dumpio: true});
|
2018-03-16 22:33:31 +00:00
|
|
|
const res = spawn('node',
|
2018-08-09 23:51:12 +00:00
|
|
|
[path.join(__dirname, 'fixtures', 'dumpio.js'), utils.projectRoot(), JSON.stringify(options), server.EMPTY_PAGE, dumpioTextToLog]);
|
2018-03-16 22:33:31 +00:00
|
|
|
res.stderr.on('data', data => dumpioData += data.toString('utf8'));
|
|
|
|
await new Promise(resolve => res.on('close', resolve));
|
|
|
|
|
|
|
|
expect(dumpioData).toContain(dumpioTextToLog);
|
|
|
|
});
|
|
|
|
it('should close the browser when the node process closes', async({ server }) => {
|
|
|
|
const {spawn, execSync} = require('child_process');
|
2018-08-09 23:51:12 +00:00
|
|
|
const res = spawn('node', [path.join(__dirname, 'fixtures', 'closeme.js'), utils.projectRoot(), JSON.stringify(defaultBrowserOptions)]);
|
2018-03-16 22:33:31 +00:00
|
|
|
let wsEndPointCallback;
|
|
|
|
const wsEndPointPromise = new Promise(x => wsEndPointCallback = x);
|
|
|
|
let output = '';
|
|
|
|
res.stdout.on('data', data => {
|
|
|
|
output += data;
|
|
|
|
if (output.indexOf('\n'))
|
|
|
|
wsEndPointCallback(output.substring(0, output.indexOf('\n')));
|
|
|
|
});
|
|
|
|
const browser = await puppeteer.connect({ browserWSEndpoint: await wsEndPointPromise });
|
|
|
|
const promises = [
|
|
|
|
new Promise(resolve => browser.once('disconnected', resolve)),
|
|
|
|
new Promise(resolve => res.on('close', resolve))];
|
|
|
|
if (process.platform === 'win32')
|
|
|
|
execSync(`taskkill /pid ${res.pid} /T /F`);
|
|
|
|
else
|
|
|
|
process.kill(res.pid);
|
|
|
|
await Promise.all(promises);
|
|
|
|
});
|
2018-04-03 22:05:27 +00:00
|
|
|
it('should support the pipe option', async() => {
|
|
|
|
const options = Object.assign({pipe: true}, defaultBrowserOptions);
|
|
|
|
const browser = await puppeteer.launch(options);
|
2018-06-01 20:57:50 +00:00
|
|
|
expect((await browser.pages()).length).toBe(1);
|
2018-04-03 22:05:27 +00:00
|
|
|
expect(browser.wsEndpoint()).toBe('');
|
|
|
|
const page = await browser.newPage();
|
|
|
|
expect(await page.evaluate('11 * 11')).toBe(121);
|
|
|
|
await page.close();
|
|
|
|
await browser.close();
|
|
|
|
});
|
|
|
|
it('should support the pipe argument', async() => {
|
|
|
|
const options = Object.assign({}, defaultBrowserOptions);
|
2018-11-13 07:26:16 +00:00
|
|
|
options.args = ['--remote-debugging-pipe'].concat(options.args || []);
|
2018-04-03 22:05:27 +00:00
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
expect(browser.wsEndpoint()).toBe('');
|
|
|
|
const page = await browser.newPage();
|
|
|
|
expect(await page.evaluate('11 * 11')).toBe(121);
|
|
|
|
await page.close();
|
|
|
|
await browser.close();
|
|
|
|
});
|
2018-10-31 23:31:29 +00:00
|
|
|
it('should fire "disconnected" when closing with pipe', async() => {
|
|
|
|
const options = Object.assign({pipe: true}, defaultBrowserOptions);
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
const disconnectedEventPromise = new Promise(resolve => browser.once('disconnected', resolve));
|
|
|
|
// Emulate user exiting browser.
|
|
|
|
browser.process().kill();
|
|
|
|
await disconnectedEventPromise;
|
|
|
|
});
|
2018-04-03 22:05:27 +00:00
|
|
|
it('should work with no default arguments', async() => {
|
|
|
|
const options = Object.assign({}, defaultBrowserOptions);
|
|
|
|
options.ignoreDefaultArgs = true;
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
const page = await browser.newPage();
|
|
|
|
expect(await page.evaluate('11 * 11')).toBe(121);
|
|
|
|
await page.close();
|
|
|
|
await browser.close();
|
|
|
|
});
|
2018-08-09 02:10:10 +00:00
|
|
|
it('should filter out ignored default arguments', async() => {
|
|
|
|
// Make sure we launch with `--enable-automation` by default.
|
|
|
|
const defaultArgs = puppeteer.defaultArgs();
|
|
|
|
const browser = await puppeteer.launch(Object.assign({}, defaultBrowserOptions, {
|
|
|
|
// Ignore first and third default argument.
|
|
|
|
ignoreDefaultArgs: [ defaultArgs[0], defaultArgs[2] ],
|
|
|
|
}));
|
|
|
|
const spawnargs = browser.process().spawnargs;
|
|
|
|
expect(spawnargs.indexOf(defaultArgs[0])).toBe(-1);
|
|
|
|
expect(spawnargs.indexOf(defaultArgs[1])).not.toBe(-1);
|
|
|
|
expect(spawnargs.indexOf(defaultArgs[2])).toBe(-1);
|
|
|
|
await browser.close();
|
|
|
|
});
|
2018-05-26 00:26:40 +00:00
|
|
|
it('should have default url when launching browser', async function() {
|
|
|
|
const browser = await puppeteer.launch(defaultBrowserOptions);
|
|
|
|
const pages = (await browser.pages()).map(page => page.url());
|
|
|
|
expect(pages).toEqual(['about:blank']);
|
|
|
|
await browser.close();
|
|
|
|
});
|
|
|
|
it('should have custom url when launching browser', async function({server}) {
|
|
|
|
const customUrl = server.PREFIX + '/empty.html';
|
|
|
|
const options = Object.assign({}, defaultBrowserOptions);
|
2018-11-13 07:26:16 +00:00
|
|
|
options.args = [customUrl].concat(options.args || []);
|
2018-05-26 00:26:40 +00:00
|
|
|
const browser = await puppeteer.launch(options);
|
2018-05-31 23:39:49 +00:00
|
|
|
const pages = await browser.pages();
|
|
|
|
expect(pages.length).toBe(1);
|
|
|
|
if (pages[0].url() !== customUrl)
|
|
|
|
await pages[0].waitForNavigation();
|
|
|
|
expect(pages[0].url()).toBe(customUrl);
|
2018-05-26 00:26:40 +00:00
|
|
|
await browser.close();
|
|
|
|
});
|
2018-08-01 23:23:03 +00:00
|
|
|
it('should set the default viewport', async() => {
|
|
|
|
const options = Object.assign({}, defaultBrowserOptions, {
|
|
|
|
defaultViewport: {
|
|
|
|
width: 456,
|
|
|
|
height: 789
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
const page = await browser.newPage();
|
|
|
|
expect(await page.evaluate('window.innerWidth')).toBe(456);
|
|
|
|
expect(await page.evaluate('window.innerHeight')).toBe(789);
|
|
|
|
await browser.close();
|
|
|
|
});
|
|
|
|
it('should disable the default viewport', async() => {
|
|
|
|
const options = Object.assign({}, defaultBrowserOptions, {
|
|
|
|
defaultViewport: null
|
|
|
|
});
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
const page = await browser.newPage();
|
|
|
|
expect(page.viewport()).toBe(null);
|
|
|
|
await browser.close();
|
|
|
|
});
|
2018-09-27 17:50:21 +00:00
|
|
|
it('should take fullPage screenshots when defaultViewport is null', async({server}) => {
|
|
|
|
const options = Object.assign({}, defaultBrowserOptions, {
|
|
|
|
defaultViewport: null
|
|
|
|
});
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
const page = await browser.newPage();
|
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
|
|
|
const screenshot = await page.screenshot({
|
|
|
|
fullPage: true
|
|
|
|
});
|
|
|
|
expect(screenshot).toBeInstanceOf(Buffer);
|
|
|
|
await browser.close();
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
|
|
|
describe('Puppeteer.connect', function() {
|
|
|
|
it('should be able to connect multiple times to the same browser', async({server}) => {
|
|
|
|
const originalBrowser = await puppeteer.launch(defaultBrowserOptions);
|
|
|
|
const browser = await puppeteer.connect({
|
|
|
|
browserWSEndpoint: originalBrowser.wsEndpoint()
|
|
|
|
});
|
|
|
|
const page = await browser.newPage();
|
|
|
|
expect(await page.evaluate(() => 7 * 8)).toBe(56);
|
|
|
|
browser.disconnect();
|
|
|
|
|
|
|
|
const secondPage = await originalBrowser.newPage();
|
|
|
|
expect(await secondPage.evaluate(() => 7 * 6)).toBe(42, 'original browser should still work');
|
|
|
|
await originalBrowser.close();
|
|
|
|
});
|
2018-05-31 23:04:48 +00:00
|
|
|
it('should support ignoreHTTPSErrors option', async({httpsServer}) => {
|
|
|
|
const originalBrowser = await puppeteer.launch(defaultBrowserOptions);
|
|
|
|
const browserWSEndpoint = originalBrowser.wsEndpoint();
|
|
|
|
|
|
|
|
const browser = await puppeteer.connect({browserWSEndpoint, ignoreHTTPSErrors: true});
|
|
|
|
const page = await browser.newPage();
|
|
|
|
let error = null;
|
|
|
|
const response = await page.goto(httpsServer.EMPTY_PAGE).catch(e => error = e);
|
|
|
|
expect(error).toBe(null);
|
|
|
|
expect(response.ok()).toBe(true);
|
|
|
|
expect(response.securityDetails()).toBeTruthy();
|
|
|
|
expect(response.securityDetails().protocol()).toBe('TLS 1.2');
|
|
|
|
await page.close();
|
|
|
|
await browser.close();
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
it('should be able to reconnect to a disconnected browser', async({server}) => {
|
|
|
|
const originalBrowser = await puppeteer.launch(defaultBrowserOptions);
|
|
|
|
const browserWSEndpoint = originalBrowser.wsEndpoint();
|
|
|
|
const page = await originalBrowser.newPage();
|
|
|
|
await page.goto(server.PREFIX + '/frames/nested-frames.html');
|
|
|
|
originalBrowser.disconnect();
|
|
|
|
|
|
|
|
const browser = await puppeteer.connect({browserWSEndpoint});
|
|
|
|
const pages = await browser.pages();
|
|
|
|
const restoredPage = pages.find(page => page.url() === server.PREFIX + '/frames/nested-frames.html');
|
2018-03-20 03:00:12 +00:00
|
|
|
expect(utils.dumpFrames(restoredPage.mainFrame())).toBeGolden('reconnect-nested-frames.txt');
|
2018-03-16 22:33:31 +00:00
|
|
|
expect(await restoredPage.evaluate(() => 7 * 8)).toBe(56);
|
|
|
|
await browser.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('Puppeteer.executablePath', function() {
|
|
|
|
it('should work', async({server}) => {
|
|
|
|
const executablePath = puppeteer.executablePath();
|
|
|
|
expect(fs.existsSync(executablePath)).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
|
2018-08-01 22:49:41 +00:00
|
|
|
describe('Browser target events', function() {
|
|
|
|
it('should work', async({server}) => {
|
|
|
|
const browser = await puppeteer.launch(defaultBrowserOptions);
|
|
|
|
const events = [];
|
|
|
|
browser.on('targetcreated', () => events.push('CREATED'));
|
|
|
|
browser.on('targetchanged', () => events.push('CHANGED'));
|
|
|
|
browser.on('targetdestroyed', () => events.push('DESTROYED'));
|
|
|
|
const page = await browser.newPage();
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.close();
|
|
|
|
expect(events).toEqual(['CREATED', 'CHANGED', 'DESTROYED']);
|
|
|
|
await browser.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
describe('Browser.Events.disconnected', function() {
|
|
|
|
it('should be emitted when: browser gets closed, disconnected or underlying websocket gets closed', async() => {
|
|
|
|
const originalBrowser = await puppeteer.launch(defaultBrowserOptions);
|
|
|
|
const browserWSEndpoint = originalBrowser.wsEndpoint();
|
|
|
|
const remoteBrowser1 = await puppeteer.connect({browserWSEndpoint});
|
|
|
|
const remoteBrowser2 = await puppeteer.connect({browserWSEndpoint});
|
|
|
|
|
|
|
|
let disconnectedOriginal = 0;
|
|
|
|
let disconnectedRemote1 = 0;
|
|
|
|
let disconnectedRemote2 = 0;
|
|
|
|
originalBrowser.on('disconnected', () => ++disconnectedOriginal);
|
|
|
|
remoteBrowser1.on('disconnected', () => ++disconnectedRemote1);
|
|
|
|
remoteBrowser2.on('disconnected', () => ++disconnectedRemote2);
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
utils.waitEvent(remoteBrowser2, 'disconnected'),
|
|
|
|
remoteBrowser2.disconnect(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(disconnectedOriginal).toBe(0);
|
|
|
|
expect(disconnectedRemote1).toBe(0);
|
|
|
|
expect(disconnectedRemote2).toBe(1);
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
utils.waitEvent(remoteBrowser1, 'disconnected'),
|
|
|
|
utils.waitEvent(originalBrowser, 'disconnected'),
|
|
|
|
originalBrowser.close(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(disconnectedOriginal).toBe(1);
|
|
|
|
expect(disconnectedRemote1).toBe(1);
|
|
|
|
expect(disconnectedRemote2).toBe(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-03-16 22:33:31 +00:00
|
|
|
};
|