2018-03-16 22:33:31 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2018-03-20 03:00:12 +00:00
|
|
|
const utils = require('./utils');
|
2018-08-09 23:51:12 +00:00
|
|
|
const {waitEvent} = utils;
|
2019-01-29 01:16:12 +00:00
|
|
|
const {TimeoutError} = utils.requireRoot('Errors');
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-08-09 23:51:12 +00:00
|
|
|
const DeviceDescriptors = utils.requireRoot('DeviceDescriptors');
|
|
|
|
const iPhone = DeviceDescriptors['iPhone 6'];
|
|
|
|
|
2018-11-01 23:43:21 +00:00
|
|
|
let asyncawait = true;
|
|
|
|
try {
|
|
|
|
new Function('async function foo() {await 1}');
|
|
|
|
} catch (e) {
|
|
|
|
asyncawait = false;
|
|
|
|
}
|
|
|
|
|
2018-08-09 23:51:12 +00:00
|
|
|
module.exports.addTests = function({testRunner, expect, headless}) {
|
2018-03-20 03:00:12 +00:00
|
|
|
const {describe, xdescribe, fdescribe} = testRunner;
|
|
|
|
const {it, fit, xit} = testRunner;
|
|
|
|
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
|
2018-04-09 23:38:00 +00:00
|
|
|
|
|
|
|
describe('Page.close', function() {
|
2018-08-01 22:49:41 +00:00
|
|
|
it('should reject all promises when page is closed', async({context}) => {
|
|
|
|
const newPage = await context.newPage();
|
2018-04-09 23:38:00 +00:00
|
|
|
const neverResolves = newPage.evaluate(() => new Promise(r => {}));
|
|
|
|
newPage.close();
|
|
|
|
let error = null;
|
|
|
|
await neverResolves.catch(e => error = e);
|
|
|
|
expect(error.message).toContain('Protocol error');
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should not be visible in browser.pages', async({browser}) => {
|
|
|
|
const newPage = await browser.newPage();
|
|
|
|
expect(await browser.pages()).toContain(newPage);
|
|
|
|
await newPage.close();
|
|
|
|
expect(await browser.pages()).not.toContain(newPage);
|
|
|
|
});
|
2018-08-01 22:49:41 +00:00
|
|
|
it('should run beforeunload if asked for', async({context, server}) => {
|
|
|
|
const newPage = await context.newPage();
|
2018-05-02 22:51:45 +00:00
|
|
|
await newPage.goto(server.PREFIX + '/beforeunload.html');
|
|
|
|
// We have to interact with a page so that 'beforeunload' handlers
|
|
|
|
// fire.
|
|
|
|
await newPage.click('body');
|
|
|
|
newPage.close({ runBeforeUnload: true });
|
|
|
|
const dialog = await waitEvent(newPage, 'dialog');
|
|
|
|
expect(dialog.type()).toBe('beforeunload');
|
|
|
|
expect(dialog.defaultValue()).toBe('');
|
|
|
|
expect(dialog.message()).toBe('');
|
|
|
|
dialog.accept();
|
|
|
|
await waitEvent(newPage, 'close');
|
|
|
|
});
|
2018-08-01 22:49:41 +00:00
|
|
|
it('should set the page close state', async({context}) => {
|
|
|
|
const newPage = await context.newPage();
|
2018-05-25 23:53:57 +00:00
|
|
|
expect(newPage.isClosed()).toBe(false);
|
|
|
|
await newPage.close();
|
|
|
|
expect(newPage.isClosed()).toBe(true);
|
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-09-19 20:54:58 +00:00
|
|
|
(asyncawait ? describe : xdescribe)('Async stacks', () => {
|
|
|
|
it('should work', async({page, server}) => {
|
|
|
|
server.setRoute('/empty.html', (req, res) => {
|
|
|
|
res.statusCode = 204;
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
let error = null;
|
|
|
|
await page.goto(server.EMPTY_PAGE).catch(e => error = e);
|
|
|
|
expect(error).not.toBe(null);
|
|
|
|
expect(error.message).toContain('net::ERR_ABORTED');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
describe('Page.Events.error', function() {
|
|
|
|
it('should throw when page crashes', async({page}) => {
|
|
|
|
let error = null;
|
|
|
|
page.on('error', err => error = err);
|
|
|
|
page.goto('chrome://crash').catch(e => {});
|
|
|
|
await waitEvent(page, 'error');
|
|
|
|
expect(error.message).toBe('Page crashed!');
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-12-13 01:09:42 +00:00
|
|
|
describe('Page.Events.Popup', function() {
|
|
|
|
it('should work', async({page}) => {
|
|
|
|
const [popup] = await Promise.all([
|
|
|
|
new Promise(x => page.once('popup', x)),
|
|
|
|
page.evaluate(() => window.open('about:blank')),
|
|
|
|
]);
|
|
|
|
expect(await page.evaluate(() => !!window.opener)).toBe(false);
|
|
|
|
expect(await popup.evaluate(() => !!window.opener)).toBe(true);
|
|
|
|
});
|
|
|
|
it('should work with noopener', async({page}) => {
|
|
|
|
const [popup] = await Promise.all([
|
|
|
|
new Promise(x => page.once('popup', x)),
|
|
|
|
page.evaluate(() => window.open('about:blank', null, 'noopener')),
|
|
|
|
]);
|
|
|
|
expect(await page.evaluate(() => !!window.opener)).toBe(false);
|
|
|
|
expect(await popup.evaluate(() => !!window.opener)).toBe(false);
|
|
|
|
});
|
|
|
|
it('should work with clicking target=_blank', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.setContent('<a target=_blank href="/one-style.html">yo</a>');
|
|
|
|
const [popup] = await Promise.all([
|
|
|
|
new Promise(x => page.once('popup', x)),
|
|
|
|
page.click('a'),
|
|
|
|
]);
|
|
|
|
expect(await page.evaluate(() => !!window.opener)).toBe(false);
|
|
|
|
expect(await popup.evaluate(() => !!window.opener)).toBe(true);
|
|
|
|
});
|
|
|
|
it('should work with fake-clicking target=_blank and rel=noopener', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.setContent('<a target=_blank rel=noopener href="/one-style.html">yo</a>');
|
|
|
|
const [popup] = await Promise.all([
|
|
|
|
new Promise(x => page.once('popup', x)),
|
|
|
|
page.$eval('a', a => a.click()),
|
|
|
|
]);
|
|
|
|
expect(await page.evaluate(() => !!window.opener)).toBe(false);
|
|
|
|
expect(await popup.evaluate(() => !!window.opener)).toBe(false);
|
|
|
|
});
|
|
|
|
it('should work with clicking target=_blank and rel=noopener', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.setContent('<a target=_blank rel=noopener href="/one-style.html">yo</a>');
|
|
|
|
const [popup] = await Promise.all([
|
|
|
|
new Promise(x => page.once('popup', x)),
|
|
|
|
page.click('a'),
|
|
|
|
]);
|
|
|
|
expect(await page.evaluate(() => !!window.opener)).toBe(false);
|
|
|
|
expect(await popup.evaluate(() => !!window.opener)).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-30 22:36:09 +00:00
|
|
|
describe('BrowserContext.overridePermissions', function() {
|
|
|
|
function getPermission(page, name) {
|
|
|
|
return page.evaluate(name => navigator.permissions.query({name}).then(result => result.state), name);
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should be prompt by default', async({page, server, context}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(await getPermission(page, 'geolocation')).toBe('prompt');
|
|
|
|
});
|
|
|
|
it('should deny permission when not listed', async({page, server, context}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await context.overridePermissions(server.EMPTY_PAGE, []);
|
|
|
|
expect(await getPermission(page, 'geolocation')).toBe('denied');
|
|
|
|
});
|
|
|
|
it('should fail when bad permission is given', async({page, server, context}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
let error = null;
|
|
|
|
await context.overridePermissions(server.EMPTY_PAGE, ['foo']).catch(e => error = e);
|
|
|
|
expect(error.message).toBe('Unknown permission: foo');
|
|
|
|
});
|
|
|
|
it('should grant permission when listed', async({page, server, context}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await context.overridePermissions(server.EMPTY_PAGE, ['geolocation']);
|
|
|
|
expect(await getPermission(page, 'geolocation')).toBe('granted');
|
|
|
|
});
|
|
|
|
it('should reset permissions', async({page, server, context}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await context.overridePermissions(server.EMPTY_PAGE, ['geolocation']);
|
|
|
|
expect(await getPermission(page, 'geolocation')).toBe('granted');
|
|
|
|
await context.clearPermissionOverrides();
|
|
|
|
expect(await getPermission(page, 'geolocation')).toBe('prompt');
|
|
|
|
});
|
|
|
|
it('should trigger permission onchange', async({page, server, context}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.evaluate(() => {
|
|
|
|
window.events = [];
|
|
|
|
return navigator.permissions.query({name: 'clipboard-read'}).then(function(result) {
|
|
|
|
window.events.push(result.state);
|
|
|
|
result.onchange = function() {
|
|
|
|
window.events.push(result.state);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
expect(await page.evaluate(() => window.events)).toEqual(['prompt']);
|
|
|
|
await context.overridePermissions(server.EMPTY_PAGE, []);
|
|
|
|
expect(await page.evaluate(() => window.events)).toEqual(['prompt', 'denied']);
|
|
|
|
await context.overridePermissions(server.EMPTY_PAGE, ['clipboard-read']);
|
|
|
|
expect(await page.evaluate(() => window.events)).toEqual(['prompt', 'denied', 'granted']);
|
|
|
|
await context.clearPermissionOverrides();
|
|
|
|
expect(await page.evaluate(() => window.events)).toEqual(['prompt', 'denied', 'granted', 'prompt']);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-31 17:04:12 +00:00
|
|
|
describe('Page.setGeolocation', function() {
|
|
|
|
it('should work', async({page, server, context}) => {
|
|
|
|
await context.overridePermissions(server.PREFIX, ['geolocation']);
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.setGeolocation({longitude: 10, latitude: 10});
|
|
|
|
const geolocation = await page.evaluate(() => new Promise(resolve => navigator.geolocation.getCurrentPosition(position => {
|
|
|
|
resolve({latitude: position.coords.latitude, longitude: position.coords.longitude});
|
|
|
|
})));
|
|
|
|
expect(geolocation).toEqual({
|
|
|
|
latitude: 10,
|
|
|
|
longitude: 10
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should throw when invalid longitude', async({page, server, context}) => {
|
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
await page.setGeolocation({longitude: 200, latitude: 10});
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error.message).toContain('Invalid longitude "200"');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
describe('Page.setOfflineMode', function() {
|
|
|
|
it('should work', async({page, server}) => {
|
|
|
|
await page.setOfflineMode(true);
|
|
|
|
let error = null;
|
|
|
|
await page.goto(server.EMPTY_PAGE).catch(e => error = e);
|
|
|
|
expect(error).toBeTruthy();
|
|
|
|
await page.setOfflineMode(false);
|
|
|
|
const response = await page.reload();
|
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
});
|
|
|
|
it('should emulate navigator.onLine', async({page, server}) => {
|
|
|
|
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);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
describe('ExecutionContext.queryObjects', function() {
|
|
|
|
it('should work', async({page, server}) => {
|
|
|
|
// 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']);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should fail for disposed handles', async({page, server}) => {
|
|
|
|
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!');
|
|
|
|
});
|
|
|
|
it('should fail primitive values as prototypes', async({page, server}) => {
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
describe('Page.Events.Console', function() {
|
|
|
|
it('should work', async({page, server}) => {
|
|
|
|
let message = null;
|
|
|
|
page.once('console', m => message = m);
|
|
|
|
await Promise.all([
|
|
|
|
page.evaluate(() => console.log('hello', 5, {foo: 'bar'})),
|
|
|
|
waitEvent(page, 'console')
|
|
|
|
]);
|
|
|
|
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'});
|
|
|
|
});
|
|
|
|
it('should work for different console API calls', async({page, server}) => {
|
|
|
|
const messages = [];
|
|
|
|
page.on('console', msg => messages.push(msg));
|
|
|
|
// All console events will be reported before `page.evaluate` is finished.
|
|
|
|
await 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');
|
|
|
|
console.log(Promise.resolve('should not wait until resolved!'));
|
|
|
|
});
|
|
|
|
expect(messages.map(msg => msg.type())).toEqual([
|
|
|
|
'timeEnd', 'trace', 'dir', 'warning', 'error', 'log'
|
|
|
|
]);
|
|
|
|
expect(messages[0].text()).toContain('calling console.time');
|
|
|
|
expect(messages.slice(1).map(msg => msg.text())).toEqual([
|
|
|
|
'calling console.trace',
|
|
|
|
'calling console.dir',
|
|
|
|
'calling console.warn',
|
|
|
|
'calling console.error',
|
|
|
|
'JSHandle@promise',
|
|
|
|
]);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should not fail for window object', async({page, server}) => {
|
|
|
|
let message = null;
|
|
|
|
page.once('console', msg => message = msg);
|
|
|
|
await Promise.all([
|
|
|
|
page.evaluate(() => console.error(window)),
|
|
|
|
waitEvent(page, 'console')
|
|
|
|
]);
|
|
|
|
expect(message.text()).toBe('JSHandle@object');
|
|
|
|
});
|
2018-04-28 04:40:09 +00:00
|
|
|
it('should trigger correct Log', async({page, server}) => {
|
|
|
|
await page.goto('about:blank');
|
2018-06-19 17:35:13 +00:00
|
|
|
const [message] = await Promise.all([
|
|
|
|
waitEvent(page, 'console'),
|
|
|
|
page.evaluate(async url => fetch(url).catch(e => {}), server.EMPTY_PAGE)
|
|
|
|
]);
|
2018-04-28 04:40:09 +00:00
|
|
|
expect(message.text()).toContain('No \'Access-Control-Allow-Origin\'');
|
|
|
|
expect(message.type()).toEqual('error');
|
|
|
|
});
|
2019-01-11 02:05:28 +00:00
|
|
|
it('should have location when fetch fails', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const [message] = await Promise.all([
|
2019-01-11 00:51:13 +00:00
|
|
|
waitEvent(page, 'console'),
|
2019-01-11 02:05:28 +00:00
|
|
|
page.setContent(`<script>fetch('http://wat');</script>`),
|
2019-01-11 00:51:13 +00:00
|
|
|
]);
|
|
|
|
expect(message.text()).toContain(`ERR_NAME_NOT_RESOLVED`);
|
|
|
|
expect(message.type()).toEqual('error');
|
|
|
|
expect(message.location()).toEqual({
|
|
|
|
url: 'http://wat/',
|
|
|
|
lineNumber: undefined
|
|
|
|
});
|
|
|
|
});
|
2019-01-11 02:05:28 +00:00
|
|
|
it('should have location for console API calls', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const [message] = await Promise.all([
|
2019-01-11 00:51:13 +00:00
|
|
|
waitEvent(page, 'console'),
|
2019-01-11 02:05:28 +00:00
|
|
|
page.goto(server.PREFIX + '/consolelog.html'),
|
2019-01-11 00:51:13 +00:00
|
|
|
]);
|
2019-01-11 02:05:28 +00:00
|
|
|
expect(message.text()).toBe('yellow');
|
|
|
|
expect(message.type()).toBe('log');
|
2019-01-11 00:51:13 +00:00
|
|
|
expect(message.location()).toEqual({
|
2019-01-11 02:05:28 +00:00
|
|
|
url: server.PREFIX + '/consolelog.html',
|
2019-01-11 00:51:13 +00:00
|
|
|
lineNumber: 7,
|
2019-01-11 02:05:28 +00:00
|
|
|
columnNumber: 14,
|
2019-01-11 00:51:13 +00:00
|
|
|
});
|
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
describe('Page.Events.DOMContentLoaded', function() {
|
|
|
|
it('should fire when expected', async({page, server}) => {
|
|
|
|
page.goto('about:blank');
|
|
|
|
await waitEvent(page, 'domcontentloaded');
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
describe('Page.metrics', function() {
|
|
|
|
it('should get metrics from a page', async({page, server}) => {
|
|
|
|
await page.goto('about:blank');
|
|
|
|
const metrics = await page.metrics();
|
|
|
|
checkMetrics(metrics);
|
|
|
|
});
|
|
|
|
it('metrics event fired on console.timeStamp', async({page, server}) => {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
function checkMetrics(metrics) {
|
|
|
|
const metricsToCheck = new Set([
|
|
|
|
'Timestamp',
|
|
|
|
'Documents',
|
|
|
|
'Frames',
|
|
|
|
'JSEventListeners',
|
|
|
|
'Nodes',
|
|
|
|
'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);
|
2018-03-16 22:33:31 +00:00
|
|
|
}
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(metricsToCheck.size).toBe(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-07-12 21:36:31 +00:00
|
|
|
describe('Page.waitForRequest', function() {
|
|
|
|
it('should work', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const [request] = await Promise.all([
|
2018-07-12 22:32:18 +00:00
|
|
|
page.waitForRequest(server.PREFIX + '/digits/2.png'),
|
|
|
|
page.evaluate(() => {
|
|
|
|
fetch('/digits/1.png');
|
|
|
|
fetch('/digits/2.png');
|
|
|
|
fetch('/digits/3.png');
|
|
|
|
})
|
2018-07-12 21:36:31 +00:00
|
|
|
]);
|
2018-07-12 22:32:18 +00:00
|
|
|
expect(request.url()).toBe(server.PREFIX + '/digits/2.png');
|
2018-07-12 21:36:31 +00:00
|
|
|
});
|
|
|
|
it('should work with predicate', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const [request] = await Promise.all([
|
2018-07-12 22:32:18 +00:00
|
|
|
page.waitForRequest(request => request.url() === server.PREFIX + '/digits/2.png'),
|
|
|
|
page.evaluate(() => {
|
|
|
|
fetch('/digits/1.png');
|
|
|
|
fetch('/digits/2.png');
|
|
|
|
fetch('/digits/3.png');
|
|
|
|
})
|
2018-07-12 21:36:31 +00:00
|
|
|
]);
|
2018-07-12 22:32:18 +00:00
|
|
|
expect(request.url()).toBe(server.PREFIX + '/digits/2.png');
|
2018-07-12 21:36:31 +00:00
|
|
|
});
|
2019-01-29 01:16:12 +00:00
|
|
|
it('should respect timeout', async({page, server}) => {
|
|
|
|
let error = null;
|
|
|
|
await page.waitForRequest(() => false, {timeout: 1}).catch(e => error = e);
|
|
|
|
expect(error).toBeInstanceOf(TimeoutError);
|
|
|
|
});
|
|
|
|
it('should respect default timeout', async({page, server}) => {
|
|
|
|
let error = null;
|
|
|
|
page.setDefaultTimeout(1);
|
|
|
|
await page.waitForRequest(() => false).catch(e => error = e);
|
|
|
|
expect(error).toBeInstanceOf(TimeoutError);
|
|
|
|
});
|
2018-07-12 21:36:31 +00:00
|
|
|
it('should work with no timeout', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const [request] = await Promise.all([
|
2018-07-12 22:32:18 +00:00
|
|
|
page.waitForRequest(server.PREFIX + '/digits/2.png', {timeout: 0}),
|
|
|
|
page.evaluate(() => setTimeout(() => {
|
|
|
|
fetch('/digits/1.png');
|
|
|
|
fetch('/digits/2.png');
|
|
|
|
fetch('/digits/3.png');
|
|
|
|
}, 50))
|
2018-07-12 21:36:31 +00:00
|
|
|
]);
|
2018-07-12 22:32:18 +00:00
|
|
|
expect(request.url()).toBe(server.PREFIX + '/digits/2.png');
|
2018-07-12 21:36:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Page.waitForResponse', function() {
|
|
|
|
it('should work', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const [response] = await Promise.all([
|
2018-07-12 22:32:18 +00:00
|
|
|
page.waitForResponse(server.PREFIX + '/digits/2.png'),
|
|
|
|
page.evaluate(() => {
|
|
|
|
fetch('/digits/1.png');
|
|
|
|
fetch('/digits/2.png');
|
|
|
|
fetch('/digits/3.png');
|
|
|
|
})
|
2018-07-12 21:36:31 +00:00
|
|
|
]);
|
2018-07-12 22:32:18 +00:00
|
|
|
expect(response.url()).toBe(server.PREFIX + '/digits/2.png');
|
2018-07-12 21:36:31 +00:00
|
|
|
});
|
2019-01-29 01:16:12 +00:00
|
|
|
it('should respect timeout', async({page, server}) => {
|
|
|
|
let error = null;
|
|
|
|
await page.waitForResponse(() => false, {timeout: 1}).catch(e => error = e);
|
|
|
|
expect(error).toBeInstanceOf(TimeoutError);
|
|
|
|
});
|
|
|
|
it('should respect default timeout', async({page, server}) => {
|
|
|
|
let error = null;
|
|
|
|
page.setDefaultTimeout(1);
|
|
|
|
await page.waitForResponse(() => false).catch(e => error = e);
|
|
|
|
expect(error).toBeInstanceOf(TimeoutError);
|
|
|
|
});
|
2018-07-12 21:36:31 +00:00
|
|
|
it('should work with predicate', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const [response] = await Promise.all([
|
2018-07-12 22:32:18 +00:00
|
|
|
page.waitForResponse(response => response.url() === server.PREFIX + '/digits/2.png'),
|
|
|
|
page.evaluate(() => {
|
|
|
|
fetch('/digits/1.png');
|
|
|
|
fetch('/digits/2.png');
|
|
|
|
fetch('/digits/3.png');
|
|
|
|
})
|
2018-07-12 21:36:31 +00:00
|
|
|
]);
|
2018-07-12 22:32:18 +00:00
|
|
|
expect(response.url()).toBe(server.PREFIX + '/digits/2.png');
|
2018-07-12 21:36:31 +00:00
|
|
|
});
|
|
|
|
it('should work with no timeout', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const [response] = await Promise.all([
|
2018-07-12 22:32:18 +00:00
|
|
|
page.waitForResponse(server.PREFIX + '/digits/2.png', {timeout: 0}),
|
|
|
|
page.evaluate(() => setTimeout(() => {
|
|
|
|
fetch('/digits/1.png');
|
|
|
|
fetch('/digits/2.png');
|
|
|
|
fetch('/digits/3.png');
|
|
|
|
}, 50))
|
2018-07-12 21:36:31 +00:00
|
|
|
]);
|
2018-07-12 22:32:18 +00:00
|
|
|
expect(response.url()).toBe(server.PREFIX + '/digits/2.png');
|
2018-07-12 21:36:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
describe('Page.exposeFunction', function() {
|
|
|
|
it('should work', async({page, server}) => {
|
|
|
|
await page.exposeFunction('compute', function(a, b) {
|
|
|
|
return a * b;
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
const result = await page.evaluate(async function() {
|
|
|
|
return await compute(9, 4);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(result).toBe(36);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-11-15 22:51:34 +00:00
|
|
|
it('should throw exception in page context', async({page, server}) => {
|
|
|
|
await page.exposeFunction('woof', function() {
|
|
|
|
throw new Error('WOOF WOOF');
|
|
|
|
});
|
|
|
|
const {message, stack} = await page.evaluate(async() => {
|
|
|
|
try {
|
|
|
|
await woof();
|
|
|
|
} catch (e) {
|
|
|
|
return {message: e.message, stack: e.stack};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(message).toBe('WOOF WOOF');
|
|
|
|
expect(stack).toContain(__filename);
|
|
|
|
});
|
|
|
|
it('should support throwing "null"', async({page, server}) => {
|
|
|
|
await page.exposeFunction('woof', function() {
|
|
|
|
throw null;
|
|
|
|
});
|
|
|
|
const thrown = await page.evaluate(async() => {
|
|
|
|
try {
|
|
|
|
await woof();
|
|
|
|
} catch (e) {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(thrown).toBe(null);
|
|
|
|
});
|
2018-08-30 00:02:53 +00:00
|
|
|
it('should be callable from-inside evaluateOnNewDocument', async({page, server}) => {
|
|
|
|
let called = false;
|
|
|
|
await page.exposeFunction('woof', function() {
|
|
|
|
called = true;
|
|
|
|
});
|
|
|
|
await page.evaluateOnNewDocument(() => woof());
|
|
|
|
await page.reload();
|
|
|
|
expect(called).toBe(true);
|
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should survive navigation', async({page, server}) => {
|
|
|
|
await page.exposeFunction('compute', function(a, b) {
|
|
|
|
return a * b;
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const result = await page.evaluate(async function() {
|
|
|
|
return await compute(9, 4);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(result).toBe(36);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should await returned promise', async({page, server}) => {
|
|
|
|
await page.exposeFunction('compute', function(a, b) {
|
|
|
|
return Promise.resolve(a * b);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
const result = await page.evaluate(async function() {
|
|
|
|
return await compute(3, 5);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(result).toBe(15);
|
|
|
|
});
|
|
|
|
it('should work on frames', async({page, server}) => {
|
|
|
|
await page.exposeFunction('compute', function(a, b) {
|
|
|
|
return Promise.resolve(a * b);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.PREFIX + '/frames/nested-frames.html');
|
|
|
|
const frame = page.frames()[1];
|
|
|
|
const result = await frame.evaluate(async function() {
|
|
|
|
return await compute(3, 5);
|
|
|
|
});
|
|
|
|
expect(result).toBe(15);
|
|
|
|
});
|
|
|
|
it('should work on frames before navigation', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/frames/nested-frames.html');
|
|
|
|
await page.exposeFunction('compute', function(a, b) {
|
|
|
|
return Promise.resolve(a * b);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
const frame = page.frames()[1];
|
|
|
|
const result = await frame.evaluate(async function() {
|
|
|
|
return await compute(3, 5);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(result).toBe(15);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
describe('Page.Events.PageError', function() {
|
|
|
|
it('should fire', async({page, server}) => {
|
|
|
|
let error = null;
|
|
|
|
page.once('pageerror', e => error = e);
|
|
|
|
await Promise.all([
|
|
|
|
page.goto(server.PREFIX + '/error.html'),
|
|
|
|
waitEvent(page, 'pageerror')
|
|
|
|
]);
|
|
|
|
expect(error.message).toContain('Fancy');
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
describe('Page.setUserAgent', function() {
|
|
|
|
it('should work', async({page, server}) => {
|
|
|
|
expect(await page.evaluate(() => navigator.userAgent)).toContain('Mozilla');
|
|
|
|
page.setUserAgent('foobar');
|
|
|
|
const [request] = await Promise.all([
|
|
|
|
server.waitForRequest('/empty.html'),
|
|
|
|
page.goto(server.EMPTY_PAGE),
|
|
|
|
]);
|
|
|
|
expect(request.headers['user-agent']).toBe('foobar');
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should emulate device user-agent', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/mobile.html');
|
|
|
|
expect(await page.evaluate(() => navigator.userAgent)).toContain('Chrome');
|
|
|
|
await page.setUserAgent(iPhone.userAgent);
|
|
|
|
expect(await page.evaluate(() => navigator.userAgent)).toContain('Safari');
|
|
|
|
});
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
describe('Page.setContent', function() {
|
|
|
|
const expectedOutput = '<html><head></head><body><div>hello</div></body></html>';
|
|
|
|
it('should work', async({page, server}) => {
|
|
|
|
await page.setContent('<div>hello</div>');
|
|
|
|
const result = await page.content();
|
|
|
|
expect(result).toBe(expectedOutput);
|
|
|
|
});
|
|
|
|
it('should work with doctype', async({page, server}) => {
|
|
|
|
const doctype = '<!DOCTYPE html>';
|
|
|
|
await page.setContent(`${doctype}<div>hello</div>`);
|
|
|
|
const result = await page.content();
|
|
|
|
expect(result).toBe(`${doctype}${expectedOutput}`);
|
|
|
|
});
|
|
|
|
it('should work with HTML 4 doctype', async({page, server}) => {
|
|
|
|
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>`);
|
|
|
|
const result = await page.content();
|
|
|
|
expect(result).toBe(`${doctype}${expectedOutput}`);
|
|
|
|
});
|
2019-01-29 01:16:12 +00:00
|
|
|
it('should respect timeout', async({page, server}) => {
|
|
|
|
const imgPath = '/img.png';
|
|
|
|
// stall for image
|
|
|
|
server.setRoute(imgPath, (req, res) => {});
|
|
|
|
let error = null;
|
|
|
|
await page.setContent(`<img src="${server.PREFIX + imgPath}"></img>`, {timeout: 1}).catch(e => error = e);
|
|
|
|
expect(error).toBeInstanceOf(TimeoutError);
|
|
|
|
});
|
|
|
|
it('should respect default navigation timeout', async({page, server}) => {
|
|
|
|
page.setDefaultNavigationTimeout(1);
|
|
|
|
const imgPath = '/img.png';
|
|
|
|
// stall for image
|
|
|
|
server.setRoute(imgPath, (req, res) => {});
|
|
|
|
let error = null;
|
|
|
|
await page.setContent(`<img src="${server.PREFIX + imgPath}"></img>`).catch(e => error = e);
|
|
|
|
expect(error).toBeInstanceOf(TimeoutError);
|
|
|
|
});
|
2018-11-20 23:32:46 +00:00
|
|
|
it('should await resources to load', async({page, server}) => {
|
|
|
|
const imgPath = '/img.png';
|
|
|
|
let imgResponse = null;
|
|
|
|
server.setRoute(imgPath, (req, res) => imgResponse = res);
|
|
|
|
let loaded = false;
|
|
|
|
const contentPromise = page.setContent(`<img src="${server.PREFIX + imgPath}"></img>`).then(() => loaded = true);
|
|
|
|
await server.waitForRequest(imgPath);
|
|
|
|
expect(loaded).toBe(false);
|
|
|
|
imgResponse.end();
|
|
|
|
await contentPromise;
|
|
|
|
});
|
2018-12-13 21:33:42 +00:00
|
|
|
it('should work fast enough', async({page, server}) => {
|
|
|
|
for (let i = 0; i < 20; ++i)
|
|
|
|
await page.setContent('<div>yo</div>');
|
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-04-06 23:35:50 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
describe('Page.setBypassCSP', function() {
|
|
|
|
it('should bypass CSP meta tag', async({page, server}) => {
|
|
|
|
// Make sure CSP prohibits addScriptTag.
|
|
|
|
await page.goto(server.PREFIX + '/csp.html');
|
|
|
|
await page.addScriptTag({content: 'window.__injected = 42;'}).catch(e => void e);
|
|
|
|
expect(await page.evaluate(() => window.__injected)).toBe(undefined);
|
|
|
|
|
|
|
|
// By-pass CSP and try one more time.
|
|
|
|
await page.setBypassCSP(true);
|
|
|
|
await page.reload();
|
|
|
|
await page.addScriptTag({content: 'window.__injected = 42;'});
|
|
|
|
expect(await page.evaluate(() => window.__injected)).toBe(42);
|
|
|
|
});
|
2018-04-06 23:35:50 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should bypass CSP header', async({page, server}) => {
|
|
|
|
// Make sure CSP prohibits addScriptTag.
|
|
|
|
server.setCSP('/empty.html', 'default-src "self"');
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.addScriptTag({content: 'window.__injected = 42;'}).catch(e => void e);
|
|
|
|
expect(await page.evaluate(() => window.__injected)).toBe(undefined);
|
|
|
|
|
|
|
|
// By-pass CSP and try one more time.
|
|
|
|
await page.setBypassCSP(true);
|
|
|
|
await page.reload();
|
|
|
|
await page.addScriptTag({content: 'window.__injected = 42;'});
|
|
|
|
expect(await page.evaluate(() => window.__injected)).toBe(42);
|
|
|
|
});
|
2018-04-06 23:35:50 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should bypass after cross-process navigation', async({page, server}) => {
|
|
|
|
await page.setBypassCSP(true);
|
|
|
|
await page.goto(server.PREFIX + '/csp.html');
|
|
|
|
await page.addScriptTag({content: 'window.__injected = 42;'});
|
|
|
|
expect(await page.evaluate(() => window.__injected)).toBe(42);
|
2018-04-06 23:35:50 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.CROSS_PROCESS_PREFIX + '/csp.html');
|
|
|
|
await page.addScriptTag({content: 'window.__injected = 42;'});
|
|
|
|
expect(await page.evaluate(() => window.__injected)).toBe(42);
|
2018-04-06 23:35:50 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-04-06 23:35:50 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
describe('Page.addScriptTag', function() {
|
|
|
|
it('should throw an error if no options are provided', async({page, server}) => {
|
|
|
|
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');
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should work with a url', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const scriptHandle = await page.addScriptTag({ url: '/injectedfile.js' });
|
|
|
|
expect(scriptHandle.asElement()).not.toBeNull();
|
|
|
|
expect(await page.evaluate(() => __injected)).toBe(42);
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should work with a url and type=module', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.addScriptTag({ url: '/es6/es6import.js', type: 'module' });
|
|
|
|
expect(await page.evaluate(() => __es6injected)).toBe(42);
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should work with a path and type=module', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.addScriptTag({ path: path.join(__dirname, 'assets/es6/es6pathimport.js'), type: 'module' });
|
|
|
|
await page.waitForFunction('window.__es6injected');
|
|
|
|
expect(await page.evaluate(() => __es6injected)).toBe(42);
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should work with a content and type=module', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.addScriptTag({ content: `import num from '/es6/es6module.js';window.__es6injected = num;`, type: 'module' });
|
|
|
|
await page.waitForFunction('window.__es6injected');
|
|
|
|
expect(await page.evaluate(() => __es6injected)).toBe(42);
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should throw an error if loading from url fail', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
await page.addScriptTag({ url: '/nonexistfile.js' });
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error.message).toBe('Loading script from /nonexistfile.js failed');
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should work with a path', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const scriptHandle = await page.addScriptTag({ path: path.join(__dirname, 'assets/injectedfile.js') });
|
|
|
|
expect(scriptHandle.asElement()).not.toBeNull();
|
|
|
|
expect(await page.evaluate(() => __injected)).toBe(42);
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should include sourcemap when path is provided', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
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'));
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should work with content', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const scriptHandle = await page.addScriptTag({ content: 'window.__injected = 35;' });
|
|
|
|
expect(scriptHandle.asElement()).not.toBeNull();
|
|
|
|
expect(await page.evaluate(() => __injected)).toBe(35);
|
|
|
|
});
|
2018-04-06 20:17:55 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should throw when added with content to the CSP page', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/csp.html');
|
|
|
|
let error = null;
|
|
|
|
await page.addScriptTag({ content: 'window.__injected = 35;' }).catch(e => error = e);
|
|
|
|
expect(error).toBeTruthy();
|
|
|
|
});
|
2018-04-06 20:17:55 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should throw when added with URL to the CSP page', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/csp.html');
|
|
|
|
let error = null;
|
|
|
|
await page.addScriptTag({ url: server.CROSS_PROCESS_PREFIX + '/injectedfile.js' }).catch(e => error = e);
|
|
|
|
expect(error).toBeTruthy();
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
describe('Page.addStyleTag', function() {
|
|
|
|
it('should throw an error if no options are provided', async({page, server}) => {
|
|
|
|
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');
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should work with a url', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const styleHandle = await page.addStyleTag({ url: '/injectedstyle.css' });
|
|
|
|
expect(styleHandle.asElement()).not.toBeNull();
|
|
|
|
expect(await page.evaluate(`window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')`)).toBe('rgb(255, 0, 0)');
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should throw an error if loading from url fail', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
await page.addStyleTag({ url: '/nonexistfile.js' });
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error.message).toBe('Loading style from /nonexistfile.js failed');
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should work with a path', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const styleHandle = await page.addStyleTag({ path: path.join(__dirname, 'assets/injectedstyle.css') });
|
|
|
|
expect(styleHandle.asElement()).not.toBeNull();
|
|
|
|
expect(await page.evaluate(`window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')`)).toBe('rgb(255, 0, 0)');
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should include sourcemap when path is provided', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
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'));
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should work with content', async({page, server}) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const styleHandle = await page.addStyleTag({ content: 'body { background-color: green; }' });
|
|
|
|
expect(styleHandle.asElement()).not.toBeNull();
|
|
|
|
expect(await page.evaluate(`window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')`)).toBe('rgb(0, 128, 0)');
|
|
|
|
});
|
2018-04-06 20:17:55 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should throw when added with content to the CSP page', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/csp.html');
|
|
|
|
let error = null;
|
|
|
|
await page.addStyleTag({ content: 'body { background-color: green; }' }).catch(e => error = e);
|
|
|
|
expect(error).toBeTruthy();
|
|
|
|
});
|
2018-04-06 20:17:55 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should throw when added with URL to the CSP page', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/csp.html');
|
|
|
|
let error = null;
|
|
|
|
await page.addStyleTag({ url: server.CROSS_PROCESS_PREFIX + '/injectedstyle.css' }).catch(e => error = e);
|
|
|
|
expect(error).toBeTruthy();
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
describe('Page.url', function() {
|
|
|
|
it('should work', async({page, server}) => {
|
|
|
|
expect(page.url()).toBe('about:blank');
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(page.url()).toBe(server.EMPTY_PAGE);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
describe('Page.setJavaScriptEnabled', function() {
|
|
|
|
it('should work', async({page, server}) => {
|
|
|
|
await page.setJavaScriptEnabled(false);
|
|
|
|
await page.goto('data:text/html, <script>var something = "forbidden"</script>');
|
|
|
|
let error = null;
|
|
|
|
await page.evaluate('something').catch(e => error = e);
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +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));
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
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);
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.setCacheEnabled(false);
|
|
|
|
await page.reload({waitUntil: 'networkidle2'});
|
|
|
|
expect(responses.get('one-style.css').fromCache()).toBe(false);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
// Printing to pdf is currently only supported in headless
|
|
|
|
(headless ? describe : xdescribe)('Page.pdf', function() {
|
|
|
|
it('should be able to save file', async({page, server}) => {
|
|
|
|
const outputFile = __dirname + '/assets/output.pdf';
|
|
|
|
await page.pdf({path: outputFile});
|
|
|
|
expect(fs.readFileSync(outputFile).byteLength).toBeGreaterThan(0);
|
|
|
|
fs.unlinkSync(outputFile);
|
|
|
|
});
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
describe('Page.title', function() {
|
|
|
|
it('should return the page title', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/button.html');
|
|
|
|
expect(await page.title()).toBe('Button test');
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
describe('Page.select', function() {
|
|
|
|
it('should select single option', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
|
|
|
await page.select('select', 'blue');
|
|
|
|
expect(await page.evaluate(() => result.onInput)).toEqual(['blue']);
|
|
|
|
expect(await page.evaluate(() => result.onChange)).toEqual(['blue']);
|
|
|
|
});
|
|
|
|
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']);
|
|
|
|
});
|
|
|
|
it('should select multiple options', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
|
|
|
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']);
|
|
|
|
});
|
|
|
|
it('should respect event bubbling', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
|
|
|
await page.select('select', 'blue');
|
|
|
|
expect(await page.evaluate(() => result.onBubblingInput)).toEqual(['blue']);
|
|
|
|
expect(await page.evaluate(() => result.onBubblingChange)).toEqual(['blue']);
|
|
|
|
});
|
|
|
|
it('should throw when element is not a <select>', async({page, server}) => {
|
|
|
|
let error = null;
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
|
|
|
await page.select('body', '').catch(e => error = e);
|
|
|
|
expect(error.message).toContain('Element is not a <select> element.');
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
it('should return [] on no matched values', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
|
|
|
const result = await page.select('select','42','abc');
|
|
|
|
expect(result).toEqual([]);
|
|
|
|
});
|
|
|
|
it('should return an array of matched values', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
it('should return an array of one element when multiple is not set', async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
|
|
|
const result = await page.select('select','42','blue','black','magenta');
|
|
|
|
expect(result.length).toEqual(1);
|
|
|
|
});
|
|
|
|
it('should return [] on no values',async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
|
|
|
const result = await page.select('select');
|
|
|
|
expect(result).toEqual([]);
|
|
|
|
});
|
|
|
|
it('should deselect all options when passed no values for a multiple select',async({page, server}) => {
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
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');
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
it('should throw if passed in non-strings', async({page, server}) => {
|
|
|
|
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');
|
|
|
|
});
|
2018-10-04 17:41:38 +00:00
|
|
|
// @see https://github.com/GoogleChrome/puppeteer/issues/3327
|
2019-01-22 22:55:33 +00:00
|
|
|
it('should work when re-defining top-level Event class', async({page, server}) => {
|
2018-10-04 17:41:38 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
|
|
|
await page.evaluate(() => window.Event = null);
|
|
|
|
await page.select('select', 'blue');
|
|
|
|
expect(await page.evaluate(() => result.onInput)).toEqual(['blue']);
|
|
|
|
expect(await page.evaluate(() => result.onChange)).toEqual(['blue']);
|
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
describe('Connection', function() {
|
|
|
|
it('should throw nice errors', async function({page}) {
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Page.Events.Close', function() {
|
2018-08-01 22:49:41 +00:00
|
|
|
it('should work with window.close', async function({ page, context, server }) {
|
|
|
|
const newPagePromise = new Promise(fulfill => context.once('targetcreated', target => fulfill(target.page())));
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.evaluate(() => window['newPage'] = window.open('about:blank'));
|
|
|
|
const newPage = await newPagePromise;
|
|
|
|
const closedPromise = new Promise(x => newPage.on('close', x));
|
|
|
|
await page.evaluate(() => window['newPage'].close());
|
|
|
|
await closedPromise;
|
|
|
|
});
|
2018-08-01 22:49:41 +00:00
|
|
|
it('should work with page.close', async function({ page, context, server }) {
|
|
|
|
const newPage = await context.newPage();
|
2018-04-09 23:38:00 +00:00
|
|
|
const closedPromise = new Promise(x => newPage.on('close', x));
|
|
|
|
await newPage.close();
|
|
|
|
await closedPromise;
|
2018-03-28 20:11:51 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-17 17:37:17 +00:00
|
|
|
|
|
|
|
describe('Page.browser', function() {
|
|
|
|
it('should return the correct browser instance', async function({ page, browser }) {
|
|
|
|
expect(page.browser()).toBe(browser);
|
|
|
|
});
|
|
|
|
});
|
2018-12-12 23:08:31 +00:00
|
|
|
|
|
|
|
describe('Page.browserContext', function() {
|
|
|
|
it('should return the correct browser instance', async function({page, context, browser}) {
|
|
|
|
expect(page.browserContext()).toBe(context);
|
|
|
|
});
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
};
|