2017-06-15 15:15:25 +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.
|
|
|
|
*/
|
2017-08-02 19:06:47 +00:00
|
|
|
const fs = require('fs');
|
2018-02-06 21:44:46 +00:00
|
|
|
const os = require('os');
|
2017-08-02 19:06:47 +00:00
|
|
|
const rm = require('rimraf').sync;
|
|
|
|
const path = require('path');
|
2017-09-11 23:21:51 +00:00
|
|
|
const {helper} = require('../lib/helper');
|
2017-07-27 23:16:37 +00:00
|
|
|
if (process.env.COVERAGE)
|
|
|
|
helper.recordPublicAPICoverage();
|
2018-02-06 21:44:46 +00:00
|
|
|
const mkdtempAsync = helper.promisify(fs.mkdtemp);
|
2018-02-07 17:31:53 +00:00
|
|
|
const readFileAsync = helper.promisify(fs.readFile);
|
2018-02-06 21:44:46 +00:00
|
|
|
const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-');
|
2017-12-08 23:14:28 +00:00
|
|
|
|
|
|
|
const PROJECT_ROOT = fs.existsSync(path.join(__dirname, '..', 'package.json')) ? path.join(__dirname, '..') : path.join(__dirname, '..', '..');
|
|
|
|
|
|
|
|
const puppeteer = require(PROJECT_ROOT);
|
|
|
|
const DeviceDescriptors = require(path.join(PROJECT_ROOT, 'DeviceDescriptors'));
|
2017-12-12 21:34:21 +00:00
|
|
|
const iPhone = DeviceDescriptors['iPhone 6'];
|
|
|
|
const iPhoneLandscape = DeviceDescriptors['iPhone 6 landscape'];
|
2017-12-08 23:14:28 +00:00
|
|
|
|
2017-08-02 19:06:47 +00:00
|
|
|
const SimpleServer = require('./server/SimpleServer');
|
|
|
|
const GoldenUtils = require('./golden-utils');
|
2018-01-10 02:47:21 +00:00
|
|
|
const FrameUtils = require('./frame-utils');
|
2017-08-02 19:06:47 +00:00
|
|
|
|
2017-08-04 04:38:55 +00:00
|
|
|
const YELLOW_COLOR = '\x1b[33m';
|
|
|
|
const RESET_COLOR = '\x1b[0m';
|
|
|
|
|
2017-08-02 19:06:47 +00:00
|
|
|
const GOLDEN_DIR = path.join(__dirname, 'golden');
|
|
|
|
const OUTPUT_DIR = path.join(__dirname, 'output');
|
|
|
|
|
2017-07-20 02:35:09 +00:00
|
|
|
const headless = (process.env.HEADLESS || 'true').trim().toLowerCase() === 'true';
|
2017-08-01 01:47:56 +00:00
|
|
|
const slowMo = parseInt((process.env.SLOW_MO || '0').trim(), 10);
|
2017-08-04 04:38:55 +00:00
|
|
|
const executablePath = process.env.CHROME;
|
2017-12-08 23:14:28 +00:00
|
|
|
|
|
|
|
console.log('Testing on Node', process.version);
|
2017-08-04 04:38:55 +00:00
|
|
|
if (executablePath)
|
|
|
|
console.warn(`${YELLOW_COLOR}WARN: running tests with ${executablePath}${RESET_COLOR}`);
|
2017-12-08 23:14:28 +00:00
|
|
|
// Make sure the `npm install` was run after the chromium roll.
|
|
|
|
console.assert(fs.existsSync(puppeteer.executablePath()), `Chromium is not Downloaded. Run 'npm install' and try to re-run tests`);
|
2017-08-04 04:38:55 +00:00
|
|
|
|
|
|
|
const defaultBrowserOptions = {
|
|
|
|
executablePath,
|
|
|
|
slowMo,
|
|
|
|
headless,
|
2017-12-22 00:29:00 +00:00
|
|
|
args: ['--no-sandbox', '--disable-dev-shm-usage']
|
2017-08-04 04:38:55 +00:00
|
|
|
};
|
2017-08-01 01:47:56 +00:00
|
|
|
|
2018-02-05 22:42:54 +00:00
|
|
|
const timeout = slowMo ? 0 : 10 * 1000;
|
2017-12-12 21:34:21 +00:00
|
|
|
let parallel = 1;
|
|
|
|
if (process.env.PPTR_PARALLEL_TESTS)
|
|
|
|
parallel = parseInt(process.env.PPTR_PARALLEL_TESTS.trim(), 10);
|
|
|
|
const parallelArgIndex = process.argv.indexOf('-j');
|
|
|
|
if (parallelArgIndex !== -1)
|
|
|
|
parallel = parseInt(process.argv[parallelArgIndex + 1], 10);
|
2018-01-04 20:08:35 +00:00
|
|
|
require('events').defaultMaxListeners *= parallel;
|
2017-12-08 00:37:22 +00:00
|
|
|
|
2018-01-04 20:08:35 +00:00
|
|
|
const {TestRunner, Reporter, Matchers} = require('../utils/testrunner/');
|
2017-12-12 21:34:21 +00:00
|
|
|
const runner = new TestRunner({timeout, parallel});
|
2017-12-08 00:37:22 +00:00
|
|
|
new Reporter(runner);
|
|
|
|
|
|
|
|
const {describe, xdescribe, fdescribe} = runner;
|
|
|
|
const {it, fit, xit} = runner;
|
|
|
|
const {beforeAll, beforeEach, afterAll, afterEach} = runner;
|
2017-11-20 23:59:07 +00:00
|
|
|
|
2017-12-08 00:37:22 +00:00
|
|
|
const {expect} = new Matchers({
|
|
|
|
toBeGolden: GoldenUtils.compare.bind(null, GOLDEN_DIR, OUTPUT_DIR)
|
2017-11-20 23:59:07 +00:00
|
|
|
});
|
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
if (fs.existsSync(OUTPUT_DIR))
|
|
|
|
rm(OUTPUT_DIR);
|
|
|
|
|
|
|
|
beforeAll(async state => {
|
2017-08-01 22:17:57 +00:00
|
|
|
const assetsPath = path.join(__dirname, 'assets');
|
2018-02-05 22:59:07 +00:00
|
|
|
const cachedPath = path.join(__dirname, 'assets', 'cached');
|
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
const port = 8907 + state.parallelIndex * 2;
|
|
|
|
state.server = await SimpleServer.create(assetsPath, port);
|
2018-02-05 22:59:07 +00:00
|
|
|
state.server.enableHTTPCache(cachedPath);
|
2017-12-12 21:34:21 +00:00
|
|
|
state.server.PREFIX = `http://localhost:${port}`;
|
|
|
|
state.server.CROSS_PROCESS_PREFIX = `http://127.0.0.1:${port}`;
|
|
|
|
state.server.EMPTY_PAGE = `http://localhost:${port}/empty.html`;
|
|
|
|
|
|
|
|
const httpsPort = port + 1;
|
|
|
|
state.httpsServer = await SimpleServer.createHTTPS(assetsPath, httpsPort);
|
2018-02-05 22:59:07 +00:00
|
|
|
state.httpsServer.enableHTTPCache(cachedPath);
|
2017-12-12 21:34:21 +00:00
|
|
|
state.httpsServer.PREFIX = `https://localhost:${httpsPort}`;
|
|
|
|
state.httpsServer.CROSS_PROCESS_PREFIX = `https://127.0.0.1:${httpsPort}`;
|
|
|
|
state.httpsServer.EMPTY_PAGE = `https://localhost:${httpsPort}/empty.html`;
|
|
|
|
});
|
2017-08-01 22:17:57 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
beforeEach(async({server, httpsServer}) => {
|
2017-10-18 02:14:57 +00:00
|
|
|
server.reset();
|
|
|
|
httpsServer.reset();
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-18 02:14:57 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
afterAll(async({server, httpsServer}) => {
|
2017-08-01 22:17:57 +00:00
|
|
|
await Promise.all([
|
|
|
|
server.stop(),
|
|
|
|
httpsServer.stop(),
|
|
|
|
]);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-01 22:17:57 +00:00
|
|
|
|
2017-09-13 19:06:57 +00:00
|
|
|
describe('Puppeteer', function() {
|
2018-02-07 17:31:53 +00:00
|
|
|
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');
|
|
|
|
expect(await browserFetcher.localRevisions()).toEqual(['123456']);
|
|
|
|
await browserFetcher.remove('123456');
|
|
|
|
expect(await browserFetcher.localRevisions()).toEqual([]);
|
|
|
|
rm(downloadsFolder);
|
|
|
|
});
|
|
|
|
});
|
2017-09-13 19:06:57 +00:00
|
|
|
describe('Puppeteer.launch', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should support ignoreHTTPSErrors option', async({httpsServer}) => {
|
2017-09-13 19:06:57 +00:00
|
|
|
const options = Object.assign({ignoreHTTPSErrors: true}, defaultBrowserOptions);
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
const page = await browser.newPage();
|
|
|
|
let error = null;
|
2017-12-12 21:34:21 +00:00
|
|
|
const response = await page.goto(httpsServer.EMPTY_PAGE).catch(e => error = e);
|
2017-09-13 19:06:57 +00:00
|
|
|
expect(error).toBe(null);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.ok()).toBe(true);
|
2018-02-13 19:26:18 +00:00
|
|
|
expect(response.securityDetails()).toBeTruthy();
|
|
|
|
expect(response.securityDetails().protocol()).toBe('TLS 1.2');
|
2017-09-13 19:06:57 +00:00
|
|
|
browser.close();
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should reject all promises when browser is closed', async() => {
|
2017-09-13 19:06:57 +00:00
|
|
|
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);
|
|
|
|
browser.close();
|
|
|
|
await neverResolves;
|
|
|
|
expect(error.message).toContain('Protocol error');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should reject if executable path is invalid', async({server}) => {
|
2017-10-10 18:10:26 +00:00
|
|
|
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);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('userDataDir option', async({server}) => {
|
2018-02-06 21:44:46 +00:00
|
|
|
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
2017-09-13 19:06:57 +00:00
|
|
|
const options = Object.assign({userDataDir}, defaultBrowserOptions);
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
2017-09-14 04:27:14 +00:00
|
|
|
await browser.close();
|
2017-09-13 19:06:57 +00:00
|
|
|
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
|
|
|
rm(userDataDir);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('userDataDir argument', async({server}) => {
|
2018-02-06 21:44:46 +00:00
|
|
|
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
2017-09-13 19:06:57 +00:00
|
|
|
const options = Object.assign({}, defaultBrowserOptions);
|
|
|
|
options.args = [`--user-data-dir=${userDataDir}`].concat(options.args);
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
2017-09-14 04:27:14 +00:00
|
|
|
await browser.close();
|
2017-09-13 19:06:57 +00:00
|
|
|
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
|
|
|
|
rm(userDataDir);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('userDataDir option should restore state', async({server}) => {
|
2018-02-06 21:44:46 +00:00
|
|
|
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
2017-10-06 00:08:35 +00:00
|
|
|
const options = Object.assign({userDataDir}, defaultBrowserOptions);
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
const page = await browser.newPage();
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-10-06 00:08:35 +00:00
|
|
|
await page.evaluate(() => localStorage.hey = 'hello');
|
|
|
|
await browser.close();
|
|
|
|
|
|
|
|
const browser2 = await puppeteer.launch(options);
|
|
|
|
const page2 = await browser2.newPage();
|
2017-12-12 21:34:21 +00:00
|
|
|
await page2.goto(server.EMPTY_PAGE);
|
2017-10-06 00:08:35 +00:00
|
|
|
expect(await page2.evaluate(() => localStorage.hey)).toBe('hello');
|
|
|
|
await browser2.close();
|
|
|
|
rm(userDataDir);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-12-04 21:45:56 +00:00
|
|
|
// @see https://github.com/GoogleChrome/puppeteer/issues/1537
|
2017-12-12 21:34:21 +00:00
|
|
|
xit('userDataDir option should restore cookies', async({server}) => {
|
2018-02-06 21:44:46 +00:00
|
|
|
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
2017-10-17 05:36:13 +00:00
|
|
|
const options = Object.assign({userDataDir}, defaultBrowserOptions);
|
|
|
|
const browser = await puppeteer.launch(options);
|
|
|
|
const page = await browser.newPage();
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-10-17 05:36:13 +00:00
|
|
|
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();
|
2017-12-12 21:34:21 +00:00
|
|
|
await page2.goto(server.EMPTY_PAGE);
|
2017-10-17 05:36:13 +00:00
|
|
|
expect(await page2.evaluate(() => document.cookie)).toBe('doSomethingOnlyOnce=true');
|
|
|
|
await browser2.close();
|
|
|
|
rm(userDataDir);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
xit('headless should be able to read cookies written by headful', async({server}) => {
|
2018-02-06 21:44:46 +00:00
|
|
|
const userDataDir = await mkdtempAsync(TMP_FOLDER);
|
2017-11-03 07:02:41 +00:00
|
|
|
const options = Object.assign({userDataDir}, defaultBrowserOptions);
|
|
|
|
// Write a cookie in headful chrome
|
|
|
|
options.headless = false;
|
|
|
|
const headfulBrowser = await puppeteer.launch(options);
|
|
|
|
const headfulPage = await headfulBrowser.newPage();
|
2017-12-12 21:34:21 +00:00
|
|
|
await headfulPage.goto(server.EMPTY_PAGE);
|
2017-11-03 07:02:41 +00:00
|
|
|
await headfulPage.evaluate(() => document.cookie = 'foo=true; expires=Fri, 31 Dec 9999 23:59:59 GMT');
|
|
|
|
await headfulBrowser.close();
|
|
|
|
// Read the cookie from headless chrome
|
|
|
|
options.headless = true;
|
|
|
|
const headlessBrowser = await puppeteer.launch(options);
|
|
|
|
const headlessPage = await headlessBrowser.newPage();
|
2017-12-12 21:34:21 +00:00
|
|
|
await headlessPage.goto(server.EMPTY_PAGE);
|
2017-11-03 07:02:41 +00:00
|
|
|
const cookie = await headlessPage.evaluate(() => document.cookie);
|
|
|
|
await headlessBrowser.close();
|
|
|
|
rm(userDataDir);
|
|
|
|
expect(cookie).toBe('foo=true');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-12-20 01:51:21 +00:00
|
|
|
it('should return the default chrome arguments', async() => {
|
|
|
|
const args = puppeteer.defaultArgs();
|
|
|
|
expect(args).toContain('--no-first-run');
|
|
|
|
});
|
2017-09-13 19:06:57 +00:00
|
|
|
});
|
|
|
|
describe('Puppeteer.connect', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should be able to connect multiple times to the same browser', async({server}) => {
|
2017-09-13 19:06:57 +00:00
|
|
|
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);
|
2017-10-17 22:35:00 +00:00
|
|
|
browser.disconnect();
|
|
|
|
|
|
|
|
const secondPage = await originalBrowser.newPage();
|
|
|
|
expect(await secondPage.evaluate(() => 7 * 6)).toBe(42, 'original browser should still work');
|
|
|
|
await originalBrowser.close();
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should be able to reconnect to a disconnected browser', async({server}) => {
|
2017-10-17 22:35:00 +00:00
|
|
|
const originalBrowser = await puppeteer.launch(defaultBrowserOptions);
|
|
|
|
const browserWSEndpoint = originalBrowser.wsEndpoint();
|
2017-10-18 02:14:57 +00:00
|
|
|
const page = await originalBrowser.newPage();
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/frames/nested-frames.html');
|
2017-10-17 22:35:00 +00:00
|
|
|
originalBrowser.disconnect();
|
|
|
|
|
|
|
|
const browser = await puppeteer.connect({browserWSEndpoint});
|
2017-10-18 02:14:57 +00:00
|
|
|
const pages = await browser.pages();
|
2017-12-12 21:34:21 +00:00
|
|
|
const restoredPage = pages.find(page => page.url() === server.PREFIX + '/frames/nested-frames.html');
|
2017-10-18 02:14:57 +00:00
|
|
|
expect(FrameUtils.dumpFrames(restoredPage.mainFrame())).toBeGolden('reconnect-nested-frames.txt');
|
|
|
|
expect(await restoredPage.evaluate(() => 7 * 8)).toBe(56);
|
2017-10-17 22:35:00 +00:00
|
|
|
await browser.close();
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-09-13 19:06:57 +00:00
|
|
|
});
|
2017-09-14 00:39:18 +00:00
|
|
|
describe('Puppeteer.executablePath', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({server}) => {
|
2017-09-14 00:39:18 +00:00
|
|
|
const executablePath = puppeteer.executablePath();
|
|
|
|
expect(fs.existsSync(executablePath)).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-09-14 00:39:18 +00:00
|
|
|
});
|
2017-08-01 22:17:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Page', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
beforeAll(async state => {
|
|
|
|
state.browser = await puppeteer.launch(defaultBrowserOptions);
|
|
|
|
});
|
2017-05-12 23:36:37 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
afterAll(async state => {
|
|
|
|
await state.browser.close();
|
|
|
|
state.browser = null;
|
|
|
|
});
|
2017-05-13 00:55:29 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
beforeEach(async state => {
|
|
|
|
state.page = await state.browser.newPage();
|
|
|
|
});
|
2017-05-13 18:05:54 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
afterEach(async state => {
|
|
|
|
await state.page.close();
|
|
|
|
state.page = null;
|
|
|
|
});
|
2017-05-13 00:55:29 +00:00
|
|
|
|
2017-07-27 18:32:58 +00:00
|
|
|
describe('Browser.version', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should return whether we are in headless', async({browser}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const version = await browser.version();
|
2017-07-27 18:32:58 +00:00
|
|
|
expect(version.length).toBeGreaterThan(0);
|
|
|
|
expect(version.startsWith('Headless')).toBe(headless);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-07-27 18:32:58 +00:00
|
|
|
});
|
|
|
|
|
2018-01-05 18:14:30 +00:00
|
|
|
describe('Browser.userAgent', function() {
|
|
|
|
it('should include WebKit', async({browser}) => {
|
|
|
|
const userAgent = await browser.userAgent();
|
|
|
|
expect(userAgent.length).toBeGreaterThan(0);
|
|
|
|
expect(userAgent).toContain('WebKit');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-11 20:11:12 +00:00
|
|
|
describe('Browser.process', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should return child_process instance', async function({browser}) {
|
2017-12-11 20:11:12 +00:00
|
|
|
const process = await browser.process();
|
|
|
|
expect(process.pid).toBeGreaterThan(0);
|
|
|
|
const browserWSEndpoint = browser.wsEndpoint();
|
|
|
|
const remoteBrowser = await puppeteer.connect({browserWSEndpoint});
|
|
|
|
expect(remoteBrowser.process()).toBe(null);
|
2017-12-12 21:34:21 +00:00
|
|
|
await remoteBrowser.disconnect();
|
|
|
|
});
|
2017-12-11 20:11:12 +00:00
|
|
|
});
|
|
|
|
|
2017-11-04 01:46:17 +00:00
|
|
|
describe('Browser.Events.disconnected', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should emitted when: browser gets closed, disconnected or underlying websocket gets closed', async() => {
|
2017-11-04 01:46:17 +00:00
|
|
|
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 remoteBrowser2.disconnect();
|
|
|
|
expect(disconnectedOriginal).toBe(0);
|
|
|
|
expect(disconnectedRemote1).toBe(0);
|
|
|
|
expect(disconnectedRemote2).toBe(1);
|
|
|
|
|
|
|
|
await originalBrowser.close();
|
|
|
|
expect(disconnectedOriginal).toBe(1);
|
|
|
|
expect(disconnectedRemote1).toBe(1);
|
|
|
|
expect(disconnectedRemote2).toBe(1);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-11-04 01:46:17 +00:00
|
|
|
});
|
|
|
|
|
2017-08-09 23:14:00 +00:00
|
|
|
describe('Page.close', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should reject all promises when page is closed', async({browser}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const newPage = await browser.newPage();
|
|
|
|
const neverResolves = newPage.evaluate(() => new Promise(r => {}));
|
2017-08-09 23:14:00 +00:00
|
|
|
newPage.close();
|
|
|
|
let error = null;
|
2017-09-13 19:06:57 +00:00
|
|
|
await neverResolves.catch(e => error = e);
|
2017-08-09 23:14:00 +00:00
|
|
|
expect(error.message).toContain('Protocol error');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-09 23:14:00 +00:00
|
|
|
});
|
|
|
|
|
2017-08-15 18:13:05 +00:00
|
|
|
describe('Page.Events.error', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should throw when page crashes', async({page}) => {
|
2017-08-15 18:13:05 +00:00
|
|
|
let error = null;
|
|
|
|
page.on('error', err => error = err);
|
|
|
|
page.goto('chrome://crash').catch(e => {});
|
|
|
|
await waitForEvents(page, 'error');
|
|
|
|
expect(error.message).toBe('Page crashed!');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-15 18:13:05 +00:00
|
|
|
});
|
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
describe('Page.evaluate', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.evaluate(() => 7 * 3);
|
2017-06-21 20:51:06 +00:00
|
|
|
expect(result).toBe(21);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should await promise', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.evaluate(() => Promise.resolve(8 * 7));
|
2017-06-21 20:51:06 +00:00
|
|
|
expect(result).toBe(56);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work right after framenavigated', async({page, server}) => {
|
2017-11-19 00:27:52 +00:00
|
|
|
let frameEvaluation = null;
|
|
|
|
page.on('framenavigated', async frame => {
|
|
|
|
frameEvaluation = frame.evaluate(() => 6 * 7);
|
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-11-19 00:27:52 +00:00
|
|
|
expect(await frameEvaluation).toBe(42);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work from-inside an exposed function', async({page, server}) => {
|
2017-06-21 20:51:06 +00:00
|
|
|
// Setup inpage callback, which calls Page.evaluate
|
2017-08-16 06:04:16 +00:00
|
|
|
await page.exposeFunction('callController', async function(a, b) {
|
2017-06-21 20:51:06 +00:00
|
|
|
return await page.evaluate((a, b) => a * b, a, b);
|
|
|
|
});
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.evaluate(async function() {
|
2017-06-21 20:51:06 +00:00
|
|
|
return await callController(9, 3);
|
|
|
|
});
|
|
|
|
expect(result).toBe(27);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should reject promise with exception', async({page, server}) => {
|
2017-06-22 20:38:10 +00:00
|
|
|
let error = null;
|
2017-09-13 19:06:57 +00:00
|
|
|
await page.evaluate(() => not.existing.object.property).catch(e => error = e);
|
2017-06-21 20:51:06 +00:00
|
|
|
expect(error).toBeTruthy();
|
|
|
|
expect(error.message).toContain('not is not defined');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should return complex objects', async({page, server}) => {
|
2017-07-06 17:41:01 +00:00
|
|
|
const object = {foo: 'bar!'};
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.evaluate(a => a, object);
|
2017-07-06 17:41:01 +00:00
|
|
|
expect(result).not.toBe(object);
|
|
|
|
expect(result).toEqual(object);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should return NaN', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.evaluate(() => NaN);
|
2017-07-06 17:41:01 +00:00
|
|
|
expect(Object.is(result, NaN)).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should return -0', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.evaluate(() => -0);
|
2017-07-06 17:41:01 +00:00
|
|
|
expect(Object.is(result, -0)).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should return Infinity', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.evaluate(() => Infinity);
|
2017-07-06 17:41:01 +00:00
|
|
|
expect(Object.is(result, Infinity)).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should return -Infinity', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.evaluate(() => -Infinity);
|
2017-07-06 17:41:01 +00:00
|
|
|
expect(Object.is(result, -Infinity)).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should accept "undefined" as one of multiple parameters', async({page, server}) => {
|
2017-09-11 23:59:38 +00:00
|
|
|
const result = await page.evaluate((a, b) => Object.is(a, undefined) && Object.is(b, 'foo'), undefined, 'foo');
|
|
|
|
expect(result).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2018-01-04 19:30:29 +00:00
|
|
|
it('should properly serialize null fields', async({page}) => {
|
|
|
|
expect(await page.evaluate(() => ({a: undefined}))).toEqual({});
|
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should fail for window object', async({page, server}) => {
|
2017-10-09 19:30:05 +00:00
|
|
|
const result = await page.evaluate(() => window);
|
|
|
|
expect(result).toBe(undefined);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should accept a string', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.evaluate('1 + 2');
|
2017-07-27 19:23:41 +00:00
|
|
|
expect(result).toBe(3);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should accept a string with semi colons', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.evaluate('1 + 5;');
|
2017-07-27 19:23:41 +00:00
|
|
|
expect(result).toBe(6);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should accept a string with comments', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.evaluate('2 + 5;\n// do some math!');
|
2017-07-27 19:23:41 +00:00
|
|
|
expect(result).toBe(7);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should accept element handle as an argument', async({page, server}) => {
|
2017-09-12 02:20:02 +00:00
|
|
|
await page.setContent('<section>42</section>');
|
|
|
|
const element = await page.$('section');
|
|
|
|
const text = await page.evaluate(e => e.textContent, element);
|
|
|
|
expect(text).toBe('42');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw if underlying element was disposed', async({page, server}) => {
|
2017-09-12 02:20:02 +00:00
|
|
|
await page.setContent('<section>39</section>');
|
|
|
|
const element = await page.$('section');
|
|
|
|
expect(element).toBeTruthy();
|
|
|
|
await element.dispose();
|
|
|
|
let error = null;
|
|
|
|
await page.evaluate(e => e.textContent, element).catch(e => error = e);
|
2017-10-06 22:35:02 +00:00
|
|
|
expect(error.message).toContain('JSHandle is disposed');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw if elementHandles are from other frames', async({page, server}) => {
|
|
|
|
await FrameUtils.attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
2017-09-12 02:20:02 +00:00
|
|
|
const bodyHandle = await page.frames()[1].$('body');
|
|
|
|
let error = null;
|
|
|
|
await page.evaluate(body => body.innerHTML, bodyHandle).catch(e => error = e);
|
|
|
|
expect(error).toBeTruthy();
|
2017-10-06 22:35:02 +00:00
|
|
|
expect(error.message).toContain('JSHandles can be evaluated only in the context they were created');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should accept object handle as an argument', async({page, server}) => {
|
2017-10-06 22:35:02 +00:00
|
|
|
const navigatorHandle = await page.evaluateHandle(() => navigator);
|
|
|
|
const text = await page.evaluate(e => e.userAgent, navigatorHandle);
|
|
|
|
expect(text).toContain('Mozilla');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should accept object handle to primitive types', async({page, server}) => {
|
2017-10-06 22:35:02 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => 5);
|
|
|
|
const isFive = await page.evaluate(e => Object.is(e, 5), aHandle);
|
|
|
|
expect(isFive).toBeTruthy();
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-06 22:35:02 +00:00
|
|
|
});
|
|
|
|
|
2017-10-13 21:41:39 +00:00
|
|
|
describe('Page.setOfflineMode', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-10-13 21:41:39 +00:00
|
|
|
await page.setOfflineMode(true);
|
|
|
|
let error = null;
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE).catch(e => error = e);
|
2017-10-13 21:41:39 +00:00
|
|
|
expect(error).toBeTruthy();
|
|
|
|
await page.setOfflineMode(false);
|
|
|
|
const response = await page.reload();
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.status()).toBe(200);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should emulate navigator.onLine', async({page, server}) => {
|
2017-10-13 21:41:39 +00:00
|
|
|
expect(await page.evaluate(() => window.navigator.onLine)).toBe(true);
|
|
|
|
await page.setOfflineMode(true);
|
|
|
|
expect(await page.evaluate(() => window.navigator.onLine)).toBe(false);
|
|
|
|
await page.setOfflineMode(false);
|
|
|
|
expect(await page.evaluate(() => window.navigator.onLine)).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-13 21:41:39 +00:00
|
|
|
});
|
|
|
|
|
2017-10-06 22:35:02 +00:00
|
|
|
describe('Page.evaluateHandle', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-10-06 22:35:02 +00:00
|
|
|
const windowHandle = await page.evaluateHandle(() => window);
|
|
|
|
expect(windowHandle).toBeTruthy();
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-06 22:35:02 +00:00
|
|
|
});
|
|
|
|
|
2017-10-11 21:41:20 +00:00
|
|
|
describe('ExecutionContext.queryObjects', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-10-11 21:41:20 +00:00
|
|
|
// Instantiate an object
|
|
|
|
await page.evaluate(() => window.set = new Set(['hello', 'world']));
|
|
|
|
const prototypeHandle = await page.evaluateHandle(() => Set.prototype);
|
|
|
|
const objectsHandle = await page.queryObjects(prototypeHandle);
|
|
|
|
const count = await page.evaluate(objects => objects.length, objectsHandle);
|
|
|
|
expect(count).toBe(1);
|
|
|
|
const values = await page.evaluate(objects => Array.from(objects[0].values()), objectsHandle);
|
|
|
|
expect(values).toEqual(['hello', 'world']);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should fail for disposed handles', async({page, server}) => {
|
2017-10-11 21:41:20 +00:00
|
|
|
const prototypeHandle = await page.evaluateHandle(() => HTMLBodyElement.prototype);
|
|
|
|
await prototypeHandle.dispose();
|
|
|
|
let error = null;
|
|
|
|
await page.queryObjects(prototypeHandle).catch(e => error = e);
|
|
|
|
expect(error.message).toBe('Prototype JSHandle is disposed!');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should fail primitive values as prototypes', async({page, server}) => {
|
2017-10-11 21:41:20 +00:00
|
|
|
const prototypeHandle = await page.evaluateHandle(() => 42);
|
|
|
|
let error = null;
|
|
|
|
await page.queryObjects(prototypeHandle).catch(e => error = e);
|
|
|
|
expect(error.message).toBe('Prototype JSHandle must not be referencing primitive value');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-11 21:41:20 +00:00
|
|
|
});
|
|
|
|
|
2017-10-06 22:35:02 +00:00
|
|
|
describe('JSHandle.getProperty', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-10-06 22:35:02 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => ({
|
|
|
|
one: 1,
|
|
|
|
two: 2,
|
|
|
|
three: 3
|
|
|
|
}));
|
|
|
|
const twoHandle = await aHandle.getProperty('two');
|
|
|
|
expect(await twoHandle.jsonValue()).toEqual(2);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-06 22:35:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('JSHandle.jsonValue', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-10-06 22:35:02 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => ({foo: 'bar'}));
|
|
|
|
const json = await aHandle.jsonValue();
|
|
|
|
expect(json).toEqual({foo: 'bar'});
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should not work with dates', async({page, server}) => {
|
2017-10-06 22:35:02 +00:00
|
|
|
const dateHandle = await page.evaluateHandle(() => new Date('2017-09-26T00:00:00.000Z'));
|
|
|
|
const json = await dateHandle.jsonValue();
|
2017-10-20 17:45:49 +00:00
|
|
|
expect(json).toEqual({});
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw for circular objects', async({page, server}) => {
|
2017-10-06 22:35:02 +00:00
|
|
|
const windowHandle = await page.evaluateHandle('window');
|
|
|
|
let error = null;
|
|
|
|
await windowHandle.jsonValue().catch(e => error = e);
|
2017-10-20 17:45:49 +00:00
|
|
|
expect(error.message).toContain('Object reference chain is too long');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-06 22:35:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('JSHandle.getProperties', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-10-06 22:35:02 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => ({
|
|
|
|
foo: 'bar'
|
|
|
|
}));
|
|
|
|
const properties = await aHandle.getProperties();
|
|
|
|
const foo = properties.get('foo');
|
|
|
|
expect(foo).toBeTruthy();
|
|
|
|
expect(await foo.jsonValue()).toBe('bar');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should return even non-own properties', async({page, server}) => {
|
2017-10-06 22:35:02 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => {
|
|
|
|
class A {
|
|
|
|
constructor() {
|
|
|
|
this.a = '1';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B extends A {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.b = '2';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new B();
|
|
|
|
});
|
|
|
|
const properties = await aHandle.getProperties();
|
|
|
|
expect(await properties.get('a').jsonValue()).toBe('1');
|
|
|
|
expect(await properties.get('b').jsonValue()).toBe('2');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-06 22:35:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('JSHandle.asElement', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-10-06 22:35:02 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => document.body);
|
|
|
|
const element = aHandle.asElement();
|
|
|
|
expect(element).toBeTruthy();
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should return null for non-elements', async({page, server}) => {
|
2017-10-06 22:35:02 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => 2);
|
|
|
|
const element = aHandle.asElement();
|
|
|
|
expect(element).toBeFalsy();
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should return ElementHandle for TextNodes', async({page, server}) => {
|
2017-10-06 22:35:02 +00:00
|
|
|
await page.setContent('<div>ee!</div>');
|
|
|
|
const aHandle = await page.evaluateHandle(() => document.querySelector('div').firstChild);
|
|
|
|
const element = aHandle.asElement();
|
|
|
|
expect(element).toBeTruthy();
|
|
|
|
expect(await page.evaluate(e => e.nodeType === HTMLElement.TEXT_NODE, element));
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-06 22:35:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('JSHandle.toString', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work for primitives', async({page, server}) => {
|
2017-10-10 17:54:20 +00:00
|
|
|
const numberHandle = await page.evaluateHandle(() => 2);
|
|
|
|
expect(numberHandle.toString()).toBe('JSHandle:2');
|
|
|
|
const stringHandle = await page.evaluateHandle(() => 'a');
|
|
|
|
expect(stringHandle.toString()).toBe('JSHandle:a');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work for complicated objects', async({page, server}) => {
|
2017-10-06 22:35:02 +00:00
|
|
|
const aHandle = await page.evaluateHandle(() => window);
|
|
|
|
expect(aHandle.toString()).toBe('JSHandle@object');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-06-21 20:51:06 +00:00
|
|
|
});
|
2017-06-14 15:21:56 +00:00
|
|
|
|
2017-10-06 22:35:02 +00:00
|
|
|
describe('Frame.context', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await FrameUtils.attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
2017-10-06 22:35:02 +00:00
|
|
|
expect(page.frames().length).toBe(2);
|
|
|
|
const [frame1, frame2] = page.frames();
|
2017-11-19 00:27:52 +00:00
|
|
|
const context1 = await frame1.executionContext();
|
|
|
|
const context2 = await frame2.executionContext();
|
|
|
|
expect(context1).toBeTruthy();
|
|
|
|
expect(context2).toBeTruthy();
|
|
|
|
expect(context1 !== context2).toBeTruthy();
|
2017-10-06 22:35:02 +00:00
|
|
|
|
|
|
|
await Promise.all([
|
2017-11-19 00:27:52 +00:00
|
|
|
context1.evaluate(() => window.a = 1),
|
|
|
|
context2.evaluate(() => window.a = 2)
|
2017-10-06 22:35:02 +00:00
|
|
|
]);
|
|
|
|
const [a1, a2] = await Promise.all([
|
2017-11-19 00:27:52 +00:00
|
|
|
context1.evaluate(() => window.a),
|
|
|
|
context2.evaluate(() => window.a)
|
2017-10-06 22:35:02 +00:00
|
|
|
]);
|
|
|
|
expect(a1).toBe(1);
|
|
|
|
expect(a2).toBe(2);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-06 22:35:02 +00:00
|
|
|
});
|
|
|
|
|
2018-01-25 05:16:01 +00:00
|
|
|
describe('Frame.evaluateHandle', function() {
|
|
|
|
it('should work', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const mainFrame = page.mainFrame();
|
|
|
|
const windowHandle = await mainFrame.evaluateHandle(() => window);
|
|
|
|
expect(windowHandle).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-27 19:40:46 +00:00
|
|
|
describe('Frame.evaluate', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should have different execution contexts', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await FrameUtils.attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
2017-06-27 19:40:46 +00:00
|
|
|
expect(page.frames().length).toBe(2);
|
2017-08-21 23:39:04 +00:00
|
|
|
const frame1 = page.frames()[0];
|
|
|
|
const frame2 = page.frames()[1];
|
2017-06-27 19:40:46 +00:00
|
|
|
await frame1.evaluate(() => window.FOO = 'foo');
|
|
|
|
await frame2.evaluate(() => window.FOO = 'bar');
|
|
|
|
expect(await frame1.evaluate(() => window.FOO)).toBe('foo');
|
|
|
|
expect(await frame2.evaluate(() => window.FOO)).toBe('bar');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should execute after cross-site navigation', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-08-21 23:39:04 +00:00
|
|
|
const mainFrame = page.mainFrame();
|
2017-07-25 21:30:04 +00:00
|
|
|
expect(await mainFrame.evaluate(() => window.location.href)).toContain('localhost');
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.CROSS_PROCESS_PREFIX + '/empty.html');
|
2017-07-25 21:30:04 +00:00
|
|
|
expect(await mainFrame.evaluate(() => window.location.href)).toContain('127');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-06-27 19:40:46 +00:00
|
|
|
});
|
|
|
|
|
2017-07-27 22:17:43 +00:00
|
|
|
describe('Frame.waitForFunction', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should accept a string', async({page, server}) => {
|
2017-07-27 22:17:43 +00:00
|
|
|
const watchdog = page.waitForFunction('window.__FOO === 1');
|
|
|
|
await page.evaluate(() => window.__FOO = 1);
|
|
|
|
await watchdog;
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should poll on interval', async({page, server}) => {
|
2017-07-27 22:17:43 +00:00
|
|
|
let success = false;
|
|
|
|
const startTime = Date.now();
|
|
|
|
const polling = 100;
|
|
|
|
const watchdog = page.waitForFunction(() => window.__FOO === 'hit', {polling})
|
|
|
|
.then(() => success = true);
|
|
|
|
await page.evaluate(() => window.__FOO = 'hit');
|
|
|
|
expect(success).toBe(false);
|
|
|
|
await page.evaluate(() => document.body.appendChild(document.createElement('div')));
|
|
|
|
await watchdog;
|
|
|
|
expect(Date.now() - startTime).not.toBeLessThan(polling / 2);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should poll on mutation', async({page, server}) => {
|
2017-07-27 22:17:43 +00:00
|
|
|
let success = false;
|
|
|
|
const watchdog = page.waitForFunction(() => window.__FOO === 'hit', {polling: 'mutation'})
|
|
|
|
.then(() => success = true);
|
|
|
|
await page.evaluate(() => window.__FOO = 'hit');
|
|
|
|
expect(success).toBe(false);
|
|
|
|
await page.evaluate(() => document.body.appendChild(document.createElement('div')));
|
|
|
|
await watchdog;
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should poll on raf', async({page, server}) => {
|
2017-07-27 22:17:43 +00:00
|
|
|
const watchdog = page.waitForFunction(() => window.__FOO === 'hit', {polling: 'raf'});
|
|
|
|
await page.evaluate(() => window.__FOO = 'hit');
|
|
|
|
await watchdog;
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw on bad polling value', async({page, server}) => {
|
2017-07-27 22:17:43 +00:00
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
await page.waitForFunction(() => !!document.body, {polling: 'unknown'});
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error).toBeTruthy();
|
|
|
|
expect(error.message).toContain('polling');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw negative polling interval', async({page, server}) => {
|
2017-07-27 22:17:43 +00:00
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
await page.waitForFunction(() => !!document.body, {polling: -10});
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error).toBeTruthy();
|
|
|
|
expect(error.message).toContain('Cannot poll with non-positive interval');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2018-01-04 22:49:13 +00:00
|
|
|
it('should return the success value as a JSHandle', async({page}) => {
|
|
|
|
expect(await (await page.waitForFunction(() => 5)).jsonValue()).toBe(5);
|
|
|
|
});
|
2018-01-08 22:14:41 +00:00
|
|
|
it('should return the window as a success value', async({ page }) => {
|
|
|
|
expect(await page.waitForFunction(() => window)).toBeTruthy();
|
|
|
|
});
|
2018-01-04 22:49:13 +00:00
|
|
|
it('should accept ElementHandle arguments', async({page}) => {
|
|
|
|
await page.setContent('<div></div>');
|
|
|
|
const div = await page.$('div');
|
|
|
|
let resolved = false;
|
|
|
|
const waitForFunction = page.waitForFunction(element => !element.parentElement, {}, div).then(() => resolved = true);
|
|
|
|
expect(resolved).toBe(false);
|
|
|
|
await page.evaluate(element => element.remove(), div);
|
|
|
|
await waitForFunction;
|
|
|
|
});
|
2017-07-27 22:17:43 +00:00
|
|
|
});
|
|
|
|
|
2017-07-21 19:41:49 +00:00
|
|
|
describe('Frame.waitForSelector', function() {
|
2017-08-21 23:39:04 +00:00
|
|
|
const addElement = tag => document.body.appendChild(document.createElement(tag));
|
2017-07-07 22:39:02 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should immediately resolve promise if node exists', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-08-21 23:39:04 +00:00
|
|
|
const frame = page.mainFrame();
|
2017-07-07 22:39:02 +00:00
|
|
|
let added = false;
|
2017-07-21 19:41:49 +00:00
|
|
|
await frame.waitForSelector('*').then(() => added = true);
|
2017-07-07 22:39:02 +00:00
|
|
|
expect(added).toBe(true);
|
|
|
|
|
|
|
|
added = false;
|
|
|
|
await frame.evaluate(addElement, 'div');
|
2017-07-21 19:41:49 +00:00
|
|
|
await frame.waitForSelector('div').then(() => added = true);
|
2017-07-07 22:39:02 +00:00
|
|
|
expect(added).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-07-07 22:39:02 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should resolve promise when node is added', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-08-21 23:39:04 +00:00
|
|
|
const frame = page.mainFrame();
|
2017-07-07 22:39:02 +00:00
|
|
|
let added = false;
|
2017-08-21 23:39:04 +00:00
|
|
|
const watchdog = frame.waitForSelector('div').then(() => added = true);
|
2017-07-07 22:39:02 +00:00
|
|
|
// run nop function..
|
|
|
|
await frame.evaluate(() => 42);
|
2017-07-21 19:41:49 +00:00
|
|
|
// .. to be sure that waitForSelector promise is not resolved yet.
|
2017-07-07 22:39:02 +00:00
|
|
|
expect(added).toBe(false);
|
|
|
|
await frame.evaluate(addElement, 'br');
|
|
|
|
expect(added).toBe(false);
|
|
|
|
await frame.evaluate(addElement, 'div');
|
2017-08-18 02:48:25 +00:00
|
|
|
await watchdog;
|
2017-07-07 22:39:02 +00:00
|
|
|
expect(added).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-07-07 22:39:02 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work when node is added through innerHTML', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-08-21 23:39:04 +00:00
|
|
|
const watchdog = page.waitForSelector('h3 div');
|
2017-08-03 20:35:31 +00:00
|
|
|
await page.evaluate(addElement, 'span');
|
|
|
|
await page.evaluate(() => document.querySelector('span').innerHTML = '<h3><div></div></h3>');
|
|
|
|
await watchdog;
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-07-18 09:10:02 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('Page.waitForSelector is shortcut for main frame', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await FrameUtils.attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
2017-08-21 23:39:04 +00:00
|
|
|
const otherFrame = page.frames()[1];
|
2017-07-07 22:39:02 +00:00
|
|
|
let added = false;
|
2017-07-21 19:41:49 +00:00
|
|
|
page.waitForSelector('div').then(() => added = true);
|
2017-07-07 22:39:02 +00:00
|
|
|
await otherFrame.evaluate(addElement, 'div');
|
|
|
|
expect(added).toBe(false);
|
|
|
|
await page.evaluate(addElement, 'div');
|
|
|
|
expect(added).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-07-14 22:55:38 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should run in specified frame', async({page, server}) => {
|
|
|
|
await FrameUtils.attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
|
|
|
await FrameUtils.attachFrame(page, 'frame2', server.EMPTY_PAGE);
|
2017-08-21 23:39:04 +00:00
|
|
|
const frame1 = page.frames()[1];
|
|
|
|
const frame2 = page.frames()[2];
|
2017-07-14 22:55:38 +00:00
|
|
|
let added = false;
|
2018-02-13 19:00:38 +00:00
|
|
|
const waitForSelectorPromise = frame2.waitForSelector('div').then(() => added = true);
|
2017-07-14 22:55:38 +00:00
|
|
|
expect(added).toBe(false);
|
|
|
|
await frame1.evaluate(addElement, 'div');
|
|
|
|
expect(added).toBe(false);
|
|
|
|
await frame2.evaluate(addElement, 'div');
|
2018-02-13 19:00:38 +00:00
|
|
|
await waitForSelectorPromise;
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-07-14 22:55:38 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should throw if evaluation failed', async({page, server}) => {
|
2017-07-25 07:00:25 +00:00
|
|
|
await page.evaluateOnNewDocument(function() {
|
2017-07-14 22:55:38 +00:00
|
|
|
document.querySelector = null;
|
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-09-13 19:06:57 +00:00
|
|
|
let error = null;
|
|
|
|
await page.waitForSelector('*').catch(e => error = e);
|
|
|
|
expect(error.message).toContain('document.querySelector is not a function');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw when frame is detached', async({page, server}) => {
|
|
|
|
await FrameUtils.attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
2017-08-21 23:39:04 +00:00
|
|
|
const frame = page.frames()[1];
|
2017-07-20 02:04:51 +00:00
|
|
|
let waitError = null;
|
2017-08-21 23:39:04 +00:00
|
|
|
const waitPromise = frame.waitForSelector('.box').catch(e => waitError = e);
|
2017-07-20 02:04:51 +00:00
|
|
|
await FrameUtils.detachFrame(page, 'frame1');
|
|
|
|
await waitPromise;
|
|
|
|
expect(waitError).toBeTruthy();
|
2018-01-22 23:16:20 +00:00
|
|
|
expect(waitError.message).toContain('waitForFunction failed: frame got detached.');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should survive cross-process navigation', async({page, server}) => {
|
2017-07-20 02:04:51 +00:00
|
|
|
let boxFound = false;
|
2017-08-21 23:39:04 +00:00
|
|
|
const waitForSelector = page.waitForSelector('.box').then(() => boxFound = true);
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-07-20 02:04:51 +00:00
|
|
|
expect(boxFound).toBe(false);
|
|
|
|
await page.reload();
|
|
|
|
expect(boxFound).toBe(false);
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.CROSS_PROCESS_PREFIX + '/grid.html');
|
2017-07-21 19:41:49 +00:00
|
|
|
await waitForSelector;
|
2017-07-20 02:04:51 +00:00
|
|
|
expect(boxFound).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should wait for visible', async({page, server}) => {
|
2017-07-21 07:58:38 +00:00
|
|
|
let divFound = false;
|
2017-08-21 23:39:04 +00:00
|
|
|
const waitForSelector = page.waitForSelector('div', {visible: true}).then(() => divFound = true);
|
2017-11-10 23:44:01 +00:00
|
|
|
await page.setContent(`<div style='display: none; visibility: hidden;'>1</div>`);
|
2017-07-21 07:58:38 +00:00
|
|
|
expect(divFound).toBe(false);
|
|
|
|
await page.evaluate(() => document.querySelector('div').style.removeProperty('display'));
|
|
|
|
expect(divFound).toBe(false);
|
|
|
|
await page.evaluate(() => document.querySelector('div').style.removeProperty('visibility'));
|
2017-07-21 19:41:49 +00:00
|
|
|
expect(await waitForSelector).toBe(true);
|
2017-10-13 16:11:11 +00:00
|
|
|
expect(divFound).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should wait for visible recursively', async({page, server}) => {
|
2017-11-10 23:44:01 +00:00
|
|
|
let divVisible = false;
|
|
|
|
const waitForSelector = page.waitForSelector('div#inner', {visible: true}).then(() => divVisible = true);
|
|
|
|
await page.setContent(`<div style='display: none; visibility: hidden;'><div id="inner">hi</div></div>`);
|
|
|
|
expect(divVisible).toBe(false);
|
|
|
|
await page.evaluate(() => document.querySelector('div').style.removeProperty('display'));
|
|
|
|
expect(divVisible).toBe(false);
|
|
|
|
await page.evaluate(() => document.querySelector('div').style.removeProperty('visibility'));
|
|
|
|
expect(await waitForSelector).toBe(true);
|
|
|
|
expect(divVisible).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('hidden should wait for visibility: hidden', async({page, server}) => {
|
2017-10-13 16:11:11 +00:00
|
|
|
let divHidden = false;
|
|
|
|
await page.setContent(`<div style='display: block;'></div>`);
|
|
|
|
const waitForSelector = page.waitForSelector('div', {hidden: true}).then(() => divHidden = true);
|
2017-11-10 23:44:01 +00:00
|
|
|
await page.waitForSelector('div'); // do a round trip
|
|
|
|
expect(divHidden).toBe(false);
|
2017-10-13 16:11:11 +00:00
|
|
|
await page.evaluate(() => document.querySelector('div').style.setProperty('visibility', 'hidden'));
|
|
|
|
expect(await waitForSelector).toBe(true);
|
|
|
|
expect(divHidden).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('hidden should wait for display: none', async({page, server}) => {
|
2017-10-13 16:11:11 +00:00
|
|
|
let divHidden = false;
|
|
|
|
await page.setContent(`<div style='display: block;'></div>`);
|
|
|
|
const waitForSelector = page.waitForSelector('div', {hidden: true}).then(() => divHidden = true);
|
2017-11-10 23:44:01 +00:00
|
|
|
await page.waitForSelector('div'); // do a round trip
|
|
|
|
expect(divHidden).toBe(false);
|
2017-10-13 16:11:11 +00:00
|
|
|
await page.evaluate(() => document.querySelector('div').style.setProperty('display', 'none'));
|
|
|
|
expect(await waitForSelector).toBe(true);
|
|
|
|
expect(divHidden).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('hidden should wait for removal', async({page, server}) => {
|
2017-10-13 16:11:11 +00:00
|
|
|
await page.setContent(`<div></div>`);
|
|
|
|
let divRemoved = false;
|
|
|
|
const waitForSelector = page.waitForSelector('div', {hidden: true}).then(() => divRemoved = true);
|
2017-11-10 23:44:01 +00:00
|
|
|
await page.waitForSelector('div'); // do a round trip
|
2017-10-13 16:11:11 +00:00
|
|
|
expect(divRemoved).toBe(false);
|
|
|
|
await page.evaluate(() => document.querySelector('div').remove());
|
|
|
|
expect(await waitForSelector).toBe(true);
|
|
|
|
expect(divRemoved).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should respect timeout', async({page, server}) => {
|
2017-07-21 18:46:57 +00:00
|
|
|
let error = null;
|
2017-07-21 19:41:49 +00:00
|
|
|
await page.waitForSelector('div', {timeout: 10}).catch(e => error = e);
|
2017-07-21 18:46:57 +00:00
|
|
|
expect(error).toBeTruthy();
|
2017-07-24 16:58:51 +00:00
|
|
|
expect(error.message).toContain('waiting failed: timeout');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-23 20:25:40 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should respond to node attribute mutation', async({page, server}) => {
|
2017-08-23 20:25:40 +00:00
|
|
|
let divFound = false;
|
|
|
|
const waitForSelector = page.waitForSelector('.zombo').then(() => divFound = true);
|
|
|
|
await page.setContent(`<div class='notZombo'></div>`);
|
|
|
|
expect(divFound).toBe(false);
|
|
|
|
await page.evaluate(() => document.querySelector('div').className = 'zombo');
|
|
|
|
expect(await waitForSelector).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2018-01-04 22:49:13 +00:00
|
|
|
it('should return the element handle', async({page, server}) => {
|
|
|
|
const waitForSelector = page.waitForSelector('.zombo');
|
|
|
|
await page.setContent(`<div class='zombo'>anything</div>`);
|
|
|
|
expect(await page.evaluate(x => x.textContent, await waitForSelector)).toBe('anything');
|
|
|
|
});
|
2017-07-21 19:41:49 +00:00
|
|
|
});
|
|
|
|
|
2018-01-22 23:16:20 +00:00
|
|
|
describe('Frame.waitForXPath', function() {
|
|
|
|
const addElement = tag => document.body.appendChild(document.createElement(tag));
|
|
|
|
|
|
|
|
it('should support some fancy xpath', async({page, server}) => {
|
|
|
|
await page.setContent(`<p>red herring</p><p>hello world </p>`);
|
|
|
|
const waitForXPath = page.waitForXPath('//p[normalize-space(.)="hello world"]');
|
|
|
|
expect(await page.evaluate(x => x.textContent, await waitForXPath)).toBe('hello world ');
|
|
|
|
});
|
|
|
|
it('should run in specified frame', async({page, server}) => {
|
|
|
|
await FrameUtils.attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
|
|
|
await FrameUtils.attachFrame(page, 'frame2', server.EMPTY_PAGE);
|
|
|
|
const frame1 = page.frames()[1];
|
|
|
|
const frame2 = page.frames()[2];
|
|
|
|
let added = false;
|
2018-02-13 19:00:38 +00:00
|
|
|
const waitForXPathPromise = frame2.waitForXPath('//div').then(() => added = true);
|
2018-01-22 23:16:20 +00:00
|
|
|
expect(added).toBe(false);
|
|
|
|
await frame1.evaluate(addElement, 'div');
|
|
|
|
expect(added).toBe(false);
|
|
|
|
await frame2.evaluate(addElement, 'div');
|
2018-02-13 19:00:38 +00:00
|
|
|
await waitForXPathPromise;
|
2018-01-22 23:16:20 +00:00
|
|
|
});
|
|
|
|
it('should throw if evaluation failed', async({page, server}) => {
|
|
|
|
await page.evaluateOnNewDocument(function() {
|
|
|
|
document.evaluate = null;
|
|
|
|
});
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
let error = null;
|
|
|
|
await page.waitForXPath('*').catch(e => error = e);
|
|
|
|
expect(error.message).toContain('document.evaluate is not a function');
|
|
|
|
});
|
|
|
|
it('should throw when frame is detached', async({page, server}) => {
|
|
|
|
await FrameUtils.attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
|
|
|
const frame = page.frames()[1];
|
|
|
|
let waitError = null;
|
|
|
|
const waitPromise = frame.waitForXPath('//*[@class="box"]').catch(e => waitError = e);
|
|
|
|
await FrameUtils.detachFrame(page, 'frame1');
|
|
|
|
await waitPromise;
|
|
|
|
expect(waitError).toBeTruthy();
|
|
|
|
expect(waitError.message).toContain('waitForFunction failed: frame got detached.');
|
|
|
|
});
|
|
|
|
it('hidden should wait for display: none', async({page, server}) => {
|
|
|
|
let divHidden = false;
|
|
|
|
await page.setContent(`<div style='display: block;'></div>`);
|
|
|
|
const waitForXPath = page.waitForXPath('//div', {hidden: true}).then(() => divHidden = true);
|
|
|
|
await page.waitForXPath('//div'); // do a round trip
|
|
|
|
expect(divHidden).toBe(false);
|
|
|
|
await page.evaluate(() => document.querySelector('div').style.setProperty('display', 'none'));
|
|
|
|
expect(await waitForXPath).toBe(true);
|
|
|
|
expect(divHidden).toBe(true);
|
|
|
|
});
|
|
|
|
it('should return the element handle', async({page, server}) => {
|
|
|
|
const waitForXPath = page.waitForXPath('//*[@class="zombo"]');
|
|
|
|
await page.setContent(`<div class='zombo'>anything</div>`);
|
|
|
|
expect(await page.evaluate(x => x.textContent, await waitForXPath)).toBe('anything');
|
|
|
|
});
|
|
|
|
it('should allow you to select a text node', async({page, server}) => {
|
|
|
|
await page.setContent(`<div>some text</div>`);
|
|
|
|
const text = await page.waitForXPath('//div/text()');
|
|
|
|
expect(await (await text.getProperty('nodeType')).jsonValue()).toBe(3 /* Node.TEXT_NODE */);
|
|
|
|
});
|
|
|
|
it('should allow you to select an element with single slash', async({page, server}) => {
|
|
|
|
await page.setContent(`<div>some text</div>`);
|
|
|
|
const waitForXPath = page.waitForXPath('/html/body/div');
|
|
|
|
expect(await page.evaluate(x => x.textContent, await waitForXPath)).toBe('some text');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-07-21 19:41:49 +00:00
|
|
|
describe('Page.waitFor', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should wait for selector', async({page, server}) => {
|
2017-07-21 19:41:49 +00:00
|
|
|
let found = false;
|
2017-08-21 23:39:04 +00:00
|
|
|
const waitFor = page.waitFor('div').then(() => found = true);
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-07-21 19:41:49 +00:00
|
|
|
expect(found).toBe(false);
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2017-07-21 19:41:49 +00:00
|
|
|
await waitFor;
|
|
|
|
expect(found).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2018-01-22 23:16:20 +00:00
|
|
|
it('should wait for an xpath', async({page, server}) => {
|
|
|
|
let found = false;
|
|
|
|
const waitFor = page.waitFor('//div').then(() => found = true);
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(found).toBe(false);
|
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
|
|
|
await waitFor;
|
|
|
|
expect(found).toBe(true);
|
|
|
|
});
|
|
|
|
it('should not allow you to select an element with single slash xpath', async({page, server}) => {
|
|
|
|
await page.setContent(`<div>some text</div>`);
|
|
|
|
let error = null;
|
|
|
|
await page.waitFor('/html/body/div').catch(e => error = e);
|
|
|
|
expect(error).toBeTruthy();
|
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should timeout', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const startTime = Date.now();
|
2017-07-21 19:41:49 +00:00
|
|
|
const timeout = 42;
|
|
|
|
await page.waitFor(timeout);
|
2017-07-21 21:17:21 +00:00
|
|
|
expect(Date.now() - startTime).not.toBeLessThan(timeout / 2);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should wait for predicate', async({page, server}) => {
|
2017-07-28 00:09:28 +00:00
|
|
|
const watchdog = page.waitFor(() => window.innerWidth < 100);
|
|
|
|
page.setViewport({width: 10, height: 10});
|
|
|
|
await watchdog;
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw when unknown type', async({page, server}) => {
|
2017-09-13 19:06:57 +00:00
|
|
|
let error = null;
|
|
|
|
await page.waitFor({foo: 'bar'}).catch(e => error = e);
|
|
|
|
expect(error.message).toContain('Unsupported target type');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should wait for predicate with arguments', async({page, server}) => {
|
2017-09-15 21:28:15 +00:00
|
|
|
await page.waitFor((arg1, arg2) => arg1 !== arg2, {}, 1, 2);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-07-07 22:39:02 +00:00
|
|
|
});
|
|
|
|
|
2017-07-18 03:38:11 +00:00
|
|
|
describe('Page.Events.Console', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-09-29 18:27:22 +00:00
|
|
|
let message = null;
|
|
|
|
page.once('console', m => message = m);
|
2017-08-02 20:24:34 +00:00
|
|
|
await Promise.all([
|
2017-09-29 18:27:22 +00:00
|
|
|
page.evaluate(() => console.log('hello', 5, {foo: 'bar'})),
|
2017-08-02 20:24:34 +00:00
|
|
|
waitForEvents(page, 'console')
|
|
|
|
]);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(message.text()).toEqual('hello 5 JSHandle@object');
|
|
|
|
expect(message.type()).toEqual('log');
|
|
|
|
expect(await message.args()[0].jsonValue()).toEqual('hello');
|
|
|
|
expect(await message.args()[1].jsonValue()).toEqual(5);
|
|
|
|
expect(await message.args()[2].jsonValue()).toEqual({foo: 'bar'});
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work for different console API calls', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const messages = [];
|
2017-07-18 03:38:11 +00:00
|
|
|
page.on('console', msg => messages.push(msg));
|
2017-08-02 20:24:34 +00:00
|
|
|
await Promise.all([
|
|
|
|
page.evaluate(() => {
|
|
|
|
// A pair of time/timeEnd generates only one Console API call.
|
|
|
|
console.time('calling console.time');
|
|
|
|
console.timeEnd('calling console.time');
|
|
|
|
console.trace('calling console.trace');
|
|
|
|
console.dir('calling console.dir');
|
|
|
|
console.warn('calling console.warn');
|
|
|
|
console.error('calling console.error');
|
2017-08-16 00:35:59 +00:00
|
|
|
console.log(Promise.resolve('should not wait until resolved!'));
|
2017-08-02 20:24:34 +00:00
|
|
|
}),
|
2017-09-29 18:27:22 +00:00
|
|
|
// Wait for 5 events to hit - console.time is not reported
|
2017-08-02 20:24:34 +00:00
|
|
|
waitForEvents(page, 'console', 5)
|
|
|
|
]);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(messages.map(msg => msg.type())).toEqual([
|
2017-09-29 18:27:22 +00:00
|
|
|
'timeEnd', 'trace', 'dir', 'warning', 'error', 'log'
|
|
|
|
]);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(messages[0].text()).toContain('calling console.time');
|
|
|
|
expect(messages.slice(1).map(msg => msg.text())).toEqual([
|
2017-07-17 08:57:37 +00:00
|
|
|
'calling console.trace',
|
|
|
|
'calling console.dir',
|
|
|
|
'calling console.warn',
|
|
|
|
'calling console.error',
|
2017-10-10 00:01:01 +00:00
|
|
|
'JSHandle@promise',
|
2017-07-17 08:57:37 +00:00
|
|
|
]);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should not fail for window object', async({page, server}) => {
|
2017-09-29 18:27:22 +00:00
|
|
|
let message = null;
|
|
|
|
page.once('console', msg => message = msg);
|
2017-08-02 20:24:34 +00:00
|
|
|
await Promise.all([
|
|
|
|
page.evaluate(() => console.error(window)),
|
|
|
|
waitForEvents(page, 'console')
|
|
|
|
]);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(message.text()).toBe('JSHandle@object');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-07-17 08:57:37 +00:00
|
|
|
});
|
2017-06-15 07:20:37 +00:00
|
|
|
|
2018-01-17 07:22:44 +00:00
|
|
|
describe('Page.Events.DOMContentLoaded', function() {
|
|
|
|
it('should fire when expected', async({page, server}) => {
|
|
|
|
page.goto('about:blank');
|
|
|
|
await waitForEvents(page, 'domcontentloaded', 1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-11-01 20:39:31 +00:00
|
|
|
describe('Page.metrics', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should get metrics from a page', async({page, server}) => {
|
2017-10-10 21:50:38 +00:00
|
|
|
await page.goto('about:blank');
|
2017-11-01 20:39:31 +00:00
|
|
|
const metrics = await page.metrics();
|
2017-10-10 21:50:38 +00:00
|
|
|
checkMetrics(metrics);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('metrics event fired on console.timeStamp', async({page, server}) => {
|
2017-10-10 21:50:38 +00:00
|
|
|
const metricsPromise = new Promise(fulfill => page.once('metrics', fulfill));
|
|
|
|
await page.evaluate(() => console.timeStamp('test42'));
|
|
|
|
const metrics = await metricsPromise;
|
|
|
|
expect(metrics.title).toBe('test42');
|
|
|
|
checkMetrics(metrics.metrics);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-10 21:50:38 +00:00
|
|
|
function checkMetrics(metrics) {
|
|
|
|
const metricsToCheck = new Set([
|
|
|
|
'Timestamp',
|
2017-10-12 08:17:06 +00:00
|
|
|
'Documents',
|
|
|
|
'Frames',
|
|
|
|
'JSEventListeners',
|
|
|
|
'Nodes',
|
2017-10-10 21:50:38 +00:00
|
|
|
'LayoutCount',
|
|
|
|
'RecalcStyleCount',
|
|
|
|
'LayoutDuration',
|
|
|
|
'RecalcStyleDuration',
|
|
|
|
'ScriptDuration',
|
|
|
|
'TaskDuration',
|
|
|
|
'JSHeapUsedSize',
|
|
|
|
'JSHeapTotalSize',
|
|
|
|
]);
|
|
|
|
for (const name in metrics) {
|
|
|
|
expect(metricsToCheck.has(name)).toBeTruthy();
|
|
|
|
expect(metrics[name]).toBeGreaterThanOrEqual(0);
|
|
|
|
metricsToCheck.delete(name);
|
|
|
|
}
|
|
|
|
expect(metricsToCheck.size).toBe(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-08-15 20:55:48 +00:00
|
|
|
describe('Page.goto', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should navigate to about:blank', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const response = await page.goto('about:blank');
|
2017-07-28 23:44:51 +00:00
|
|
|
expect(response).toBe(null);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should navigate to empty page with domcontentloaded', async({page, server}) => {
|
|
|
|
const response = await page.goto(server.EMPTY_PAGE, {waitUntil: 'domcontentloaded'});
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.status()).toBe(200);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should navigate to empty page with networkidle0', async({page, server}) => {
|
|
|
|
const response = await page.goto(server.EMPTY_PAGE, {waitUntil: 'networkidle0'});
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.status()).toBe(200);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should navigate to empty page with networkidle2', async({page, server}) => {
|
|
|
|
const response = await page.goto(server.EMPTY_PAGE, {waitUntil: 'networkidle2'});
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.status()).toBe(200);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should fail when navigating to bad url', async({page, server}) => {
|
2017-07-10 22:09:52 +00:00
|
|
|
let error = null;
|
2017-09-13 19:06:57 +00:00
|
|
|
await page.goto('asdfasdf').catch(e => error = e);
|
2017-07-10 22:09:52 +00:00
|
|
|
expect(error.message).toContain('Cannot navigate to invalid URL');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should fail when navigating to bad SSL', async({page, httpsServer}) => {
|
2017-09-29 22:27:02 +00:00
|
|
|
// Make sure that network events do not emit 'undefined'.
|
2017-08-11 08:07:33 +00:00
|
|
|
// @see https://crbug.com/750469
|
2017-07-30 01:16:15 +00:00
|
|
|
page.on('request', request => expect(request).toBeTruthy());
|
|
|
|
page.on('requestfinished', request => expect(request).toBeTruthy());
|
|
|
|
page.on('requestfailed', request => expect(request).toBeTruthy());
|
2017-07-10 22:09:52 +00:00
|
|
|
let error = null;
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(httpsServer.EMPTY_PAGE).catch(e => error = e);
|
2017-12-07 21:40:02 +00:00
|
|
|
expect(error.message).toContain('net::ERR_CERT_AUTHORITY_INVALID');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should fail when navigating to bad SSL after redirects', async({page, server, httpsServer}) => {
|
2017-11-01 20:28:00 +00:00
|
|
|
server.setRedirect('/redirect/1.html', '/redirect/2.html');
|
|
|
|
server.setRedirect('/redirect/2.html', '/empty.html');
|
|
|
|
let error = null;
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(httpsServer.PREFIX + '/redirect/1.html').catch(e => error = e);
|
2017-12-07 21:40:02 +00:00
|
|
|
expect(error.message).toContain('net::ERR_CERT_AUTHORITY_INVALID');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw if networkidle is passed as an option', async({page, server}) => {
|
2017-10-24 01:10:59 +00:00
|
|
|
let error = null;
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE, {waitUntil: 'networkidle'}).catch(err => error = err);
|
2017-10-24 01:10:59 +00:00
|
|
|
expect(error.message).toContain('"networkidle" option is no longer supported');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should fail when main resources failed to load', async({page, server}) => {
|
2017-07-28 00:54:39 +00:00
|
|
|
let error = null;
|
2017-09-13 19:06:57 +00:00
|
|
|
await page.goto('http://localhost:44123/non-existing-url').catch(e => error = e);
|
2017-11-10 23:33:14 +00:00
|
|
|
expect(error.message).toContain('net::ERR_CONNECTION_REFUSED');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should fail when exceeding maximum navigation timeout', async({page, server}) => {
|
2017-07-10 22:09:52 +00:00
|
|
|
// Hang for request to the empty.html
|
|
|
|
server.setRoute('/empty.html', (req, res) => { });
|
2017-09-11 23:43:37 +00:00
|
|
|
let error = null;
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/empty.html', {timeout: 1}).catch(e => error = e);
|
2017-09-11 23:43:37 +00:00
|
|
|
expect(error.message).toContain('Navigation Timeout Exceeded: 1ms');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2018-01-10 21:04:01 +00:00
|
|
|
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');
|
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should disable timeout when its set to 0', async({page, server}) => {
|
2017-09-30 07:29:38 +00:00
|
|
|
let error = null;
|
2017-11-21 05:21:25 +00:00
|
|
|
let loaded = false;
|
|
|
|
page.once('load', () => loaded = true);
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/grid.html', {timeout: 0, waitUntil: ['load']}).catch(e => error = e);
|
2017-09-30 07:29:38 +00:00
|
|
|
expect(error).toBe(null);
|
2017-11-21 05:21:25 +00:00
|
|
|
expect(loaded).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work when navigating to valid url', async({page, server}) => {
|
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.ok()).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work when navigating to data url', async({page, server}) => {
|
2017-08-10 07:02:10 +00:00
|
|
|
const response = await page.goto('data:text/html,hello');
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.ok()).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work when navigating to 404', async({page, server}) => {
|
|
|
|
const response = await page.goto(server.PREFIX + '/not-found');
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.ok()).toBe(false);
|
|
|
|
expect(response.status()).toBe(404);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should return last response in redirect chain', async({page, server}) => {
|
2017-07-10 22:09:52 +00:00
|
|
|
server.setRedirect('/redirect/1.html', '/redirect/2.html');
|
|
|
|
server.setRedirect('/redirect/2.html', '/redirect/3.html');
|
2017-12-12 21:34:21 +00:00
|
|
|
server.setRedirect('/redirect/3.html', server.EMPTY_PAGE);
|
|
|
|
const response = await page.goto(server.PREFIX + '/redirect/1.html');
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.ok()).toBe(true);
|
|
|
|
expect(response.url()).toBe(server.EMPTY_PAGE);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should wait for network idle to succeed navigation', async({page, server}) => {
|
2017-06-28 05:02:46 +00:00
|
|
|
let responses = [];
|
2017-06-28 21:39:37 +00:00
|
|
|
// Hold on to a bunch of requests without answering.
|
2017-07-07 15:14:25 +00:00
|
|
|
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));
|
2017-08-21 23:39:04 +00:00
|
|
|
const initialFetchResourcesRequested = Promise.all([
|
2017-07-07 15:14:25 +00:00
|
|
|
server.waitForRequest('/fetch-request-a.js'),
|
|
|
|
server.waitForRequest('/fetch-request-b.js'),
|
|
|
|
server.waitForRequest('/fetch-request-c.js'),
|
2017-06-28 05:02:46 +00:00
|
|
|
]);
|
2017-08-21 23:39:04 +00:00
|
|
|
const secondFetchResourceRequested = server.waitForRequest('/fetch-request-d.js');
|
2017-06-28 21:39:37 +00:00
|
|
|
|
2017-06-28 05:02:46 +00:00
|
|
|
// Navigate to a page which loads immediately and then does a bunch of
|
|
|
|
// requests via javascript's fetch method.
|
2017-12-12 21:34:21 +00:00
|
|
|
const navigationPromise = page.goto(server.PREFIX + '/networkidle.html', {
|
2017-10-24 01:10:59 +00:00
|
|
|
waitUntil: 'networkidle0',
|
2017-06-28 21:39:37 +00:00
|
|
|
});
|
|
|
|
// 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.
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const response of responses) {
|
2017-06-28 21:39:37 +00:00
|
|
|
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.
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const response of responses) {
|
2017-06-28 21:39:37 +00:00
|
|
|
response.statusCode = 404;
|
|
|
|
response.end(`File not found`);
|
|
|
|
}
|
|
|
|
|
2017-07-10 22:09:52 +00:00
|
|
|
const response = await navigationPromise;
|
2017-06-28 21:39:37 +00:00
|
|
|
// Expect navigation to succeed.
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.ok()).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should not leak listeners during navigation', async({page, server}) => {
|
2017-07-23 00:03:58 +00:00
|
|
|
let warning = null;
|
|
|
|
const warningHandler = w => warning = w;
|
|
|
|
process.on('warning', warningHandler);
|
|
|
|
for (let i = 0; i < 20; ++i)
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-07-23 00:03:58 +00:00
|
|
|
process.removeListener('warning', warningHandler);
|
|
|
|
expect(warning).toBe(null);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should not leak listeners during bad navigation', async({page, server}) => {
|
2017-09-30 07:50:13 +00:00
|
|
|
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);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should navigate to dataURL and fire dataURL requests', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const requests = [];
|
2017-08-15 20:55:48 +00:00
|
|
|
page.on('request', request => requests.push(request));
|
2017-08-21 23:39:04 +00:00
|
|
|
const dataURL = 'data:text/html,<div>yo</div>';
|
|
|
|
const response = await page.goto(dataURL);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.status()).toBe(200);
|
2017-08-15 20:55:48 +00:00
|
|
|
expect(requests.length).toBe(1);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(requests[0].url()).toBe(dataURL);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should navigate to URL with hash and fire requests without hash', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const requests = [];
|
2017-08-15 20:55:48 +00:00
|
|
|
page.on('request', request => requests.push(request));
|
2017-12-12 21:34:21 +00:00
|
|
|
const response = await page.goto(server.EMPTY_PAGE + '#hash');
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
expect(response.url()).toBe(server.EMPTY_PAGE);
|
2017-08-15 20:55:48 +00:00
|
|
|
expect(requests.length).toBe(1);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(requests[0].url()).toBe(server.EMPTY_PAGE);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2018-01-12 08:16:36 +00:00
|
|
|
it('should work with self requesting page', async({page, server}) => {
|
|
|
|
const response = await page.goto(server.PREFIX + '/self-request.html');
|
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
expect(response.url()).toContain('self-request.html');
|
|
|
|
});
|
2017-06-21 20:51:06 +00:00
|
|
|
});
|
2017-06-16 04:22:41 +00:00
|
|
|
|
2017-07-19 01:54:24 +00:00
|
|
|
describe('Page.waitForNavigation', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-08-02 20:24:34 +00:00
|
|
|
const [result] = await Promise.all([
|
|
|
|
page.waitForNavigation(),
|
2017-12-12 21:34:21 +00:00
|
|
|
page.evaluate(url => window.location.href = url, server.PREFIX + '/grid.html')
|
2017-08-02 20:24:34 +00:00
|
|
|
]);
|
2017-07-19 01:54:24 +00:00
|
|
|
const response = await result;
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.ok()).toBe(true);
|
|
|
|
expect(response.url()).toContain('grid.html');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work with both domcontentloaded and load', async({page, server}) => {
|
2017-10-24 17:05:15 +00:00
|
|
|
let response = null;
|
|
|
|
server.setRoute('/one-style.css', (req, res) => response = res);
|
2017-12-20 00:28:39 +00:00
|
|
|
const navigationPromise = page.goto(server.PREFIX + '/one-style.html');
|
2017-10-24 17:05:15 +00:00
|
|
|
const domContentLoadedPromise = page.waitForNavigation({
|
|
|
|
waitUntil: 'domcontentloaded'
|
|
|
|
});
|
|
|
|
|
|
|
|
let bothFired = false;
|
|
|
|
const bothFiredPromise = page.waitForNavigation({
|
|
|
|
waitUntil: ['load', 'domcontentloaded']
|
|
|
|
}).then(() => bothFired = true);
|
|
|
|
|
|
|
|
await server.waitForRequest('/one-style.css');
|
|
|
|
await domContentLoadedPromise;
|
|
|
|
expect(bothFired).toBe(false);
|
|
|
|
response.end();
|
|
|
|
await bothFiredPromise;
|
2017-12-20 00:28:39 +00:00
|
|
|
await navigationPromise;
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-07-19 01:54:24 +00:00
|
|
|
});
|
|
|
|
|
2017-07-19 02:11:37 +00:00
|
|
|
describe('Page.goBack', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2017-07-19 02:11:37 +00:00
|
|
|
|
|
|
|
let response = await page.goBack();
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.ok()).toBe(true);
|
|
|
|
expect(response.url()).toContain(server.EMPTY_PAGE);
|
2017-07-19 02:11:37 +00:00
|
|
|
|
|
|
|
response = await page.goForward();
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.ok()).toBe(true);
|
|
|
|
expect(response.url()).toContain('/grid.html');
|
2017-07-19 02:11:37 +00:00
|
|
|
|
|
|
|
response = await page.goForward();
|
|
|
|
expect(response).toBe(null);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-07-19 02:11:37 +00:00
|
|
|
});
|
|
|
|
|
2017-08-16 06:04:16 +00:00
|
|
|
describe('Page.exposeFunction', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-08-16 06:04:16 +00:00
|
|
|
await page.exposeFunction('compute', function(a, b) {
|
2017-06-21 20:51:06 +00:00
|
|
|
return a * b;
|
|
|
|
});
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.evaluate(async function() {
|
2017-08-11 04:44:49 +00:00
|
|
|
return await compute(9, 4);
|
2017-06-21 20:51:06 +00:00
|
|
|
});
|
|
|
|
expect(result).toBe(36);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should survive navigation', async({page, server}) => {
|
2017-08-16 06:04:16 +00:00
|
|
|
await page.exposeFunction('compute', function(a, b) {
|
2017-06-21 20:51:06 +00:00
|
|
|
return a * b;
|
|
|
|
});
|
2017-06-16 18:21:44 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.evaluate(async function() {
|
2017-08-11 04:44:49 +00:00
|
|
|
return await compute(9, 4);
|
2017-06-21 20:51:06 +00:00
|
|
|
});
|
|
|
|
expect(result).toBe(36);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should await returned promise', async({page, server}) => {
|
2017-08-16 06:04:16 +00:00
|
|
|
await page.exposeFunction('compute', function(a, b) {
|
2017-06-21 20:51:06 +00:00
|
|
|
return Promise.resolve(a * b);
|
|
|
|
});
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.evaluate(async function() {
|
2017-08-11 04:44:49 +00:00
|
|
|
return await compute(3, 5);
|
2017-11-07 21:26:23 +00:00
|
|
|
});
|
|
|
|
expect(result).toBe(15);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work on frames', async({page, server}) => {
|
2017-11-07 21:26:23 +00:00
|
|
|
await page.exposeFunction('compute', function(a, b) {
|
|
|
|
return Promise.resolve(a * b);
|
|
|
|
});
|
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/frames/nested-frames.html');
|
2017-11-07 21:26:23 +00:00
|
|
|
const frame = page.frames()[1];
|
|
|
|
const result = await frame.evaluate(async function() {
|
|
|
|
return await compute(3, 5);
|
|
|
|
});
|
|
|
|
expect(result).toBe(15);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work on frames before navigation', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/frames/nested-frames.html');
|
2017-11-07 21:26:23 +00:00
|
|
|
await page.exposeFunction('compute', function(a, b) {
|
|
|
|
return Promise.resolve(a * b);
|
|
|
|
});
|
|
|
|
|
|
|
|
const frame = page.frames()[1];
|
|
|
|
const result = await frame.evaluate(async function() {
|
|
|
|
return await compute(3, 5);
|
2017-06-21 20:51:06 +00:00
|
|
|
});
|
|
|
|
expect(result).toBe(15);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-06-21 20:51:06 +00:00
|
|
|
});
|
2017-06-17 04:40:00 +00:00
|
|
|
|
2017-10-24 21:45:03 +00:00
|
|
|
describe('Page.setRequestInterception', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should intercept', async({page, server}) => {
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(true);
|
2017-08-12 00:24:31 +00:00
|
|
|
page.on('request', request => {
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(request.url()).toContain('empty.html');
|
|
|
|
expect(request.headers()['user-agent']).toBeTruthy();
|
|
|
|
expect(request.method()).toBe('GET');
|
|
|
|
expect(request.postData()).toBe(undefined);
|
|
|
|
expect(request.resourceType()).toBe('document');
|
2018-01-10 02:47:21 +00:00
|
|
|
expect(request.frame() === page.mainFrame()).toBe(true);
|
|
|
|
expect(request.frame().url()).toBe('about:blank');
|
2017-06-21 20:51:06 +00:00
|
|
|
request.continue();
|
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.ok()).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should stop intercepting', async({page, server}) => {
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(true);
|
|
|
|
page.once('request', request => request.continue());
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(false);
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
});
|
|
|
|
it('should show custom HTTP headers', async({page, server}) => {
|
2017-08-28 19:09:24 +00:00
|
|
|
await page.setExtraHTTPHeaders({
|
2017-06-21 20:51:06 +00:00
|
|
|
foo: 'bar'
|
2017-08-28 19:09:24 +00:00
|
|
|
});
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(true);
|
2017-08-12 00:24:31 +00:00
|
|
|
page.on('request', request => {
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(request.headers()['foo']).toBe('bar');
|
2017-06-21 20:51:06 +00:00
|
|
|
request.continue();
|
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.ok()).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2018-01-10 00:52:28 +00:00
|
|
|
it('should works with customizing referer headers', async({page, server}) => {
|
|
|
|
await page.setExtraHTTPHeaders({ 'referer': server.EMPTY_PAGE });
|
|
|
|
await page.setRequestInterception(true);
|
|
|
|
page.on('request', request => {
|
|
|
|
expect(request.headers()['referer']).toBe(server.EMPTY_PAGE);
|
|
|
|
request.continue();
|
|
|
|
});
|
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(response.ok()).toBe(true);
|
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should be abortable', async({page, server}) => {
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(true);
|
2017-08-12 00:24:31 +00:00
|
|
|
page.on('request', request => {
|
2017-12-19 01:05:57 +00:00
|
|
|
if (request.url().endsWith('.css'))
|
2017-06-21 20:51:06 +00:00
|
|
|
request.abort();
|
|
|
|
else
|
|
|
|
request.continue();
|
|
|
|
});
|
2017-06-29 06:09:28 +00:00
|
|
|
let failedRequests = 0;
|
|
|
|
page.on('requestfailed', event => ++failedRequests);
|
2017-12-12 21:34:21 +00:00
|
|
|
const response = await page.goto(server.PREFIX + '/one-style.html');
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.ok()).toBe(true);
|
2017-10-18 07:26:48 +00:00
|
|
|
expect(response.request().failure()).toBe(null);
|
2017-06-29 06:09:28 +00:00
|
|
|
expect(failedRequests).toBe(1);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should be abortable with custom error codes', async({page, server}) => {
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(true);
|
2017-10-18 07:26:48 +00:00
|
|
|
page.on('request', request => {
|
|
|
|
request.abort('internetdisconnected');
|
|
|
|
});
|
|
|
|
let failedRequest = null;
|
|
|
|
page.on('requestfailed', request => failedRequest = request);
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE).catch(e => {});
|
2017-10-18 07:26:48 +00:00
|
|
|
expect(failedRequest).toBeTruthy();
|
|
|
|
expect(failedRequest.failure().errorText).toBe('net::ERR_INTERNET_DISCONNECTED');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2018-02-01 00:02:56 +00:00
|
|
|
it('should send referer', async({page, server}) => {
|
|
|
|
await page.setExtraHTTPHeaders({
|
|
|
|
referer: 'http://google.com/'
|
|
|
|
});
|
|
|
|
await page.setRequestInterception(true);
|
|
|
|
page.on('request', request => request.continue());
|
|
|
|
const [request] = await Promise.all([
|
|
|
|
server.waitForRequest('/grid.html'),
|
|
|
|
page.goto(server.PREFIX + '/grid.html'),
|
|
|
|
]);
|
|
|
|
expect(request.headers['referer']).toBe('http://google.com/');
|
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should amend HTTP headers', async({page, server}) => {
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(true);
|
2017-08-12 00:24:31 +00:00
|
|
|
page.on('request', request => {
|
2017-12-19 01:05:57 +00:00
|
|
|
const headers = Object.assign({}, request.headers());
|
2017-08-28 19:09:24 +00:00
|
|
|
headers['FOO'] = 'bar';
|
2017-08-12 00:24:31 +00:00
|
|
|
request.continue({ headers });
|
2017-06-28 06:31:38 +00:00
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-08-02 20:24:34 +00:00
|
|
|
const [request] = await Promise.all([
|
|
|
|
server.waitForRequest('/sleep.zzz'),
|
|
|
|
page.evaluate(() => fetch('/sleep.zzz'))
|
|
|
|
]);
|
2017-06-28 06:31:38 +00:00
|
|
|
expect(request.headers['foo']).toBe('bar');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should fail navigation when aborting main resource', async({page, server}) => {
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(true);
|
2017-08-12 00:24:31 +00:00
|
|
|
page.on('request', request => request.abort());
|
2017-08-07 22:13:17 +00:00
|
|
|
let error = null;
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE).catch(e => error = e);
|
2017-08-07 22:13:17 +00:00
|
|
|
expect(error).toBeTruthy();
|
2017-11-10 23:33:14 +00:00
|
|
|
expect(error.message).toContain('net::ERR_FAILED');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work with redirects', async({page, server}) => {
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(true);
|
2017-08-29 23:27:59 +00:00
|
|
|
const requests = [];
|
|
|
|
page.on('request', request => {
|
|
|
|
request.continue();
|
|
|
|
requests.push(request);
|
|
|
|
});
|
2017-08-12 00:24:31 +00:00
|
|
|
server.setRedirect('/non-existing-page.html', '/non-existing-page-2.html');
|
|
|
|
server.setRedirect('/non-existing-page-2.html', '/non-existing-page-3.html');
|
|
|
|
server.setRedirect('/non-existing-page-3.html', '/non-existing-page-4.html');
|
|
|
|
server.setRedirect('/non-existing-page-4.html', '/empty.html');
|
2017-12-12 21:34:21 +00:00
|
|
|
const response = await page.goto(server.PREFIX + '/non-existing-page.html');
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
expect(response.url()).toContain('empty.html');
|
2017-08-29 23:27:59 +00:00
|
|
|
expect(requests.length).toBe(5);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(requests[2].resourceType()).toBe('document');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should be able to abort redirects', async({page, server}) => {
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(true);
|
2017-08-12 00:24:31 +00:00
|
|
|
server.setRedirect('/non-existing.json', '/non-existing-2.json');
|
|
|
|
server.setRedirect('/non-existing-2.json', '/simple.html');
|
|
|
|
page.on('request', request => {
|
2017-12-19 01:05:57 +00:00
|
|
|
if (request.url().includes('non-existing-2'))
|
2017-08-12 00:24:31 +00:00
|
|
|
request.abort();
|
|
|
|
else
|
|
|
|
request.continue();
|
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.evaluate(async() => {
|
2017-08-12 00:24:31 +00:00
|
|
|
try {
|
|
|
|
await fetch('/non-existing.json');
|
|
|
|
} catch (e) {
|
|
|
|
return e.message;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(result).toContain('Failed to fetch');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work with equal requests', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-08-12 00:24:31 +00:00
|
|
|
let responseCount = 1;
|
|
|
|
server.setRoute('/zzz', (req, res) => res.end((responseCount++) * 11 + ''));
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(true);
|
2017-08-12 00:24:31 +00:00
|
|
|
|
|
|
|
let spinner = false;
|
|
|
|
// Cancel 2nd request.
|
|
|
|
page.on('request', request => {
|
|
|
|
spinner ? request.abort() : request.continue();
|
|
|
|
spinner = !spinner;
|
|
|
|
});
|
2017-08-21 23:39:04 +00:00
|
|
|
const results = await page.evaluate(() => Promise.all([
|
2017-08-12 00:24:31 +00:00
|
|
|
fetch('/zzz').then(response => response.text()).catch(e => 'FAILED'),
|
|
|
|
fetch('/zzz').then(response => response.text()).catch(e => 'FAILED'),
|
|
|
|
fetch('/zzz').then(response => response.text()).catch(e => 'FAILED'),
|
|
|
|
]));
|
|
|
|
expect(results).toEqual(['11', 'FAILED', '22']);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should navigate to dataURL and fire dataURL requests', async({page, server}) => {
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(true);
|
2017-08-21 23:39:04 +00:00
|
|
|
const requests = [];
|
2017-08-15 20:55:48 +00:00
|
|
|
page.on('request', request => {
|
|
|
|
requests.push(request);
|
|
|
|
request.continue();
|
|
|
|
});
|
2017-08-21 23:39:04 +00:00
|
|
|
const dataURL = 'data:text/html,<div>yo</div>';
|
|
|
|
const response = await page.goto(dataURL);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.status()).toBe(200);
|
2017-08-15 20:55:48 +00:00
|
|
|
expect(requests.length).toBe(1);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(requests[0].url()).toBe(dataURL);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should abort data server', async({page, server}) => {
|
2017-11-01 21:04:10 +00:00
|
|
|
await page.setRequestInterception(true);
|
|
|
|
page.on('request', request => {
|
|
|
|
request.abort();
|
|
|
|
});
|
|
|
|
let error = null;
|
|
|
|
await page.goto('data:text/html,No way!').catch(err => error = err);
|
2017-11-10 23:33:14 +00:00
|
|
|
expect(error.message).toContain('net::ERR_FAILED');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should navigate to URL with hash and and fire requests without hash', async({page, server}) => {
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(true);
|
2017-08-21 23:39:04 +00:00
|
|
|
const requests = [];
|
2017-08-15 20:55:48 +00:00
|
|
|
page.on('request', request => {
|
|
|
|
requests.push(request);
|
|
|
|
request.continue();
|
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
const response = await page.goto(server.EMPTY_PAGE + '#hash');
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
expect(response.url()).toBe(server.EMPTY_PAGE);
|
2017-08-15 20:55:48 +00:00
|
|
|
expect(requests.length).toBe(1);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(requests[0].url()).toBe(server.EMPTY_PAGE);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work with encoded server', async({page, server}) => {
|
2017-08-27 19:41:09 +00:00
|
|
|
// The requestWillBeSent will report encoded URL, whereas interception will
|
|
|
|
// report URL as-is. @see crbug.com/759388
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(true);
|
2017-08-27 19:41:09 +00:00
|
|
|
page.on('request', request => request.continue());
|
2017-12-12 21:34:21 +00:00
|
|
|
const response = await page.goto(server.PREFIX + '/some nonexisting page');
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.status()).toBe(404);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work with badly encoded server', async({page, server}) => {
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(true);
|
2017-09-28 23:29:19 +00:00
|
|
|
server.setRoute('/malformed?rnd=%911', (req, res) => res.end());
|
|
|
|
page.on('request', request => request.continue());
|
2017-12-12 21:34:21 +00:00
|
|
|
const response = await page.goto(server.PREFIX + '/malformed?rnd=%911');
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.status()).toBe(200);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work with encoded server - 2', async({page, server}) => {
|
2017-08-27 19:41:09 +00:00
|
|
|
// The requestWillBeSent will report URL as-is, whereas interception will
|
|
|
|
// report encoded URL for stylesheet. @see crbug.com/759388
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(true);
|
2017-08-27 19:41:09 +00:00
|
|
|
const requests = [];
|
|
|
|
page.on('request', request => {
|
|
|
|
request.continue();
|
|
|
|
requests.push(request);
|
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
const response = await page.goto(`data:text/html,<link rel="stylesheet" href="${server.PREFIX}/fonts?helvetica|arial"/>`);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.status()).toBe(200);
|
2017-08-27 19:41:09 +00:00
|
|
|
expect(requests.length).toBe(2);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(requests[1].response().status()).toBe(404);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should not throw "Invalid Interception Id" if the request was cancelled', async({page, server}) => {
|
2017-09-11 23:21:51 +00:00
|
|
|
await page.setContent('<iframe></iframe>');
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(true);
|
2017-09-11 23:21:51 +00:00
|
|
|
let request = null;
|
|
|
|
page.on('request', async r => request = r);
|
2017-12-12 21:34:21 +00:00
|
|
|
page.$eval('iframe', (frame, url) => frame.src = url, server.EMPTY_PAGE),
|
2017-09-11 23:21:51 +00:00
|
|
|
// Wait for request interception.
|
|
|
|
await waitForEvents(page, 'request');
|
|
|
|
// Delete frame to cause request to be canceled.
|
|
|
|
await page.$eval('iframe', frame => frame.remove());
|
|
|
|
let error = null;
|
|
|
|
await request.continue().catch(e => error = e);
|
|
|
|
expect(error).toBe(null);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw if interception is not enabled', async({page, server}) => {
|
2017-10-20 23:55:15 +00:00
|
|
|
let error = null;
|
|
|
|
page.on('request', async request => {
|
|
|
|
try {
|
|
|
|
await request.continue();
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-10-20 23:55:15 +00:00
|
|
|
expect(error.message).toContain('Request Interception is not enabled');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-20 23:55:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Request.respond', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(true);
|
2017-10-20 23:55:15 +00:00
|
|
|
page.on('request', request => {
|
|
|
|
request.respond({
|
|
|
|
status: 201,
|
|
|
|
headers: {
|
|
|
|
foo: 'bar'
|
|
|
|
},
|
|
|
|
body: 'Yo, page!'
|
|
|
|
});
|
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.status()).toBe(201);
|
|
|
|
expect(response.headers().foo).toBe('bar');
|
2017-10-20 23:55:15 +00:00
|
|
|
expect(await page.evaluate(() => document.body.textContent)).toBe('Yo, page!');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should allow mocking binary responses', async({page, server}) => {
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(true);
|
2017-10-20 23:55:15 +00:00
|
|
|
page.on('request', request => {
|
|
|
|
const imageBuffer = fs.readFileSync(path.join(__dirname, 'assets', 'pptr.png'));
|
|
|
|
request.respond({
|
|
|
|
contentType: 'image/png',
|
|
|
|
body: imageBuffer
|
|
|
|
});
|
|
|
|
});
|
|
|
|
await page.evaluate(PREFIX => {
|
|
|
|
const img = document.createElement('img');
|
|
|
|
img.src = PREFIX + '/does-not-exist.png';
|
|
|
|
document.body.appendChild(img);
|
|
|
|
return new Promise(fulfill => img.onload = fulfill);
|
2017-12-12 21:34:21 +00:00
|
|
|
}, server.PREFIX);
|
2017-10-20 23:55:15 +00:00
|
|
|
const img = await page.$('img');
|
|
|
|
expect(await img.screenshot()).toBeGolden('mock-binary-response.png');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-06-21 20:51:06 +00:00
|
|
|
});
|
2017-06-17 21:27:51 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
describe('Page.Events.Dialog', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should fire', async({page, server}) => {
|
2017-06-21 20:51:06 +00:00
|
|
|
page.on('dialog', dialog => {
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(dialog.type()).toBe('alert');
|
2017-08-11 19:17:43 +00:00
|
|
|
expect(dialog.defaultValue()).toBe('');
|
2017-06-21 20:51:06 +00:00
|
|
|
expect(dialog.message()).toBe('yo');
|
2017-08-02 20:12:44 +00:00
|
|
|
dialog.accept();
|
2017-06-21 20:51:06 +00:00
|
|
|
});
|
2017-08-02 20:12:44 +00:00
|
|
|
await page.evaluate(() => alert('yo'));
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should allow accepting prompts', async({page, server}) => {
|
2017-06-21 20:51:06 +00:00
|
|
|
page.on('dialog', dialog => {
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(dialog.type()).toBe('prompt');
|
2017-08-11 19:17:43 +00:00
|
|
|
expect(dialog.defaultValue()).toBe('yes.');
|
2017-06-21 20:51:06 +00:00
|
|
|
expect(dialog.message()).toBe('question?');
|
|
|
|
dialog.accept('answer!');
|
|
|
|
});
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.evaluate(() => prompt('question?', 'yes.'));
|
2017-06-21 20:51:06 +00:00
|
|
|
expect(result).toBe('answer!');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should dismiss the prompt', async({page, server}) => {
|
2017-08-02 22:32:20 +00:00
|
|
|
page.on('dialog', dialog => {
|
|
|
|
dialog.dismiss();
|
|
|
|
});
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.evaluate(() => prompt('question?'));
|
2017-08-02 22:32:20 +00:00
|
|
|
expect(result).toBe(null);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-06-21 20:51:06 +00:00
|
|
|
});
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2017-07-06 18:22:32 +00:00
|
|
|
describe('Page.Events.PageError', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should fire', async({page, server}) => {
|
2017-07-28 00:20:02 +00:00
|
|
|
let error = null;
|
|
|
|
page.once('pageerror', e => error = e);
|
2017-12-20 00:28:39 +00:00
|
|
|
await Promise.all([
|
|
|
|
page.goto(server.PREFIX + '/error.html'),
|
|
|
|
waitForEvents(page, 'pageerror')
|
|
|
|
]);
|
2017-07-28 00:20:02 +00:00
|
|
|
expect(error.message).toContain('Fancy');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-06-21 20:51:06 +00:00
|
|
|
});
|
2017-06-17 21:27:51 +00:00
|
|
|
|
2017-06-28 08:10:23 +00:00
|
|
|
describe('Page.Events.Request', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should fire', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const requests = [];
|
2017-06-28 08:10:23 +00:00
|
|
|
page.on('request', request => requests.push(request));
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2018-01-10 02:47:21 +00:00
|
|
|
await FrameUtils.attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
|
|
|
expect(requests.length).toBe(2);
|
|
|
|
expect(requests[0].url()).toBe(server.EMPTY_PAGE);
|
|
|
|
expect(requests[0].frame() === page.mainFrame()).toBe(true);
|
|
|
|
expect(requests[0].frame().url()).toBe(server.EMPTY_PAGE);
|
|
|
|
expect(requests[1].url()).toBe(server.EMPTY_PAGE);
|
|
|
|
expect(requests[1].frame() === page.frames()[1]).toBe(true);
|
|
|
|
expect(requests[1].frame().url()).toBe(server.EMPTY_PAGE);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-06-28 08:10:23 +00:00
|
|
|
});
|
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
describe('Frame Management', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should handle nested frames', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/frames/nested-frames.html');
|
2017-06-21 20:51:06 +00:00
|
|
|
expect(FrameUtils.dumpFrames(page.mainFrame())).toBeGolden('nested-frames.txt');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should send events when frames are manipulated dynamically', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-06-21 20:51:06 +00:00
|
|
|
// validate frameattached events
|
2017-08-21 23:39:04 +00:00
|
|
|
const attachedFrames = [];
|
2017-06-21 20:51:06 +00:00
|
|
|
page.on('frameattached', frame => attachedFrames.push(frame));
|
|
|
|
await FrameUtils.attachFrame(page, 'frame1', './assets/frame.html');
|
|
|
|
expect(attachedFrames.length).toBe(1);
|
|
|
|
expect(attachedFrames[0].url()).toContain('/assets/frame.html');
|
2017-06-17 21:27:51 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
// validate framenavigated events
|
2017-08-21 23:39:04 +00:00
|
|
|
const navigatedFrames = [];
|
2017-06-21 20:51:06 +00:00
|
|
|
page.on('framenavigated', frame => navigatedFrames.push(frame));
|
|
|
|
await FrameUtils.navigateFrame(page, 'frame1', './empty.html');
|
|
|
|
expect(navigatedFrames.length).toBe(1);
|
2018-01-10 02:47:21 +00:00
|
|
|
expect(navigatedFrames[0].url()).toBe(server.EMPTY_PAGE);
|
2017-06-21 14:45:13 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
// validate framedetached events
|
2017-08-21 23:39:04 +00:00
|
|
|
const detachedFrames = [];
|
2017-06-21 20:51:06 +00:00
|
|
|
page.on('framedetached', frame => detachedFrames.push(frame));
|
|
|
|
await FrameUtils.detachFrame(page, 'frame1');
|
|
|
|
expect(detachedFrames.length).toBe(1);
|
|
|
|
expect(detachedFrames[0].isDetached()).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should persist mainFrame on cross-process navigation', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-08-21 23:39:04 +00:00
|
|
|
const mainFrame = page.mainFrame();
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.CROSS_PROCESS_PREFIX + '/empty.html');
|
2017-06-21 20:51:06 +00:00
|
|
|
expect(page.mainFrame() === mainFrame).toBeTruthy();
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should not send attach/detach events for main frame', async({page, server}) => {
|
2017-06-22 20:38:10 +00:00
|
|
|
let hasEvents = false;
|
2017-06-21 20:51:06 +00:00
|
|
|
page.on('frameattached', frame => hasEvents = true);
|
|
|
|
page.on('framedetached', frame => hasEvents = true);
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-06-21 20:51:06 +00:00
|
|
|
expect(hasEvents).toBe(false);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should detach child frames on navigation', async({page, server}) => {
|
2017-06-22 20:38:10 +00:00
|
|
|
let attachedFrames = [];
|
|
|
|
let detachedFrames = [];
|
|
|
|
let navigatedFrames = [];
|
2017-06-21 20:51:06 +00:00
|
|
|
page.on('frameattached', frame => attachedFrames.push(frame));
|
|
|
|
page.on('framedetached', frame => detachedFrames.push(frame));
|
|
|
|
page.on('framenavigated', frame => navigatedFrames.push(frame));
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/frames/nested-frames.html');
|
2017-06-21 20:51:06 +00:00
|
|
|
expect(attachedFrames.length).toBe(4);
|
|
|
|
expect(detachedFrames.length).toBe(0);
|
|
|
|
expect(navigatedFrames.length).toBe(5);
|
|
|
|
|
2017-06-22 20:38:10 +00:00
|
|
|
attachedFrames = [];
|
|
|
|
detachedFrames = [];
|
|
|
|
navigatedFrames = [];
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-06-21 20:51:06 +00:00
|
|
|
expect(attachedFrames.length).toBe(0);
|
|
|
|
expect(detachedFrames.length).toBe(4);
|
|
|
|
expect(navigatedFrames.length).toBe(1);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should report frame.name()', async({page, server}) => {
|
|
|
|
await FrameUtils.attachFrame(page, 'theFrameId', server.EMPTY_PAGE);
|
2017-07-27 18:36:03 +00:00
|
|
|
await page.evaluate(url => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const frame = document.createElement('iframe');
|
2017-07-27 18:36:03 +00:00
|
|
|
frame.name = 'theFrameName';
|
|
|
|
frame.src = url;
|
|
|
|
document.body.appendChild(frame);
|
|
|
|
return new Promise(x => frame.onload = x);
|
2017-12-12 21:34:21 +00:00
|
|
|
}, server.EMPTY_PAGE);
|
2017-07-27 18:36:03 +00:00
|
|
|
expect(page.frames()[0].name()).toBe('');
|
|
|
|
expect(page.frames()[1].name()).toBe('theFrameId');
|
|
|
|
expect(page.frames()[2].name()).toBe('theFrameName');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should report frame.parent()', async({page, server}) => {
|
|
|
|
await FrameUtils.attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
|
|
|
await FrameUtils.attachFrame(page, 'frame2', server.EMPTY_PAGE);
|
2017-07-27 18:36:03 +00:00
|
|
|
expect(page.frames()[0].parentFrame()).toBe(null);
|
|
|
|
expect(page.frames()[1].parentFrame()).toBe(page.mainFrame());
|
|
|
|
expect(page.frames()[2].parentFrame()).toBe(page.mainFrame());
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-06-21 20:51:06 +00:00
|
|
|
});
|
2017-06-28 01:27:22 +00:00
|
|
|
|
2017-08-31 22:38:01 +00:00
|
|
|
describe('Page.$eval', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-08-31 22:38:01 +00:00
|
|
|
await page.setContent('<section id="testAttribute">43543</section>');
|
|
|
|
const idAttribute = await page.$eval('section', e => e.id);
|
|
|
|
expect(idAttribute).toBe('testAttribute');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should accept arguments', async({page, server}) => {
|
2017-08-31 22:38:01 +00:00
|
|
|
await page.setContent('<section>hello</section>');
|
|
|
|
const text = await page.$eval('section', (e, suffix) => e.textContent + suffix, ' world!');
|
|
|
|
expect(text).toBe('hello world!');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should accept ElementHandles as arguments', async({page, server}) => {
|
2017-09-12 02:20:02 +00:00
|
|
|
await page.setContent('<section>hello</section><div> world</div>');
|
|
|
|
const divHandle = await page.$('div');
|
|
|
|
const text = await page.$eval('section', (e, div) => e.textContent + div.textContent, divHandle);
|
|
|
|
expect(text).toBe('hello world');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw error if no element is found', async({page, server}) => {
|
2017-08-31 22:38:01 +00:00
|
|
|
let error = null;
|
|
|
|
await page.$eval('section', e => e.id).catch(e => error = e);
|
|
|
|
expect(error.message).toContain('failed to find element matching selector "section"');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-31 22:38:01 +00:00
|
|
|
});
|
|
|
|
|
2017-10-11 06:23:14 +00:00
|
|
|
describe('Page.$$eval', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-10-11 06:23:14 +00:00
|
|
|
await page.setContent('<div>hello</div><div>beautiful</div><div>world!</div>');
|
|
|
|
const divsCount = await page.$$eval('div', divs => divs.length);
|
|
|
|
expect(divsCount).toBe(3);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-11 06:23:14 +00:00
|
|
|
});
|
|
|
|
|
2017-08-15 21:54:02 +00:00
|
|
|
describe('Page.$', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should query existing element', async({page, server}) => {
|
2017-08-15 21:54:02 +00:00
|
|
|
await page.setContent('<section>test</section>');
|
2017-08-21 23:39:04 +00:00
|
|
|
const element = await page.$('section');
|
2017-08-15 21:54:02 +00:00
|
|
|
expect(element).toBeTruthy();
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should return null for non-existing element', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const element = await page.$('non-existing-element');
|
2017-08-15 21:54:02 +00:00
|
|
|
expect(element).toBe(null);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-15 21:54:02 +00:00
|
|
|
});
|
2017-09-25 09:23:34 +00:00
|
|
|
|
2017-08-23 05:56:55 +00:00
|
|
|
describe('Page.$$', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should query existing elements', async({page, server}) => {
|
2017-08-23 05:56:55 +00:00
|
|
|
await page.setContent('<div>A</div><br/><div>B</div>');
|
|
|
|
const elements = await page.$$('div');
|
|
|
|
expect(elements.length).toBe(2);
|
2017-09-12 02:20:02 +00:00
|
|
|
const promises = elements.map(element => page.evaluate(e => e.textContent, element));
|
2017-08-23 05:56:55 +00:00
|
|
|
expect(await Promise.all(promises)).toEqual(['A', 'B']);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should return empty array if nothing is found', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-08-23 05:56:55 +00:00
|
|
|
const elements = await page.$$('div');
|
|
|
|
expect(elements.length).toBe(0);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-23 05:56:55 +00:00
|
|
|
});
|
2017-08-15 21:54:02 +00:00
|
|
|
|
2018-01-03 23:37:08 +00:00
|
|
|
describe('Path.$x', function() {
|
2017-12-20 00:23:45 +00:00
|
|
|
it('should query existing element', async({page, server}) => {
|
|
|
|
await page.setContent('<section>test</section>');
|
2018-01-03 23:37:08 +00:00
|
|
|
const elements = await page.$x('/html/body/section');
|
|
|
|
expect(elements[0]).toBeTruthy();
|
|
|
|
expect(elements.length).toBe(1);
|
2017-12-20 00:23:45 +00:00
|
|
|
});
|
2018-01-03 23:37:08 +00:00
|
|
|
it('should return empty array for non-existing element', async({page, server}) => {
|
|
|
|
const element = await page.$x('/html/body/non-existing-element');
|
|
|
|
expect(element).toEqual([]);
|
|
|
|
});
|
|
|
|
it('should return multiple elements', async({page, sever}) => {
|
|
|
|
await page.setContent('<div></div><div></div>');
|
|
|
|
const elements = await page.$x('/html/body/div');
|
|
|
|
expect(elements.length).toBe(2);
|
2017-12-20 00:23:45 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-10 06:14:09 +00:00
|
|
|
describe('ElementHandle.boundingBox', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-10-10 06:14:09 +00:00
|
|
|
await page.setViewport({width: 500, height: 500});
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2017-10-10 06:14:09 +00:00
|
|
|
const elementHandle = await page.$('.box:nth-of-type(13)');
|
|
|
|
const box = await elementHandle.boundingBox();
|
|
|
|
expect(box).toEqual({ x: 100, y: 50, width: 50, height: 50 });
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should handle nested frames', async({page, server}) => {
|
2017-10-10 06:14:09 +00:00
|
|
|
await page.setViewport({width: 500, height: 500});
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/frames/nested-frames.html');
|
2017-10-10 06:14:09 +00:00
|
|
|
const nestedFrame = page.frames()[1].childFrames()[1];
|
|
|
|
const elementHandle = await nestedFrame.$('div');
|
|
|
|
const box = await elementHandle.boundingBox();
|
|
|
|
expect(box).toEqual({ x: 28, y: 260, width: 264, height: 18 });
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should return null for invisible elements', async({page, server}) => {
|
2017-11-07 21:54:40 +00:00
|
|
|
await page.setContent('<div style="display:none">hi</div>');
|
|
|
|
const element = await page.$('div');
|
|
|
|
expect(await element.boundingBox()).toBe(null);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-10 06:14:09 +00:00
|
|
|
});
|
|
|
|
|
2017-08-15 21:54:02 +00:00
|
|
|
describe('ElementHandle.click', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
2017-08-21 23:39:04 +00:00
|
|
|
const button = await page.$('button');
|
2017-10-06 22:35:02 +00:00
|
|
|
await button.click();
|
2017-08-15 21:54:02 +00:00
|
|
|
expect(await page.evaluate(() => result)).toBe('Clicked');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work for Shadow DOM v1', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/shadow.html');
|
2017-10-31 19:02:16 +00:00
|
|
|
const buttonHandle = await page.evaluateHandle(() => button);
|
|
|
|
await buttonHandle.click();
|
|
|
|
expect(await page.evaluate(() => clicked)).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work for TextNodes', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
2017-10-06 22:35:02 +00:00
|
|
|
const buttonTextNode = await page.evaluateHandle(() => document.querySelector('button').firstChild);
|
|
|
|
let error = null;
|
|
|
|
await buttonTextNode.click().catch(err => error = err);
|
|
|
|
expect(error.message).toBe('Node is not of type HTMLElement');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw for detached nodes', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
2017-10-06 22:35:02 +00:00
|
|
|
const button = await page.$('button');
|
|
|
|
await page.evaluate(button => button.remove(), button);
|
|
|
|
let error = null;
|
|
|
|
await button.click().catch(err => error = err);
|
|
|
|
expect(error.message).toBe('Node is detached from document');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw for hidden nodes', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
2017-11-07 21:54:40 +00:00
|
|
|
const button = await page.$('button');
|
|
|
|
await page.evaluate(button => button.style.display = 'none', button);
|
|
|
|
const error = await button.click().catch(err => err);
|
|
|
|
expect(error.message).toBe('Node is not visible');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw for recursively hidden nodes', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
2017-11-07 21:54:40 +00:00
|
|
|
const button = await page.$('button');
|
|
|
|
await page.evaluate(button => button.parentElement.style.display = 'none', button);
|
|
|
|
const error = await button.click().catch(err => err);
|
|
|
|
expect(error.message).toBe('Node is not visible');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw for <br> elements', async({page, server}) => {
|
2017-11-07 21:54:40 +00:00
|
|
|
await page.setContent('hello<br>goodbye');
|
|
|
|
const br = await page.$('br');
|
|
|
|
const error = await br.click().catch(err => err);
|
|
|
|
expect(error.message).toBe('Node is not visible');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-15 21:54:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('ElementHandle.hover', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/scrollable.html');
|
2017-08-21 23:39:04 +00:00
|
|
|
const button = await page.$('#button-6');
|
2017-08-15 21:54:02 +00:00
|
|
|
await button.hover();
|
|
|
|
expect(await page.evaluate(() => document.querySelector('button:hover').id)).toBe('button-6');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-15 21:54:02 +00:00
|
|
|
});
|
|
|
|
|
2017-10-10 06:14:09 +00:00
|
|
|
describe('ElementHandle.screenshot', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-10-10 06:14:09 +00:00
|
|
|
await page.setViewport({width: 500, height: 500});
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2017-10-10 06:14:09 +00:00
|
|
|
await page.evaluate(() => window.scrollBy(50, 100));
|
|
|
|
const elementHandle = await page.$('.box:nth-of-type(3)');
|
|
|
|
const screenshot = await elementHandle.screenshot();
|
|
|
|
expect(screenshot).toBeGolden('screenshot-element-bounding-box.png');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should take into account padding and border', async({page, server}) => {
|
2017-10-13 00:53:59 +00:00
|
|
|
await page.setViewport({width: 500, height: 500});
|
2017-10-13 20:52:28 +00:00
|
|
|
await page.setContent(`
|
|
|
|
something above
|
|
|
|
<style>div {
|
|
|
|
border: 2px solid blue;
|
|
|
|
background: green;
|
|
|
|
width: 50px;
|
|
|
|
height: 50px;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<div></div>
|
|
|
|
`);
|
|
|
|
const elementHandle = await page.$('div');
|
2017-10-13 00:53:59 +00:00
|
|
|
const screenshot = await elementHandle.screenshot();
|
|
|
|
expect(screenshot).toBeGolden('screenshot-element-padding-border.png');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should scroll element into view', async({page, server}) => {
|
2017-11-11 00:02:52 +00:00
|
|
|
await page.setViewport({width: 500, height: 500});
|
|
|
|
await page.setContent(`
|
|
|
|
something above
|
|
|
|
<style>div.above {
|
|
|
|
border: 2px solid blue;
|
|
|
|
background: red;
|
|
|
|
height: 1500px;
|
|
|
|
}
|
|
|
|
div.to-screenshot {
|
|
|
|
border: 2px solid blue;
|
|
|
|
background: green;
|
|
|
|
width: 50px;
|
|
|
|
height: 50px;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<div class="above"></div>
|
|
|
|
<div class="to-screenshot"></div>
|
|
|
|
`);
|
|
|
|
const elementHandle = await page.$('div.to-screenshot');
|
|
|
|
const screenshot = await elementHandle.screenshot();
|
|
|
|
expect(screenshot).toBeGolden('screenshot-element-scrolled-into-view.png');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work with a rotated element', async({page, server}) => {
|
2017-10-13 00:53:59 +00:00
|
|
|
await page.setViewport({width: 500, height: 500});
|
|
|
|
await page.setContent(`<div style="position:absolute;
|
|
|
|
top: 100px;
|
|
|
|
left: 100px;
|
|
|
|
width: 100px;
|
|
|
|
height: 100px;
|
|
|
|
background: green;
|
|
|
|
transform: rotateZ(200deg);"> </div>`);
|
|
|
|
const elementHandle = await page.$('div');
|
|
|
|
const screenshot = await elementHandle.screenshot();
|
|
|
|
expect(screenshot).toBeGolden('screenshot-element-rotate.png');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should fail to screenshot a detached element', async({page, server}) => {
|
2017-10-13 00:53:59 +00:00
|
|
|
await page.setContent('<h1>remove this</h1>');
|
|
|
|
const elementHandle = await page.$('h1');
|
|
|
|
await page.evaluate(element => element.remove(), elementHandle);
|
|
|
|
const screenshotError = await elementHandle.screenshot().catch(error => error);
|
|
|
|
expect(screenshotError.message).toBe('Node is detached from document');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-10 06:14:09 +00:00
|
|
|
});
|
|
|
|
|
2017-10-27 09:08:58 +00:00
|
|
|
describe('ElementHandle.$', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should query existing element', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/playground.html');
|
2017-10-27 09:08:58 +00:00
|
|
|
await page.setContent('<html><body><div class="second"><div class="inner">A</div></div></body></html>');
|
|
|
|
const html = await page.$('html');
|
|
|
|
const second = await html.$('.second');
|
|
|
|
const inner = await second.$('.inner');
|
|
|
|
const content = await page.evaluate(e => e.textContent, inner);
|
|
|
|
expect(content).toBe('A');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-27 09:08:58 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should return null for non-existing element', async({page, server}) => {
|
2017-10-27 09:08:58 +00:00
|
|
|
await page.setContent('<html><body><div class="second"><div class="inner">B</div></div></body></html>');
|
|
|
|
const html = await page.$('html');
|
|
|
|
const second = await html.$('.third');
|
|
|
|
expect(second).toBe(null);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-27 09:08:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('ElementHandle.$$', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should query existing elements', async({page, server}) => {
|
2017-10-27 09:08:58 +00:00
|
|
|
await page.setContent('<html><body><div>A</div><br/><div>B</div></body></html>');
|
|
|
|
const html = await page.$('html');
|
|
|
|
const elements = await html.$$('div');
|
|
|
|
expect(elements.length).toBe(2);
|
|
|
|
const promises = elements.map(element => page.evaluate(e => e.textContent, element));
|
|
|
|
expect(await Promise.all(promises)).toEqual(['A', 'B']);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-27 09:08:58 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should return empty array for non-existing elements', async({page, server}) => {
|
2017-10-27 09:08:58 +00:00
|
|
|
await page.setContent('<html><body><span>A</span><br/><span>B</span></body></html>');
|
|
|
|
const html = await page.$('html');
|
|
|
|
const elements = await html.$$('div');
|
|
|
|
expect(elements.length).toBe(0);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-27 09:08:58 +00:00
|
|
|
});
|
|
|
|
|
2017-12-20 00:23:45 +00:00
|
|
|
|
2018-01-03 23:37:08 +00:00
|
|
|
describe('ElementHandle.$x', function() {
|
2017-12-20 00:23:45 +00:00
|
|
|
it('should query existing element', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/playground.html');
|
|
|
|
await page.setContent('<html><body><div class="second"><div class="inner">A</div></div></body></html>');
|
|
|
|
const html = await page.$('html');
|
2018-01-03 23:37:08 +00:00
|
|
|
const second = await html.$x(`./body/div[contains(@class, 'second')]`);
|
|
|
|
const inner = await second[0].$x(`./div[contains(@class, 'inner')]`);
|
|
|
|
const content = await page.evaluate(e => e.textContent, inner[0]);
|
2017-12-20 00:23:45 +00:00
|
|
|
expect(content).toBe('A');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return null for non-existing element', async({page, server}) => {
|
|
|
|
await page.setContent('<html><body><div class="second"><div class="inner">B</div></div></body></html>');
|
|
|
|
const html = await page.$('html');
|
2018-01-03 23:37:08 +00:00
|
|
|
const second = await html.$x(`/div[contains(@class, 'third')]`);
|
|
|
|
expect(second).toEqual([]);
|
2017-12-20 00:23:45 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-28 01:27:22 +00:00
|
|
|
describe('input', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should click the button', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
2017-06-28 01:27:22 +00:00
|
|
|
await page.click('button');
|
|
|
|
expect(await page.evaluate(() => result)).toBe('Clicked');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-11-08 04:38:22 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should click on checkbox input and toggle', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/checkbox.html');
|
2017-11-08 04:38:22 +00:00
|
|
|
expect(await page.evaluate(() => result.check)).toBe(null);
|
|
|
|
await page.click('input#agree');
|
|
|
|
expect(await page.evaluate(() => result.check)).toBe(true);
|
|
|
|
expect(await page.evaluate(() => result.events)).toEqual([
|
|
|
|
'mouseover',
|
|
|
|
'mouseenter',
|
|
|
|
'mousemove',
|
|
|
|
'mousedown',
|
|
|
|
'mouseup',
|
|
|
|
'click',
|
2018-02-01 00:02:56 +00:00
|
|
|
'input',
|
2017-11-08 04:38:22 +00:00
|
|
|
'change',
|
|
|
|
]);
|
|
|
|
await page.click('input#agree');
|
|
|
|
expect(await page.evaluate(() => result.check)).toBe(false);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-11-08 04:38:22 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should click on checkbox label and toggle', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/checkbox.html');
|
2017-11-08 04:38:22 +00:00
|
|
|
expect(await page.evaluate(() => result.check)).toBe(null);
|
|
|
|
await page.click('label[for="agree"]');
|
|
|
|
expect(await page.evaluate(() => result.check)).toBe(true);
|
|
|
|
expect(await page.evaluate(() => result.events)).toEqual([
|
|
|
|
'click',
|
2018-02-01 00:02:56 +00:00
|
|
|
'input',
|
2017-11-08 04:38:22 +00:00
|
|
|
'change',
|
|
|
|
]);
|
|
|
|
await page.click('label[for="agree"]');
|
|
|
|
expect(await page.evaluate(() => result.check)).toBe(false);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-11-08 04:38:22 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should fail to click a missing button', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
2017-09-13 19:06:57 +00:00
|
|
|
let error = null;
|
|
|
|
await page.click('button.does-not-exist').catch(e => error = e);
|
|
|
|
expect(error.message).toBe('No node found for selector: button.does-not-exist');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-04 05:51:51 +00:00
|
|
|
// @see https://github.com/GoogleChrome/puppeteer/issues/161
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should not hang with touch-enabled viewports', async({page, server}) => {
|
2017-08-04 05:51:51 +00:00
|
|
|
await page.setViewport(iPhone.viewport);
|
|
|
|
await page.mouse.down();
|
|
|
|
await page.mouse.move(100, 10);
|
|
|
|
await page.mouse.up();
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should type into the textarea', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
2017-10-07 07:28:24 +00:00
|
|
|
|
|
|
|
const textarea = await page.$('textarea');
|
|
|
|
await textarea.type('Type in this text!');
|
2017-06-28 01:27:22 +00:00
|
|
|
expect(await page.evaluate(() => result)).toBe('Type in this text!');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should click the button after navigation ', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
2017-06-28 01:27:22 +00:00
|
|
|
await page.click('button');
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
2017-06-28 01:27:22 +00:00
|
|
|
await page.click('button');
|
|
|
|
expect(await page.evaluate(() => result)).toBe('Clicked');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should upload the file', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/fileupload.html');
|
2017-07-28 07:06:57 +00:00
|
|
|
const filePath = path.relative(process.cwd(), __dirname + '/assets/file-to-upload.txt');
|
2017-08-21 23:39:04 +00:00
|
|
|
const input = await page.$('input');
|
2017-08-17 21:53:37 +00:00
|
|
|
await input.uploadFile(filePath);
|
2017-09-12 02:20:02 +00:00
|
|
|
expect(await page.evaluate(e => e.files[0].name, input)).toBe('file-to-upload.txt');
|
|
|
|
expect(await page.evaluate(e => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const reader = new FileReader();
|
|
|
|
const promise = new Promise(fulfill => reader.onload = fulfill);
|
2017-08-17 21:53:37 +00:00
|
|
|
reader.readAsText(e.files[0]);
|
2017-07-10 18:21:46 +00:00
|
|
|
return promise.then(() => reader.result);
|
2017-09-12 02:20:02 +00:00
|
|
|
}, input)).toBe('contents of the file');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should move with the arrow keys', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
2017-10-07 07:28:24 +00:00
|
|
|
await page.type('textarea', 'Hello World!');
|
2017-07-18 01:49:52 +00:00
|
|
|
expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('Hello World!');
|
|
|
|
for (let i = 0; i < 'World!'.length; i++)
|
2017-10-07 07:28:24 +00:00
|
|
|
page.keyboard.press('ArrowLeft');
|
|
|
|
await page.keyboard.type('inserted ');
|
2017-07-18 01:49:52 +00:00
|
|
|
expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('Hello inserted World!');
|
2017-07-28 00:20:02 +00:00
|
|
|
page.keyboard.down('Shift');
|
2017-07-18 01:49:52 +00:00
|
|
|
for (let i = 0; i < 'inserted '.length; i++)
|
2017-10-07 07:28:24 +00:00
|
|
|
page.keyboard.press('ArrowLeft');
|
2017-07-28 00:20:02 +00:00
|
|
|
page.keyboard.up('Shift');
|
2017-10-07 07:28:24 +00:00
|
|
|
await page.keyboard.press('Backspace');
|
2017-07-18 01:49:52 +00:00
|
|
|
expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('Hello World!');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should send a character with ElementHandle.press', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
2017-10-07 07:28:24 +00:00
|
|
|
const textarea = await page.$('textarea');
|
|
|
|
await textarea.press('a', {text: 'f'});
|
2017-08-03 20:35:31 +00:00
|
|
|
expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('f');
|
2017-07-19 21:43:07 +00:00
|
|
|
|
|
|
|
await page.evaluate(() => window.addEventListener('keydown', e => e.preventDefault(), true));
|
|
|
|
|
2017-10-07 07:28:24 +00:00
|
|
|
await textarea.press('a', {text: 'y'});
|
2017-08-03 20:35:31 +00:00
|
|
|
expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('f');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should send a character with sendCharacter', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
2017-07-19 21:43:07 +00:00
|
|
|
await page.focus('textarea');
|
|
|
|
await page.keyboard.sendCharacter('嗨');
|
2017-08-03 20:35:31 +00:00
|
|
|
expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('嗨');
|
2017-07-19 21:43:07 +00:00
|
|
|
await page.evaluate(() => window.addEventListener('keydown', e => e.preventDefault(), true));
|
|
|
|
await page.keyboard.sendCharacter('a');
|
2017-08-03 20:35:31 +00:00
|
|
|
expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('嗨a');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should report shiftKey', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/keyboard.html');
|
2017-08-21 23:39:04 +00:00
|
|
|
const keyboard = page.keyboard;
|
|
|
|
const codeForKey = {'Shift': 16, 'Alt': 18, 'Meta': 91, 'Control': 17};
|
|
|
|
for (const modifierKey in codeForKey) {
|
2017-07-19 21:27:01 +00:00
|
|
|
await keyboard.down(modifierKey);
|
2017-10-12 01:09:43 +00:00
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keydown: ' + modifierKey + ' ' + modifierKey + 'Left ' + codeForKey[modifierKey] + ' [' + modifierKey + ']');
|
2017-07-19 21:27:01 +00:00
|
|
|
await keyboard.down('!');
|
2017-10-13 22:22:55 +00:00
|
|
|
// Shift+! will generate a keypress
|
|
|
|
if (modifierKey === 'Shift')
|
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keydown: ! Digit1 49 [' + modifierKey + ']\nKeypress: ! Digit1 33 33 33 [' + modifierKey + ']');
|
|
|
|
else
|
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keydown: ! Digit1 49 [' + modifierKey + ']');
|
|
|
|
|
2017-07-19 21:27:01 +00:00
|
|
|
await keyboard.up('!');
|
2017-10-12 01:09:43 +00:00
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keyup: ! Digit1 49 [' + modifierKey + ']');
|
2017-07-19 21:27:01 +00:00
|
|
|
await keyboard.up(modifierKey);
|
2017-10-12 01:09:43 +00:00
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keyup: ' + modifierKey + ' ' + modifierKey + 'Left ' + codeForKey[modifierKey] + ' []');
|
2017-07-18 01:49:52 +00:00
|
|
|
}
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should report multiple modifiers', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/keyboard.html');
|
2017-08-21 23:39:04 +00:00
|
|
|
const keyboard = page.keyboard;
|
2017-07-19 21:27:01 +00:00
|
|
|
await keyboard.down('Control');
|
2017-10-12 01:09:43 +00:00
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keydown: Control ControlLeft 17 [Control]');
|
2017-07-19 21:27:01 +00:00
|
|
|
await keyboard.down('Meta');
|
2017-10-12 01:09:43 +00:00
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keydown: Meta MetaLeft 91 [Control Meta]');
|
2017-07-19 21:27:01 +00:00
|
|
|
await keyboard.down(';');
|
2017-10-12 01:09:43 +00:00
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keydown: ; Semicolon 186 [Control Meta]');
|
2017-07-19 21:27:01 +00:00
|
|
|
await keyboard.up(';');
|
2017-10-12 01:09:43 +00:00
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keyup: ; Semicolon 186 [Control Meta]');
|
2017-07-19 21:27:01 +00:00
|
|
|
await keyboard.up('Control');
|
2017-10-12 01:09:43 +00:00
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keyup: Control ControlLeft 17 [Meta]');
|
2017-07-19 21:27:01 +00:00
|
|
|
await keyboard.up('Meta');
|
2017-10-12 01:09:43 +00:00
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keyup: Meta MetaLeft 91 []');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should send proper codes while typing', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/keyboard.html');
|
2017-10-07 07:28:24 +00:00
|
|
|
await page.keyboard.type('!');
|
2017-07-18 01:49:52 +00:00
|
|
|
expect(await page.evaluate(() => getResult())).toBe(
|
2017-10-12 01:09:43 +00:00
|
|
|
[ 'Keydown: ! Digit1 49 []',
|
|
|
|
'Keypress: ! Digit1 33 33 33 []',
|
|
|
|
'Keyup: ! Digit1 49 []'].join('\n'));
|
2017-10-07 07:28:24 +00:00
|
|
|
await page.keyboard.type('^');
|
2017-07-18 01:49:52 +00:00
|
|
|
expect(await page.evaluate(() => getResult())).toBe(
|
2017-10-12 01:09:43 +00:00
|
|
|
[ 'Keydown: ^ Digit6 54 []',
|
|
|
|
'Keypress: ^ Digit6 94 94 94 []',
|
|
|
|
'Keyup: ^ Digit6 54 []'].join('\n'));
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should send proper codes while typing with shift', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/keyboard.html');
|
2017-08-21 23:39:04 +00:00
|
|
|
const keyboard = page.keyboard;
|
2017-07-19 21:27:01 +00:00
|
|
|
await keyboard.down('Shift');
|
2017-10-07 07:28:24 +00:00
|
|
|
await page.keyboard.type('~');
|
2017-07-18 01:49:52 +00:00
|
|
|
expect(await page.evaluate(() => getResult())).toBe(
|
2017-10-12 01:09:43 +00:00
|
|
|
[ 'Keydown: Shift ShiftLeft 16 [Shift]',
|
|
|
|
'Keydown: ~ Backquote 192 [Shift]', // 192 is ` keyCode
|
|
|
|
'Keypress: ~ Backquote 126 126 126 [Shift]', // 126 is ~ charCode
|
|
|
|
'Keyup: ~ Backquote 192 [Shift]'].join('\n'));
|
2017-07-19 21:27:01 +00:00
|
|
|
await keyboard.up('Shift');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should not type canceled events', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
2017-07-18 01:49:52 +00:00
|
|
|
await page.focus('textarea');
|
|
|
|
await page.evaluate(() => {
|
|
|
|
window.addEventListener('keydown', event => {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
if (event.key === 'l')
|
|
|
|
event.preventDefault();
|
|
|
|
if (event.key === 'o')
|
|
|
|
Promise.resolve().then(() => event.preventDefault());
|
|
|
|
}, false);
|
|
|
|
});
|
2017-10-07 07:28:24 +00:00
|
|
|
await page.keyboard.type('Hello World!');
|
2017-07-18 01:49:52 +00:00
|
|
|
expect(await page.evaluate(() => textarea.value)).toBe('He Wrd!');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('keyboard.modifiers()', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const keyboard = page.keyboard;
|
2017-07-25 21:35:03 +00:00
|
|
|
expect(keyboard._modifiers).toBe(0);
|
2017-07-28 00:20:02 +00:00
|
|
|
await keyboard.down('Shift');
|
2017-07-25 21:35:03 +00:00
|
|
|
expect(keyboard._modifiers).toBe(8);
|
2017-07-28 00:20:02 +00:00
|
|
|
await keyboard.down('Alt');
|
2017-07-25 21:35:03 +00:00
|
|
|
expect(keyboard._modifiers).toBe(9);
|
2017-07-28 00:20:02 +00:00
|
|
|
await keyboard.up('Shift');
|
|
|
|
await keyboard.up('Alt');
|
2017-07-25 21:35:03 +00:00
|
|
|
expect(keyboard._modifiers).toBe(0);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should resize the textarea', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
2017-08-21 23:39:04 +00:00
|
|
|
const {x, y, width, height} = await page.evaluate(dimensions);
|
|
|
|
const mouse = page.mouse;
|
2017-07-22 03:29:31 +00:00
|
|
|
await mouse.move(x + width - 4, y + height - 4);
|
|
|
|
await mouse.down();
|
|
|
|
await mouse.move(x + width + 100, y + height + 100);
|
|
|
|
await mouse.up();
|
2017-08-21 23:39:04 +00:00
|
|
|
const newDimensions = await page.evaluate(dimensions);
|
2017-07-22 03:29:31 +00:00
|
|
|
expect(newDimensions.width).toBe(width + 104);
|
|
|
|
expect(newDimensions.height).toBe(height + 104);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should scroll and click the button', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/scrollable.html');
|
2017-07-22 03:29:31 +00:00
|
|
|
await page.click('#button-5');
|
2017-08-03 20:35:31 +00:00
|
|
|
expect(await page.evaluate(() => document.querySelector('#button-5').textContent)).toBe('clicked');
|
2017-07-22 03:29:31 +00:00
|
|
|
await page.click('#button-80');
|
2017-08-03 20:35:31 +00:00
|
|
|
expect(await page.evaluate(() => document.querySelector('#button-80').textContent)).toBe('clicked');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-12-28 19:34:03 +00:00
|
|
|
it('should double click the button', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
|
|
|
await page.evaluate(() => {
|
|
|
|
window.double = false;
|
|
|
|
const button = document.querySelector('button');
|
|
|
|
button.addEventListener('dblclick', event => {
|
|
|
|
window.double = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const button = await page.$('button');
|
|
|
|
await button.click({ clickCount: 2 });
|
|
|
|
expect(await page.evaluate('double')).toBe(true);
|
|
|
|
expect(await page.evaluate('result')).toBe('Clicked');
|
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should click a partially obscured button', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
2017-08-03 20:35:31 +00:00
|
|
|
await page.evaluate(() => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const button = document.querySelector('button');
|
2017-07-24 23:14:32 +00:00
|
|
|
button.textContent = 'Some really long text that will go offscreen';
|
|
|
|
button.style.position = 'absolute';
|
|
|
|
button.style.left = '368px';
|
|
|
|
});
|
|
|
|
await page.click('button');
|
|
|
|
expect(await page.evaluate(() => window.result)).toBe('Clicked');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should select the text with mouse', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
2017-07-22 03:29:31 +00:00
|
|
|
await page.focus('textarea');
|
2017-08-21 23:39:04 +00:00
|
|
|
const text = 'This is the text that we are going to try to select. Let\'s see how it goes.';
|
2017-10-07 07:28:24 +00:00
|
|
|
await page.keyboard.type(text);
|
2017-08-03 20:35:31 +00:00
|
|
|
await page.evaluate(() => document.querySelector('textarea').scrollTop = 0);
|
2017-08-21 23:39:04 +00:00
|
|
|
const {x, y} = await page.evaluate(dimensions);
|
2017-07-22 03:29:31 +00:00
|
|
|
await page.mouse.move(x + 2,y + 2);
|
|
|
|
await page.mouse.down();
|
|
|
|
await page.mouse.move(100,100);
|
|
|
|
await page.mouse.up();
|
|
|
|
expect(await page.evaluate(() => window.getSelection().toString())).toBe(text);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should select the text by triple clicking', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
2017-07-22 03:29:31 +00:00
|
|
|
await page.focus('textarea');
|
2017-08-21 23:39:04 +00:00
|
|
|
const text = 'This is the text that we are going to try to select. Let\'s see how it goes.';
|
2017-10-07 07:28:24 +00:00
|
|
|
await page.keyboard.type(text);
|
2017-07-22 03:29:31 +00:00
|
|
|
await page.click('textarea');
|
|
|
|
await page.click('textarea', {clickCount: 2});
|
|
|
|
await page.click('textarea', {clickCount: 3});
|
|
|
|
expect(await page.evaluate(() => window.getSelection().toString())).toBe(text);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should trigger hover state', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/scrollable.html');
|
2017-07-22 03:29:31 +00:00
|
|
|
await page.hover('#button-6');
|
2017-08-03 20:35:31 +00:00
|
|
|
expect(await page.evaluate(() => document.querySelector('button:hover').id)).toBe('button-6');
|
2017-07-22 03:29:31 +00:00
|
|
|
await page.hover('#button-2');
|
2017-08-03 20:35:31 +00:00
|
|
|
expect(await page.evaluate(() => document.querySelector('button:hover').id)).toBe('button-2');
|
2017-07-22 03:29:31 +00:00
|
|
|
await page.hover('#button-91');
|
2017-08-03 20:35:31 +00:00
|
|
|
expect(await page.evaluate(() => document.querySelector('button:hover').id)).toBe('button-91');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should fire contextmenu event on right click', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/scrollable.html');
|
2017-07-22 03:29:31 +00:00
|
|
|
await page.click('#button-8', {button: 'right'});
|
2017-08-03 20:35:31 +00:00
|
|
|
expect(await page.evaluate(() => document.querySelector('#button-8').textContent)).toBe('context menu');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should set modifier keys on click', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/scrollable.html');
|
2017-08-03 20:35:31 +00:00
|
|
|
await page.evaluate(() => document.querySelector('#button-3').addEventListener('mousedown', e => window.lastEvent = e, true));
|
2017-08-21 23:39:04 +00:00
|
|
|
const modifiers = {'Shift': 'shiftKey', 'Control': 'ctrlKey', 'Alt': 'altKey', 'Meta': 'metaKey'};
|
|
|
|
for (const modifier in modifiers) {
|
2017-07-22 03:29:31 +00:00
|
|
|
await page.keyboard.down(modifier);
|
|
|
|
await page.click('#button-3');
|
|
|
|
if (!(await page.evaluate(mod => window.lastEvent[mod], modifiers[modifier])))
|
|
|
|
fail(modifiers[modifier] + ' should be true');
|
|
|
|
await page.keyboard.up(modifier);
|
|
|
|
}
|
|
|
|
await page.click('#button-3');
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const modifier in modifiers) {
|
2017-07-22 03:29:31 +00:00
|
|
|
if ((await page.evaluate(mod => window.lastEvent[mod], modifiers[modifier])))
|
|
|
|
fail(modifiers[modifier] + ' should be false');
|
|
|
|
}
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should specify repeat property', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
2017-07-31 19:05:46 +00:00
|
|
|
await page.focus('textarea');
|
2017-08-03 20:35:31 +00:00
|
|
|
await page.evaluate(() => document.querySelector('textarea').addEventListener('keydown', e => window.lastEvent = e, true));
|
2018-02-13 18:59:29 +00:00
|
|
|
await page.keyboard.down('a');
|
2017-07-31 19:05:46 +00:00
|
|
|
expect(await page.evaluate(() => window.lastEvent.repeat)).toBe(false);
|
2017-10-07 07:28:24 +00:00
|
|
|
await page.keyboard.press('a');
|
2017-07-31 19:05:46 +00:00
|
|
|
expect(await page.evaluate(() => window.lastEvent.repeat)).toBe(true);
|
2018-02-13 18:59:29 +00:00
|
|
|
|
|
|
|
await page.keyboard.down('b');
|
|
|
|
expect(await page.evaluate(() => window.lastEvent.repeat)).toBe(false);
|
|
|
|
await page.keyboard.down('b');
|
|
|
|
expect(await page.evaluate(() => window.lastEvent.repeat)).toBe(true);
|
|
|
|
|
|
|
|
await page.keyboard.up('a');
|
|
|
|
await page.keyboard.down('a');
|
|
|
|
expect(await page.evaluate(() => window.lastEvent.repeat)).toBe(false);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-04 05:51:51 +00:00
|
|
|
// @see https://github.com/GoogleChrome/puppeteer/issues/206
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should click links which cause navigation', async({page, server}) => {
|
|
|
|
await page.setContent(`<a href="${server.EMPTY_PAGE}">empty.html</a>`);
|
2017-08-04 05:10:06 +00:00
|
|
|
// This await should not hang.
|
|
|
|
await page.click('a');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should tween mouse movement', async({page, server}) => {
|
2017-12-04 01:15:13 +00:00
|
|
|
await page.mouse.move(100, 100);
|
2017-08-29 21:13:38 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
window.result = [];
|
|
|
|
document.addEventListener('mousemove', event => {
|
|
|
|
window.result.push([event.clientX, event.clientY]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
await page.mouse.move(200, 300, {steps: 5});
|
|
|
|
expect(await page.evaluate('result')).toEqual([
|
|
|
|
[120, 140],
|
|
|
|
[140, 180],
|
|
|
|
[160, 220],
|
|
|
|
[180, 260],
|
|
|
|
[200, 300]
|
|
|
|
]);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should tap the button', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
2017-09-02 02:03:51 +00:00
|
|
|
await page.tap('button');
|
|
|
|
expect(await page.evaluate(() => result)).toBe('Clicked');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
xit('should report touches', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/touches.html');
|
2017-09-02 02:03:51 +00:00
|
|
|
const button = await page.$('button');
|
|
|
|
await button.tap();
|
|
|
|
expect(await page.evaluate(() => getResult())).toEqual(['Touchstart: 0', 'Touchend: 0']);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should click the button inside an iframe', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-10-09 20:23:36 +00:00
|
|
|
await page.setContent('<div style="width:100px;height:100px">spacer</div>');
|
2017-12-12 21:34:21 +00:00
|
|
|
await FrameUtils.attachFrame(page, 'button-test', server.PREFIX + '/input/button.html');
|
2017-10-09 20:23:36 +00:00
|
|
|
const frame = page.frames()[1];
|
|
|
|
const button = await frame.$('button');
|
|
|
|
await button.click();
|
|
|
|
expect(await frame.evaluate(() => window.result)).toBe('Clicked');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should click the button with deviceScaleFactor set', async({page, server}) => {
|
2017-10-09 20:23:36 +00:00
|
|
|
await page.setViewport({width: 400, height: 400, deviceScaleFactor: 5});
|
|
|
|
expect(await page.evaluate(() => window.devicePixelRatio)).toBe(5);
|
|
|
|
await page.setContent('<div style="width:100px;height:100px">spacer</div>');
|
2017-12-12 21:34:21 +00:00
|
|
|
await FrameUtils.attachFrame(page, 'button-test', server.PREFIX + '/input/button.html');
|
2017-10-09 20:23:36 +00:00
|
|
|
const frame = page.frames()[1];
|
|
|
|
const button = await frame.$('button');
|
|
|
|
await button.click();
|
|
|
|
expect(await frame.evaluate(() => window.result)).toBe('Clicked');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should type all kinds of characters', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
2017-10-23 19:43:45 +00:00
|
|
|
await page.focus('textarea');
|
|
|
|
const text = 'This text goes onto two lines.\nThis character is 嗨.';
|
|
|
|
await page.keyboard.type(text);
|
|
|
|
expect(await page.evaluate('result')).toBe(text);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should specify location', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/textarea.html');
|
2017-10-23 19:43:45 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
window.addEventListener('keydown', event => window.keyLocation = event.location, true);
|
|
|
|
});
|
|
|
|
const textarea = await page.$('textarea');
|
|
|
|
|
|
|
|
await textarea.press('Digit5');
|
|
|
|
expect(await page.evaluate('keyLocation')).toBe(0);
|
|
|
|
|
|
|
|
await textarea.press('ControlLeft');
|
|
|
|
expect(await page.evaluate('keyLocation')).toBe(1);
|
|
|
|
|
|
|
|
await textarea.press('ControlRight');
|
|
|
|
expect(await page.evaluate('keyLocation')).toBe(2);
|
|
|
|
|
|
|
|
await textarea.press('NumpadSubtract');
|
|
|
|
expect(await page.evaluate('keyLocation')).toBe(3);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw on unknown keys', async({page, server}) => {
|
2017-10-23 19:43:45 +00:00
|
|
|
let error = await page.keyboard.press('NotARealKey').catch(e => e);
|
|
|
|
expect(error.message).toBe('Unknown key: "NotARealKey"');
|
|
|
|
|
|
|
|
error = await page.keyboard.press('ё').catch(e => e);
|
|
|
|
expect(error && error.message).toBe('Unknown key: "ё"');
|
|
|
|
|
|
|
|
error = await page.keyboard.press('😊').catch(e => e);
|
|
|
|
expect(error && error.message).toBe('Unknown key: "😊"');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-07-22 03:29:31 +00:00
|
|
|
function dimensions() {
|
2017-08-21 23:39:04 +00:00
|
|
|
const rect = document.querySelector('textarea').getBoundingClientRect();
|
2017-07-22 03:29:31 +00:00
|
|
|
return {
|
|
|
|
x: rect.left,
|
|
|
|
y: rect.top,
|
|
|
|
width: rect.width,
|
|
|
|
height: rect.height
|
|
|
|
};
|
|
|
|
}
|
2017-06-28 01:27:22 +00:00
|
|
|
});
|
2017-07-20 22:29:09 +00:00
|
|
|
|
2017-06-29 06:09:28 +00:00
|
|
|
describe('Page.setUserAgent', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-07-25 09:01:10 +00:00
|
|
|
expect(await page.evaluate(() => navigator.userAgent)).toContain('Mozilla');
|
2017-06-29 06:09:28 +00:00
|
|
|
page.setUserAgent('foobar');
|
2017-09-14 05:52:43 +00:00
|
|
|
const [request] = await Promise.all([
|
|
|
|
server.waitForRequest('/empty.html'),
|
2017-12-12 21:34:21 +00:00
|
|
|
page.goto(server.EMPTY_PAGE),
|
2017-09-14 05:52:43 +00:00
|
|
|
]);
|
2017-06-29 06:09:28 +00:00
|
|
|
expect(request.headers['user-agent']).toBe('foobar');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should emulate device user-agent', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/mobile.html');
|
2017-07-21 01:14:43 +00:00
|
|
|
expect(await page.evaluate(() => navigator.userAgent)).toContain('Chrome');
|
|
|
|
await page.setUserAgent(iPhone.userAgent);
|
|
|
|
expect(await page.evaluate(() => navigator.userAgent)).toContain('Safari');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-06-29 06:09:28 +00:00
|
|
|
});
|
2017-09-25 09:23:34 +00:00
|
|
|
|
2017-07-27 19:25:52 +00:00
|
|
|
describe('Page.setExtraHTTPHeaders', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-08-28 19:09:24 +00:00
|
|
|
await page.setExtraHTTPHeaders({
|
2017-07-27 19:25:52 +00:00
|
|
|
foo: 'bar'
|
2017-08-28 19:09:24 +00:00
|
|
|
});
|
2017-09-14 05:52:43 +00:00
|
|
|
const [request] = await Promise.all([
|
|
|
|
server.waitForRequest('/empty.html'),
|
2017-12-12 21:34:21 +00:00
|
|
|
page.goto(server.EMPTY_PAGE),
|
2017-09-14 05:52:43 +00:00
|
|
|
]);
|
2017-06-29 06:09:28 +00:00
|
|
|
expect(request.headers['foo']).toBe('bar');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw for non-string header values', async({page, server}) => {
|
2017-09-15 02:08:48 +00:00
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
await page.setExtraHTTPHeaders({ 'foo': 1 });
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error.message).toBe('Expected value of header "foo" to be String, but "number" is found.');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-06-29 06:09:28 +00:00
|
|
|
});
|
2017-09-25 09:23:34 +00:00
|
|
|
|
2017-09-11 23:32:13 +00:00
|
|
|
describe('Page.authenticate', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-09-11 23:32:13 +00:00
|
|
|
server.setAuth('/empty.html', 'user', 'pass');
|
2017-12-12 21:34:21 +00:00
|
|
|
let response = await page.goto(server.EMPTY_PAGE);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.status()).toBe(401);
|
2017-09-11 23:32:13 +00:00
|
|
|
await page.authenticate({
|
|
|
|
username: 'user',
|
|
|
|
password: 'pass'
|
|
|
|
});
|
|
|
|
response = await page.reload();
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.status()).toBe(200);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should fail if wrong credentials', async({page, server}) => {
|
2017-09-11 23:32:13 +00:00
|
|
|
// Use unique user/password since Chrome caches credentials per origin.
|
|
|
|
server.setAuth('/empty.html', 'user2', 'pass2');
|
|
|
|
await page.authenticate({
|
|
|
|
username: 'foo',
|
|
|
|
password: 'bar'
|
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.status()).toBe(401);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should allow disable authentication', async({page, server}) => {
|
2017-09-11 23:32:13 +00:00
|
|
|
// Use unique user/password since Chrome caches credentials per origin.
|
|
|
|
server.setAuth('/empty.html', 'user3', 'pass3');
|
|
|
|
await page.authenticate({
|
|
|
|
username: 'user3',
|
|
|
|
password: 'pass3'
|
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
let response = await page.goto(server.EMPTY_PAGE);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.status()).toBe(200);
|
2017-09-11 23:32:13 +00:00
|
|
|
await page.authenticate(null);
|
|
|
|
// Navigate to a different origin to bust Chrome's credential caching.
|
2017-12-12 21:34:21 +00:00
|
|
|
response = await page.goto(server.CROSS_PROCESS_PREFIX + '/empty.html');
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(response.status()).toBe(401);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-09-11 23:32:13 +00:00
|
|
|
});
|
2017-09-25 09:23:34 +00:00
|
|
|
|
2017-07-06 01:09:17 +00:00
|
|
|
describe('Page.setContent', function() {
|
2017-08-21 16:02:30 +00:00
|
|
|
const expectedOutput = '<html><head></head><body><div>hello</div></body></html>';
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-07-06 01:09:17 +00:00
|
|
|
await page.setContent('<div>hello</div>');
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.content();
|
2017-08-21 16:02:30 +00:00
|
|
|
expect(result).toBe(expectedOutput);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work with doctype', async({page, server}) => {
|
2017-08-21 16:02:30 +00:00
|
|
|
const doctype = '<!DOCTYPE html>';
|
|
|
|
await page.setContent(`${doctype}<div>hello</div>`);
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.content();
|
2017-08-21 16:02:30 +00:00
|
|
|
expect(result).toBe(`${doctype}${expectedOutput}`);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work with HTML 4 doctype', async({page, server}) => {
|
2017-08-21 16:02:30 +00:00
|
|
|
const doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" ' +
|
|
|
|
'"http://www.w3.org/TR/html4/strict.dtd">';
|
|
|
|
await page.setContent(`${doctype}<div>hello</div>`);
|
2017-08-21 23:39:04 +00:00
|
|
|
const result = await page.content();
|
2017-08-21 16:02:30 +00:00
|
|
|
expect(result).toBe(`${doctype}${expectedOutput}`);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-07-06 01:09:17 +00:00
|
|
|
});
|
2017-09-25 09:23:34 +00:00
|
|
|
|
2017-06-29 06:09:28 +00:00
|
|
|
describe('Network Events', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('Page.Events.Request', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const requests = [];
|
2017-06-29 06:09:28 +00:00
|
|
|
page.on('request', request => requests.push(request));
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-06-29 06:09:28 +00:00
|
|
|
expect(requests.length).toBe(1);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(requests[0].url()).toBe(server.EMPTY_PAGE);
|
|
|
|
expect(requests[0].resourceType()).toBe('document');
|
|
|
|
expect(requests[0].method()).toBe('GET');
|
2017-06-29 06:09:28 +00:00
|
|
|
expect(requests[0].response()).toBeTruthy();
|
2018-01-10 02:47:21 +00:00
|
|
|
expect(requests[0].frame() === page.mainFrame()).toBe(true);
|
|
|
|
expect(requests[0].frame().url()).toBe(server.EMPTY_PAGE);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('Page.Events.Request should report post data', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-07-28 06:11:24 +00:00
|
|
|
server.setRoute('/post', (req, res) => res.end());
|
|
|
|
let request = null;
|
|
|
|
page.on('request', r => request = r);
|
|
|
|
await page.evaluate(() => fetch('./post', { method: 'POST', body: JSON.stringify({foo: 'bar'})}));
|
|
|
|
expect(request).toBeTruthy();
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(request.postData()).toBe('{"foo":"bar"}');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('Page.Events.Response', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const responses = [];
|
2017-06-29 06:09:28 +00:00
|
|
|
page.on('response', response => responses.push(response));
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-06-29 06:09:28 +00:00
|
|
|
expect(responses.length).toBe(1);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(responses[0].url()).toBe(server.EMPTY_PAGE);
|
|
|
|
expect(responses[0].status()).toBe(200);
|
|
|
|
expect(responses[0].ok()).toBe(true);
|
2018-02-05 22:59:07 +00:00
|
|
|
expect(responses[0].fromCache()).toBe(false);
|
|
|
|
expect(responses[0].fromServiceWorker()).toBe(false);
|
2017-06-29 06:09:28 +00:00
|
|
|
expect(responses[0].request()).toBeTruthy();
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2018-02-05 22:59:07 +00:00
|
|
|
|
|
|
|
it('Response.fromCache()', async({page, server}) => {
|
|
|
|
const responses = new Map();
|
|
|
|
page.on('response', r => responses.set(r.url().split('/').pop(), r));
|
|
|
|
|
|
|
|
// Load and re-load to make sure it's cached.
|
|
|
|
await page.goto(server.PREFIX + '/cached/one-style.html');
|
|
|
|
await page.reload();
|
|
|
|
|
|
|
|
expect(responses.size).toBe(2);
|
|
|
|
expect(responses.get('one-style.html').status()).toBe(304);
|
|
|
|
expect(responses.get('one-style.html').fromCache()).toBe(false);
|
|
|
|
expect(responses.get('one-style.css').status()).toBe(200);
|
|
|
|
expect(responses.get('one-style.css').fromCache()).toBe(true);
|
|
|
|
});
|
|
|
|
it('Response.fromServiceWorker', async({page, server}) => {
|
|
|
|
const responses = new Map();
|
|
|
|
page.on('response', r => responses.set(r.url().split('/').pop(), r));
|
|
|
|
|
|
|
|
// Load and re-load to make sure serviceworker is installed and running.
|
|
|
|
await page.goto(server.PREFIX + '/serviceworkers/fetch/sw.html', {waitUntil: 'networkidle2'});
|
|
|
|
await page.reload();
|
|
|
|
|
|
|
|
expect(responses.size).toBe(2);
|
|
|
|
expect(responses.get('sw.html').status()).toBe(200);
|
|
|
|
expect(responses.get('sw.html').fromServiceWorker()).toBe(true);
|
|
|
|
expect(responses.get('style.css').status()).toBe(200);
|
|
|
|
expect(responses.get('style.css').fromServiceWorker()).toBe(true);
|
|
|
|
});
|
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('Page.Events.Response should provide body', async({page, server}) => {
|
2017-07-28 06:11:24 +00:00
|
|
|
let response = null;
|
|
|
|
page.on('response', r => response = r);
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/simple.json');
|
2017-07-28 06:11:24 +00:00
|
|
|
expect(response).toBeTruthy();
|
|
|
|
expect(await response.text()).toBe('{"foo": "bar"}\n');
|
|
|
|
expect(await response.json()).toEqual({foo: 'bar'});
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('Page.Events.Response should not report body unless request is finished', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-07-30 00:42:00 +00:00
|
|
|
// Setup server to trap request.
|
|
|
|
let serverResponse = null;
|
|
|
|
server.setRoute('/get', (req, res) => {
|
|
|
|
serverResponse = res;
|
|
|
|
res.write('hello ');
|
|
|
|
});
|
|
|
|
// Setup page to trap response.
|
|
|
|
let pageResponse = null;
|
|
|
|
let requestFinished = false;
|
|
|
|
page.on('response', r => pageResponse = r);
|
|
|
|
page.on('requestfinished', () => requestFinished = true);
|
|
|
|
// send request and wait for server response
|
2017-08-02 20:24:34 +00:00
|
|
|
await Promise.all([
|
|
|
|
page.evaluate(() => fetch('./get', { method: 'GET'})),
|
|
|
|
waitForEvents(page, 'response')
|
|
|
|
]);
|
2017-07-30 00:42:00 +00:00
|
|
|
|
|
|
|
expect(serverResponse).toBeTruthy();
|
|
|
|
expect(pageResponse).toBeTruthy();
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(pageResponse.status()).toBe(200);
|
2017-07-30 00:42:00 +00:00
|
|
|
expect(requestFinished).toBe(false);
|
|
|
|
|
2017-08-21 23:39:04 +00:00
|
|
|
const responseText = pageResponse.text();
|
2017-07-30 00:42:00 +00:00
|
|
|
// Write part of the response and wait for it to be flushed.
|
|
|
|
await new Promise(x => serverResponse.write('wor', x));
|
|
|
|
// Finish response.
|
|
|
|
await new Promise(x => serverResponse.end('ld!', x));
|
|
|
|
expect(await responseText).toBe('hello world!');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('Page.Events.RequestFailed', async({page, server}) => {
|
2017-10-24 21:45:03 +00:00
|
|
|
await page.setRequestInterception(true);
|
2017-08-12 00:24:31 +00:00
|
|
|
page.on('request', request => {
|
2017-12-19 01:05:57 +00:00
|
|
|
if (request.url().endsWith('css'))
|
2017-06-29 06:09:28 +00:00
|
|
|
request.abort();
|
|
|
|
else
|
|
|
|
request.continue();
|
|
|
|
});
|
2017-08-21 23:39:04 +00:00
|
|
|
const failedRequests = [];
|
2017-06-29 06:09:28 +00:00
|
|
|
page.on('requestfailed', request => failedRequests.push(request));
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/one-style.html');
|
2017-06-29 06:09:28 +00:00
|
|
|
expect(failedRequests.length).toBe(1);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(failedRequests[0].url()).toContain('one-style.css');
|
2017-06-29 06:09:28 +00:00
|
|
|
expect(failedRequests[0].response()).toBe(null);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(failedRequests[0].resourceType()).toBe('stylesheet');
|
2017-10-18 00:48:04 +00:00
|
|
|
expect(failedRequests[0].failure().errorText).toBe('net::ERR_FAILED');
|
2018-01-10 02:47:21 +00:00
|
|
|
expect(failedRequests[0].frame()).toBeTruthy();
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('Page.Events.RequestFinished', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const requests = [];
|
2017-06-29 06:09:28 +00:00
|
|
|
page.on('requestfinished', request => requests.push(request));
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-06-29 06:09:28 +00:00
|
|
|
expect(requests.length).toBe(1);
|
2017-12-19 01:05:57 +00:00
|
|
|
expect(requests[0].url()).toBe(server.EMPTY_PAGE);
|
2017-06-29 06:09:28 +00:00
|
|
|
expect(requests[0].response()).toBeTruthy();
|
2018-01-10 02:47:21 +00:00
|
|
|
expect(requests[0].frame() === page.mainFrame()).toBe(true);
|
|
|
|
expect(requests[0].frame().url()).toBe(server.EMPTY_PAGE);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should fire events in proper order', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const events = [];
|
2017-06-29 06:09:28 +00:00
|
|
|
page.on('request', request => events.push('request'));
|
|
|
|
page.on('response', response => events.push('response'));
|
|
|
|
page.on('requestfinished', request => events.push('requestfinished'));
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-06-29 06:09:28 +00:00
|
|
|
expect(events).toEqual(['request', 'response', 'requestfinished']);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should support redirects', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const events = [];
|
2017-12-19 01:05:57 +00:00
|
|
|
page.on('request', request => events.push(`${request.method()} ${request.url()}`));
|
|
|
|
page.on('response', response => events.push(`${response.status()} ${response.url()}`));
|
|
|
|
page.on('requestfinished', request => events.push(`DONE ${request.url()}`));
|
|
|
|
page.on('requestfailed', request => events.push(`FAIL ${request.url()}`));
|
2017-07-07 15:14:25 +00:00
|
|
|
server.setRedirect('/foo.html', '/empty.html');
|
2017-12-12 21:34:21 +00:00
|
|
|
const FOO_URL = server.PREFIX + '/foo.html';
|
2017-08-10 07:02:10 +00:00
|
|
|
await page.goto(FOO_URL);
|
2017-06-30 01:54:01 +00:00
|
|
|
expect(events).toEqual([
|
|
|
|
`GET ${FOO_URL}`,
|
|
|
|
`302 ${FOO_URL}`,
|
|
|
|
`DONE ${FOO_URL}`,
|
2017-12-12 21:34:21 +00:00
|
|
|
`GET ${server.EMPTY_PAGE}`,
|
|
|
|
`200 ${server.EMPTY_PAGE}`,
|
|
|
|
`DONE ${server.EMPTY_PAGE}`
|
2017-06-30 01:54:01 +00:00
|
|
|
]);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-06-29 06:09:28 +00:00
|
|
|
});
|
2017-07-10 17:09:59 +00:00
|
|
|
|
|
|
|
describe('Page.addScriptTag', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should throw an error if no options are provided', async({page, server}) => {
|
2017-10-12 08:26:44 +00:00
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
await page.addScriptTag('/injectedfile.js');
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error.message).toBe('Provide an object with a `url`, `path` or `content` property');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-12 08:26:44 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work with a url', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-11-03 07:05:38 +00:00
|
|
|
const scriptHandle = await page.addScriptTag({ url: '/injectedfile.js' });
|
|
|
|
expect(scriptHandle.asElement()).not.toBeNull();
|
2017-07-10 17:09:59 +00:00
|
|
|
expect(await page.evaluate(() => __injected)).toBe(42);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-12 08:26:44 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should throw an error if loading from url fail', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-11-07 06:04:40 +00:00
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
await page.addScriptTag({ url: '/nonexistfile.js' });
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error.message).toBe('Loading script from /nonexistfile.js failed');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-11-07 06:04:40 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work with a path', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-11-03 07:05:38 +00:00
|
|
|
const scriptHandle = await page.addScriptTag({ path: path.join(__dirname, 'assets/injectedfile.js') });
|
|
|
|
expect(scriptHandle.asElement()).not.toBeNull();
|
2017-10-12 08:26:44 +00:00
|
|
|
expect(await page.evaluate(() => __injected)).toBe(42);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-12 08:26:44 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should include sourcemap when path is provided', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-10-12 08:26:44 +00:00
|
|
|
await page.addScriptTag({ path: path.join(__dirname, 'assets/injectedfile.js') });
|
|
|
|
const result = await page.evaluate(() => __injectedError.stack);
|
|
|
|
expect(result).toContain(path.join('assets', 'injectedfile.js'));
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-12 08:26:44 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work with content', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-11-03 07:05:38 +00:00
|
|
|
const scriptHandle = await page.addScriptTag({ content: 'window.__injected = 35;' });
|
|
|
|
expect(scriptHandle.asElement()).not.toBeNull();
|
2017-10-12 08:26:44 +00:00
|
|
|
expect(await page.evaluate(() => __injected)).toBe(35);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-07-10 17:09:59 +00:00
|
|
|
});
|
|
|
|
|
2017-10-04 20:42:26 +00:00
|
|
|
describe('Page.addStyleTag', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should throw an error if no options are provided', async({page, server}) => {
|
2017-10-12 08:26:44 +00:00
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
await page.addStyleTag('/injectedstyle.css');
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error.message).toBe('Provide an object with a `url`, `path` or `content` property');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-12 08:26:44 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work with a url', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-11-03 07:05:38 +00:00
|
|
|
const styleHandle = await page.addStyleTag({ url: '/injectedstyle.css' });
|
|
|
|
expect(styleHandle.asElement()).not.toBeNull();
|
2017-10-12 08:26:44 +00:00
|
|
|
expect(await page.evaluate(`window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')`)).toBe('rgb(255, 0, 0)');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-12 08:26:44 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should throw an error if loading from url fail', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-11-07 06:04:40 +00:00
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
await page.addStyleTag({ url: '/nonexistfile.js' });
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error.message).toBe('Loading style from /nonexistfile.js failed');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-11-07 06:04:40 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work with a path', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-11-03 07:05:38 +00:00
|
|
|
const styleHandle = await page.addStyleTag({ path: path.join(__dirname, 'assets/injectedstyle.css') });
|
|
|
|
expect(styleHandle.asElement()).not.toBeNull();
|
2017-10-04 20:42:26 +00:00
|
|
|
expect(await page.evaluate(`window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')`)).toBe('rgb(255, 0, 0)');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-12 08:26:44 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should include sourcemap when path is provided', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-10-12 08:26:44 +00:00
|
|
|
await page.addStyleTag({ path: path.join(__dirname, 'assets/injectedstyle.css') });
|
|
|
|
const styleHandle = await page.$('style');
|
|
|
|
const styleContent = await page.evaluate(style => style.innerHTML, styleHandle);
|
|
|
|
expect(styleContent).toContain(path.join('assets', 'injectedstyle.css'));
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-12 08:26:44 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work with content', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-11-03 07:05:38 +00:00
|
|
|
const styleHandle = await page.addStyleTag({ content: 'body { background-color: green; }' });
|
|
|
|
expect(styleHandle.asElement()).not.toBeNull();
|
2017-10-12 08:26:44 +00:00
|
|
|
expect(await page.evaluate(`window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')`)).toBe('rgb(0, 128, 0)');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-04 20:42:26 +00:00
|
|
|
});
|
|
|
|
|
2017-07-10 17:09:59 +00:00
|
|
|
describe('Page.url', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-07-14 21:05:27 +00:00
|
|
|
expect(page.url()).toBe('about:blank');
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(page.url()).toBe(server.EMPTY_PAGE);
|
|
|
|
});
|
2017-07-10 17:09:59 +00:00
|
|
|
});
|
|
|
|
|
2017-07-18 01:13:04 +00:00
|
|
|
describe('Page.viewport', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should get the proper viewport size', async({page, server}) => {
|
2017-08-02 22:47:00 +00:00
|
|
|
expect(page.viewport()).toEqual({width: 800, height: 600});
|
2017-07-18 01:13:04 +00:00
|
|
|
await page.setViewport({width: 123, height: 456});
|
|
|
|
expect(page.viewport()).toEqual({width: 123, height: 456});
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should support mobile emulation', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/mobile.html');
|
2017-08-02 22:47:00 +00:00
|
|
|
expect(await page.evaluate(() => window.innerWidth)).toBe(800);
|
2017-07-21 01:14:43 +00:00
|
|
|
await page.setViewport(iPhone.viewport);
|
|
|
|
expect(await page.evaluate(() => window.innerWidth)).toBe(375);
|
|
|
|
await page.setViewport({width: 400, height: 300});
|
|
|
|
expect(await page.evaluate(() => window.innerWidth)).toBe(400);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should support touch emulation', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/mobile.html');
|
2017-07-21 01:14:43 +00:00
|
|
|
expect(await page.evaluate(() => 'ontouchstart' in window)).toBe(false);
|
|
|
|
await page.setViewport(iPhone.viewport);
|
|
|
|
expect(await page.evaluate(() => 'ontouchstart' in window)).toBe(true);
|
2018-02-09 03:59:46 +00:00
|
|
|
expect(await page.evaluate(dispatchTouch)).toBe('Received touch');
|
2017-07-21 01:14:43 +00:00
|
|
|
await page.setViewport({width: 100, height: 100});
|
|
|
|
expect(await page.evaluate(() => 'ontouchstart' in window)).toBe(false);
|
2017-08-11 01:25:56 +00:00
|
|
|
|
|
|
|
function dispatchTouch() {
|
|
|
|
let fulfill;
|
2017-08-21 23:39:04 +00:00
|
|
|
const promise = new Promise(x => fulfill = x);
|
2017-08-11 01:25:56 +00:00
|
|
|
window.ontouchstart = function(e) {
|
2018-02-09 03:59:46 +00:00
|
|
|
fulfill('Received touch');
|
2017-08-11 01:25:56 +00:00
|
|
|
};
|
|
|
|
window.dispatchEvent(new Event('touchstart'));
|
|
|
|
|
2018-02-09 03:59:46 +00:00
|
|
|
fulfill('Did not receive touch');
|
2017-08-11 01:25:56 +00:00
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should be detectable by Modernizr', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/detect-touch.html');
|
2017-08-11 20:59:23 +00:00
|
|
|
expect(await page.evaluate(() => document.body.textContent.trim())).toBe('NO');
|
|
|
|
await page.setViewport(iPhone.viewport);
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/detect-touch.html');
|
2017-08-11 20:59:23 +00:00
|
|
|
expect(await page.evaluate(() => document.body.textContent.trim())).toBe('YES');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should support landscape emulation', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/mobile.html');
|
2017-07-21 01:14:43 +00:00
|
|
|
expect(await page.evaluate(() => screen.orientation.type)).toBe('portrait-primary');
|
|
|
|
await page.setViewport(iPhoneLandscape.viewport);
|
|
|
|
expect(await page.evaluate(() => screen.orientation.type)).toBe('landscape-primary');
|
|
|
|
await page.setViewport({width: 100, height: 100});
|
|
|
|
expect(await page.evaluate(() => screen.orientation.type)).toBe('portrait-primary');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-18 23:49:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Page.emulate', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/mobile.html');
|
2017-08-11 01:42:30 +00:00
|
|
|
await page.emulate(iPhone);
|
|
|
|
expect(await page.evaluate(() => window.innerWidth)).toBe(375);
|
|
|
|
expect(await page.evaluate(() => navigator.userAgent)).toContain('Safari');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should support clicking', async({page, server}) => {
|
2017-11-07 21:54:40 +00:00
|
|
|
await page.emulate(iPhone);
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
2017-11-07 21:54:40 +00:00
|
|
|
const button = await page.$('button');
|
|
|
|
await page.evaluate(button => button.style.marginTop = '200px', button);
|
|
|
|
await button.click();
|
|
|
|
expect(await page.evaluate(() => result)).toBe('Clicked');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-07-10 17:09:59 +00:00
|
|
|
});
|
|
|
|
|
2017-08-18 23:49:02 +00:00
|
|
|
describe('Page.emulateMedia', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-08-18 23:49:02 +00:00
|
|
|
expect(await page.evaluate(() => window.matchMedia('screen').matches)).toBe(true);
|
|
|
|
expect(await page.evaluate(() => window.matchMedia('print').matches)).toBe(false);
|
|
|
|
await page.emulateMedia('print');
|
|
|
|
expect(await page.evaluate(() => window.matchMedia('screen').matches)).toBe(false);
|
|
|
|
expect(await page.evaluate(() => window.matchMedia('print').matches)).toBe(true);
|
|
|
|
await page.emulateMedia(null);
|
|
|
|
expect(await page.evaluate(() => window.matchMedia('screen').matches)).toBe(true);
|
|
|
|
expect(await page.evaluate(() => window.matchMedia('print').matches)).toBe(false);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw in case of bad argument', async({page, server}) => {
|
2017-08-18 23:49:02 +00:00
|
|
|
let error = null;
|
|
|
|
await page.emulateMedia('bad').catch(e => error = e);
|
|
|
|
expect(error.message).toBe('Unsupported media type: bad');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-18 23:49:02 +00:00
|
|
|
});
|
|
|
|
|
2017-08-23 21:08:56 +00:00
|
|
|
describe('Page.setJavaScriptEnabled', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-08-23 21:08:56 +00:00
|
|
|
await page.setJavaScriptEnabled(false);
|
|
|
|
await page.goto('data:text/html, <script>var something = "forbidden"</script>');
|
|
|
|
let error = null;
|
2017-09-13 19:06:57 +00:00
|
|
|
await page.evaluate('something').catch(e => error = e);
|
2017-08-23 21:08:56 +00:00
|
|
|
expect(error.message).toContain('something is not defined');
|
|
|
|
|
|
|
|
await page.setJavaScriptEnabled(true);
|
|
|
|
await page.goto('data:text/html, <script>var something = "forbidden"</script>');
|
|
|
|
expect(await page.evaluate('something')).toBe('forbidden');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-23 21:08:56 +00:00
|
|
|
});
|
|
|
|
|
2017-07-25 07:00:25 +00:00
|
|
|
describe('Page.evaluateOnNewDocument', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should evaluate before anything else on the page', async({page, server}) => {
|
2017-07-25 07:00:25 +00:00
|
|
|
await page.evaluateOnNewDocument(function(){
|
2017-07-10 17:09:59 +00:00
|
|
|
window.injected = 123;
|
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/tamperable.html');
|
2017-07-10 17:09:59 +00:00
|
|
|
expect(await page.evaluate(() => window.result)).toBe(123);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-07-10 17:09:59 +00:00
|
|
|
});
|
|
|
|
|
2018-02-08 05:58:48 +00:00
|
|
|
describe('Page.setCacheEnabled', function() {
|
|
|
|
it('should enable or disable the cache based on the state passed', async({page, server}) => {
|
|
|
|
const responses = new Map();
|
|
|
|
page.on('response', r => responses.set(r.url().split('/').pop(), r));
|
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/cached/one-style.html', {waitUntil: 'networkidle2'});
|
|
|
|
await page.reload({waitUntil: 'networkidle2'});
|
|
|
|
expect(responses.get('one-style.css').fromCache()).toBe(true);
|
|
|
|
|
|
|
|
await page.setCacheEnabled(false);
|
|
|
|
await page.reload({waitUntil: 'networkidle2'});
|
|
|
|
expect(responses.get('one-style.css').fromCache()).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-08-03 21:10:52 +00:00
|
|
|
// Printing to pdf is currently only supported in headless
|
|
|
|
(headless ? describe : xdescribe)('Page.pdf', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should be able to save file', async({page, server}) => {
|
2017-08-03 21:10:52 +00:00
|
|
|
const outputFile = __dirname + '/assets/output.pdf';
|
2017-08-03 20:42:38 +00:00
|
|
|
await page.pdf({path: outputFile});
|
|
|
|
expect(fs.readFileSync(outputFile).byteLength).toBeGreaterThan(0);
|
2017-08-03 21:10:52 +00:00
|
|
|
fs.unlinkSync(outputFile);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should default to printing in Letter format', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const pages = await getPDFPages(await page.pdf());
|
2017-08-03 21:10:52 +00:00
|
|
|
expect(pages.length).toBe(1);
|
|
|
|
expect(pages[0].width).toBeCloseTo(8.5, 2);
|
|
|
|
expect(pages[0].height).toBeCloseTo(11, 2);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should support setting custom format', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const pages = await getPDFPages(await page.pdf({
|
2017-08-04 08:01:10 +00:00
|
|
|
format: 'a4'
|
2017-08-03 21:10:52 +00:00
|
|
|
}));
|
|
|
|
expect(pages.length).toBe(1);
|
|
|
|
expect(pages[0].width).toBeCloseTo(8.27, 1);
|
|
|
|
expect(pages[0].height).toBeCloseTo(11.7, 1);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should support setting paper width and height', async({page, server}) => {
|
2017-08-21 23:39:04 +00:00
|
|
|
const pages = await getPDFPages(await page.pdf({
|
2017-08-03 21:10:52 +00:00
|
|
|
width: '10in',
|
|
|
|
height: '10in',
|
|
|
|
}));
|
|
|
|
expect(pages.length).toBe(1);
|
|
|
|
expect(pages[0].width).toBeCloseTo(10, 2);
|
|
|
|
expect(pages[0].height).toBeCloseTo(10, 2);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should print multiple pages', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2017-08-03 21:10:52 +00:00
|
|
|
// Define width and height in CSS pixels.
|
|
|
|
const width = 50 * 5 + 1;
|
|
|
|
const height = 50 * 5 + 1;
|
2017-08-21 23:39:04 +00:00
|
|
|
const pages = await getPDFPages(await page.pdf({width, height}));
|
2017-08-03 21:10:52 +00:00
|
|
|
expect(pages.length).toBe(8);
|
|
|
|
expect(pages[0].width).toBeCloseTo(cssPixelsToInches(width), 2);
|
|
|
|
expect(pages[0].height).toBeCloseTo(cssPixelsToInches(height), 2);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should support page ranges', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2017-08-03 21:10:52 +00:00
|
|
|
// Define width and height in CSS pixels.
|
|
|
|
const width = 50 * 5 + 1;
|
|
|
|
const height = 50 * 5 + 1;
|
2017-08-21 23:39:04 +00:00
|
|
|
const pages = await getPDFPages(await page.pdf({width, height, pageRanges: '1,4-7'}));
|
2017-08-03 21:10:52 +00:00
|
|
|
expect(pages.length).toBe(5);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw if format is unknown', async({page, server}) => {
|
2017-08-03 21:10:52 +00:00
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
await getPDFPages(await page.pdf({
|
|
|
|
format: 'something'
|
|
|
|
}));
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error).toBeTruthy();
|
|
|
|
expect(error.message).toContain('Unknown paper format');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw if units are unknown', async({page, server}) => {
|
2017-08-03 21:10:52 +00:00
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
await getPDFPages(await page.pdf({
|
|
|
|
width: '10em',
|
|
|
|
height: '10em',
|
|
|
|
}));
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error).toBeTruthy();
|
|
|
|
expect(error.message).toContain('Failed to parse parameter value');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-07-10 17:09:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Page.title', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should return the page title', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
2017-07-10 17:09:59 +00:00
|
|
|
expect(await page.title()).toBe('Button test');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-07-10 17:09:59 +00:00
|
|
|
});
|
2017-07-18 01:56:56 +00:00
|
|
|
|
2017-07-19 05:10:38 +00:00
|
|
|
describe('Page.screenshot', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should work', async({page, server}) => {
|
2017-07-19 05:10:38 +00:00
|
|
|
await page.setViewport({width: 500, height: 500});
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2017-08-21 23:39:04 +00:00
|
|
|
const screenshot = await page.screenshot();
|
2017-07-19 05:10:38 +00:00
|
|
|
expect(screenshot).toBeGolden('screenshot-sanity.png');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should clip rect', async({page, server}) => {
|
2017-07-19 05:10:38 +00:00
|
|
|
await page.setViewport({width: 500, height: 500});
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2017-08-21 23:39:04 +00:00
|
|
|
const screenshot = await page.screenshot({
|
2017-07-19 05:10:38 +00:00
|
|
|
clip: {
|
|
|
|
x: 50,
|
|
|
|
y: 100,
|
|
|
|
width: 150,
|
|
|
|
height: 100
|
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(screenshot).toBeGolden('screenshot-clip-rect.png');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work for offscreen clip', async({page, server}) => {
|
2017-07-19 05:10:38 +00:00
|
|
|
await page.setViewport({width: 500, height: 500});
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2017-08-21 23:39:04 +00:00
|
|
|
const screenshot = await page.screenshot({
|
2017-07-19 05:10:38 +00:00
|
|
|
clip: {
|
|
|
|
x: 50,
|
|
|
|
y: 600,
|
|
|
|
width: 100,
|
|
|
|
height: 100
|
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(screenshot).toBeGolden('screenshot-offscreen-clip.png');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should run in parallel', async({page, server}) => {
|
2017-07-19 05:10:38 +00:00
|
|
|
await page.setViewport({width: 500, height: 500});
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2017-08-21 23:39:04 +00:00
|
|
|
const promises = [];
|
2017-07-19 05:10:38 +00:00
|
|
|
for (let i = 0; i < 3; ++i) {
|
|
|
|
promises.push(page.screenshot({
|
|
|
|
clip: {
|
|
|
|
x: 50 * i,
|
|
|
|
y: 0,
|
|
|
|
width: 50,
|
|
|
|
height: 50
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
2017-08-21 23:39:04 +00:00
|
|
|
const screenshots = await Promise.all(promises);
|
2017-07-19 05:10:38 +00:00
|
|
|
expect(screenshots[1]).toBeGolden('grid-cell-1.png');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should take fullPage screenshots', async({page, server}) => {
|
2017-07-19 05:10:38 +00:00
|
|
|
await page.setViewport({width: 500, height: 500});
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2017-08-21 23:39:04 +00:00
|
|
|
const screenshot = await page.screenshot({
|
2017-07-19 05:10:38 +00:00
|
|
|
fullPage: true
|
|
|
|
});
|
|
|
|
expect(screenshot).toBeGolden('screenshot-grid-fullpage.png');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should run in parallel in multiple pages', async({page, server, browser}) => {
|
2017-07-19 05:10:38 +00:00
|
|
|
const N = 2;
|
2017-08-21 23:39:04 +00:00
|
|
|
const pages = await Promise.all(Array(N).fill(0).map(async() => {
|
|
|
|
const page = await browser.newPage();
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2017-07-19 05:10:38 +00:00
|
|
|
return page;
|
|
|
|
}));
|
2017-08-21 23:39:04 +00:00
|
|
|
const promises = [];
|
2017-07-19 05:10:38 +00:00
|
|
|
for (let i = 0; i < N; ++i)
|
|
|
|
promises.push(pages[i].screenshot({ clip: { x: 50 * i, y: 0, width: 50, height: 50 } }));
|
2017-08-21 23:39:04 +00:00
|
|
|
const screenshots = await Promise.all(promises);
|
2017-07-19 05:10:38 +00:00
|
|
|
for (let i = 0; i < N; ++i)
|
|
|
|
expect(screenshots[i]).toBeGolden(`grid-cell-${i}.png`);
|
|
|
|
await Promise.all(pages.map(page => page.close()));
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should allow transparency', async({page, server}) => {
|
2017-08-18 04:11:39 +00:00
|
|
|
await page.setViewport({ width: 100, height: 100 });
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-08-22 21:18:07 +00:00
|
|
|
const screenshot = await page.screenshot({omitBackground: true});
|
2017-08-18 04:11:39 +00:00
|
|
|
expect(screenshot).toBeGolden('transparent.png');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should work with odd clip size on Retina displays', async({page, server}) => {
|
2017-10-20 17:45:22 +00:00
|
|
|
const screenshot = await page.screenshot({
|
|
|
|
clip: {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 11,
|
|
|
|
height: 11,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(screenshot).toBeGolden('screenshot-clip-odd-size.png');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-07-19 05:10:38 +00:00
|
|
|
});
|
2017-08-02 17:45:11 +00:00
|
|
|
|
2017-09-25 09:23:34 +00:00
|
|
|
describe('Page.select', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should select single option', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
2017-09-25 09:23:34 +00:00
|
|
|
await page.select('select', 'blue');
|
|
|
|
expect(await page.evaluate(() => result.onInput)).toEqual(['blue']);
|
|
|
|
expect(await page.evaluate(() => result.onChange)).toEqual(['blue']);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2018-01-25 05:12:27 +00:00
|
|
|
it('should select only first option', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
|
|
|
await page.select('select', 'blue', 'green', 'red');
|
|
|
|
expect(await page.evaluate(() => result.onInput)).toEqual(['blue']);
|
|
|
|
expect(await page.evaluate(() => result.onChange)).toEqual(['blue']);
|
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should select multiple options', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
2017-09-25 09:23:34 +00:00
|
|
|
await page.evaluate(() => makeMultiple());
|
|
|
|
await page.select('select', 'blue', 'green', 'red');
|
|
|
|
expect(await page.evaluate(() => result.onInput)).toEqual(['blue', 'green', 'red']);
|
|
|
|
expect(await page.evaluate(() => result.onChange)).toEqual(['blue', 'green', 'red']);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should respect event bubbling', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
2017-10-07 17:27:38 +00:00
|
|
|
await page.select('select', 'blue');
|
|
|
|
expect(await page.evaluate(() => result.onBubblingInput)).toEqual(['blue']);
|
|
|
|
expect(await page.evaluate(() => result.onBubblingChange)).toEqual(['blue']);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw when element is not a <select>', async({page, server}) => {
|
2017-11-01 04:47:52 +00:00
|
|
|
let error = null;
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
2017-11-01 04:47:52 +00:00
|
|
|
await page.select('body', '').catch(e => error = e);
|
|
|
|
expect(error.message).toContain('Element is not a <select> element.');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should return [] on no matched values', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
2017-11-01 04:47:52 +00:00
|
|
|
const result = await page.select('select','42','abc');
|
|
|
|
expect(result).toEqual([]);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should return an array of matched values', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
2017-11-01 04:47:52 +00:00
|
|
|
await page.evaluate(() => makeMultiple());
|
|
|
|
const result = await page.select('select','blue','black','magenta');
|
|
|
|
expect(result.reduce((accumulator,current) => ['blue', 'black', 'magenta'].includes(current) && accumulator, true)).toEqual(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should return an array of one element when multiple is not set', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
2017-11-01 04:47:52 +00:00
|
|
|
const result = await page.select('select','42','blue','black','magenta');
|
|
|
|
expect(result.length).toEqual(1);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should return [] on no values',async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
2017-11-01 04:47:52 +00:00
|
|
|
const result = await page.select('select');
|
|
|
|
expect(result).toEqual([]);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should deselect all options when passed no values for a multiple select',async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
2017-11-01 04:47:52 +00:00
|
|
|
await page.evaluate(() => makeMultiple());
|
|
|
|
await page.select('select','blue','black','magenta');
|
|
|
|
await page.select('select');
|
|
|
|
expect(await page.$eval('select', select => Array.from(select.options).every(option => !option.selected))).toEqual(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should deselect all options when passed no values for a select without multiple',async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
2017-11-01 04:47:52 +00:00
|
|
|
await page.select('select','blue','black','magenta');
|
|
|
|
await page.select('select');
|
|
|
|
expect(await page.$eval('select', select => Array.from(select.options).every(option => !option.selected))).toEqual(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw if passed in non-strings', async({page, server}) => {
|
2017-11-04 02:20:12 +00:00
|
|
|
await page.setContent('<select><option value="12"/></select>');
|
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
await page.select('select', 12);
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error.message).toContain('Values must be strings');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-09-25 09:23:34 +00:00
|
|
|
});
|
|
|
|
|
2017-08-02 17:45:11 +00:00
|
|
|
describe('Tracing', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
beforeEach(function(state) {
|
2017-12-18 18:49:01 +00:00
|
|
|
state.outputFile = path.join(__dirname, 'assets', `trace-${state.parallelIndex}.json`);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
afterEach(function(state) {
|
|
|
|
fs.unlinkSync(state.outputFile);
|
|
|
|
state.outputFile = null;
|
2017-08-02 17:45:11 +00:00
|
|
|
});
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should output a trace', async({page, server, outputFile}) => {
|
2017-08-02 22:41:05 +00:00
|
|
|
await page.tracing.start({screenshots: true, path: outputFile});
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2017-08-02 22:41:05 +00:00
|
|
|
await page.tracing.stop();
|
2017-08-02 17:45:11 +00:00
|
|
|
expect(fs.existsSync(outputFile)).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should run with custom categories if provided', async({page, outputFile}) => {
|
2017-12-04 02:36:34 +00:00
|
|
|
await page.tracing.start({path: outputFile, categories: ['disabled-by-default-v8.cpu_profiler.hires']});
|
|
|
|
await page.tracing.stop();
|
|
|
|
|
|
|
|
const traceJson = JSON.parse(fs.readFileSync(outputFile));
|
|
|
|
expect(traceJson.metadata['trace-config']).toContain('disabled-by-default-v8.cpu_profiler.hires');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should throw if tracing on two pages', async({page, server, browser, outputFile}) => {
|
2017-08-02 22:41:05 +00:00
|
|
|
await page.tracing.start({path: outputFile});
|
2017-08-21 23:39:04 +00:00
|
|
|
const newPage = await browser.newPage();
|
2017-08-02 17:45:11 +00:00
|
|
|
let error = null;
|
2017-09-13 19:06:57 +00:00
|
|
|
await newPage.tracing.start({path: outputFile}).catch(e => error = e);
|
2017-08-02 17:45:11 +00:00
|
|
|
await newPage.close();
|
|
|
|
expect(error).toBeTruthy();
|
2017-08-02 22:41:05 +00:00
|
|
|
await page.tracing.stop();
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-02 17:45:11 +00:00
|
|
|
});
|
2017-08-24 19:21:46 +00:00
|
|
|
|
|
|
|
describe('Cookies', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
afterEach(async({page, server}) => {
|
|
|
|
const cookies = await page.cookies(server.PREFIX + '/grid.html', server.CROSS_PROCESS_PREFIX);
|
2017-08-24 19:21:46 +00:00
|
|
|
for (const cookie of cookies)
|
|
|
|
await page.deleteCookie(cookie);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should set and get cookies', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2017-08-24 19:21:46 +00:00
|
|
|
expect(await page.cookies()).toEqual([]);
|
|
|
|
await page.evaluate(() => {
|
|
|
|
document.cookie = 'username=John Doe';
|
|
|
|
});
|
|
|
|
expect(await page.cookies()).toEqual([{
|
|
|
|
name: 'username',
|
|
|
|
value: 'John Doe',
|
|
|
|
domain: 'localhost',
|
|
|
|
path: '/',
|
2017-12-07 21:40:02 +00:00
|
|
|
expires: -1,
|
2017-08-24 19:21:46 +00:00
|
|
|
size: 16,
|
|
|
|
httpOnly: false,
|
|
|
|
secure: false,
|
|
|
|
session: true }
|
|
|
|
]);
|
|
|
|
await page.setCookie({
|
|
|
|
name: 'password',
|
|
|
|
value: '123456'
|
|
|
|
});
|
|
|
|
expect(await page.evaluate('document.cookie')).toBe('username=John Doe; password=123456');
|
2017-09-14 00:30:25 +00:00
|
|
|
const cookies = await page.cookies();
|
|
|
|
expect(cookies.sort((a, b) => a.name.localeCompare(b.name))).toEqual([{
|
2017-08-24 19:21:46 +00:00
|
|
|
name: 'password',
|
|
|
|
value: '123456',
|
|
|
|
domain: 'localhost',
|
|
|
|
path: '/',
|
2017-12-07 21:40:02 +00:00
|
|
|
expires: -1,
|
2017-08-24 19:21:46 +00:00
|
|
|
size: 14,
|
|
|
|
httpOnly: false,
|
|
|
|
secure: false,
|
|
|
|
session: true
|
|
|
|
}, {
|
|
|
|
name: 'username',
|
|
|
|
value: 'John Doe',
|
|
|
|
domain: 'localhost',
|
|
|
|
path: '/',
|
2017-12-07 21:40:02 +00:00
|
|
|
expires: -1,
|
2017-08-24 19:21:46 +00:00
|
|
|
size: 16,
|
|
|
|
httpOnly: false,
|
|
|
|
secure: false,
|
|
|
|
session: true
|
|
|
|
}]);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-24 19:21:46 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should set a cookie with a path', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2017-08-24 19:21:46 +00:00
|
|
|
await page.setCookie({
|
|
|
|
name: 'gridcookie',
|
|
|
|
value: 'GRID',
|
|
|
|
path: '/grid.html'
|
|
|
|
});
|
|
|
|
expect(await page.cookies()).toEqual([{
|
|
|
|
name: 'gridcookie',
|
|
|
|
value: 'GRID',
|
|
|
|
domain: 'localhost',
|
|
|
|
path: '/grid.html',
|
2017-12-07 21:40:02 +00:00
|
|
|
expires: -1,
|
2017-08-24 19:21:46 +00:00
|
|
|
size: 14,
|
|
|
|
httpOnly: false,
|
|
|
|
secure: false,
|
|
|
|
session: true
|
|
|
|
}]);
|
|
|
|
expect(await page.evaluate('document.cookie')).toBe('gridcookie=GRID');
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/empty.html');
|
2017-08-24 19:21:46 +00:00
|
|
|
expect(await page.cookies()).toEqual([]);
|
|
|
|
expect(await page.evaluate('document.cookie')).toBe('');
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2017-08-24 19:21:46 +00:00
|
|
|
expect(await page.evaluate('document.cookie')).toBe('gridcookie=GRID');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-24 19:21:46 +00:00
|
|
|
|
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should delete a cookie', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2017-08-24 19:21:46 +00:00
|
|
|
await page.setCookie({
|
|
|
|
name: 'cookie1',
|
|
|
|
value: '1'
|
|
|
|
}, {
|
|
|
|
name: 'cookie2',
|
|
|
|
value: '2'
|
|
|
|
}, {
|
|
|
|
name: 'cookie3',
|
|
|
|
value: '3'
|
|
|
|
});
|
|
|
|
expect(await page.evaluate('document.cookie')).toBe('cookie1=1; cookie2=2; cookie3=3');
|
|
|
|
await page.deleteCookie({name: 'cookie2'});
|
|
|
|
expect(await page.evaluate('document.cookie')).toBe('cookie1=1; cookie3=3');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-24 19:21:46 +00:00
|
|
|
|
2017-12-16 09:17:20 +00:00
|
|
|
it('should not set a cookie on a blank page', async function({page}) {
|
|
|
|
let error = null;
|
|
|
|
await page.goto('about:blank');
|
|
|
|
try {
|
|
|
|
await page.setCookie({name: 'example-cookie', value: 'best'});
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error).toBeTruthy();
|
|
|
|
expect(error.message).toEqual('Protocol error (Network.deleteCookies): At least one of the url and domain needs to be specified undefined');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not set a cookie with blank page URL', async function({page, server}) {
|
|
|
|
let error = null;
|
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
|
|
|
try {
|
|
|
|
await page.setCookie(
|
|
|
|
{name: 'example-cookie', value: 'best'},
|
|
|
|
{url: 'about:blank', name: 'example-cookie-blank', value: 'best'}
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error).toBeTruthy();
|
|
|
|
expect(error.message).toEqual(
|
|
|
|
`Blank page can not have cookie "example-cookie-blank"`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not set a cookie on a data URL page', async function({page}) {
|
|
|
|
let error = null;
|
|
|
|
await page.goto('data:,Hello%2C%20World!');
|
|
|
|
try {
|
|
|
|
await page.setCookie({name: 'example-cookie', value: 'best'});
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error).toBeTruthy();
|
|
|
|
expect(error.message).toEqual(
|
|
|
|
'Protocol error (Network.deleteCookies): At least one of the url and domain needs to be specified undefined'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not set a cookie with blank page URL', async function({page, server}) {
|
|
|
|
let error = null;
|
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
|
|
|
try {
|
|
|
|
await page.setCookie({name: 'example-cookie', value: 'best'}, {url: 'about:blank', name: 'example-cookie-blank', value: 'best'});
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error).toBeTruthy();
|
|
|
|
expect(error.message).toEqual(`Blank page can not have cookie "example-cookie-blank"`);
|
|
|
|
});
|
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should set a cookie on a different domain', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2017-08-24 19:21:46 +00:00
|
|
|
await page.setCookie({name: 'example-cookie', value: 'best', url: 'https://www.example.com'});
|
|
|
|
expect(await page.evaluate('document.cookie')).toBe('');
|
|
|
|
expect(await page.cookies()).toEqual([]);
|
|
|
|
expect(await page.cookies('https://www.example.com')).toEqual([{
|
|
|
|
name: 'example-cookie',
|
|
|
|
value: 'best',
|
|
|
|
domain: 'www.example.com',
|
|
|
|
path: '/',
|
2017-12-07 21:40:02 +00:00
|
|
|
expires: -1,
|
2017-08-24 19:21:46 +00:00
|
|
|
size: 18,
|
|
|
|
httpOnly: false,
|
|
|
|
secure: true,
|
|
|
|
session: true
|
|
|
|
}]);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-24 19:21:46 +00:00
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should set cookies from a frame', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2017-08-24 19:21:46 +00:00
|
|
|
await page.setCookie({name: 'localhost-cookie', value: 'best'});
|
|
|
|
await page.evaluate(src => {
|
|
|
|
let fulfill;
|
|
|
|
const promise = new Promise(x => fulfill = x);
|
|
|
|
const iframe = document.createElement('iframe');
|
|
|
|
document.body.appendChild(iframe);
|
|
|
|
iframe.onload = fulfill;
|
|
|
|
iframe.src = src;
|
|
|
|
return promise;
|
2017-12-12 21:34:21 +00:00
|
|
|
}, server.CROSS_PROCESS_PREFIX);
|
|
|
|
await page.setCookie({name: '127-cookie', value: 'worst', url: server.CROSS_PROCESS_PREFIX});
|
2017-08-24 19:21:46 +00:00
|
|
|
expect(await page.evaluate('document.cookie')).toBe('localhost-cookie=best');
|
|
|
|
expect(await page.frames()[1].evaluate('document.cookie')).toBe('127-cookie=worst');
|
|
|
|
|
|
|
|
expect(await page.cookies()).toEqual([{
|
|
|
|
name: 'localhost-cookie',
|
|
|
|
value: 'best',
|
|
|
|
domain: 'localhost',
|
|
|
|
path: '/',
|
2017-12-07 21:40:02 +00:00
|
|
|
expires: -1,
|
2017-08-24 19:21:46 +00:00
|
|
|
size: 20,
|
|
|
|
httpOnly: false,
|
|
|
|
secure: false,
|
|
|
|
session: true
|
|
|
|
}]);
|
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
expect(await page.cookies(server.CROSS_PROCESS_PREFIX)).toEqual([{
|
2017-08-24 19:21:46 +00:00
|
|
|
name: '127-cookie',
|
|
|
|
value: 'worst',
|
|
|
|
domain: '127.0.0.1',
|
|
|
|
path: '/',
|
2017-12-07 21:40:02 +00:00
|
|
|
expires: -1,
|
2017-08-24 19:21:46 +00:00
|
|
|
size: 15,
|
|
|
|
httpOnly: false,
|
|
|
|
secure: false,
|
|
|
|
session: true
|
|
|
|
}]);
|
|
|
|
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-08-24 19:21:46 +00:00
|
|
|
});
|
2017-10-18 02:14:57 +00:00
|
|
|
|
|
|
|
describe('Target', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('Browser.targets should return all of the targets', async({page, server, browser}) => {
|
2017-10-18 02:14:57 +00:00
|
|
|
// The pages will be the testing page and the original newtab page
|
|
|
|
const targets = browser.targets();
|
|
|
|
expect(targets.some(target => target.type() === 'page' &&
|
|
|
|
target.url() === 'about:blank')).toBeTruthy('Missing blank page');
|
|
|
|
expect(targets.some(target => target.type() === 'other' &&
|
|
|
|
target.url() === '')).toBeTruthy('Missing browser target');
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('Browser.pages should return all of the pages', async({page, server, browser}) => {
|
2017-10-18 02:14:57 +00:00
|
|
|
// The pages will be the testing page and the original newtab page
|
|
|
|
const allPages = await browser.pages();
|
|
|
|
expect(allPages.length).toBe(2);
|
|
|
|
expect(allPages).toContain(page);
|
|
|
|
expect(allPages[0]).not.toBe(allPages[1]);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should be able to use the default page in the browser', async({page, server, browser}) => {
|
2017-10-18 02:14:57 +00:00
|
|
|
// The pages will be the testing page and the original newtab page
|
|
|
|
const allPages = await browser.pages();
|
|
|
|
const originalPage = allPages.find(p => p !== page);
|
|
|
|
expect(await originalPage.evaluate(() => ['Hello', 'world'].join(' '))).toBe('Hello world');
|
|
|
|
expect(await originalPage.$('body')).toBeTruthy();
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should report when a new page is created and closed', async({page, server, browser}) => {
|
2017-10-18 02:14:57 +00:00
|
|
|
const otherPagePromise = new Promise(fulfill => browser.once('targetcreated', target => fulfill(target.page())));
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.evaluate(url => window.open(url), server.CROSS_PROCESS_PREFIX);
|
2017-10-18 02:14:57 +00:00
|
|
|
const otherPage = await otherPagePromise;
|
2017-12-12 21:34:21 +00:00
|
|
|
expect(otherPage.url()).toContain(server.CROSS_PROCESS_PREFIX);
|
2017-10-18 02:14:57 +00:00
|
|
|
|
|
|
|
expect(await otherPage.evaluate(() => ['Hello', 'world'].join(' '))).toBe('Hello world');
|
|
|
|
expect(await otherPage.$('body')).toBeTruthy();
|
|
|
|
|
|
|
|
let allPages = await browser.pages();
|
|
|
|
expect(allPages).toContain(page);
|
|
|
|
expect(allPages).toContain(otherPage);
|
|
|
|
|
|
|
|
const closePagePromise = new Promise(fulfill => browser.once('targetdestroyed', target => fulfill(target.page())));
|
|
|
|
await otherPage.close();
|
|
|
|
expect(await closePagePromise).toBe(otherPage);
|
|
|
|
|
|
|
|
allPages = await Promise.all(browser.targets().map(target => target.page()));
|
|
|
|
expect(allPages).toContain(page);
|
|
|
|
expect(allPages).not.toContain(otherPage);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should report when a service worker is created and destroyed', async({page, server, browser}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-10-18 02:14:57 +00:00
|
|
|
const createdTarget = new Promise(fulfill => browser.once('targetcreated', target => fulfill(target)));
|
2018-02-05 22:59:07 +00:00
|
|
|
await page.goto(server.PREFIX + '/serviceworkers/empty/sw.html');
|
2017-10-18 02:14:57 +00:00
|
|
|
|
|
|
|
expect((await createdTarget).type()).toBe('service_worker');
|
2018-02-05 22:59:07 +00:00
|
|
|
expect((await createdTarget).url()).toBe(server.PREFIX + '/serviceworkers/empty/sw.js');
|
2017-10-18 02:14:57 +00:00
|
|
|
|
|
|
|
const destroyedTarget = new Promise(fulfill => browser.once('targetdestroyed', target => fulfill(target)));
|
2018-02-05 22:59:07 +00:00
|
|
|
await page.evaluate(() => window.registrationPromise.then(registration => registration.unregister()));
|
2017-10-18 02:14:57 +00:00
|
|
|
expect(await destroyedTarget).toBe(await createdTarget);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should report when a target url changes', async({page, server, browser}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2017-10-18 02:14:57 +00:00
|
|
|
let changedTarget = new Promise(fulfill => browser.once('targetchanged', target => fulfill(target)));
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.CROSS_PROCESS_PREFIX + '/');
|
|
|
|
expect((await changedTarget).url()).toBe(server.CROSS_PROCESS_PREFIX + '/');
|
2017-10-18 02:14:57 +00:00
|
|
|
|
|
|
|
changedTarget = new Promise(fulfill => browser.once('targetchanged', target => fulfill(target)));
|
2017-12-12 21:34:21 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect((await changedTarget).url()).toBe(server.EMPTY_PAGE);
|
|
|
|
});
|
|
|
|
it('should not report uninitialized pages', async({page, server, browser}) => {
|
2017-12-28 02:38:33 +00:00
|
|
|
let targetChanged = false;
|
|
|
|
const listener = () => targetChanged = true;
|
|
|
|
browser.on('targetchanged', listener);
|
2017-10-18 02:14:57 +00:00
|
|
|
const targetPromise = new Promise(fulfill => browser.once('targetcreated', target => fulfill(target)));
|
|
|
|
const newPagePromise = browser.newPage();
|
|
|
|
const target = await targetPromise;
|
|
|
|
expect(target.url()).toBe('about:blank');
|
|
|
|
|
|
|
|
const newPage = await newPagePromise;
|
|
|
|
const targetPromise2 = new Promise(fulfill => browser.once('targetcreated', target => fulfill(target)));
|
|
|
|
const evaluatePromise = newPage.evaluate(() => window.open('about:blank'));
|
|
|
|
const target2 = await targetPromise2;
|
|
|
|
expect(target2.url()).toBe('about:blank');
|
|
|
|
await evaluatePromise;
|
|
|
|
await newPage.close();
|
2017-12-28 02:38:33 +00:00
|
|
|
expect(targetChanged).toBe(false, 'target should not be reported as changed');
|
|
|
|
browser.removeListener('targetchanged', listener);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
|
|
|
it('should not crash while redirecting if original request was missed', async({page, server, browser}) => {
|
2017-11-20 23:59:07 +00:00
|
|
|
let serverResponse = null;
|
|
|
|
server.setRoute('/one-style.css', (req, res) => serverResponse = res);
|
|
|
|
// Open a new page. Use window.open to connect to the page later.
|
|
|
|
await Promise.all([
|
2017-12-12 21:34:21 +00:00
|
|
|
page.evaluate(url => window.open(url), server.PREFIX + '/one-style.html'),
|
2017-11-20 23:59:07 +00:00
|
|
|
server.waitForRequest('/one-style.css')
|
|
|
|
]);
|
|
|
|
// Connect to the opened page.
|
|
|
|
const target = browser.targets().find(target => target.url().includes('one-style.html'));
|
|
|
|
const newPage = await target.page();
|
|
|
|
// Issue a redirect.
|
|
|
|
serverResponse.writeHead(302, { location: '/injectedstyle.css' });
|
|
|
|
serverResponse.end();
|
|
|
|
// Wait for the new page to load.
|
|
|
|
await waitForEvents(newPage, 'load');
|
|
|
|
// Cleanup.
|
|
|
|
await newPage.close();
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-10-18 02:14:57 +00:00
|
|
|
});
|
2017-12-09 03:05:46 +00:00
|
|
|
|
|
|
|
describe('Connection', function() {
|
2017-12-12 21:34:21 +00:00
|
|
|
it('should throw nice errors', async function({page}) {
|
2017-12-09 03:05:46 +00:00
|
|
|
const error = await theSourceOfTheProblems().catch(error => error);
|
|
|
|
expect(error.stack).toContain('theSourceOfTheProblems');
|
|
|
|
expect(error.message).toContain('ThisCommand.DoesNotExist');
|
|
|
|
async function theSourceOfTheProblems() {
|
|
|
|
await page._client.send('ThisCommand.DoesNotExist');
|
|
|
|
}
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-12-09 03:05:46 +00:00
|
|
|
});
|
2018-01-03 03:53:53 +00:00
|
|
|
|
2018-01-11 03:33:22 +00:00
|
|
|
describe('Target.createCDPSession', function() {
|
|
|
|
it('should work', async function({page, server}) {
|
|
|
|
const client = await page.target().createCDPSession();
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
client.send('Runtime.enable'),
|
|
|
|
client.send('Runtime.evaluate', { expression: 'window.foo = "bar"' })
|
|
|
|
]);
|
|
|
|
const foo = await page.evaluate(() => window.foo);
|
|
|
|
expect(foo).toBe('bar');
|
|
|
|
});
|
|
|
|
it('should send events', async function({page, server}) {
|
|
|
|
const client = await page.target().createCDPSession();
|
|
|
|
await client.send('Network.enable');
|
|
|
|
const events = [];
|
|
|
|
client.on('Network.requestWillBeSent', event => events.push(event));
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(events.length).toBe(1);
|
|
|
|
});
|
|
|
|
it('should enable and disable domains independently', async function({page, server}) {
|
|
|
|
const client = await page.target().createCDPSession();
|
|
|
|
await client.send('Runtime.enable');
|
|
|
|
await client.send('Debugger.enable');
|
|
|
|
// JS coverage enables and then disables Debugger domain.
|
|
|
|
await page.coverage.startJSCoverage();
|
|
|
|
await page.coverage.stopJSCoverage();
|
|
|
|
// generate a script in page and wait for the event.
|
|
|
|
const [event] = await Promise.all([
|
|
|
|
waitForEvents(client, 'Debugger.scriptParsed'),
|
|
|
|
page.evaluate('//# sourceURL=foo.js')
|
|
|
|
]);
|
|
|
|
// expect events to be dispatched.
|
|
|
|
expect(event.url).toBe('foo.js');
|
|
|
|
});
|
|
|
|
it('should be able to detach session', async function({page, server}) {
|
|
|
|
const client = await page.target().createCDPSession();
|
|
|
|
await client.send('Runtime.enable');
|
|
|
|
const evalResponse = await client.send('Runtime.evaluate', {expression: '1 + 2', returnByValue: true});
|
|
|
|
expect(evalResponse.result.value).toBe(3);
|
|
|
|
await client.detach();
|
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
await client.send('Runtime.evaluate', {expression: '3 + 1', returnByValue: true});
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error.message).toContain('Session closed.');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-01-03 03:53:53 +00:00
|
|
|
describe('JSCoverage', function() {
|
|
|
|
it('should work', async function({page, server}) {
|
|
|
|
await page.coverage.startJSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/jscoverage/simple.html');
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
|
|
|
expect(coverage.length).toBe(1);
|
|
|
|
expect(coverage[0].url).toContain('/jscoverage/simple.html');
|
|
|
|
expect(coverage[0].ranges).toEqual([
|
|
|
|
{ start: 0, end: 17 },
|
|
|
|
{ start: 35, end: 61 },
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
it('should report sourceURLs', async function({page, server}) {
|
|
|
|
await page.coverage.startJSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/jscoverage/sourceurl.html');
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
|
|
|
expect(coverage.length).toBe(1);
|
|
|
|
expect(coverage[0].url).toBe('nicename.js');
|
|
|
|
});
|
|
|
|
it('should ignore anonymous scripts', async function({page, server}) {
|
|
|
|
await page.coverage.startJSCoverage();
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.evaluate(() => console.log(1));
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
|
|
|
expect(coverage.length).toBe(0);
|
|
|
|
});
|
|
|
|
it('should report multiple scripts', async function({page, server}) {
|
|
|
|
await page.coverage.startJSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/jscoverage/multiple.html');
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
|
|
|
expect(coverage.length).toBe(2);
|
|
|
|
coverage.sort((a, b) => a.url.localeCompare(b.url));
|
|
|
|
expect(coverage[0].url).toContain('/jscoverage/script1.js');
|
|
|
|
expect(coverage[1].url).toContain('/jscoverage/script2.js');
|
|
|
|
});
|
|
|
|
it('should report right ranges', async function({page, server}) {
|
|
|
|
await page.coverage.startJSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/jscoverage/ranges.html');
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
|
|
|
expect(coverage.length).toBe(1);
|
|
|
|
const entry = coverage[0];
|
|
|
|
expect(entry.ranges.length).toBe(1);
|
|
|
|
const range = entry.ranges[0];
|
|
|
|
expect(entry.text.substring(range.start, range.end)).toBe(`console.log('used!');`);
|
|
|
|
});
|
|
|
|
it('should report scripts that have no coverage', async function({page, server}) {
|
|
|
|
await page.coverage.startJSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/jscoverage/unused.html');
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
|
|
|
expect(coverage.length).toBe(1);
|
|
|
|
const entry = coverage[0];
|
|
|
|
expect(entry.url).toContain('unused.html');
|
|
|
|
expect(entry.ranges.length).toBe(0);
|
|
|
|
});
|
|
|
|
it('should work with conditionals', async function({page, server}) {
|
|
|
|
await page.coverage.startJSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/jscoverage/involved.html');
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
2018-01-04 19:47:30 +00:00
|
|
|
expect(JSON.stringify(coverage, null, 2).replace(/:\d{4}\//g, ':<PORT>/')).toBeGolden('jscoverage-involved.txt');
|
2018-01-03 03:53:53 +00:00
|
|
|
});
|
|
|
|
describe('resetOnNavigation', function() {
|
|
|
|
it('should report scripts across navigations when disabled', async function({page, server}) {
|
|
|
|
await page.coverage.startJSCoverage({resetOnNavigation: false});
|
|
|
|
await page.goto(server.PREFIX + '/jscoverage/multiple.html');
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
|
|
|
expect(coverage.length).toBe(2);
|
|
|
|
});
|
|
|
|
it('should NOT report scripts across navigations when enabled', async function({page, server}) {
|
|
|
|
await page.coverage.startJSCoverage(); // Enabled by default.
|
|
|
|
await page.goto(server.PREFIX + '/jscoverage/multiple.html');
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
|
|
|
expect(coverage.length).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-01-04 02:21:40 +00:00
|
|
|
describe('CSSCoverage', function() {
|
|
|
|
it('should work', async function({page, server}) {
|
|
|
|
await page.coverage.startCSSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/csscoverage/simple.html');
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
|
|
|
expect(coverage.length).toBe(1);
|
|
|
|
expect(coverage[0].url).toContain('/csscoverage/simple.html');
|
|
|
|
expect(coverage[0].ranges).toEqual([
|
|
|
|
{start: 1, end: 22}
|
|
|
|
]);
|
|
|
|
const range = coverage[0].ranges[0];
|
|
|
|
expect(coverage[0].text.substring(range.start, range.end)).toBe('div { color: green; }');
|
|
|
|
});
|
|
|
|
it('should report sourceURLs', async function({page, server}) {
|
|
|
|
await page.coverage.startCSSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/csscoverage/sourceurl.html');
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
|
|
|
expect(coverage.length).toBe(1);
|
|
|
|
expect(coverage[0].url).toBe('nicename.css');
|
|
|
|
});
|
|
|
|
it('should report multiple stylesheets', async function({page, server}) {
|
|
|
|
await page.coverage.startCSSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/csscoverage/multiple.html');
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
|
|
|
expect(coverage.length).toBe(2);
|
|
|
|
coverage.sort((a, b) => a.url.localeCompare(b.url));
|
|
|
|
expect(coverage[0].url).toContain('/csscoverage/stylesheet1.css');
|
|
|
|
expect(coverage[1].url).toContain('/csscoverage/stylesheet2.css');
|
|
|
|
});
|
|
|
|
it('should report stylesheets that have no coverage', async function({page, server}) {
|
|
|
|
await page.coverage.startCSSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/csscoverage/unused.html');
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
|
|
|
expect(coverage.length).toBe(1);
|
|
|
|
expect(coverage[0].url).toBe('unused.css');
|
|
|
|
expect(coverage[0].ranges.length).toBe(0);
|
|
|
|
});
|
|
|
|
it('should work with media queries', async function({page, server}) {
|
|
|
|
await page.coverage.startCSSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/csscoverage/media.html');
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
|
|
|
expect(coverage.length).toBe(1);
|
|
|
|
expect(coverage[0].url).toContain('/csscoverage/media.html');
|
|
|
|
expect(coverage[0].ranges).toEqual([
|
|
|
|
{start: 17, end: 38}
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
it('should work with complicated usecases', async function({page, server}) {
|
|
|
|
await page.coverage.startCSSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/csscoverage/involved.html');
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
2018-01-04 19:47:30 +00:00
|
|
|
expect(JSON.stringify(coverage, null, 2).replace(/:\d{4}\//g, ':<PORT>/')).toBeGolden('csscoverage-involved.txt');
|
2018-01-04 02:21:40 +00:00
|
|
|
});
|
|
|
|
it('should ignore injected stylesheets', async function({page, server}) {
|
|
|
|
await page.coverage.startCSSCoverage();
|
|
|
|
await page.addStyleTag({content: 'body { margin: 10px;}'});
|
|
|
|
// trigger style recalc
|
|
|
|
const margin = await page.evaluate(() => window.getComputedStyle(document.body).margin);
|
|
|
|
expect(margin).toBe('10px');
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
|
|
|
expect(coverage.length).toBe(0);
|
|
|
|
});
|
|
|
|
describe('resetOnNavigation', function() {
|
|
|
|
it('should report stylesheets across navigations', async function({page, server}) {
|
|
|
|
await page.coverage.startCSSCoverage({resetOnNavigation: false});
|
|
|
|
await page.goto(server.PREFIX + '/csscoverage/multiple.html');
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
|
|
|
expect(coverage.length).toBe(2);
|
|
|
|
});
|
|
|
|
it('should NOT report scripts across navigations', async function({page, server}) {
|
|
|
|
await page.coverage.startCSSCoverage(); // Enabled by default.
|
|
|
|
await page.goto(server.PREFIX + '/csscoverage/multiple.html');
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
|
|
|
expect(coverage.length).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-05-12 23:36:37 +00:00
|
|
|
});
|
|
|
|
|
2017-07-27 23:16:37 +00:00
|
|
|
if (process.env.COVERAGE) {
|
2017-07-28 00:32:02 +00:00
|
|
|
describe('COVERAGE', function(){
|
2017-08-21 23:39:04 +00:00
|
|
|
const coverage = helper.publicAPICoverage();
|
2017-11-07 21:17:36 +00:00
|
|
|
const disabled = new Set(['page.bringToFront']);
|
2017-08-02 22:32:20 +00:00
|
|
|
if (!headless)
|
2017-07-27 23:16:37 +00:00
|
|
|
disabled.add('page.pdf');
|
|
|
|
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const method of coverage.keys()) {
|
2017-12-12 21:34:21 +00:00
|
|
|
(disabled.has(method) ? xit : it)(`public api '${method}' should be called`, async({page, server}) => {
|
2017-07-27 23:27:00 +00:00
|
|
|
expect(coverage.get(method)).toBe(true);
|
2017-12-12 21:34:21 +00:00
|
|
|
});
|
2017-07-27 23:16:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-12-08 00:37:22 +00:00
|
|
|
|
2017-12-28 23:41:57 +00:00
|
|
|
if (process.env.CI && runner.hasFocusedTestsOrSuites()) {
|
|
|
|
console.error('ERROR: "focused" tests/suites are prohibitted on bots. Remove any "fit"/"fdescribe" declarations.');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2017-12-08 00:37:22 +00:00
|
|
|
runner.run();
|
2017-07-17 08:57:37 +00:00
|
|
|
/**
|
|
|
|
* @param {!EventEmitter} emitter
|
|
|
|
* @param {string} eventName
|
|
|
|
* @param {number=} eventCount
|
2018-01-11 03:33:22 +00:00
|
|
|
* @return {!Promise<!Object>}
|
2017-07-17 08:57:37 +00:00
|
|
|
*/
|
|
|
|
function waitForEvents(emitter, eventName, eventCount = 1) {
|
|
|
|
let fulfill;
|
2017-08-21 23:39:04 +00:00
|
|
|
const promise = new Promise(x => fulfill = x);
|
2017-07-17 08:57:37 +00:00
|
|
|
emitter.on(eventName, onEvent);
|
|
|
|
return promise;
|
|
|
|
|
2018-01-11 03:33:22 +00:00
|
|
|
function onEvent(event) {
|
2017-07-17 08:57:37 +00:00
|
|
|
--eventCount;
|
|
|
|
if (eventCount)
|
|
|
|
return;
|
|
|
|
emitter.removeListener(eventName, onEvent);
|
2018-01-11 03:33:22 +00:00
|
|
|
fulfill(event);
|
2017-07-17 08:57:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 21:10:52 +00:00
|
|
|
/**
|
|
|
|
* @param {!Buffer} pdfBuffer
|
|
|
|
* @return {!Promise<!Array<!Object>>}
|
|
|
|
*/
|
|
|
|
async function getPDFPages(pdfBuffer) {
|
|
|
|
const PDFJS = require('pdfjs-dist');
|
|
|
|
PDFJS.disableWorker = true;
|
|
|
|
const data = new Uint8Array(pdfBuffer);
|
|
|
|
const doc = await PDFJS.getDocument(data);
|
2017-08-21 23:39:04 +00:00
|
|
|
const pages = [];
|
2017-08-03 21:10:52 +00:00
|
|
|
for (let i = 0; i < doc.numPages; ++i) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const page = await doc.getPage(i + 1);
|
|
|
|
const viewport = page.getViewport(1);
|
2017-08-03 21:10:52 +00:00
|
|
|
// Viewport width and height is in PDF points, which is
|
|
|
|
// 1/72 of an inch.
|
|
|
|
pages.push({
|
|
|
|
width: viewport.width / 72,
|
|
|
|
height: viewport.height / 72,
|
|
|
|
});
|
|
|
|
page.cleanup();
|
|
|
|
}
|
|
|
|
doc.cleanup();
|
|
|
|
return pages;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} px
|
|
|
|
* @return {number}
|
|
|
|
*/
|
|
|
|
function cssPixelsToInches(px) {
|
|
|
|
return px / 96;
|
|
|
|
}
|