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.
|
|
|
|
*/
|
2020-06-23 05:18:46 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2020-07-13 09:22:26 +00:00
|
|
|
import utils from './utils.js';
|
2020-05-07 10:54:55 +00:00
|
|
|
const { waitEvent } = utils;
|
2020-06-23 05:18:46 +00:00
|
|
|
import expect from 'expect';
|
|
|
|
import sinon from 'sinon';
|
|
|
|
import {
|
2020-05-07 10:54:55 +00:00
|
|
|
getTestState,
|
|
|
|
setupTestBrowserHooks,
|
|
|
|
setupTestPageAndContextHooks,
|
2020-06-23 05:18:46 +00:00
|
|
|
itFailsFirefox,
|
|
|
|
describeFailsFirefox,
|
2020-07-13 09:22:26 +00:00
|
|
|
} from './mocha-utils'; // eslint-disable-line import/extensions
|
2020-07-14 15:57:29 +00:00
|
|
|
import { Page, Metrics } from '../lib/cjs/puppeteer/common/Page.js';
|
|
|
|
import { JSHandle } from '../lib/cjs/puppeteer/common/JSHandle.js';
|
2018-08-09 23:51:12 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page', function () {
|
2020-04-09 05:56:25 +00:00
|
|
|
setupTestBrowserHooks();
|
|
|
|
setupTestPageAndContextHooks();
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.close', function () {
|
|
|
|
it('should reject all promises when page is closed', async () => {
|
|
|
|
const { context } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-08-01 22:49:41 +00:00
|
|
|
const newPage = await context.newPage();
|
2018-04-09 23:38:00 +00:00
|
|
|
let error = null;
|
2019-02-19 21:20:39 +00:00
|
|
|
await Promise.all([
|
2020-05-07 10:54:55 +00:00
|
|
|
newPage
|
2020-06-23 05:18:46 +00:00
|
|
|
.evaluate(() => new Promise(() => {}))
|
2020-05-07 10:54:55 +00:00
|
|
|
.catch((error_) => (error = error_)),
|
2019-02-19 21:20:39 +00:00
|
|
|
newPage.close(),
|
|
|
|
]);
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(error.message).toContain('Protocol error');
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should not be visible in browser.pages', async () => {
|
|
|
|
const { browser } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
const newPage = await browser.newPage();
|
|
|
|
expect(await browser.pages()).toContain(newPage);
|
|
|
|
await newPage.close();
|
|
|
|
expect(await browser.pages()).not.toContain(newPage);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox('should run beforeunload if asked for', async () => {
|
|
|
|
const { context, server, isChrome } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-08-01 22:49:41 +00:00
|
|
|
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');
|
2020-05-07 10:54:55 +00:00
|
|
|
const pageClosingPromise = newPage.close({ runBeforeUnload: true });
|
2018-05-02 22:51:45 +00:00
|
|
|
const dialog = await waitEvent(newPage, 'dialog');
|
|
|
|
expect(dialog.type()).toBe('beforeunload');
|
|
|
|
expect(dialog.defaultValue()).toBe('');
|
2020-05-07 10:54:55 +00:00
|
|
|
if (isChrome) expect(dialog.message()).toBe('');
|
2019-02-14 06:16:12 +00:00
|
|
|
else
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(dialog.message()).toBe(
|
|
|
|
'This page is asking you to confirm that you want to leave - data you have entered may not be saved.'
|
|
|
|
);
|
2019-02-14 06:16:12 +00:00
|
|
|
await dialog.accept();
|
|
|
|
await pageClosingPromise;
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox('should *not* run beforeunload by default', async () => {
|
|
|
|
const { context, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-14 06:16:12 +00:00
|
|
|
const newPage = await context.newPage();
|
|
|
|
await newPage.goto(server.PREFIX + '/beforeunload.html');
|
|
|
|
// We have to interact with a page so that 'beforeunload' handlers
|
|
|
|
// fire.
|
|
|
|
await newPage.click('body');
|
|
|
|
await newPage.close();
|
2018-05-02 22:51:45 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should set the page close state', async () => {
|
|
|
|
const { context } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-08-01 22:49:41 +00:00
|
|
|
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);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox('should terminate network waiters', async () => {
|
|
|
|
const { context, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-08-21 17:26:48 +00:00
|
|
|
const newPage = await context.newPage();
|
|
|
|
const results = await Promise.all([
|
2020-05-07 10:54:55 +00:00
|
|
|
newPage.waitForRequest(server.EMPTY_PAGE).catch((error) => error),
|
|
|
|
newPage.waitForResponse(server.EMPTY_PAGE).catch((error) => error),
|
|
|
|
newPage.close(),
|
2019-08-21 17:26:48 +00:00
|
|
|
]);
|
|
|
|
for (let i = 0; i < 2; i++) {
|
|
|
|
const message = results[i].message;
|
|
|
|
expect(message).toContain('Target closed');
|
|
|
|
expect(message).not.toContain('Timeout');
|
|
|
|
}
|
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.Events.Load', function () {
|
|
|
|
it('should fire when expected', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-02 19:55:15 +00:00
|
|
|
await Promise.all([
|
|
|
|
page.goto('about:blank'),
|
|
|
|
utils.waitEvent(page, 'load'),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-15 10:52:19 +00:00
|
|
|
// This test fails on Firefox on CI consistently but cannot be replicated
|
|
|
|
// locally. Skipping for now to unblock the Mitt release and given FF support
|
|
|
|
// isn't fully done yet but raising an issue to ask the FF folks to have a
|
|
|
|
// look at this.
|
|
|
|
describeFailsFirefox('removing and adding event handlers', () => {
|
|
|
|
it('should correctly fire event handlers as they are added and then removed', async () => {
|
|
|
|
const { page, server } = getTestState();
|
|
|
|
|
|
|
|
const handler = sinon.spy();
|
|
|
|
page.on('response', handler);
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(handler.callCount).toBe(1);
|
|
|
|
page.off('response', handler);
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
// Still one because we removed the handler.
|
|
|
|
expect(handler.callCount).toBe(1);
|
|
|
|
page.on('response', handler);
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
// Two now because we added the handler back.
|
|
|
|
expect(handler.callCount).toBe(2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('Page.Events.error', function () {
|
|
|
|
it('should throw when page crashes', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on('error', (err) => (error = err));
|
2020-06-23 05:18:46 +00:00
|
|
|
page.goto('chrome://crash').catch(() => {});
|
2018-04-09 23:38:00 +00:00
|
|
|
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
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('Page.Events.Popup', function () {
|
|
|
|
it('should work', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-12-13 01:09:42 +00:00
|
|
|
const [popup] = await Promise.all([
|
2020-06-23 05:18:46 +00:00
|
|
|
new Promise<Page>((x) => page.once('popup', x)),
|
2018-12-13 01:09:42 +00:00
|
|
|
page.evaluate(() => window.open('about:blank')),
|
|
|
|
]);
|
|
|
|
expect(await page.evaluate(() => !!window.opener)).toBe(false);
|
|
|
|
expect(await popup.evaluate(() => !!window.opener)).toBe(true);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with noopener', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-12-13 01:09:42 +00:00
|
|
|
const [popup] = await Promise.all([
|
2020-06-23 05:18:46 +00:00
|
|
|
new Promise<Page>((x) => page.once('popup', x)),
|
2018-12-13 01:09:42 +00:00
|
|
|
page.evaluate(() => window.open('about:blank', null, 'noopener')),
|
|
|
|
]);
|
|
|
|
expect(await page.evaluate(() => !!window.opener)).toBe(false);
|
|
|
|
expect(await popup.evaluate(() => !!window.opener)).toBe(false);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with clicking target=_blank', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-12-13 01:09:42 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.setContent('<a target=_blank href="/one-style.html">yo</a>');
|
|
|
|
const [popup] = await Promise.all([
|
2020-06-23 05:18:46 +00:00
|
|
|
new Promise<Page>((x) => page.once('popup', x)),
|
2018-12-13 01:09:42 +00:00
|
|
|
page.click('a'),
|
|
|
|
]);
|
|
|
|
expect(await page.evaluate(() => !!window.opener)).toBe(false);
|
|
|
|
expect(await popup.evaluate(() => !!window.opener)).toBe(true);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with fake-clicking target=_blank and rel=noopener', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-12-13 01:09:42 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.setContent(
|
|
|
|
'<a target=_blank rel=noopener href="/one-style.html">yo</a>'
|
|
|
|
);
|
2018-12-13 01:09:42 +00:00
|
|
|
const [popup] = await Promise.all([
|
2020-06-23 05:18:46 +00:00
|
|
|
new Promise<Page>((x) => page.once('popup', x)),
|
2020-07-02 09:09:34 +00:00
|
|
|
page.$eval('a', (a: HTMLAnchorElement) => a.click()),
|
2018-12-13 01:09:42 +00:00
|
|
|
]);
|
|
|
|
expect(await page.evaluate(() => !!window.opener)).toBe(false);
|
|
|
|
expect(await popup.evaluate(() => !!window.opener)).toBe(false);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with clicking target=_blank and rel=noopener', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-12-13 01:09:42 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.setContent(
|
|
|
|
'<a target=_blank rel=noopener href="/one-style.html">yo</a>'
|
|
|
|
);
|
2018-12-13 01:09:42 +00:00
|
|
|
const [popup] = await Promise.all([
|
2020-06-23 05:18:46 +00:00
|
|
|
new Promise<Page>((x) => page.once('popup', x)),
|
2018-12-13 01:09:42 +00:00
|
|
|
page.click('a'),
|
|
|
|
]);
|
|
|
|
expect(await page.evaluate(() => !!window.opener)).toBe(false);
|
|
|
|
expect(await popup.evaluate(() => !!window.opener)).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('BrowserContext.overridePermissions', function () {
|
2018-08-30 22:36:09 +00:00
|
|
|
function getPermission(page, name) {
|
2020-05-07 10:54:55 +00:00
|
|
|
return page.evaluate(
|
|
|
|
(name) =>
|
|
|
|
navigator.permissions.query({ name }).then((result) => result.state),
|
|
|
|
name
|
|
|
|
);
|
2018-08-30 22:36:09 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should be prompt by default', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-08-30 22:36:09 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(await getPermission(page, 'geolocation')).toBe('prompt');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox('should deny permission when not listed', async () => {
|
|
|
|
const { page, server, context } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-08-30 22:36:09 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await context.overridePermissions(server.EMPTY_PAGE, []);
|
|
|
|
expect(await getPermission(page, 'geolocation')).toBe('denied');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should fail when bad permission is given', async () => {
|
|
|
|
const { page, server, context } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-08-30 22:36:09 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await context
|
|
|
|
.overridePermissions(server.EMPTY_PAGE, ['foo'])
|
|
|
|
.catch((error_) => (error = error_));
|
2018-08-30 22:36:09 +00:00
|
|
|
expect(error.message).toBe('Unknown permission: foo');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox('should grant permission when listed', async () => {
|
|
|
|
const { page, server, context } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-08-30 22:36:09 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await context.overridePermissions(server.EMPTY_PAGE, ['geolocation']);
|
|
|
|
expect(await getPermission(page, 'geolocation')).toBe('granted');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox('should reset permissions', async () => {
|
|
|
|
const { page, server, context } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-08-30 22:36:09 +00:00
|
|
|
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');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox('should trigger permission onchange', async () => {
|
|
|
|
const { page, server, context } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-08-30 22:36:09 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.evaluate(() => {
|
2020-06-23 05:18:46 +00:00
|
|
|
globalThis.events = [];
|
2020-05-07 10:54:55 +00:00
|
|
|
return navigator.permissions
|
|
|
|
.query({ name: 'geolocation' })
|
|
|
|
.then(function (result) {
|
2020-06-23 05:18:46 +00:00
|
|
|
globalThis.events.push(result.state);
|
2020-05-07 10:54:55 +00:00
|
|
|
result.onchange = function () {
|
2020-06-23 05:18:46 +00:00
|
|
|
globalThis.events.push(result.state);
|
2020-05-07 10:54:55 +00:00
|
|
|
};
|
|
|
|
});
|
2018-08-30 22:36:09 +00:00
|
|
|
});
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.events)).toEqual(['prompt']);
|
2018-08-30 22:36:09 +00:00
|
|
|
await context.overridePermissions(server.EMPTY_PAGE, []);
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.events)).toEqual([
|
2020-05-07 10:54:55 +00:00
|
|
|
'prompt',
|
|
|
|
'denied',
|
|
|
|
]);
|
2019-02-23 07:59:32 +00:00
|
|
|
await context.overridePermissions(server.EMPTY_PAGE, ['geolocation']);
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.events)).toEqual([
|
2020-05-07 10:54:55 +00:00
|
|
|
'prompt',
|
|
|
|
'denied',
|
|
|
|
'granted',
|
|
|
|
]);
|
2019-02-23 07:59:32 +00:00
|
|
|
await context.clearPermissionOverrides();
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.events)).toEqual([
|
2020-05-07 10:54:55 +00:00
|
|
|
'prompt',
|
|
|
|
'denied',
|
|
|
|
'granted',
|
|
|
|
'prompt',
|
|
|
|
]);
|
2019-02-23 07:59:32 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox(
|
|
|
|
'should isolate permissions between browser contexs',
|
|
|
|
async () => {
|
|
|
|
const { page, server, context, browser } = getTestState();
|
|
|
|
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const otherContext = await browser.createIncognitoBrowserContext();
|
|
|
|
const otherPage = await otherContext.newPage();
|
|
|
|
await otherPage.goto(server.EMPTY_PAGE);
|
|
|
|
expect(await getPermission(page, 'geolocation')).toBe('prompt');
|
|
|
|
expect(await getPermission(otherPage, 'geolocation')).toBe('prompt');
|
|
|
|
|
|
|
|
await context.overridePermissions(server.EMPTY_PAGE, []);
|
|
|
|
await otherContext.overridePermissions(server.EMPTY_PAGE, [
|
|
|
|
'geolocation',
|
|
|
|
]);
|
|
|
|
expect(await getPermission(page, 'geolocation')).toBe('denied');
|
|
|
|
expect(await getPermission(otherPage, 'geolocation')).toBe('granted');
|
|
|
|
|
|
|
|
await context.clearPermissionOverrides();
|
|
|
|
expect(await getPermission(page, 'geolocation')).toBe('prompt');
|
|
|
|
expect(await getPermission(otherPage, 'geolocation')).toBe('granted');
|
|
|
|
|
|
|
|
await otherContext.close();
|
|
|
|
}
|
|
|
|
);
|
2018-08-30 22:36:09 +00:00
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.setGeolocation', function () {
|
|
|
|
itFailsFirefox('should work', async () => {
|
|
|
|
const { page, server, context } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-08-31 17:04:12 +00:00
|
|
|
await context.overridePermissions(server.PREFIX, ['geolocation']);
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
2018-08-31 17:04:12 +00:00
|
|
|
expect(geolocation).toEqual({
|
|
|
|
latitude: 10,
|
2020-05-07 10:54:55 +00:00
|
|
|
longitude: 10,
|
2018-08-31 17:04:12 +00:00
|
|
|
});
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw when invalid longitude', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-08-31 17:04:12 +00:00
|
|
|
let error = null;
|
|
|
|
try {
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.setGeolocation({ longitude: 200, latitude: 10 });
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error_) {
|
|
|
|
error = error_;
|
2018-08-31 17:04:12 +00:00
|
|
|
}
|
|
|
|
expect(error.message).toContain('Invalid longitude "200"');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('Page.setOfflineMode', function () {
|
|
|
|
it('should work', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.setOfflineMode(true);
|
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE).catch((error_) => (error = error_));
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(error).toBeTruthy();
|
|
|
|
await page.setOfflineMode(false);
|
|
|
|
const response = await page.reload();
|
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should emulate navigator.onLine', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +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);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('ExecutionContext.queryObjects', function () {
|
|
|
|
itFailsFirefox('should work', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
// Instantiate an object
|
2020-06-23 05:18:46 +00:00
|
|
|
await page.evaluate(() => (globalThis.set = new Set(['hello', 'world'])));
|
2018-04-09 23:38:00 +00:00
|
|
|
const prototypeHandle = await page.evaluateHandle(() => Set.prototype);
|
|
|
|
const objectsHandle = await page.queryObjects(prototypeHandle);
|
2020-05-07 10:54:55 +00:00
|
|
|
const count = await page.evaluate(
|
2020-07-10 10:52:13 +00:00
|
|
|
(objects: JSHandle[]) => objects.length,
|
2020-05-07 10:54:55 +00:00
|
|
|
objectsHandle
|
|
|
|
);
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(count).toBe(1);
|
2020-05-07 10:54:55 +00:00
|
|
|
const values = await page.evaluate(
|
|
|
|
(objects) => Array.from(objects[0].values()),
|
|
|
|
objectsHandle
|
|
|
|
);
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(values).toEqual(['hello', 'world']);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox('should work for non-blank page', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-04-18 17:03:06 +00:00
|
|
|
// Instantiate an object
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-06-23 05:18:46 +00:00
|
|
|
await page.evaluate(() => (globalThis.set = new Set(['hello', 'world'])));
|
2019-04-18 17:03:06 +00:00
|
|
|
const prototypeHandle = await page.evaluateHandle(() => Set.prototype);
|
|
|
|
const objectsHandle = await page.queryObjects(prototypeHandle);
|
2020-05-07 10:54:55 +00:00
|
|
|
const count = await page.evaluate(
|
2020-07-10 10:52:13 +00:00
|
|
|
(objects: JSHandle[]) => objects.length,
|
2020-05-07 10:54:55 +00:00
|
|
|
objectsHandle
|
|
|
|
);
|
2019-04-18 17:03:06 +00:00
|
|
|
expect(count).toBe(1);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should fail for disposed handles', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const prototypeHandle = await page.evaluateHandle(
|
|
|
|
() => HTMLBodyElement.prototype
|
|
|
|
);
|
2018-04-09 23:38:00 +00:00
|
|
|
await prototypeHandle.dispose();
|
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
|
|
|
.queryObjects(prototypeHandle)
|
|
|
|
.catch((error_) => (error = error_));
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(error.message).toBe('Prototype JSHandle is disposed!');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should fail primitive values as prototypes', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
const prototypeHandle = await page.evaluateHandle(() => 42);
|
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
|
|
|
.queryObjects(prototypeHandle)
|
|
|
|
.catch((error_) => (error = error_));
|
|
|
|
expect(error.message).toBe(
|
|
|
|
'Prototype JSHandle must not be referencing primitive value'
|
|
|
|
);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('Page.Events.Console', function () {
|
|
|
|
it('should work', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
let message = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
page.once('console', (m) => (message = m));
|
2018-04-09 23:38:00 +00:00
|
|
|
await Promise.all([
|
2020-05-07 10:54:55 +00:00
|
|
|
page.evaluate(() => console.log('hello', 5, { foo: 'bar' })),
|
|
|
|
waitEvent(page, 'console'),
|
2018-04-09 23:38:00 +00:00
|
|
|
]);
|
|
|
|
expect(message.text()).toEqual('hello 5 JSHandle@object');
|
|
|
|
expect(message.type()).toEqual('log');
|
2020-07-29 11:13:50 +00:00
|
|
|
expect(message.args()).toHaveLength(3);
|
|
|
|
expect(message.location()).toEqual({
|
|
|
|
url: expect.any(String),
|
|
|
|
lineNumber: expect.any(Number),
|
|
|
|
columnNumber: expect.any(Number),
|
|
|
|
});
|
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(await message.args()[0].jsonValue()).toEqual('hello');
|
|
|
|
expect(await message.args()[1].jsonValue()).toEqual(5);
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(await message.args()[2].jsonValue()).toEqual({ foo: 'bar' });
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work for different console API calls', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
const messages = [];
|
2020-05-07 10:54:55 +00:00
|
|
|
page.on('console', (msg) => messages.push(msg));
|
2018-04-09 23:38:00 +00:00
|
|
|
// 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!'));
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(messages.map((msg) => msg.type())).toEqual([
|
|
|
|
'timeEnd',
|
|
|
|
'trace',
|
|
|
|
'dir',
|
|
|
|
'warning',
|
|
|
|
'error',
|
|
|
|
'log',
|
2018-04-09 23:38:00 +00:00
|
|
|
]);
|
|
|
|
expect(messages[0].text()).toContain('calling console.time');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(messages.slice(1).map((msg) => msg.text())).toEqual([
|
2018-04-09 23:38:00 +00:00
|
|
|
'calling console.trace',
|
|
|
|
'calling console.dir',
|
|
|
|
'calling console.warn',
|
|
|
|
'calling console.error',
|
|
|
|
'JSHandle@promise',
|
|
|
|
]);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should not fail for window object', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
let message = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
page.once('console', (msg) => (message = msg));
|
2018-04-09 23:38:00 +00:00
|
|
|
await Promise.all([
|
|
|
|
page.evaluate(() => console.error(window)),
|
2020-05-07 10:54:55 +00:00
|
|
|
waitEvent(page, 'console'),
|
2018-04-09 23:38:00 +00:00
|
|
|
]);
|
|
|
|
expect(message.text()).toBe('JSHandle@object');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should trigger correct Log', async () => {
|
|
|
|
const { page, server, isChrome } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-28 04:40:09 +00:00
|
|
|
await page.goto('about:blank');
|
2018-06-19 17:35:13 +00:00
|
|
|
const [message] = await Promise.all([
|
|
|
|
waitEvent(page, 'console'),
|
2020-05-07 10:54:55 +00:00
|
|
|
page.evaluate(
|
2020-07-10 10:52:13 +00:00
|
|
|
async (url: string) => fetch(url).catch(() => {}),
|
2020-05-07 10:54:55 +00:00
|
|
|
server.EMPTY_PAGE
|
|
|
|
),
|
2018-06-19 17:35:13 +00:00
|
|
|
]);
|
2019-02-14 07:22:45 +00:00
|
|
|
expect(message.text()).toContain('Access-Control-Allow-Origin');
|
2020-05-07 10:54:55 +00:00
|
|
|
if (isChrome) expect(message.type()).toEqual('error');
|
|
|
|
else expect(message.type()).toEqual('warn');
|
2018-04-28 04:40:09 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should have location when fetch fails', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-04-08 19:11:35 +00:00
|
|
|
// The point of this test is to make sure that we report console messages from
|
|
|
|
// Log domain: https://vanilla.aslushnikov.com/?Log.entryAdded
|
2019-01-11 02:05:28 +00:00
|
|
|
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/',
|
2020-05-07 10:54:55 +00:00
|
|
|
lineNumber: undefined,
|
2019-01-11 00:51:13 +00:00
|
|
|
});
|
|
|
|
});
|
2020-09-25 13:27:13 +00:00
|
|
|
it('should have location and stack trace for console API calls', async () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { page, server, isChrome } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-11 02:05:28 +00:00
|
|
|
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',
|
2020-09-25 13:27:13 +00:00
|
|
|
lineNumber: 8,
|
|
|
|
columnNumber: isChrome ? 16 : 8, // console.|log vs |console.log
|
2019-01-11 00:51:13 +00:00
|
|
|
});
|
2020-09-25 13:27:13 +00:00
|
|
|
expect(message.stackTrace()).toEqual([
|
|
|
|
{
|
|
|
|
url: server.PREFIX + '/consolelog.html',
|
|
|
|
lineNumber: 8,
|
|
|
|
columnNumber: isChrome ? 16 : 8, // console.|log vs |console.log
|
|
|
|
},
|
|
|
|
{
|
|
|
|
url: server.PREFIX + '/consolelog.html',
|
|
|
|
lineNumber: 11,
|
|
|
|
columnNumber: 8,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
url: server.PREFIX + '/consolelog.html',
|
|
|
|
lineNumber: 13,
|
|
|
|
columnNumber: 6,
|
|
|
|
},
|
|
|
|
]);
|
2019-01-11 00:51:13 +00:00
|
|
|
});
|
2019-11-26 12:12:25 +00:00
|
|
|
// @see https://github.com/puppeteer/puppeteer/issues/3865
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should not throw when there are console messages in detached iframes', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-31 00:19:02 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.evaluate(async () => {
|
2019-01-31 00:19:02 +00:00
|
|
|
// 1. Create a popup that Puppeteer is not connected to.
|
2020-05-07 10:54:55 +00:00
|
|
|
const win = window.open(
|
|
|
|
window.location.href,
|
|
|
|
'Title',
|
|
|
|
'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=780,height=200,top=0,left=0'
|
|
|
|
);
|
|
|
|
await new Promise((x) => (win.onload = x));
|
2019-01-31 00:19:02 +00:00
|
|
|
// 2. In this popup, create an iframe that console.logs a message.
|
|
|
|
win.document.body.innerHTML = `<iframe src='/consolelog.html'></iframe>`;
|
|
|
|
const frame = win.document.querySelector('iframe');
|
2020-05-07 10:54:55 +00:00
|
|
|
await new Promise((x) => (frame.onload = x));
|
2019-01-31 00:19:02 +00:00
|
|
|
// 3. After that, remove the iframe.
|
|
|
|
frame.remove();
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
const popupTarget = page
|
|
|
|
.browserContext()
|
|
|
|
.targets()
|
|
|
|
.find((target) => target !== page.target());
|
2019-01-31 00:19:02 +00:00
|
|
|
// 4. Connect to the popup and make sure it doesn't throw.
|
|
|
|
await popupTarget.page();
|
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.Events.DOMContentLoaded', function () {
|
|
|
|
it('should fire when expected', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
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
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('Page.metrics', function () {
|
|
|
|
it('should get metrics from a page', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto('about:blank');
|
|
|
|
const metrics = await page.metrics();
|
|
|
|
checkMetrics(metrics);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('metrics event fired on console.timeStamp', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
const metricsPromise = new Promise<{ metrics: Metrics; title: string }>(
|
|
|
|
(fulfill) => page.once('metrics', fulfill)
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2018-04-09 23:38:00 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.waitForRequest', function () {
|
|
|
|
it('should work', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-07-12 21:36:31 +00:00
|
|
|
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');
|
2020-05-07 10:54:55 +00:00
|
|
|
}),
|
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
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with predicate', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-07-12 21:36:31 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const [request] = await Promise.all([
|
2020-05-07 10:54:55 +00:00
|
|
|
page.waitForRequest(
|
|
|
|
(request) => request.url() === server.PREFIX + '/digits/2.png'
|
|
|
|
),
|
2018-07-12 22:32:18 +00:00
|
|
|
page.evaluate(() => {
|
|
|
|
fetch('/digits/1.png');
|
|
|
|
fetch('/digits/2.png');
|
|
|
|
fetch('/digits/3.png');
|
2020-05-07 10:54:55 +00:00
|
|
|
}),
|
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
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should respect timeout', async () => {
|
|
|
|
const { page, puppeteer } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-29 01:16:12 +00:00
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
|
|
|
.waitForRequest(() => false, { timeout: 1 })
|
|
|
|
.catch((error_) => (error = error_));
|
2019-04-19 22:33:06 +00:00
|
|
|
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError);
|
2019-01-29 01:16:12 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should respect default timeout', async () => {
|
|
|
|
const { page, puppeteer } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-29 01:16:12 +00:00
|
|
|
let error = null;
|
|
|
|
page.setDefaultTimeout(1);
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
|
|
|
.waitForRequest(() => false)
|
|
|
|
.catch((error_) => (error = error_));
|
2019-04-19 22:33:06 +00:00
|
|
|
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError);
|
2019-01-29 01:16:12 +00:00
|
|
|
});
|
2020-11-25 10:35:47 +00:00
|
|
|
it('should work with async predicate', async () => {
|
|
|
|
const { page, server } = getTestState();
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const [response] = await Promise.all([
|
|
|
|
page.waitForResponse(async (response) => {
|
|
|
|
console.log(response.url());
|
|
|
|
return response.url() === server.PREFIX + '/digits/2.png';
|
|
|
|
}),
|
|
|
|
page.evaluate(() => {
|
|
|
|
fetch('/digits/1.png');
|
|
|
|
fetch('/digits/2.png');
|
|
|
|
fetch('/digits/3.png');
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
expect(response.url()).toBe(server.PREFIX + '/digits/2.png');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with no timeout', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-07-12 21:36:31 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const [request] = await Promise.all([
|
2020-05-07 10:54:55 +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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.waitForResponse', function () {
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should work', async () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-07-12 21:36:31 +00:00
|
|
|
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');
|
2020-05-07 10:54:55 +00:00
|
|
|
}),
|
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
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should respect timeout', async () => {
|
|
|
|
const { page, puppeteer } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-29 01:16:12 +00:00
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
|
|
|
.waitForResponse(() => false, { timeout: 1 })
|
|
|
|
.catch((error_) => (error = error_));
|
2019-04-19 22:33:06 +00:00
|
|
|
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError);
|
2019-01-29 01:16:12 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should respect default timeout', async () => {
|
|
|
|
const { page, puppeteer } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-29 01:16:12 +00:00
|
|
|
let error = null;
|
|
|
|
page.setDefaultTimeout(1);
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
|
|
|
.waitForResponse(() => false)
|
|
|
|
.catch((error_) => (error = error_));
|
2019-04-19 22:33:06 +00:00
|
|
|
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError);
|
2019-01-29 01:16:12 +00:00
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should work with predicate', async () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-07-12 21:36:31 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const [response] = await Promise.all([
|
2020-05-07 10:54:55 +00:00
|
|
|
page.waitForResponse(
|
|
|
|
(response) => response.url() === server.PREFIX + '/digits/2.png'
|
|
|
|
),
|
2018-07-12 22:32:18 +00:00
|
|
|
page.evaluate(() => {
|
|
|
|
fetch('/digits/1.png');
|
|
|
|
fetch('/digits/2.png');
|
|
|
|
fetch('/digits/3.png');
|
2020-05-07 10:54:55 +00:00
|
|
|
}),
|
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
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should work with no timeout', async () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-07-12 21:36:31 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const [response] = await Promise.all([
|
2020-05-07 10:54:55 +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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('Page.exposeFunction', function () {
|
|
|
|
it('should work', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.exposeFunction('compute', function (a, b) {
|
2018-04-09 23:38:00 +00:00
|
|
|
return a * b;
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
const result = await page.evaluate(async function () {
|
2020-06-23 05:18:46 +00:00
|
|
|
return await globalThis.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
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw exception in page context', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.exposeFunction('woof', function () {
|
2018-11-15 22:51:34 +00:00
|
|
|
throw new Error('WOOF WOOF');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
const { message, stack } = await page.evaluate(async () => {
|
2018-11-15 22:51:34 +00:00
|
|
|
try {
|
2020-06-23 05:18:46 +00:00
|
|
|
await globalThis.woof();
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error) {
|
2020-05-07 10:54:55 +00:00
|
|
|
return { message: error.message, stack: error.stack };
|
2018-11-15 22:51:34 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(message).toBe('WOOF WOOF');
|
|
|
|
expect(stack).toContain(__filename);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should support throwing "null"', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.exposeFunction('woof', function () {
|
2018-11-15 22:51:34 +00:00
|
|
|
throw null;
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
const thrown = await page.evaluate(async () => {
|
2018-11-15 22:51:34 +00:00
|
|
|
try {
|
2020-06-23 05:18:46 +00:00
|
|
|
await globalThis.woof();
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error) {
|
|
|
|
return error;
|
2018-11-15 22:51:34 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(thrown).toBe(null);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should be callable from-inside evaluateOnNewDocument', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-08-30 00:02:53 +00:00
|
|
|
let called = false;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.exposeFunction('woof', function () {
|
2018-08-30 00:02:53 +00:00
|
|
|
called = true;
|
|
|
|
});
|
2020-06-23 05:18:46 +00:00
|
|
|
await page.evaluateOnNewDocument(() => globalThis.woof());
|
2018-08-30 00:02:53 +00:00
|
|
|
await page.reload();
|
|
|
|
expect(called).toBe(true);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should survive navigation', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.exposeFunction('compute', function (a, b) {
|
2018-04-09 23:38:00 +00:00
|
|
|
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);
|
2020-05-07 10:54:55 +00:00
|
|
|
const result = await page.evaluate(async function () {
|
2020-06-23 05:18:46 +00:00
|
|
|
return await globalThis.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
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should await returned promise', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.exposeFunction('compute', function (a, b) {
|
2018-04-09 23:38:00 +00:00
|
|
|
return Promise.resolve(a * b);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const result = await page.evaluate(async function () {
|
2020-06-23 05:18:46 +00:00
|
|
|
return await globalThis.compute(3, 5);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(result).toBe(15);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work on frames', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.exposeFunction('compute', function (a, b) {
|
2018-04-09 23:38:00 +00:00
|
|
|
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];
|
2020-05-07 10:54:55 +00:00
|
|
|
const result = await frame.evaluate(async function () {
|
2020-06-23 05:18:46 +00:00
|
|
|
return await globalThis.compute(3, 5);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
|
|
|
expect(result).toBe(15);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work on frames before navigation', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.PREFIX + '/frames/nested-frames.html');
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.exposeFunction('compute', function (a, b) {
|
2018-04-09 23:38:00 +00:00
|
|
|
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];
|
2020-05-07 10:54:55 +00:00
|
|
|
const result = await frame.evaluate(async function () {
|
2020-06-23 05:18:46 +00:00
|
|
|
return await globalThis.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
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with complex objects', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.exposeFunction('complexObject', function (a, b) {
|
|
|
|
return { x: a.x + b.x };
|
2019-05-10 01:02:46 +00:00
|
|
|
});
|
2020-07-10 10:52:13 +00:00
|
|
|
const result = await page.evaluate<() => Promise<{ x: number }>>(
|
|
|
|
async () => globalThis.complexObject({ x: 5 }, { x: 2 })
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2019-05-10 01:02:46 +00:00
|
|
|
expect(result.x).toBe(7);
|
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('Page.Events.PageError', function () {
|
|
|
|
it('should fire', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
page.once('pageerror', (e) => (error = e));
|
2018-04-09 23:38:00 +00:00
|
|
|
await Promise.all([
|
|
|
|
page.goto(server.PREFIX + '/error.html'),
|
2020-05-07 10:54:55 +00:00
|
|
|
waitEvent(page, 'pageerror'),
|
2018-04-09 23:38:00 +00:00
|
|
|
]);
|
|
|
|
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
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.setUserAgent', function () {
|
2020-05-14 09:24:30 +00:00
|
|
|
it('should work', async () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(await page.evaluate(() => navigator.userAgent)).toContain(
|
|
|
|
'Mozilla'
|
|
|
|
);
|
2019-02-09 03:53:27 +00:00
|
|
|
await page.setUserAgent('foobar');
|
2018-04-09 23:38:00 +00:00
|
|
|
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
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should work for subframes', async () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(await page.evaluate(() => navigator.userAgent)).toContain(
|
|
|
|
'Mozilla'
|
|
|
|
);
|
2019-02-09 03:53:27 +00:00
|
|
|
await page.setUserAgent('foobar');
|
|
|
|
const [request] = await Promise.all([
|
|
|
|
server.waitForRequest('/empty.html'),
|
|
|
|
utils.attachFrame(page, 'frame1', server.EMPTY_PAGE),
|
|
|
|
]);
|
|
|
|
expect(request.headers['user-agent']).toBe('foobar');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should emulate device user-agent', async () => {
|
|
|
|
const { page, server, puppeteer } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.PREFIX + '/mobile.html');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(await page.evaluate(() => navigator.userAgent)).not.toContain(
|
|
|
|
'iPhone'
|
|
|
|
);
|
2019-04-19 22:33:06 +00:00
|
|
|
await page.setUserAgent(puppeteer.devices['iPhone 6'].userAgent);
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(await page.evaluate(() => navigator.userAgent)).toContain(
|
|
|
|
'iPhone'
|
|
|
|
);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
describe('Page.setContent', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
const expectedOutput =
|
|
|
|
'<html><head></head><body><div>hello</div></body></html>';
|
|
|
|
it('should work', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.setContent('<div>hello</div>');
|
|
|
|
const result = await page.content();
|
|
|
|
expect(result).toBe(expectedOutput);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with doctype', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
const doctype = '<!DOCTYPE html>';
|
|
|
|
await page.setContent(`${doctype}<div>hello</div>`);
|
|
|
|
const result = await page.content();
|
|
|
|
expect(result).toBe(`${doctype}${expectedOutput}`);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with HTML 4 doctype', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const doctype =
|
|
|
|
'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" ' +
|
2018-04-09 23:38:00 +00:00
|
|
|
'"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}`);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should respect timeout', async () => {
|
|
|
|
const { page, server, puppeteer } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-29 01:16:12 +00:00
|
|
|
const imgPath = '/img.png';
|
|
|
|
// stall for image
|
2020-06-23 05:18:46 +00:00
|
|
|
server.setRoute(imgPath, () => {});
|
2019-01-29 01:16:12 +00:00
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
|
|
|
.setContent(`<img src="${server.PREFIX + imgPath}"></img>`, {
|
|
|
|
timeout: 1,
|
|
|
|
})
|
|
|
|
.catch((error_) => (error = error_));
|
2019-04-19 22:33:06 +00:00
|
|
|
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError);
|
2019-01-29 01:16:12 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should respect default navigation timeout', async () => {
|
|
|
|
const { page, server, puppeteer } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-29 01:16:12 +00:00
|
|
|
page.setDefaultNavigationTimeout(1);
|
|
|
|
const imgPath = '/img.png';
|
|
|
|
// stall for image
|
2020-06-23 05:18:46 +00:00
|
|
|
server.setRoute(imgPath, () => {});
|
2019-01-29 01:16:12 +00:00
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
|
|
|
.setContent(`<img src="${server.PREFIX + imgPath}"></img>`)
|
|
|
|
.catch((error_) => (error = error_));
|
2019-04-19 22:33:06 +00:00
|
|
|
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError);
|
2019-01-29 01:16:12 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should await resources to load', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-11-20 23:32:46 +00:00
|
|
|
const imgPath = '/img.png';
|
|
|
|
let imgResponse = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
server.setRoute(imgPath, (req, res) => (imgResponse = res));
|
2018-11-20 23:32:46 +00:00
|
|
|
let loaded = false;
|
2020-05-07 10:54:55 +00:00
|
|
|
const contentPromise = page
|
|
|
|
.setContent(`<img src="${server.PREFIX + imgPath}"></img>`)
|
|
|
|
.then(() => (loaded = true));
|
2018-11-20 23:32:46 +00:00
|
|
|
await server.waitForRequest(imgPath);
|
|
|
|
expect(loaded).toBe(false);
|
|
|
|
imgResponse.end();
|
|
|
|
await contentPromise;
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work fast enough', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
for (let i = 0; i < 20; ++i) await page.setContent('<div>yo</div>');
|
2018-12-13 21:33:42 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with tricky content', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-04-30 07:35:05 +00:00
|
|
|
await page.setContent('<div>hello world</div>' + '\x7F');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(await page.$eval('div', (div) => div.textContent)).toBe(
|
|
|
|
'hello world'
|
|
|
|
);
|
2019-04-30 07:35:05 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with accents', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-05-18 14:15:16 +00:00
|
|
|
await page.setContent('<div>aberración</div>');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(await page.$eval('div', (div) => div.textContent)).toBe(
|
|
|
|
'aberración'
|
|
|
|
);
|
2019-05-18 14:15:16 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with emojis', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-05-18 14:15:16 +00:00
|
|
|
await page.setContent('<div>🐥</div>');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(await page.$eval('div', (div) => div.textContent)).toBe('🐥');
|
2019-05-18 14:15:16 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with newline', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-05-18 14:15:16 +00:00
|
|
|
await page.setContent('<div>\n</div>');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(await page.$eval('div', (div) => div.textContent)).toBe('\n');
|
2019-05-18 14:15:16 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-04-06 23:35:50 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('Page.setBypassCSP', function () {
|
|
|
|
it('should bypass CSP meta tag', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
// Make sure CSP prohibits addScriptTag.
|
|
|
|
await page.goto(server.PREFIX + '/csp.html');
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
|
|
|
.addScriptTag({ content: 'window.__injected = 42;' })
|
|
|
|
.catch((error) => void error);
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.__injected)).toBe(undefined);
|
2018-04-09 23:38:00 +00:00
|
|
|
|
|
|
|
// By-pass CSP and try one more time.
|
|
|
|
await page.setBypassCSP(true);
|
|
|
|
await page.reload();
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.addScriptTag({ content: 'window.__injected = 42;' });
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.__injected)).toBe(42);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-04-06 23:35:50 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should bypass CSP header', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
// Make sure CSP prohibits addScriptTag.
|
|
|
|
server.setCSP('/empty.html', 'default-src "self"');
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
|
|
|
.addScriptTag({ content: 'window.__injected = 42;' })
|
|
|
|
.catch((error) => void error);
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.__injected)).toBe(undefined);
|
2018-04-09 23:38:00 +00:00
|
|
|
|
|
|
|
// By-pass CSP and try one more time.
|
|
|
|
await page.setBypassCSP(true);
|
|
|
|
await page.reload();
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.addScriptTag({ content: 'window.__injected = 42;' });
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.__injected)).toBe(42);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-04-06 23:35:50 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should bypass after cross-process navigation', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.setBypassCSP(true);
|
|
|
|
await page.goto(server.PREFIX + '/csp.html');
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.addScriptTag({ content: 'window.__injected = 42;' });
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.__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');
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.addScriptTag({ content: 'window.__injected = 42;' });
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.__injected)).toBe(42);
|
2018-04-06 23:35:50 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should bypass CSP in iframes as well', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-03-12 00:56:32 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
{
|
|
|
|
// Make sure CSP prohibits addScriptTag in an iframe.
|
2020-05-07 10:54:55 +00:00
|
|
|
const frame = await utils.attachFrame(
|
|
|
|
page,
|
|
|
|
'frame1',
|
|
|
|
server.PREFIX + '/csp.html'
|
|
|
|
);
|
|
|
|
await frame
|
|
|
|
.addScriptTag({ content: 'window.__injected = 42;' })
|
|
|
|
.catch((error) => void error);
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await frame.evaluate(() => globalThis.__injected)).toBe(
|
|
|
|
undefined
|
|
|
|
);
|
2019-03-12 00:56:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// By-pass CSP and try one more time.
|
|
|
|
await page.setBypassCSP(true);
|
|
|
|
await page.reload();
|
|
|
|
|
|
|
|
{
|
2020-05-07 10:54:55 +00:00
|
|
|
const frame = await utils.attachFrame(
|
|
|
|
page,
|
|
|
|
'frame1',
|
|
|
|
server.PREFIX + '/csp.html'
|
|
|
|
);
|
|
|
|
await frame
|
|
|
|
.addScriptTag({ content: 'window.__injected = 42;' })
|
|
|
|
.catch((error) => void error);
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await frame.evaluate(() => globalThis.__injected)).toBe(42);
|
2019-03-12 00:56:32 +00:00
|
|
|
}
|
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-04-06 23:35:50 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.addScriptTag', function () {
|
|
|
|
it('should throw an error if no options are provided', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
let error = null;
|
|
|
|
try {
|
2020-10-12 09:30:35 +00:00
|
|
|
// @ts-expect-error purposefully passing bad options
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.addScriptTag('/injectedfile.js');
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error_) {
|
|
|
|
error = error_;
|
2018-04-09 23:38:00 +00:00
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(error.message).toBe(
|
|
|
|
'Provide an object with a `url`, `path` or `content` property'
|
|
|
|
);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with a url', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
const scriptHandle = await page.addScriptTag({ url: '/injectedfile.js' });
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(scriptHandle.asElement()).not.toBeNull();
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.__injected)).toBe(42);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with a url and type=module', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.addScriptTag({ url: '/es6/es6import.js', type: 'module' });
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.__es6injected)).toBe(42);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with a path and type=module', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.addScriptTag({
|
|
|
|
path: path.join(__dirname, 'assets/es6/es6pathimport.js'),
|
|
|
|
type: 'module',
|
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.waitForFunction('window.__es6injected');
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.__es6injected)).toBe(42);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with a content and type=module', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.addScriptTag({
|
|
|
|
content: `import num from '/es6/es6module.js';window.__es6injected = num;`,
|
|
|
|
type: 'module',
|
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.waitForFunction('window.__es6injected');
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.__es6injected)).toBe(42);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw an error if loading from url fail', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
let error = null;
|
|
|
|
try {
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.addScriptTag({ url: '/nonexistfile.js' });
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error_) {
|
|
|
|
error = error_;
|
2018-04-09 23:38:00 +00:00
|
|
|
}
|
|
|
|
expect(error.message).toBe('Loading script from /nonexistfile.js failed');
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with a path', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
const scriptHandle = await page.addScriptTag({
|
|
|
|
path: path.join(__dirname, 'assets/injectedfile.js'),
|
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(scriptHandle.asElement()).not.toBeNull();
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.__injected)).toBe(42);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should include sourcemap when path is provided', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.addScriptTag({
|
|
|
|
path: path.join(__dirname, 'assets/injectedfile.js'),
|
|
|
|
});
|
2020-06-23 05:18:46 +00:00
|
|
|
const result = await page.evaluate(
|
|
|
|
() => globalThis.__injectedError.stack
|
|
|
|
);
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(result).toContain(path.join('assets', 'injectedfile.js'));
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with content', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
const scriptHandle = await page.addScriptTag({
|
|
|
|
content: 'window.__injected = 35;',
|
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(scriptHandle.asElement()).not.toBeNull();
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.__injected)).toBe(35);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-04-06 20:17:55 +00:00
|
|
|
|
2019-11-26 12:12:25 +00:00
|
|
|
// @see https://github.com/puppeteer/puppeteer/issues/4840
|
2020-05-07 10:54:55 +00:00
|
|
|
xit('should throw when added with content to the CSP page', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.PREFIX + '/csp.html');
|
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
|
|
|
.addScriptTag({ content: 'window.__injected = 35;' })
|
|
|
|
.catch((error_) => (error = error_));
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(error).toBeTruthy();
|
|
|
|
});
|
2018-04-06 20:17:55 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw when added with URL to the CSP page', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.PREFIX + '/csp.html');
|
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
|
|
|
.addScriptTag({ url: server.CROSS_PROCESS_PREFIX + '/injectedfile.js' })
|
|
|
|
.catch((error_) => (error = error_));
|
2018-04-09 23:38:00 +00:00
|
|
|
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
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.addStyleTag', function () {
|
|
|
|
it('should throw an error if no options are provided', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
let error = null;
|
|
|
|
try {
|
2020-10-12 09:30:35 +00:00
|
|
|
// @ts-expect-error purposefully passing bad input
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.addStyleTag('/injectedstyle.css');
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error_) {
|
|
|
|
error = error_;
|
2018-04-09 23:38:00 +00:00
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(error.message).toBe(
|
|
|
|
'Provide an object with a `url`, `path` or `content` property'
|
|
|
|
);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with a url', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
const styleHandle = await page.addStyleTag({ url: '/injectedstyle.css' });
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(styleHandle.asElement()).not.toBeNull();
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(
|
|
|
|
`window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')`
|
|
|
|
)
|
|
|
|
).toBe('rgb(255, 0, 0)');
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw an error if loading from url fail', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
let error = null;
|
|
|
|
try {
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.addStyleTag({ url: '/nonexistfile.js' });
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error_) {
|
|
|
|
error = error_;
|
2018-04-09 23:38:00 +00:00
|
|
|
}
|
|
|
|
expect(error.message).toBe('Loading style from /nonexistfile.js failed');
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with a path', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
const styleHandle = await page.addStyleTag({
|
|
|
|
path: path.join(__dirname, 'assets/injectedstyle.css'),
|
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(styleHandle.asElement()).not.toBeNull();
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(
|
|
|
|
`window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')`
|
|
|
|
)
|
|
|
|
).toBe('rgb(255, 0, 0)');
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should include sourcemap when path is provided', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.addStyleTag({
|
|
|
|
path: path.join(__dirname, 'assets/injectedstyle.css'),
|
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
const styleHandle = await page.$('style');
|
2020-05-07 10:54:55 +00:00
|
|
|
const styleContent = await page.evaluate(
|
2020-07-10 10:52:13 +00:00
|
|
|
(style: HTMLStyleElement) => style.innerHTML,
|
2020-05-07 10:54:55 +00:00
|
|
|
styleHandle
|
|
|
|
);
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(styleContent).toContain(path.join('assets', 'injectedstyle.css'));
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should work with content', async () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
const styleHandle = await page.addStyleTag({
|
|
|
|
content: 'body { background-color: green; }',
|
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(styleHandle.asElement()).not.toBeNull();
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(
|
|
|
|
`window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')`
|
|
|
|
)
|
|
|
|
).toBe('rgb(0, 128, 0)');
|
|
|
|
});
|
|
|
|
|
|
|
|
itFailsFirefox(
|
|
|
|
'should throw when added with content to the CSP page',
|
|
|
|
async () => {
|
|
|
|
const { page, server } = getTestState();
|
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/csp.html');
|
|
|
|
let error = null;
|
|
|
|
await page
|
|
|
|
.addStyleTag({ content: 'body { background-color: green; }' })
|
|
|
|
.catch((error_) => (error = error_));
|
|
|
|
expect(error).toBeTruthy();
|
|
|
|
}
|
|
|
|
);
|
2018-04-06 20:17:55 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw when added with URL to the CSP page', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.PREFIX + '/csp.html');
|
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page
|
|
|
|
.addStyleTag({
|
|
|
|
url: server.CROSS_PROCESS_PREFIX + '/injectedstyle.css',
|
|
|
|
})
|
|
|
|
.catch((error_) => (error = error_));
|
2018-04-09 23:38:00 +00:00
|
|
|
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
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.url', function () {
|
|
|
|
it('should work', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
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
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeFailsFirefox('Page.setJavaScriptEnabled', function () {
|
|
|
|
it('should work', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.setJavaScriptEnabled(false);
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.goto(
|
|
|
|
'data:text/html, <script>var something = "forbidden"</script>'
|
|
|
|
);
|
2018-04-09 23:38:00 +00:00
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.evaluate('something').catch((error_) => (error = error_));
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(error.message).toContain('something is not defined');
|
|
|
|
|
|
|
|
await page.setJavaScriptEnabled(true);
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.goto(
|
|
|
|
'data:text/html, <script>var something = "forbidden"</script>'
|
|
|
|
);
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(await page.evaluate('something')).toBe('forbidden');
|
|
|
|
});
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.setCacheEnabled', function () {
|
|
|
|
it('should enable or disable the cache based on the state passed', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-06 03:49:47 +00:00
|
|
|
await page.goto(server.PREFIX + '/cached/one-style.html');
|
2019-02-09 04:57:16 +00:00
|
|
|
const [cachedRequest] = await Promise.all([
|
|
|
|
server.waitForRequest('/cached/one-style.html'),
|
|
|
|
page.reload(),
|
|
|
|
]);
|
|
|
|
// Rely on "if-modified-since" caching in our test server.
|
|
|
|
expect(cachedRequest.headers['if-modified-since']).not.toBe(undefined);
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.setCacheEnabled(false);
|
2019-02-09 04:57:16 +00:00
|
|
|
const [nonCachedRequest] = await Promise.all([
|
|
|
|
server.waitForRequest('/cached/one-style.html'),
|
|
|
|
page.reload(),
|
|
|
|
]);
|
|
|
|
expect(nonCachedRequest.headers['if-modified-since']).toBe(undefined);
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox(
|
|
|
|
'should stay disabled when toggling request interception on/off',
|
|
|
|
async () => {
|
|
|
|
const { page, server } = getTestState();
|
|
|
|
|
|
|
|
await page.setCacheEnabled(false);
|
|
|
|
await page.setRequestInterception(true);
|
|
|
|
await page.setRequestInterception(false);
|
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/cached/one-style.html');
|
|
|
|
const [nonCachedRequest] = await Promise.all([
|
|
|
|
server.waitForRequest('/cached/one-style.html'),
|
|
|
|
page.reload(),
|
|
|
|
]);
|
|
|
|
expect(nonCachedRequest.headers['if-modified-since']).toBe(undefined);
|
|
|
|
}
|
|
|
|
);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('printing to PDF', function () {
|
2020-06-12 13:55:51 +00:00
|
|
|
it('can print to PDF and save to file', async () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
// Printing to pdf is currently only supported in headless
|
|
|
|
const { isHeadless, page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
if (!isHeadless) return;
|
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
const outputFile = __dirname + '/assets/output.pdf';
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.pdf({ path: outputFile });
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(fs.readFileSync(outputFile).byteLength).toBeGreaterThan(0);
|
|
|
|
fs.unlinkSync(outputFile);
|
|
|
|
});
|
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.title', function () {
|
|
|
|
it('should return the page title', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-06 21:49:14 +00:00
|
|
|
await page.goto(server.PREFIX + '/title.html');
|
|
|
|
expect(await page.title()).toBe('Woof-Woof');
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.select', function () {
|
|
|
|
it('should select single option', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
|
|
|
await page.select('select', 'blue');
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.result.onInput)).toEqual([
|
|
|
|
'blue',
|
|
|
|
]);
|
|
|
|
expect(await page.evaluate(() => globalThis.result.onChange)).toEqual([
|
|
|
|
'blue',
|
|
|
|
]);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should select only first option', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
|
|
|
await page.select('select', 'blue', 'green', 'red');
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.result.onInput)).toEqual([
|
|
|
|
'blue',
|
|
|
|
]);
|
|
|
|
expect(await page.evaluate(() => globalThis.result.onChange)).toEqual([
|
|
|
|
'blue',
|
|
|
|
]);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should not throw when select causes navigation', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-06-07 20:46:43 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.$eval('select', (select) =>
|
|
|
|
select.addEventListener(
|
|
|
|
'input',
|
2020-06-23 05:18:46 +00:00
|
|
|
() => ((window as any).location = '/empty.html')
|
2020-05-07 10:54:55 +00:00
|
|
|
)
|
|
|
|
);
|
2019-06-07 20:46:43 +00:00
|
|
|
await Promise.all([
|
|
|
|
page.select('select', 'blue'),
|
|
|
|
page.waitForNavigation(),
|
|
|
|
]);
|
|
|
|
expect(page.url()).toContain('empty.html');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should select multiple options', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
2020-06-23 05:18:46 +00:00
|
|
|
await page.evaluate(() => globalThis.makeMultiple());
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.select('select', 'blue', 'green', 'red');
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.result.onInput)).toEqual([
|
2020-05-07 10:54:55 +00:00
|
|
|
'blue',
|
|
|
|
'green',
|
|
|
|
'red',
|
|
|
|
]);
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.result.onChange)).toEqual([
|
2020-05-07 10:54:55 +00:00
|
|
|
'blue',
|
|
|
|
'green',
|
|
|
|
'red',
|
|
|
|
]);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should respect event bubbling', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
|
|
|
await page.select('select', 'blue');
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => globalThis.result.onBubblingInput)
|
|
|
|
).toEqual(['blue']);
|
|
|
|
expect(
|
|
|
|
await page.evaluate(() => globalThis.result.onBubblingChange)
|
|
|
|
).toEqual(['blue']);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should throw when element is not a <select>', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
let error = null;
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.select('body', '').catch((error_) => (error = error_));
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(error.message).toContain('Element is not a <select> element.');
|
2018-03-16 22:33:31 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should return [] on no matched values', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
2020-05-07 10:54:55 +00:00
|
|
|
const result = await page.select('select', '42', 'abc');
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(result).toEqual([]);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should return an array of matched values', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
2020-06-23 05:18:46 +00:00
|
|
|
await page.evaluate(() => globalThis.makeMultiple());
|
2020-05-07 10:54:55 +00:00
|
|
|
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 () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
2020-05-07 10:54:55 +00:00
|
|
|
const result = await page.select(
|
|
|
|
'select',
|
|
|
|
'42',
|
|
|
|
'blue',
|
|
|
|
'black',
|
|
|
|
'magenta'
|
|
|
|
);
|
2018-04-09 23:38:00 +00:00
|
|
|
expect(result.length).toEqual(1);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should return [] on no values', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
|
|
|
const result = await page.select('select');
|
|
|
|
expect(result).toEqual([]);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should deselect all options when passed no values for a multiple select', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
2020-06-23 05:18:46 +00:00
|
|
|
await page.evaluate(() => globalThis.makeMultiple());
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.select('select', 'blue', 'black', 'magenta');
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.select('select');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2020-07-02 09:09:34 +00:00
|
|
|
await page.$eval('select', (select: HTMLSelectElement) =>
|
2020-06-23 05:18:46 +00:00
|
|
|
Array.from(select.options).every(
|
|
|
|
(option: HTMLOptionElement) => !option.selected
|
|
|
|
)
|
2020-05-07 10:54:55 +00:00
|
|
|
)
|
|
|
|
).toEqual(true);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should deselect all options when passed no values for a select without multiple', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.select('select', 'blue', 'black', 'magenta');
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.select('select');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2020-07-02 09:09:34 +00:00
|
|
|
await page.$eval('select', (select: HTMLSelectElement) =>
|
2020-06-23 05:18:46 +00:00
|
|
|
Array.from(select.options).every(
|
|
|
|
(option: HTMLOptionElement) => !option.selected
|
|
|
|
)
|
2020-05-07 10:54:55 +00:00
|
|
|
)
|
|
|
|
).toEqual(true);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should throw if passed in non-strings', async () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.setContent('<select><option value="12"/></select>');
|
|
|
|
let error = null;
|
|
|
|
try {
|
2020-10-12 09:30:35 +00:00
|
|
|
// @ts-expect-error purposefully passing bad input
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.select('select', 12);
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error_) {
|
|
|
|
error = error_;
|
2018-04-09 23:38:00 +00:00
|
|
|
}
|
|
|
|
expect(error.message).toContain('Values must be strings');
|
|
|
|
});
|
2019-11-26 12:12:25 +00:00
|
|
|
// @see https://github.com/puppeteer/puppeteer/issues/3327
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox(
|
|
|
|
'should work when re-defining top-level Event class',
|
|
|
|
async () => {
|
|
|
|
const { page, server } = getTestState();
|
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/input/select.html');
|
|
|
|
await page.evaluate(() => (window.Event = null));
|
|
|
|
await page.select('select', 'blue');
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(await page.evaluate(() => globalThis.result.onInput)).toEqual([
|
|
|
|
'blue',
|
|
|
|
]);
|
|
|
|
expect(await page.evaluate(() => globalThis.result.onChange)).toEqual([
|
|
|
|
'blue',
|
|
|
|
]);
|
2020-05-07 10:54:55 +00:00
|
|
|
}
|
|
|
|
);
|
2018-04-09 23:38:00 +00:00
|
|
|
});
|
2018-03-16 22:33:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.Events.Close', function () {
|
|
|
|
itFailsFirefox('should work with window.close', async () => {
|
|
|
|
const { page, context } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
const newPagePromise = new Promise<Page>((fulfill) =>
|
2020-05-07 10:54:55 +00:00
|
|
|
context.once('targetcreated', (target) => fulfill(target.page()))
|
|
|
|
);
|
|
|
|
await page.evaluate(
|
|
|
|
() => (window['newPage'] = window.open('about:blank'))
|
|
|
|
);
|
2018-04-09 23:38:00 +00:00
|
|
|
const newPage = await newPagePromise;
|
2020-05-07 10:54:55 +00:00
|
|
|
const closedPromise = new Promise((x) => newPage.on('close', x));
|
2018-04-09 23:38:00 +00:00
|
|
|
await page.evaluate(() => window['newPage'].close());
|
|
|
|
await closedPromise;
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with page.close', async () => {
|
|
|
|
const { context } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-08-01 22:49:41 +00:00
|
|
|
const newPage = await context.newPage();
|
2020-05-07 10:54:55 +00:00
|
|
|
const closedPromise = new Promise((x) => newPage.on('close', x));
|
2018-04-09 23:38:00 +00:00
|
|
|
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
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.browser', function () {
|
|
|
|
it('should return the correct browser instance', async () => {
|
|
|
|
const { page, browser } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-04-17 17:37:17 +00:00
|
|
|
expect(page.browser()).toBe(browser);
|
|
|
|
});
|
|
|
|
});
|
2018-12-12 23:08:31 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.browserContext', function () {
|
|
|
|
it('should return the correct browser instance', async () => {
|
|
|
|
const { page, context } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-12-12 23:08:31 +00:00
|
|
|
expect(page.browserContext()).toBe(context);
|
|
|
|
});
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|