2017-06-15 15:15:25 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-07-10 17:09:59 +00:00
|
|
|
let fs = require('fs');
|
2017-07-13 17:07:24 +00:00
|
|
|
let rm = require('rimraf').sync;
|
2017-06-22 20:38:10 +00:00
|
|
|
let path = require('path');
|
|
|
|
let Browser = require('../lib/Browser');
|
2017-07-10 22:09:52 +00:00
|
|
|
let SimpleServer = require('./server/SimpleServer');
|
2017-06-22 20:38:10 +00:00
|
|
|
let GoldenUtils = require('./golden-utils');
|
2017-05-12 23:36:37 +00:00
|
|
|
|
2017-07-13 17:07:24 +00:00
|
|
|
let GOLDEN_DIR = path.join(__dirname, 'golden');
|
|
|
|
let OUTPUT_DIR = path.join(__dirname, 'output');
|
|
|
|
|
2017-06-22 20:38:10 +00:00
|
|
|
let PORT = 8907;
|
2017-07-10 22:09:52 +00:00
|
|
|
let PREFIX = 'http://localhost:' + PORT;
|
|
|
|
let EMPTY_PAGE = PREFIX + '/empty.html';
|
|
|
|
let HTTPS_PORT = 8908;
|
|
|
|
let HTTPS_PREFIX = 'https://localhost:' + HTTPS_PORT;
|
2017-05-13 00:55:29 +00:00
|
|
|
|
2017-07-07 20:08:36 +00:00
|
|
|
if (process.env.DEBUG_TEST)
|
|
|
|
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 1000 * 1000;
|
|
|
|
else
|
|
|
|
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10 * 1000;
|
2017-06-21 00:32:08 +00:00
|
|
|
|
2017-07-12 02:38:20 +00:00
|
|
|
// Make sure the `npm install` was run after the chromium roll.
|
|
|
|
{
|
|
|
|
const Downloader = require('../utils/ChromiumDownloader');
|
|
|
|
const chromiumRevision = require('../package.json').puppeteer.chromium_revision;
|
|
|
|
const revisionInfo = Downloader.revisionInfo(Downloader.currentPlatform(), chromiumRevision);
|
|
|
|
console.assert(revisionInfo, `Chromium r${chromiumRevision} is not downloaded. Run 'npm install' and try to re-run tests.`);
|
|
|
|
}
|
|
|
|
|
2017-05-13 00:55:29 +00:00
|
|
|
describe('Puppeteer', function() {
|
2017-06-22 20:38:10 +00:00
|
|
|
let browser;
|
2017-07-07 15:14:25 +00:00
|
|
|
let server;
|
2017-07-10 22:09:52 +00:00
|
|
|
let httpsServer;
|
2017-06-22 20:38:10 +00:00
|
|
|
let page;
|
2017-05-12 23:36:37 +00:00
|
|
|
|
2017-07-07 06:45:18 +00:00
|
|
|
beforeAll(SX(async function() {
|
2017-06-21 20:51:06 +00:00
|
|
|
browser = new Browser({args: ['--no-sandbox']});
|
2017-07-10 22:09:52 +00:00
|
|
|
const assetsPath = path.join(__dirname, 'assets');
|
|
|
|
server = await SimpleServer.create(assetsPath, PORT);
|
|
|
|
httpsServer = await SimpleServer.createHTTPS(assetsPath, HTTPS_PORT);
|
2017-07-13 17:07:24 +00:00
|
|
|
if (fs.existsSync(OUTPUT_DIR))
|
|
|
|
rm(OUTPUT_DIR);
|
2017-07-07 06:45:18 +00:00
|
|
|
}));
|
2017-05-12 23:36:37 +00:00
|
|
|
|
2017-07-07 15:55:30 +00:00
|
|
|
afterAll(SX(async function() {
|
2017-07-10 22:09:52 +00:00
|
|
|
await Promise.all([
|
|
|
|
server.stop(),
|
|
|
|
httpsServer.stop(),
|
|
|
|
]);
|
2017-07-06 02:06:47 +00:00
|
|
|
browser.close();
|
2017-07-07 15:55:30 +00:00
|
|
|
}));
|
2017-05-13 00:55:29 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
beforeEach(SX(async function() {
|
|
|
|
page = await browser.newPage();
|
2017-07-07 15:14:25 +00:00
|
|
|
server.reset();
|
2017-07-10 22:09:52 +00:00
|
|
|
httpsServer.reset();
|
2017-07-13 17:07:24 +00:00
|
|
|
GoldenUtils.addMatchers(jasmine, GOLDEN_DIR, OUTPUT_DIR);
|
2017-06-21 20:51:06 +00:00
|
|
|
}));
|
2017-05-13 18:05:54 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
afterEach(function() {
|
|
|
|
page.close();
|
|
|
|
});
|
2017-05-13 00:55:29 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
describe('Page.evaluate', function() {
|
|
|
|
it('should work', SX(async function() {
|
2017-06-22 20:38:10 +00:00
|
|
|
let result = await page.evaluate(() => 7 * 3);
|
2017-06-21 20:51:06 +00:00
|
|
|
expect(result).toBe(21);
|
|
|
|
}));
|
|
|
|
it('should await promise', SX(async function() {
|
2017-06-22 20:38:10 +00:00
|
|
|
let result = await page.evaluate(() => Promise.resolve(8 * 7));
|
2017-06-21 20:51:06 +00:00
|
|
|
expect(result).toBe(56);
|
2017-06-21 14:45:13 +00:00
|
|
|
}));
|
2017-06-21 20:51:06 +00:00
|
|
|
it('should work from-inside inPageCallback', SX(async function() {
|
|
|
|
// Setup inpage callback, which calls Page.evaluate
|
|
|
|
await page.setInPageCallback('callController', async function(a, b) {
|
|
|
|
return await page.evaluate((a, b) => a * b, a, b);
|
|
|
|
});
|
2017-06-22 20:38:10 +00:00
|
|
|
let result = await page.evaluate(async function() {
|
2017-06-21 20:51:06 +00:00
|
|
|
return await callController(9, 3);
|
|
|
|
});
|
|
|
|
expect(result).toBe(27);
|
|
|
|
}));
|
|
|
|
it('should reject promise with exception', SX(async function() {
|
2017-06-22 20:38:10 +00:00
|
|
|
let error = null;
|
2017-06-21 20:51:06 +00:00
|
|
|
try {
|
|
|
|
await page.evaluate(() => not.existing.object.property);
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error).toBeTruthy();
|
|
|
|
expect(error.message).toContain('not is not defined');
|
|
|
|
}));
|
2017-07-06 17:41:01 +00:00
|
|
|
it('should return complex objects', SX(async function() {
|
|
|
|
const object = {foo: 'bar!'};
|
|
|
|
let result = await page.evaluate(a => a, object);
|
|
|
|
expect(result).not.toBe(object);
|
|
|
|
expect(result).toEqual(object);
|
|
|
|
}));
|
|
|
|
it('should return NaN', SX(async function() {
|
|
|
|
let result = await page.evaluate(() => NaN);
|
|
|
|
expect(Object.is(result, NaN)).toBe(true);
|
|
|
|
}));
|
|
|
|
it('should return -0', SX(async function() {
|
|
|
|
let result = await page.evaluate(() => -0);
|
|
|
|
expect(Object.is(result, -0)).toBe(true);
|
|
|
|
}));
|
|
|
|
it('should return Infinity', SX(async function() {
|
|
|
|
let result = await page.evaluate(() => Infinity);
|
|
|
|
expect(Object.is(result, Infinity)).toBe(true);
|
|
|
|
}));
|
|
|
|
it('should return -Infinity', SX(async function() {
|
|
|
|
let result = await page.evaluate(() => -Infinity);
|
|
|
|
expect(Object.is(result, -Infinity)).toBe(true);
|
|
|
|
}));
|
2017-07-18 07:03:52 +00:00
|
|
|
it('should not fail for window object', SX(async function() {
|
|
|
|
let result = await page.evaluate(() => window);
|
|
|
|
expect(result).toBe('Window');
|
|
|
|
}));
|
2017-06-21 20:51:06 +00:00
|
|
|
});
|
2017-06-14 15:21:56 +00:00
|
|
|
|
2017-07-06 23:09:23 +00:00
|
|
|
describe('Page.injectFile', function() {
|
2017-07-07 15:55:30 +00:00
|
|
|
it('should work', SX(async function() {
|
2017-07-06 23:09:23 +00:00
|
|
|
const helloPath = path.join(__dirname, './assets/injectedfile.js');
|
|
|
|
await page.injectFile(helloPath);
|
|
|
|
const result = await page.evaluate(() => __injected);
|
|
|
|
expect(result).toBe(42);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2017-06-27 19:40:46 +00:00
|
|
|
describe('Frame.evaluate', function() {
|
|
|
|
let FrameUtils = require('./frame-utils');
|
|
|
|
it('should have different execution contexts', SX(async function() {
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
|
|
|
await FrameUtils.attachFrame(page, 'frame1', EMPTY_PAGE);
|
|
|
|
expect(page.frames().length).toBe(2);
|
|
|
|
let frame1 = page.frames()[0];
|
|
|
|
let frame2 = page.frames()[1];
|
|
|
|
await frame1.evaluate(() => window.FOO = 'foo');
|
|
|
|
await frame2.evaluate(() => window.FOO = 'bar');
|
|
|
|
expect(await frame1.evaluate(() => window.FOO)).toBe('foo');
|
|
|
|
expect(await frame2.evaluate(() => window.FOO)).toBe('bar');
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2017-07-07 22:39:02 +00:00
|
|
|
describe('Frame.waitFor', function() {
|
|
|
|
let FrameUtils = require('./frame-utils');
|
|
|
|
let addElement = tag => document.body.appendChild(document.createElement(tag));
|
|
|
|
|
|
|
|
it('should immediately resolve promise if node exists', SX(async function() {
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
|
|
|
let frame = page.mainFrame();
|
|
|
|
let added = false;
|
|
|
|
await frame.waitFor('*').then(() => added = true);
|
|
|
|
expect(added).toBe(true);
|
|
|
|
|
|
|
|
added = false;
|
|
|
|
await frame.evaluate(addElement, 'div');
|
|
|
|
await frame.waitFor('div').then(() => added = true);
|
|
|
|
expect(added).toBe(true);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should resolve promise when node is added', SX(async function() {
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
|
|
|
let frame = page.mainFrame();
|
|
|
|
let added = false;
|
|
|
|
frame.waitFor('div').then(() => added = true);
|
|
|
|
// run nop function..
|
|
|
|
await frame.evaluate(() => 42);
|
|
|
|
// .. to be sure that waitFor promise is not resolved yet.
|
|
|
|
expect(added).toBe(false);
|
|
|
|
await frame.evaluate(addElement, 'br');
|
|
|
|
expect(added).toBe(false);
|
|
|
|
await frame.evaluate(addElement, 'div');
|
|
|
|
expect(added).toBe(true);
|
|
|
|
}));
|
|
|
|
|
2017-07-18 09:10:02 +00:00
|
|
|
it('should work when node is added through innerHTML', SX(async function() {
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
|
|
|
let frame = page.mainFrame();
|
|
|
|
let added = false;
|
|
|
|
frame.waitFor('h3 div').then(() => added = true);
|
|
|
|
expect(added).toBe(false);
|
|
|
|
await frame.evaluate(addElement, 'span');
|
|
|
|
await page.$('span', span => span.innerHTML = '<h3><div></div></h3>');
|
|
|
|
expect(added).toBe(true);
|
|
|
|
}));
|
|
|
|
|
2017-07-07 22:39:02 +00:00
|
|
|
it('Page.waitFor is shortcut for main frame', SX(async function() {
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
|
|
|
await FrameUtils.attachFrame(page, 'frame1', EMPTY_PAGE);
|
|
|
|
let otherFrame = page.frames()[1];
|
|
|
|
let added = false;
|
|
|
|
page.waitFor('div').then(() => added = true);
|
|
|
|
await otherFrame.evaluate(addElement, 'div');
|
|
|
|
expect(added).toBe(false);
|
|
|
|
await page.evaluate(addElement, 'div');
|
|
|
|
expect(added).toBe(true);
|
|
|
|
}));
|
2017-07-14 22:55:38 +00:00
|
|
|
|
|
|
|
it('should run in specified frame', SX(async function() {
|
|
|
|
await FrameUtils.attachFrame(page, 'frame1', EMPTY_PAGE);
|
|
|
|
await FrameUtils.attachFrame(page, 'frame2', EMPTY_PAGE);
|
|
|
|
let frame1 = page.frames()[1];
|
|
|
|
let frame2 = page.frames()[2];
|
|
|
|
let added = false;
|
|
|
|
frame2.waitFor('div').then(() => added = true);
|
|
|
|
expect(added).toBe(false);
|
|
|
|
await frame1.evaluate(addElement, 'div');
|
|
|
|
expect(added).toBe(false);
|
|
|
|
await frame2.evaluate(addElement, 'div');
|
|
|
|
expect(added).toBe(true);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should throw if evaluation failed', SX(async function() {
|
|
|
|
await page.evaluateOnInitialized(function() {
|
|
|
|
document.querySelector = null;
|
|
|
|
});
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
|
|
|
try {
|
|
|
|
await page.waitFor('*');
|
|
|
|
fail('Failed waitFor did not throw.');
|
|
|
|
} catch (e) {
|
2017-07-17 19:15:06 +00:00
|
|
|
expect(e.message).toContain('Evaluation failed: document.querySelector is not a function');
|
2017-07-14 22:55:38 +00:00
|
|
|
}
|
|
|
|
}));
|
2017-07-07 22:39:02 +00:00
|
|
|
});
|
|
|
|
|
2017-07-18 03:38:11 +00:00
|
|
|
describe('Page.Events.Console', function() {
|
2017-07-17 08:57:37 +00:00
|
|
|
it('should work', SX(async function() {
|
|
|
|
let commandArgs = [];
|
2017-07-18 03:38:11 +00:00
|
|
|
page.once('console', (...args) => commandArgs = args);
|
2017-07-17 08:57:37 +00:00
|
|
|
page.evaluate(() => console.log(5, 'hello', {foo: 'bar'}));
|
2017-07-18 03:38:11 +00:00
|
|
|
await waitForEvents(page, 'console');
|
2017-07-17 08:57:37 +00:00
|
|
|
expect(commandArgs).toEqual([5, 'hello', {foo: 'bar'}]);
|
|
|
|
}));
|
|
|
|
it('should work for different console API calls', SX(async function() {
|
|
|
|
let messages = [];
|
2017-07-18 03:38:11 +00:00
|
|
|
page.on('console', msg => messages.push(msg));
|
2017-07-17 08:57:37 +00:00
|
|
|
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');
|
|
|
|
});
|
|
|
|
// Wait for 5 events to hit.
|
2017-07-18 03:38:11 +00:00
|
|
|
await waitForEvents(page, 'console', 5);
|
2017-07-17 08:57:37 +00:00
|
|
|
expect(messages[0]).toContain('calling console.time');
|
|
|
|
expect(messages.slice(1)).toEqual([
|
|
|
|
'calling console.trace',
|
|
|
|
'calling console.dir',
|
|
|
|
'calling console.warn',
|
|
|
|
'calling console.error',
|
|
|
|
]);
|
|
|
|
}));
|
2017-07-18 07:03:52 +00:00
|
|
|
it('should not fail for window object', SX(async function() {
|
|
|
|
let windowObj = null;
|
|
|
|
page.once('console', arg => windowObj = arg);
|
|
|
|
page.evaluate(() => console.error(window));
|
|
|
|
await waitForEvents(page, 'console');
|
|
|
|
expect(windowObj).toBe('Window');
|
|
|
|
}));
|
2017-07-17 08:57:37 +00:00
|
|
|
});
|
2017-06-15 07:20:37 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
describe('Page.navigate', function() {
|
|
|
|
it('should fail when navigating to bad url', SX(async function() {
|
2017-07-10 22:09:52 +00:00
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
await page.navigate('asdfasdf');
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error.message).toContain('Cannot navigate to invalid URL');
|
|
|
|
}));
|
|
|
|
it('should fail when navigating to bad SSL', SX(async function() {
|
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
await page.navigate(HTTPS_PREFIX + '/empty.html');
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error.message).toContain('SSL Certiciate error');
|
|
|
|
}));
|
|
|
|
it('should fail when exceeding maximum navigation timeout', SX(async function() {
|
|
|
|
let error = null;
|
|
|
|
// Hang for request to the empty.html
|
|
|
|
server.setRoute('/empty.html', (req, res) => { });
|
|
|
|
try {
|
|
|
|
await page.navigate(PREFIX + '/empty.html', {maxTime: 59});
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error.message).toContain('Navigation Timeout Exceeded: 59ms');
|
|
|
|
}));
|
|
|
|
it('should work when navigating to valid url', SX(async function() {
|
|
|
|
const response = await page.navigate(EMPTY_PAGE);
|
|
|
|
expect(response.ok).toBe(true);
|
|
|
|
}));
|
|
|
|
it('should work when navigating to data url', SX(async function() {
|
|
|
|
const response = await page.navigate('data:text/html,hello');
|
|
|
|
expect(response.ok).toBe(true);
|
|
|
|
}));
|
|
|
|
it('should work when navigating to 404', SX(async function() {
|
|
|
|
const response = await page.navigate(PREFIX + '/not-found');
|
|
|
|
expect(response.ok).toBe(false);
|
|
|
|
expect(response.status).toBe(404);
|
2017-06-21 20:51:06 +00:00
|
|
|
}));
|
2017-07-10 22:09:52 +00:00
|
|
|
it('should return last response in redirect chain', SX(async function() {
|
|
|
|
server.setRedirect('/redirect/1.html', '/redirect/2.html');
|
|
|
|
server.setRedirect('/redirect/2.html', '/redirect/3.html');
|
|
|
|
server.setRedirect('/redirect/3.html', EMPTY_PAGE);
|
|
|
|
const response = await page.navigate(PREFIX + '/redirect/1.html');
|
|
|
|
expect(response.ok).toBe(true);
|
|
|
|
expect(response.url).toBe(EMPTY_PAGE);
|
2017-06-21 20:51:06 +00:00
|
|
|
}));
|
2017-06-28 05:02:46 +00:00
|
|
|
it('should wait for network idle to succeed navigation', SX(async function() {
|
|
|
|
let responses = [];
|
2017-06-28 21:39:37 +00:00
|
|
|
// Hold on to a bunch of requests without answering.
|
2017-07-07 15:14:25 +00:00
|
|
|
server.setRoute('/fetch-request-a.js', (req, res) => responses.push(res));
|
|
|
|
server.setRoute('/fetch-request-b.js', (req, res) => responses.push(res));
|
|
|
|
server.setRoute('/fetch-request-c.js', (req, res) => responses.push(res));
|
|
|
|
server.setRoute('/fetch-request-d.js', (req, res) => responses.push(res));
|
2017-06-28 21:39:37 +00:00
|
|
|
let initialFetchResourcesRequested = Promise.all([
|
2017-07-07 15:14:25 +00:00
|
|
|
server.waitForRequest('/fetch-request-a.js'),
|
|
|
|
server.waitForRequest('/fetch-request-b.js'),
|
|
|
|
server.waitForRequest('/fetch-request-c.js'),
|
2017-06-28 05:02:46 +00:00
|
|
|
]);
|
2017-07-07 15:14:25 +00:00
|
|
|
let secondFetchResourceRequested = server.waitForRequest('/fetch-request-d.js');
|
2017-06-28 21:39:37 +00:00
|
|
|
|
2017-06-28 05:02:46 +00:00
|
|
|
// Navigate to a page which loads immediately and then does a bunch of
|
|
|
|
// requests via javascript's fetch method.
|
2017-07-10 22:09:52 +00:00
|
|
|
let navigationPromise = page.navigate(PREFIX + '/networkidle.html', {
|
2017-07-14 20:59:36 +00:00
|
|
|
waitUntil: 'networkidle',
|
2017-06-28 21:39:37 +00:00
|
|
|
networkIdleTimeout: 100,
|
|
|
|
networkIdleInflight: 0, // Only be idle when there are 0 inflight requests
|
|
|
|
});
|
|
|
|
// Track when the navigation gets completed.
|
|
|
|
let navigationFinished = false;
|
|
|
|
navigationPromise.then(() => navigationFinished = true);
|
|
|
|
|
|
|
|
// Wait for the page's 'load' event.
|
|
|
|
await new Promise(fulfill => page.once('load', fulfill));
|
|
|
|
expect(navigationFinished).toBe(false);
|
|
|
|
|
|
|
|
// Wait for the initial three resources to be requested.
|
|
|
|
await initialFetchResourcesRequested;
|
|
|
|
|
|
|
|
// Expect navigation still to be not finished.
|
|
|
|
expect(navigationFinished).toBe(false);
|
|
|
|
|
|
|
|
// Respond to initial requests.
|
|
|
|
for (let response of responses) {
|
|
|
|
response.statusCode = 404;
|
|
|
|
response.end(`File not found`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset responses array
|
|
|
|
responses = [];
|
|
|
|
|
|
|
|
// Wait for the second round to be requested.
|
|
|
|
await secondFetchResourceRequested;
|
|
|
|
// Expect navigation still to be not finished.
|
|
|
|
expect(navigationFinished).toBe(false);
|
|
|
|
|
|
|
|
// Respond to requests.
|
|
|
|
for (let response of responses) {
|
|
|
|
response.statusCode = 404;
|
|
|
|
response.end(`File not found`);
|
|
|
|
}
|
|
|
|
|
2017-07-10 22:09:52 +00:00
|
|
|
const response = await navigationPromise;
|
2017-06-28 21:39:37 +00:00
|
|
|
// Expect navigation to succeed.
|
2017-07-10 22:09:52 +00:00
|
|
|
expect(response.ok).toBe(true);
|
2017-06-28 21:39:37 +00:00
|
|
|
}));
|
|
|
|
it('should wait for websockets to succeed navigation', SX(async function() {
|
|
|
|
let responses = [];
|
|
|
|
// Hold on to the fetch request without answering.
|
2017-07-07 15:14:25 +00:00
|
|
|
server.setRoute('/fetch-request.js', (req, res) => responses.push(res));
|
|
|
|
let fetchResourceRequested = server.waitForRequest('/fetch-request.js');
|
2017-06-28 21:39:37 +00:00
|
|
|
// Navigate to a page which loads immediately and then opens a bunch of
|
|
|
|
// websocket connections and then a fetch request.
|
2017-07-10 22:09:52 +00:00
|
|
|
let navigationPromise = page.navigate(PREFIX + '/websocket.html', {
|
2017-07-14 20:59:36 +00:00
|
|
|
waitUntil: 'networkidle',
|
2017-06-28 21:39:37 +00:00
|
|
|
networkIdleTimeout: 100,
|
|
|
|
networkIdleInflight: 0, // Only be idle when there are 0 inflight requests/connections
|
2017-06-28 05:02:46 +00:00
|
|
|
});
|
|
|
|
// Track when the navigation gets completed.
|
|
|
|
let navigationFinished = false;
|
|
|
|
navigationPromise.then(() => navigationFinished = true);
|
|
|
|
|
|
|
|
// Wait for the page's 'load' event.
|
|
|
|
await new Promise(fulfill => page.once('load', fulfill));
|
|
|
|
expect(navigationFinished).toBe(false);
|
|
|
|
|
2017-06-28 21:39:37 +00:00
|
|
|
// Wait for the resource to be requested.
|
|
|
|
await fetchResourceRequested;
|
2017-06-28 05:02:46 +00:00
|
|
|
|
|
|
|
// Expect navigation still to be not finished.
|
|
|
|
expect(navigationFinished).toBe(false);
|
|
|
|
|
2017-06-28 21:39:37 +00:00
|
|
|
// Respond to the request.
|
2017-06-28 05:02:46 +00:00
|
|
|
for (let response of responses) {
|
|
|
|
response.statusCode = 404;
|
|
|
|
response.end(`File not found`);
|
|
|
|
}
|
2017-07-10 22:09:52 +00:00
|
|
|
const response = await navigationPromise;
|
2017-06-28 05:02:46 +00:00
|
|
|
// Expect navigation to succeed.
|
2017-07-10 22:09:52 +00:00
|
|
|
expect(response.ok).toBe(true);
|
2017-06-28 05:02:46 +00:00
|
|
|
}));
|
2017-06-21 20:51:06 +00:00
|
|
|
});
|
2017-06-16 04:22:41 +00:00
|
|
|
|
2017-07-19 01:54:24 +00:00
|
|
|
describe('Page.waitForNavigation', function() {
|
|
|
|
it('should work', SX(async function() {
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
|
|
|
const result = page.waitForNavigation();
|
|
|
|
page.evaluate(url => window.location.href = url, PREFIX + '/grid.html');
|
|
|
|
const response = await result;
|
|
|
|
expect(response.ok).toBe(true);
|
|
|
|
expect(response.url).toContain('grid.html');
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2017-07-19 02:11:37 +00:00
|
|
|
describe('Page.goBack', function() {
|
|
|
|
it('should work', SX(async function() {
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
|
|
|
await page.navigate(PREFIX + '/grid.html');
|
|
|
|
|
|
|
|
let response = await page.goBack();
|
|
|
|
expect(response.ok).toBe(true);
|
|
|
|
expect(response.url).toContain(EMPTY_PAGE);
|
|
|
|
|
|
|
|
response = await page.goForward();
|
|
|
|
expect(response.ok).toBe(true);
|
|
|
|
expect(response.url).toContain('/grid.html');
|
|
|
|
|
|
|
|
response = await page.goForward();
|
|
|
|
expect(response).toBe(null);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
describe('Page.setInPageCallback', function() {
|
|
|
|
it('should work', SX(async function() {
|
|
|
|
await page.setInPageCallback('callController', function(a, b) {
|
|
|
|
return a * b;
|
|
|
|
});
|
2017-06-22 20:38:10 +00:00
|
|
|
let result = await page.evaluate(async function() {
|
2017-06-21 20:51:06 +00:00
|
|
|
return await callController(9, 4);
|
|
|
|
});
|
|
|
|
expect(result).toBe(36);
|
|
|
|
}));
|
|
|
|
it('should survive navigation', SX(async function() {
|
|
|
|
await page.setInPageCallback('callController', function(a, b) {
|
|
|
|
return a * b;
|
|
|
|
});
|
2017-06-16 18:21:44 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
await page.navigate(EMPTY_PAGE);
|
2017-06-22 20:38:10 +00:00
|
|
|
let result = await page.evaluate(async function() {
|
2017-06-21 20:51:06 +00:00
|
|
|
return await callController(9, 4);
|
|
|
|
});
|
|
|
|
expect(result).toBe(36);
|
|
|
|
}));
|
|
|
|
it('should await returned promise', SX(async function() {
|
|
|
|
await page.setInPageCallback('callController', function(a, b) {
|
|
|
|
return Promise.resolve(a * b);
|
|
|
|
});
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2017-06-22 20:38:10 +00:00
|
|
|
let result = await page.evaluate(async function() {
|
2017-06-21 20:51:06 +00:00
|
|
|
return await callController(3, 5);
|
|
|
|
});
|
|
|
|
expect(result).toBe(15);
|
|
|
|
}));
|
|
|
|
});
|
2017-06-17 04:40:00 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
describe('Page.setRequestInterceptor', function() {
|
|
|
|
it('should intercept', SX(async function() {
|
|
|
|
page.setRequestInterceptor(request => {
|
2017-06-28 06:31:38 +00:00
|
|
|
expect(request.url).toContain('empty.html');
|
|
|
|
expect(request.headers.has('User-Agent')).toBeTruthy();
|
|
|
|
expect(request.method).toBe('GET');
|
|
|
|
expect(request.postData).toBe(undefined);
|
2017-06-21 20:51:06 +00:00
|
|
|
request.continue();
|
|
|
|
});
|
2017-07-10 22:09:52 +00:00
|
|
|
const response = await page.navigate(EMPTY_PAGE);
|
|
|
|
expect(response.ok).toBe(true);
|
2017-06-21 20:51:06 +00:00
|
|
|
}));
|
2017-06-29 06:09:28 +00:00
|
|
|
it('should show custom HTTP headers', SX(async function() {
|
|
|
|
await page.setHTTPHeaders({
|
2017-06-21 20:51:06 +00:00
|
|
|
foo: 'bar'
|
|
|
|
});
|
|
|
|
page.setRequestInterceptor(request => {
|
2017-06-28 06:31:38 +00:00
|
|
|
expect(request.headers.get('foo')).toBe('bar');
|
2017-06-21 20:51:06 +00:00
|
|
|
request.continue();
|
|
|
|
});
|
2017-07-10 22:09:52 +00:00
|
|
|
const response = await page.navigate(EMPTY_PAGE);
|
|
|
|
expect(response.ok).toBe(true);
|
2017-06-21 20:51:06 +00:00
|
|
|
}));
|
|
|
|
it('should be abortable', SX(async function() {
|
|
|
|
page.setRequestInterceptor(request => {
|
2017-06-28 06:31:38 +00:00
|
|
|
if (request.url.endsWith('.css'))
|
2017-06-21 20:51:06 +00:00
|
|
|
request.abort();
|
|
|
|
else
|
|
|
|
request.continue();
|
|
|
|
});
|
2017-06-29 06:09:28 +00:00
|
|
|
let failedRequests = 0;
|
|
|
|
page.on('requestfailed', event => ++failedRequests);
|
2017-07-10 22:09:52 +00:00
|
|
|
const response = await page.navigate(PREFIX + '/one-style.html');
|
|
|
|
expect(response.ok).toBe(true);
|
2017-06-29 06:09:28 +00:00
|
|
|
expect(failedRequests).toBe(1);
|
2017-06-21 20:51:06 +00:00
|
|
|
}));
|
2017-06-28 06:31:38 +00:00
|
|
|
it('should amend HTTP headers', SX(async function() {
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
|
|
|
page.setRequestInterceptor(request => {
|
|
|
|
request.headers.set('foo', 'bar');
|
|
|
|
request.continue();
|
|
|
|
});
|
2017-07-07 15:14:25 +00:00
|
|
|
let serverRequest = server.waitForRequest('/sleep.zzz');
|
2017-06-28 06:31:38 +00:00
|
|
|
page.evaluate(() => {
|
|
|
|
fetch('/sleep.zzz');
|
|
|
|
});
|
|
|
|
let request = await serverRequest;
|
|
|
|
expect(request.headers['foo']).toBe('bar');
|
|
|
|
}));
|
2017-06-21 20:51:06 +00:00
|
|
|
});
|
2017-06-17 21:27:51 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
describe('Page.Events.Dialog', function() {
|
|
|
|
it('should fire', function(done) {
|
|
|
|
page.on('dialog', dialog => {
|
|
|
|
expect(dialog.type).toBe('alert');
|
|
|
|
expect(dialog.message()).toBe('yo');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
page.evaluate(() => alert('yo'));
|
2017-06-21 20:36:04 +00:00
|
|
|
});
|
2017-06-21 20:51:06 +00:00
|
|
|
// TODO Enable this when crbug.com/718235 is fixed.
|
|
|
|
xit('should allow accepting prompts', SX(async function(done) {
|
|
|
|
page.on('dialog', dialog => {
|
|
|
|
expect(dialog.type).toBe('prompt');
|
|
|
|
expect(dialog.message()).toBe('question?');
|
|
|
|
dialog.accept('answer!');
|
|
|
|
});
|
2017-06-22 20:38:10 +00:00
|
|
|
let result = await page.evaluate(() => prompt('question?'));
|
2017-06-21 20:51:06 +00:00
|
|
|
expect(result).toBe('answer!');
|
|
|
|
}));
|
|
|
|
});
|
2017-06-21 20:36:04 +00:00
|
|
|
|
2017-07-06 18:22:32 +00:00
|
|
|
describe('Page.Events.PageError', function() {
|
2017-06-21 20:51:06 +00:00
|
|
|
it('should fire', function(done) {
|
2017-07-06 18:22:32 +00:00
|
|
|
page.on('pageerror', error => {
|
2017-06-21 20:51:06 +00:00
|
|
|
expect(error.message).toContain('Fancy');
|
|
|
|
done();
|
|
|
|
});
|
2017-07-10 22:09:52 +00:00
|
|
|
page.navigate(PREFIX + '/error.html');
|
2017-06-21 20:36:04 +00:00
|
|
|
});
|
2017-06-21 20:51:06 +00:00
|
|
|
});
|
2017-06-17 21:27:51 +00:00
|
|
|
|
2017-06-28 08:10:23 +00:00
|
|
|
describe('Page.Events.Request', function() {
|
|
|
|
it('should fire', SX(async function(done) {
|
|
|
|
let requests = [];
|
|
|
|
page.on('request', request => requests.push(request));
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
|
|
|
expect(requests.length).toBe(1);
|
|
|
|
expect(requests[0].url).toContain('empty.html');
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
describe('Page.screenshot', function() {
|
|
|
|
it('should work', SX(async function() {
|
2017-07-18 01:13:04 +00:00
|
|
|
await page.setViewport({width: 500, height: 500});
|
2017-07-10 22:09:52 +00:00
|
|
|
await page.navigate(PREFIX + '/grid.html');
|
2017-06-22 20:38:10 +00:00
|
|
|
let screenshot = await page.screenshot();
|
2017-06-21 20:51:06 +00:00
|
|
|
expect(screenshot).toBeGolden('screenshot-sanity.png');
|
|
|
|
}));
|
|
|
|
it('should clip rect', SX(async function() {
|
2017-07-18 01:13:04 +00:00
|
|
|
await page.setViewport({width: 500, height: 500});
|
2017-07-10 22:09:52 +00:00
|
|
|
await page.navigate(PREFIX + '/grid.html');
|
2017-06-22 20:38:10 +00:00
|
|
|
let screenshot = await page.screenshot({
|
2017-06-21 20:51:06 +00:00
|
|
|
clip: {
|
|
|
|
x: 50,
|
|
|
|
y: 100,
|
|
|
|
width: 150,
|
|
|
|
height: 100
|
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(screenshot).toBeGolden('screenshot-clip-rect.png');
|
|
|
|
}));
|
|
|
|
it('should work for offscreen clip', SX(async function() {
|
2017-07-18 01:13:04 +00:00
|
|
|
await page.setViewport({width: 500, height: 500});
|
2017-07-10 22:09:52 +00:00
|
|
|
await page.navigate(PREFIX + '/grid.html');
|
2017-06-22 20:38:10 +00:00
|
|
|
let screenshot = await page.screenshot({
|
2017-06-21 20:51:06 +00:00
|
|
|
clip: {
|
|
|
|
x: 50,
|
|
|
|
y: 600,
|
|
|
|
width: 100,
|
|
|
|
height: 100
|
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(screenshot).toBeGolden('screenshot-offscreen-clip.png');
|
|
|
|
}));
|
|
|
|
it('should run in parallel', SX(async function() {
|
2017-07-18 01:13:04 +00:00
|
|
|
await page.setViewport({width: 500, height: 500});
|
2017-07-10 22:09:52 +00:00
|
|
|
await page.navigate(PREFIX + '/grid.html');
|
2017-06-22 20:38:10 +00:00
|
|
|
let promises = [];
|
|
|
|
for (let i = 0; i < 3; ++i) {
|
2017-06-21 20:51:06 +00:00
|
|
|
promises.push(page.screenshot({
|
|
|
|
clip: {
|
|
|
|
x: 50 * i,
|
|
|
|
y: 0,
|
|
|
|
width: 50,
|
|
|
|
height: 50
|
|
|
|
}
|
2017-06-21 20:36:04 +00:00
|
|
|
}));
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-07-19 07:40:52 +00:00
|
|
|
let screenshots = await Promise.all(promises);
|
|
|
|
expect(screenshots[1]).toBeGolden('screenshot-parallel-calls.png');
|
2017-06-21 20:51:06 +00:00
|
|
|
}));
|
|
|
|
it('should take fullPage screenshots', SX(async function() {
|
2017-07-18 01:13:04 +00:00
|
|
|
await page.setViewport({width: 500, height: 500});
|
2017-07-10 22:09:52 +00:00
|
|
|
await page.navigate(PREFIX + '/grid.html');
|
2017-06-22 20:38:10 +00:00
|
|
|
let screenshot = await page.screenshot({
|
2017-06-21 20:51:06 +00:00
|
|
|
fullPage: true
|
|
|
|
});
|
|
|
|
expect(screenshot).toBeGolden('screenshot-grid-fullpage.png');
|
|
|
|
}));
|
|
|
|
});
|
2017-06-17 21:27:51 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
describe('Frame Management', function() {
|
2017-06-22 20:38:10 +00:00
|
|
|
let FrameUtils = require('./frame-utils');
|
2017-06-21 20:51:06 +00:00
|
|
|
it('should handle nested frames', SX(async function() {
|
2017-07-10 22:09:52 +00:00
|
|
|
await page.navigate(PREFIX + '/frames/nested-frames.html');
|
2017-06-21 20:51:06 +00:00
|
|
|
expect(FrameUtils.dumpFrames(page.mainFrame())).toBeGolden('nested-frames.txt');
|
|
|
|
}));
|
|
|
|
it('should send events when frames are manipulated dynamically', SX(async function() {
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
|
|
|
// validate frameattached events
|
2017-06-22 20:38:10 +00:00
|
|
|
let attachedFrames = [];
|
2017-06-21 20:51:06 +00:00
|
|
|
page.on('frameattached', frame => attachedFrames.push(frame));
|
|
|
|
await FrameUtils.attachFrame(page, 'frame1', './assets/frame.html');
|
|
|
|
expect(attachedFrames.length).toBe(1);
|
|
|
|
expect(attachedFrames[0].url()).toContain('/assets/frame.html');
|
2017-06-17 21:27:51 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
// validate framenavigated events
|
2017-06-22 20:38:10 +00:00
|
|
|
let navigatedFrames = [];
|
2017-06-21 20:51:06 +00:00
|
|
|
page.on('framenavigated', frame => navigatedFrames.push(frame));
|
|
|
|
await FrameUtils.navigateFrame(page, 'frame1', './empty.html');
|
|
|
|
expect(navigatedFrames.length).toBe(1);
|
|
|
|
expect(navigatedFrames[0].url()).toContain('/empty.html');
|
2017-06-21 14:45:13 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
// validate framedetached events
|
2017-06-22 20:38:10 +00:00
|
|
|
let detachedFrames = [];
|
2017-06-21 20:51:06 +00:00
|
|
|
page.on('framedetached', frame => detachedFrames.push(frame));
|
|
|
|
await FrameUtils.detachFrame(page, 'frame1');
|
|
|
|
expect(detachedFrames.length).toBe(1);
|
|
|
|
expect(detachedFrames[0].isDetached()).toBe(true);
|
|
|
|
}));
|
|
|
|
it('should persist mainFrame on cross-process navigation', SX(async function() {
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
2017-06-22 20:38:10 +00:00
|
|
|
let mainFrame = page.mainFrame();
|
2017-06-21 20:51:06 +00:00
|
|
|
await page.navigate('http://127.0.0.1:' + PORT + '/empty.html');
|
|
|
|
expect(page.mainFrame() === mainFrame).toBeTruthy();
|
|
|
|
}));
|
|
|
|
it('should not send attach/detach events for main frame', SX(async function() {
|
2017-06-22 20:38:10 +00:00
|
|
|
let hasEvents = false;
|
2017-06-21 20:51:06 +00:00
|
|
|
page.on('frameattached', frame => hasEvents = true);
|
|
|
|
page.on('framedetached', frame => hasEvents = true);
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
|
|
|
expect(hasEvents).toBe(false);
|
|
|
|
}));
|
|
|
|
it('should detach child frames on navigation', SX(async function() {
|
2017-06-22 20:38:10 +00:00
|
|
|
let attachedFrames = [];
|
|
|
|
let detachedFrames = [];
|
|
|
|
let navigatedFrames = [];
|
2017-06-21 20:51:06 +00:00
|
|
|
page.on('frameattached', frame => attachedFrames.push(frame));
|
|
|
|
page.on('framedetached', frame => detachedFrames.push(frame));
|
|
|
|
page.on('framenavigated', frame => navigatedFrames.push(frame));
|
2017-07-10 22:09:52 +00:00
|
|
|
await page.navigate(PREFIX + '/frames/nested-frames.html');
|
2017-06-21 20:51:06 +00:00
|
|
|
expect(attachedFrames.length).toBe(4);
|
|
|
|
expect(detachedFrames.length).toBe(0);
|
|
|
|
expect(navigatedFrames.length).toBe(5);
|
|
|
|
|
2017-06-22 20:38:10 +00:00
|
|
|
attachedFrames = [];
|
|
|
|
detachedFrames = [];
|
|
|
|
navigatedFrames = [];
|
2017-06-21 20:51:06 +00:00
|
|
|
await page.navigate(EMPTY_PAGE);
|
|
|
|
expect(attachedFrames.length).toBe(0);
|
|
|
|
expect(detachedFrames.length).toBe(4);
|
|
|
|
expect(navigatedFrames.length).toBe(1);
|
|
|
|
}));
|
|
|
|
});
|
2017-06-28 01:27:22 +00:00
|
|
|
|
|
|
|
describe('input', function() {
|
|
|
|
it('should click the button', SX(async function() {
|
2017-07-10 22:09:52 +00:00
|
|
|
await page.navigate(PREFIX + '/input/button.html');
|
2017-06-28 01:27:22 +00:00
|
|
|
await page.click('button');
|
|
|
|
expect(await page.evaluate(() => result)).toBe('Clicked');
|
|
|
|
}));
|
2017-07-10 18:21:46 +00:00
|
|
|
it('should fail to click a missing button', SX(async function() {
|
2017-07-10 22:09:52 +00:00
|
|
|
await page.navigate(PREFIX + '/input/button.html');
|
2017-07-10 18:21:46 +00:00
|
|
|
try {
|
|
|
|
await page.click('button.does-not-exist');
|
|
|
|
fail('Clicking the button did not throw.');
|
|
|
|
} catch (error) {
|
|
|
|
expect(error.message).toBe('No node found for selector: button.does-not-exist');
|
|
|
|
}
|
|
|
|
}));
|
2017-06-28 01:27:22 +00:00
|
|
|
it('should type into the textarea', SX(async function() {
|
2017-07-10 22:09:52 +00:00
|
|
|
await page.navigate(PREFIX + '/input/textarea.html');
|
2017-06-28 01:27:22 +00:00
|
|
|
await page.focus('textarea');
|
|
|
|
await page.type('Type in this text!');
|
|
|
|
expect(await page.evaluate(() => result)).toBe('Type in this text!');
|
|
|
|
}));
|
|
|
|
it('should click the button after navigation ', SX(async function() {
|
2017-07-10 22:09:52 +00:00
|
|
|
await page.navigate(PREFIX + '/input/button.html');
|
2017-06-28 01:27:22 +00:00
|
|
|
await page.click('button');
|
2017-07-10 22:09:52 +00:00
|
|
|
await page.navigate(PREFIX + '/input/button.html');
|
2017-06-28 01:27:22 +00:00
|
|
|
await page.click('button');
|
|
|
|
expect(await page.evaluate(() => result)).toBe('Clicked');
|
|
|
|
}));
|
2017-07-10 18:21:46 +00:00
|
|
|
it('should upload the file', SX(async function(){
|
2017-07-10 22:09:52 +00:00
|
|
|
await page.navigate(PREFIX + '/input/fileupload.html');
|
2017-07-10 18:21:46 +00:00
|
|
|
await page.uploadFile('input', __dirname + '/assets/file-to-upload.txt');
|
|
|
|
expect(await page.evaluate(() => {
|
|
|
|
let input = document.querySelector('input');
|
|
|
|
return input.files[0].name;
|
|
|
|
})).toBe('file-to-upload.txt');
|
|
|
|
expect(await page.evaluate(() => {
|
|
|
|
let input = document.querySelector('input');
|
|
|
|
let reader = new FileReader();
|
|
|
|
let promise = new Promise(fulfill => reader.onload = fulfill);
|
|
|
|
reader.readAsText(input.files[0]);
|
|
|
|
return promise.then(() => reader.result);
|
|
|
|
})).toBe('contents of the file');
|
|
|
|
}));
|
2017-07-18 01:49:52 +00:00
|
|
|
it('should move with the arrow keys', SX(async function(){
|
|
|
|
await page.navigate(PREFIX + '/input/textarea.html');
|
|
|
|
await page.focus('textarea');
|
|
|
|
let keyboard = page.keyboard;
|
|
|
|
await keyboard.type('Hello World!');
|
|
|
|
expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('Hello World!');
|
|
|
|
for (let i = 0; i < 'World!'.length; i++)
|
|
|
|
keyboard.press('ArrowLeft');
|
|
|
|
await keyboard.type('inserted ');
|
|
|
|
expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('Hello inserted World!');
|
|
|
|
keyboard.hold('Shift');
|
|
|
|
for (let i = 0; i < 'inserted '.length; i++)
|
|
|
|
keyboard.press('ArrowLeft');
|
|
|
|
keyboard.release('Shift');
|
|
|
|
keyboard.hold('Backspace');
|
|
|
|
await keyboard.release('Backspace');
|
|
|
|
expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('Hello World!');
|
|
|
|
}));
|
|
|
|
it('should report shiftKey', SX(async function(){
|
|
|
|
await page.navigate(PREFIX + '/input/keyboard.html');
|
|
|
|
let keyboard = page.keyboard;
|
|
|
|
let codeForKey = {'Shift': 16, 'Alt': 18, 'Meta': 91, 'Control': 17};
|
|
|
|
for (let modifierKey in codeForKey) {
|
|
|
|
await keyboard.hold(modifierKey);
|
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keydown: ' + modifierKey + ' ' + codeForKey[modifierKey] + ' [' + modifierKey + ']');
|
|
|
|
await keyboard.hold('!');
|
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keydown: ! 49 [' + modifierKey + ']');
|
|
|
|
await keyboard.release('!');
|
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keyup: ! 49 [' + modifierKey + ']');
|
|
|
|
await keyboard.release(modifierKey);
|
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keyup: ' + modifierKey + ' ' + codeForKey[modifierKey] + ' []');
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
it('should report multiple modifiers', SX(async function(){
|
|
|
|
await page.navigate(PREFIX + '/input/keyboard.html');
|
|
|
|
let keyboard = page.keyboard;
|
|
|
|
await keyboard.hold('Control');
|
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keydown: Control 17 [Control]');
|
|
|
|
await keyboard.hold('Meta');
|
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keydown: Meta 91 [Control Meta]');
|
|
|
|
await keyboard.hold(';');
|
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keydown: ; 186 [Control Meta]');
|
|
|
|
await keyboard.release(';');
|
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keyup: ; 186 [Control Meta]');
|
|
|
|
await keyboard.release('Control');
|
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keyup: Control 17 [Meta]');
|
|
|
|
await keyboard.release('Meta');
|
|
|
|
expect(await page.evaluate(() => getResult())).toBe('Keyup: Meta 91 []');
|
|
|
|
}));
|
|
|
|
it('should send proper codes while typing', SX(async function(){
|
|
|
|
await page.navigate(PREFIX + '/input/keyboard.html');
|
|
|
|
let keyboard = page.keyboard;
|
|
|
|
await keyboard.type('!');
|
|
|
|
expect(await page.evaluate(() => getResult())).toBe(
|
|
|
|
[ 'Keydown: ! 49 []',
|
|
|
|
'Keypress: ! 33 33 33 []',
|
|
|
|
'Keyup: ! 49 []'].join('\n'));
|
|
|
|
await keyboard.type('^');
|
|
|
|
expect(await page.evaluate(() => getResult())).toBe(
|
|
|
|
[ 'Keydown: ^ 54 []',
|
|
|
|
'Keypress: ^ 94 94 94 []',
|
|
|
|
'Keyup: ^ 54 []'].join('\n'));
|
|
|
|
}));
|
|
|
|
it('should send propery codes while typing with shift', SX(async function(){
|
|
|
|
await page.navigate(PREFIX + '/input/keyboard.html');
|
|
|
|
let keyboard = page.keyboard;
|
|
|
|
await keyboard.hold('Shift');
|
|
|
|
await keyboard.type('~');
|
|
|
|
expect(await page.evaluate(() => getResult())).toBe(
|
|
|
|
[ 'Keydown: Shift 16 [Shift]',
|
|
|
|
'Keydown: ~ 192 [Shift]', // 192 is ` keyCode
|
|
|
|
'Keypress: ~ 126 126 126 [Shift]', // 126 is ~ charCode
|
|
|
|
'Keyup: ~ 192 [Shift]'].join('\n'));
|
|
|
|
await keyboard.release('Shift');
|
|
|
|
}));
|
|
|
|
it('should not type canceled events', SX(async function(){
|
|
|
|
await page.navigate(PREFIX + '/input/textarea.html');
|
|
|
|
await page.focus('textarea');
|
|
|
|
await page.evaluate(() => {
|
|
|
|
window.addEventListener('keydown', event => {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
if (event.key === 'l')
|
|
|
|
event.preventDefault();
|
|
|
|
if (event.key === 'o')
|
|
|
|
Promise.resolve().then(() => event.preventDefault());
|
|
|
|
}, false);
|
|
|
|
});
|
|
|
|
let keyboard = page.keyboard;
|
|
|
|
await keyboard.type('Hello World!');
|
|
|
|
expect(await page.evaluate(() => textarea.value)).toBe('He Wrd!');
|
|
|
|
}));
|
|
|
|
it('keyboard.modifiers()', SX(async function(){
|
|
|
|
let keyboard = page.keyboard;
|
|
|
|
expect(keyboard.modifiers().Shift).toBe(false);
|
|
|
|
expect(keyboard.modifiers().Meta).toBe(false);
|
|
|
|
expect(keyboard.modifiers().Alt).toBe(false);
|
|
|
|
expect(keyboard.modifiers().Control).toBe(false);
|
|
|
|
keyboard.hold('Shift');
|
|
|
|
expect(keyboard.modifiers().Shift).toBe(true);
|
|
|
|
expect(keyboard.modifiers().Alt).toBe(false);
|
|
|
|
keyboard.release('Shift');
|
|
|
|
expect(keyboard.modifiers().Shift).toBe(false);
|
|
|
|
expect(keyboard.modifiers().Alt).toBe(false);
|
|
|
|
}));
|
2017-06-28 01:27:22 +00:00
|
|
|
});
|
2017-06-29 06:09:28 +00:00
|
|
|
describe('Page.setUserAgent', function() {
|
|
|
|
it('should work', SX(async function() {
|
|
|
|
expect(page.userAgent()).toContain('Mozilla');
|
|
|
|
page.setUserAgent('foobar');
|
|
|
|
page.navigate(EMPTY_PAGE);
|
2017-07-07 15:14:25 +00:00
|
|
|
let request = await server.waitForRequest('/empty.html');
|
2017-06-29 06:09:28 +00:00
|
|
|
expect(request.headers['user-agent']).toBe('foobar');
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
describe('Page.setHTTPHeaders', function() {
|
|
|
|
it('should work', SX(async function() {
|
|
|
|
expect(page.httpHeaders()).toEqual({});
|
|
|
|
page.setHTTPHeaders({'foo': 'bar'});
|
|
|
|
expect(page.httpHeaders()).toEqual({'foo': 'bar'});
|
|
|
|
page.navigate(EMPTY_PAGE);
|
2017-07-07 15:14:25 +00:00
|
|
|
let request = await server.waitForRequest('/empty.html');
|
2017-06-29 06:09:28 +00:00
|
|
|
expect(request.headers['foo']).toBe('bar');
|
|
|
|
}));
|
|
|
|
});
|
2017-07-06 01:09:17 +00:00
|
|
|
describe('Page.setContent', function() {
|
|
|
|
it('should work', SX(async function() {
|
|
|
|
await page.setContent('<div>hello</div>');
|
|
|
|
let result = await page.evaluate(() => document.body.innerHTML);
|
|
|
|
expect(result).toBe('<div>hello</div>');
|
|
|
|
}));
|
|
|
|
});
|
2017-07-06 02:06:47 +00:00
|
|
|
describe('Request implements Body', function() {
|
|
|
|
it('should work', SX(async function() {
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
2017-07-07 15:14:25 +00:00
|
|
|
server.setRoute('/post', (req, res) => res.end());
|
2017-07-06 02:06:47 +00:00
|
|
|
let request = null;
|
|
|
|
page.on('request', r => request = r);
|
|
|
|
await page.evaluate(() => fetch('./post', { method: 'POST', body: JSON.stringify({foo: 'bar'})}));
|
|
|
|
expect(request).toBeTruthy();
|
|
|
|
expect(request.bodyUsed).toBe(false);
|
|
|
|
expect(await request.text()).toBe('{"foo":"bar"}');
|
|
|
|
expect(await request.json()).toEqual({foo: 'bar'});
|
|
|
|
expect(request.bodyUsed).toBe(true);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
describe('Response implements Body', function() {
|
|
|
|
it('should work', SX(async function() {
|
|
|
|
let response = null;
|
|
|
|
page.on('response', r => response = r);
|
2017-07-10 22:09:52 +00:00
|
|
|
await page.navigate(PREFIX + '/simple.json');
|
2017-07-06 02:06:47 +00:00
|
|
|
expect(response).toBeTruthy();
|
|
|
|
expect(response.bodyUsed).toBe(false);
|
|
|
|
expect(await response.text()).toBe('{"foo": "bar"}\n');
|
|
|
|
expect(await response.json()).toEqual({foo: 'bar'});
|
|
|
|
expect(response.bodyUsed).toBe(true);
|
|
|
|
}));
|
|
|
|
});
|
2017-06-29 06:09:28 +00:00
|
|
|
describe('Network Events', function() {
|
|
|
|
it('Page.Events.Request', SX(async function() {
|
|
|
|
let requests = [];
|
|
|
|
page.on('request', request => requests.push(request));
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
|
|
|
expect(requests.length).toBe(1);
|
|
|
|
expect(requests[0].url).toBe(EMPTY_PAGE);
|
|
|
|
expect(requests[0].method).toBe('GET');
|
|
|
|
expect(requests[0].response()).toBeTruthy();
|
|
|
|
}));
|
|
|
|
it('Page.Events.Response', SX(async function() {
|
|
|
|
let responses = [];
|
|
|
|
page.on('response', response => responses.push(response));
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
|
|
|
expect(responses.length).toBe(1);
|
|
|
|
expect(responses[0].url).toBe(EMPTY_PAGE);
|
|
|
|
expect(responses[0].status).toBe(200);
|
|
|
|
expect(responses[0].ok).toBe(true);
|
|
|
|
expect(responses[0].request()).toBeTruthy();
|
|
|
|
}));
|
|
|
|
it('Page.Events.RequestFailed', SX(async function() {
|
|
|
|
page.setRequestInterceptor(request => {
|
|
|
|
if (request.url.endsWith('css'))
|
|
|
|
request.abort();
|
|
|
|
else
|
|
|
|
request.continue();
|
|
|
|
});
|
|
|
|
let failedRequests = [];
|
|
|
|
page.on('requestfailed', request => failedRequests.push(request));
|
2017-07-10 22:09:52 +00:00
|
|
|
await page.navigate(PREFIX + '/one-style.html');
|
2017-06-29 06:09:28 +00:00
|
|
|
expect(failedRequests.length).toBe(1);
|
|
|
|
expect(failedRequests[0].url).toContain('one-style.css');
|
|
|
|
expect(failedRequests[0].response()).toBe(null);
|
|
|
|
}));
|
|
|
|
it('Page.Events.RequestFinished', SX(async function() {
|
|
|
|
let requests = [];
|
|
|
|
page.on('requestfinished', request => requests.push(request));
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
|
|
|
expect(requests.length).toBe(1);
|
|
|
|
expect(requests[0].url).toBe(EMPTY_PAGE);
|
|
|
|
expect(requests[0].response()).toBeTruthy();
|
|
|
|
}));
|
|
|
|
it('should fire events in proper order', SX(async function() {
|
|
|
|
let events = [];
|
|
|
|
page.on('request', request => events.push('request'));
|
|
|
|
page.on('response', response => events.push('response'));
|
|
|
|
page.on('requestfinished', request => events.push('requestfinished'));
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
|
|
|
expect(events).toEqual(['request', 'response', 'requestfinished']);
|
|
|
|
}));
|
2017-06-30 01:54:01 +00:00
|
|
|
it('should support redirects', SX(async function() {
|
|
|
|
let events = [];
|
|
|
|
page.on('request', request => events.push(`${request.method} ${request.url}`));
|
|
|
|
page.on('response', response => events.push(`${response.status} ${response.url}`));
|
|
|
|
page.on('requestfinished', request => events.push(`DONE ${request.url}`));
|
|
|
|
page.on('requestfailed', request => events.push(`FAIL ${request.url}`));
|
2017-07-07 15:14:25 +00:00
|
|
|
server.setRedirect('/foo.html', '/empty.html');
|
2017-07-10 22:09:52 +00:00
|
|
|
const FOO_URL = PREFIX + '/foo.html';
|
2017-06-30 01:54:01 +00:00
|
|
|
await page.navigate(FOO_URL);
|
|
|
|
expect(events).toEqual([
|
|
|
|
`GET ${FOO_URL}`,
|
|
|
|
`302 ${FOO_URL}`,
|
|
|
|
`DONE ${FOO_URL}`,
|
|
|
|
`GET ${EMPTY_PAGE}`,
|
|
|
|
`200 ${EMPTY_PAGE}`,
|
|
|
|
`DONE ${EMPTY_PAGE}`
|
|
|
|
]);
|
|
|
|
}));
|
2017-06-29 06:09:28 +00:00
|
|
|
});
|
2017-07-10 17:09:59 +00:00
|
|
|
|
|
|
|
describe('Page.addScriptTag', function() {
|
|
|
|
it('should work', SX(async function() {
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
|
|
|
await page.addScriptTag('/injectedfile.js');
|
|
|
|
expect(await page.evaluate(() => __injected)).toBe(42);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Page.url', function() {
|
|
|
|
it('should work', SX(async function() {
|
2017-07-14 21:05:27 +00:00
|
|
|
expect(page.url()).toBe('about:blank');
|
2017-07-10 17:09:59 +00:00
|
|
|
await page.navigate(EMPTY_PAGE);
|
2017-07-14 21:05:27 +00:00
|
|
|
expect(page.url()).toBe(EMPTY_PAGE);
|
2017-07-10 17:09:59 +00:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2017-07-18 01:13:04 +00:00
|
|
|
describe('Page.viewport', function() {
|
2017-07-10 17:09:59 +00:00
|
|
|
it('should get the proper viewport size', SX(async function() {
|
2017-07-18 01:13:04 +00:00
|
|
|
expect(page.viewport()).toEqual({width: 400, height: 300});
|
|
|
|
await page.setViewport({width: 123, height: 456});
|
|
|
|
expect(page.viewport()).toEqual({width: 123, height: 456});
|
2017-07-10 17:09:59 +00:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Page.evaluateOnInitialized', function() {
|
|
|
|
it('should evaluate before anything else on the page', SX(async function() {
|
|
|
|
await page.evaluateOnInitialized(function(){
|
|
|
|
window.injected = 123;
|
|
|
|
});
|
2017-07-10 22:09:52 +00:00
|
|
|
await page.navigate(PREFIX + '/tamperable.html');
|
2017-07-10 17:09:59 +00:00
|
|
|
expect(await page.evaluate(() => window.result)).toBe(123);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2017-07-17 09:06:52 +00:00
|
|
|
describe('Page.pdf', function() {
|
2017-07-10 17:09:59 +00:00
|
|
|
let outputFile = __dirname + '/assets/output.pdf';
|
|
|
|
afterEach(function() {
|
|
|
|
fs.unlinkSync(outputFile);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should print to pdf', SX(async function() {
|
2017-07-10 22:09:52 +00:00
|
|
|
await page.navigate(PREFIX + '/grid.html');
|
2017-07-17 09:06:52 +00:00
|
|
|
await page.pdf({path: outputFile});
|
2017-07-10 17:09:59 +00:00
|
|
|
expect(fs.readFileSync(outputFile).byteLength).toBeGreaterThan(0);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Page.plainText', function() {
|
|
|
|
it('should return the page text', SX(async function(){
|
|
|
|
await page.setContent('<div>the result text</div>');
|
|
|
|
expect(await page.plainText()).toBe('the result text');
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Page.title', function() {
|
|
|
|
it('should return the page title', SX(async function(){
|
2017-07-10 22:09:52 +00:00
|
|
|
await page.navigate(PREFIX + '/input/button.html');
|
2017-07-10 17:09:59 +00:00
|
|
|
expect(await page.title()).toBe('Button test');
|
|
|
|
}));
|
|
|
|
});
|
2017-07-18 01:56:56 +00:00
|
|
|
|
|
|
|
describe('Query selector', function() {
|
|
|
|
it('Page.$', SX(async function() {
|
|
|
|
await page.navigate(PREFIX + '/playground.html');
|
|
|
|
expect(await page.$('#first', element => element.textContent)).toBe('First div');
|
|
|
|
expect(await page.$('#second span', element => element.textContent)).toBe('Inner span');
|
|
|
|
expect(await page.$('#first', (element, arg1) => arg1, 'value1')).toBe('value1');
|
|
|
|
expect(await page.$('#first', (element, arg1, arg2) => arg2, 'value1', 'value2')).toBe('value2');
|
|
|
|
expect(await page.$('doesnot-exist', element => 5)).toBe(null);
|
|
|
|
expect(await page.$('button', function(element, arg1) {
|
|
|
|
element.textContent = arg1;
|
|
|
|
return true;
|
|
|
|
}, 'new button text')).toBe(true);
|
|
|
|
expect(await page.$('button', function(element) {
|
|
|
|
return element.textContent;
|
|
|
|
})).toBe('new button text');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('Page.$$', SX(async function() {
|
|
|
|
await page.navigate(PREFIX + '/playground.html');
|
|
|
|
expect((await page.$$('div', element => element.textContent)).length).toBe(2);
|
|
|
|
expect((await page.$$('div', (element, index) => index))[0]).toBe(0);
|
|
|
|
expect((await page.$$('div', (element, index) => index))[1]).toBe(1);
|
|
|
|
expect((await page.$$('doesnotexist', function(){})).length).toBe(0);
|
|
|
|
expect((await page.$$('div', element => element.textContent))[0]).toBe('First div');
|
|
|
|
expect((await page.$$('span', (element, index, arg1) => arg1, 'value1'))[0]).toBe('value1');
|
|
|
|
}));
|
|
|
|
});
|
2017-05-12 23:36:37 +00:00
|
|
|
});
|
|
|
|
|
2017-07-17 08:57:37 +00:00
|
|
|
/**
|
|
|
|
* @param {!EventEmitter} emitter
|
|
|
|
* @param {string} eventName
|
|
|
|
* @param {number=} eventCount
|
|
|
|
* @return {!Promise}
|
|
|
|
*/
|
|
|
|
function waitForEvents(emitter, eventName, eventCount = 1) {
|
|
|
|
let fulfill;
|
|
|
|
let promise = new Promise(x => fulfill = x);
|
|
|
|
emitter.on(eventName, onEvent);
|
|
|
|
return promise;
|
|
|
|
|
|
|
|
function onEvent() {
|
|
|
|
--eventCount;
|
|
|
|
if (eventCount)
|
|
|
|
return;
|
|
|
|
emitter.removeListener(eventName, onEvent);
|
|
|
|
fulfill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 23:36:37 +00:00
|
|
|
// Since Jasmine doesn't like async functions, they should be wrapped
|
|
|
|
// in a SX function.
|
|
|
|
function SX(fun) {
|
2017-06-21 20:51:06 +00:00
|
|
|
return done => Promise.resolve(fun()).then(done).catch(done.fail);
|
2017-05-12 23:36:37 +00:00
|
|
|
}
|