test(firefox): further unify Puppeteer-Firefox and Puppeteer tests (#3894)

This patch:
- changes Puppeteer-Firefox plumbing of defaultBrowserOptions to align
  with the way we do it for Puppeteer.
- plumbs puppeeteer-dependent Errors and DeviceDescriptors down to every
  test.
- unifies a few tests between Puppeteer-Firefox and Puppeteer.

**Note:** in future, we should expose errors as `puppeteer.errors` and
device descriptors as `puppeteer.devices` to make it easy to pass around
Puppeteer/Puppeteer-Firefox instance.

References #3889.
This commit is contained in:
Andrey Lushnikov 2019-02-02 18:49:12 -07:00 committed by GitHub
parent 47fbb117f5
commit 69c434af75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 185 additions and 85 deletions

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>console.log test</title>
</head>
<body>
<script>
console.log('yellow')
</script>
</body>
</html>

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
module.exports.addTests = function({testRunner, expect, product, puppeteer}) {
module.exports.addTests = function({testRunner, expect, product, puppeteer, defaultBrowserOptions}) {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
@ -23,7 +23,7 @@ module.exports.addTests = function({testRunner, expect, product, puppeteer}) {
describe('ignoreHTTPSErrors', function() {
beforeAll(async state => {
const options = Object.assign({ignoreHTTPSErrors: true}, state.defaultBrowserOptions);
const options = Object.assign({ignoreHTTPSErrors: true}, defaultBrowserOptions);
state.browser = await puppeteer.launch(options);
});
afterAll(async state => {

View File

@ -66,7 +66,7 @@ module.exports.addTests = function({testRunner, expect, product}) {
await page.keyboard.sendCharacter('a');
expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('嗨a');
});
it('should press the metaKey', async ({page}) => {
it('should press the metaKey', async({page}) => {
await page.evaluate(() => {
window.keyPromise = new Promise(resolve => document.addEventListener('keydown', event => resolve(event.key)));
});

View File

@ -15,7 +15,7 @@
*/
const fs = require('fs');
module.exports.addTests = function({testRunner, expect, product, puppeteer}) {
module.exports.addTests = function({testRunner, expect, product, puppeteer, defaultBrowserOptions}) {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
@ -23,14 +23,14 @@ module.exports.addTests = function({testRunner, expect, product, puppeteer}) {
const FFOX = product === 'firefox';
const CHROME = product === 'chromium';
describe('Launcher.executablePath', function() {
it('should work', async() => {
const executablePath = puppeteer.executablePath({product});
it('should work', async({server}) => {
const executablePath = puppeteer.executablePath();
expect(fs.existsSync(executablePath)).toBe(true);
});
});
describe('Launcher.launch', () => {
it('should set the default viewport', async({defaultBrowserOptions}) => {
it('should set the default viewport', async() => {
const options = Object.assign({}, defaultBrowserOptions, {
defaultViewport: {
width: 456,
@ -43,7 +43,7 @@ module.exports.addTests = function({testRunner, expect, product, puppeteer}) {
expect(await page.evaluate('window.innerHeight')).toBe(789);
await browser.close();
});
it('should disable the default viewport', async({defaultBrowserOptions}) => {
it('should disable the default viewport', async() => {
const options = Object.assign({}, defaultBrowserOptions, {
defaultViewport: null
});

View File

@ -75,12 +75,11 @@ module.exports.addTests = function({testRunner, expect, product}) {
await newPage.close();
expect(newPage.isClosed()).toBe(true);
});
it('should emit the close event', async({browser}) => {
const newPage = await browser.newPage();
let gotClosedEvent = false;
newPage.on('close', () => gotClosedEvent = true);
it('should work with page.close', async function({ page, context, server }) {
const newPage = await context.newPage();
const closedPromise = new Promise(x => newPage.on('close', x));
await newPage.close();
expect(gotClosedEvent).toBe(true);
await closedPromise;
});
});

View File

@ -28,29 +28,28 @@ module.exports.addTests = ({testRunner, product, puppeteer}) => testRunner.descr
toBeGolden: GoldenUtils.compare.bind(null, GOLDEN_DIR, OUTPUT_DIR)
});
const defaultBrowserOptions = {
handleSIGINT: false,
executablePath: product === 'chromium' ? process.env.CHROME : process.env.FFOX,
dumpio: !!process.env.DUMPIO,
args: product === 'chromium' ? ['--no-sandbox'] : [],
};
beforeAll(async state => {
state.defaultBrowserOptions = {
handleSIGINT: false,
executablePath: product === 'chromium' ? process.env.CHROME : process.env.FFOX,
dumpio: !!process.env.DUMPIO,
args: product === 'chromium' ? ['--no-sandbox'] : [],
};
if (product === 'firefox' && state.defaultBrowserOptions.executablePath) {
await require('../misc/install-preferences')(state.defaultBrowserOptions.executablePath);
console.log('RUNNING CUSTOM FIREFOX: ' + state.defaultBrowserOptions.executablePath);
}
});
afterAll(state => {
state.defaultBrowserOptions = undefined;
});
const testOptions = {testRunner, expect, product, puppeteer, defaultBrowserOptions};
require('./launcher.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./ignorehttpserrors.spec.js').addTests({testRunner, expect, product, puppeteer});
if (product === 'firefox' && defaultBrowserOptions.executablePath) {
beforeAll(async () => {
await require('../misc/install-preferences')(defaultBrowserOptions.executablePath);
console.log('RUNNING CUSTOM FIREFOX: ' + defaultBrowserOptions.executablePath);
});
}
require('./launcher.spec.js').addTests(testOptions);
require('./ignorehttpserrors.spec.js').addTests(testOptions);
describe('Browser', () => {
beforeAll(async state => {
state.browser = await puppeteer.launch(state.defaultBrowserOptions);
state.browser = await puppeteer.launch(defaultBrowserOptions);
});
afterAll(async state => {
@ -58,8 +57,8 @@ module.exports.addTests = ({testRunner, product, puppeteer}) => testRunner.descr
state.browser = null;
});
require('./browser.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./browsercontext.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./browser.spec.js').addTests(testOptions);
require('./browsercontext.spec.js').addTests(testOptions);
describe('Page', () => {
beforeEach(async state => {
@ -73,31 +72,31 @@ module.exports.addTests = ({testRunner, product, puppeteer}) => testRunner.descr
state.page = null;
});
require('./page.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./evaluation.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./navigation.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./dialog.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./frame.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./jshandle.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./elementhandle.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./target.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./waittask.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./queryselector.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./emulation.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./screenshot.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./page.spec.js').addTests(testOptions);
require('./evaluation.spec.js').addTests(testOptions);
require('./navigation.spec.js').addTests(testOptions);
require('./dialog.spec.js').addTests(testOptions);
require('./frame.spec.js').addTests(testOptions);
require('./jshandle.spec.js').addTests(testOptions);
require('./elementhandle.spec.js').addTests(testOptions);
require('./target.spec.js').addTests(testOptions);
require('./waittask.spec.js').addTests(testOptions);
require('./queryselector.spec.js').addTests(testOptions);
require('./emulation.spec.js').addTests(testOptions);
require('./screenshot.spec.js').addTests(testOptions);
// Input tests
require('./keyboard.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./mouse.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./click.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./type.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./hover.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./keyboard.spec.js').addTests(testOptions);
require('./mouse.spec.js').addTests(testOptions);
require('./click.spec.js').addTests(testOptions);
require('./type.spec.js').addTests(testOptions);
require('./hover.spec.js').addTests(testOptions);
// Browser-specific page tests
if (product === 'firefox')
require('./firefoxonly.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./firefoxonly.spec.js').addTests(testOptions);
else
require('./chromiumonly.spec.js').addTests({testRunner, expect, product, puppeteer});
require('./chromiumonly.spec.js').addTests(testOptions);
});
});
});

View File

@ -80,18 +80,18 @@ module.exports.addTests = function({testRunner, expect, product}) {
describe('Browser.waitForTarget', () => {
it('should wait for a target', async function({browser, server}) {
let resolved = false;
const targetPromise = browser.waitForTarget(target => target.url() === server.EMPTY_PAGE2);
const targetPromise = browser.waitForTarget(target => target.url() === server.EMPTY_PAGE);
targetPromise.then(() => resolved = true);
const page = await browser.newPage();
expect(resolved).toBe(false);
await page.goto(server.EMPTY_PAGE2);
await page.goto(server.EMPTY_PAGE);
const target = await targetPromise;
expect(await target.page()).toBe(page);
await page.close();
});
it('should timeout waiting for a non-existent target', async function({browser, server}) {
let error = null;
await browser.waitForTarget(target => target.url() === server.EMPTY_PAGE2, {
await browser.waitForTarget(target => target.url() === server.EMPTY_PAGE, {
timeout: 1
}).catch(e => error = e);
expect(error).toBeInstanceOf(TimeoutError);

View File

@ -123,11 +123,9 @@ module.exports.addTests = function({testRunner, expect, product}) {
await watchdog;
});
it('should survive navigations', async({page, server}) => {
const watchdog = page.waitForFunction(() => {
return window.__done;
});
const watchdog = page.waitForFunction(() => window.__done);
await page.goto(server.EMPTY_PAGE);
await page.goto(server.EMPTY_PAGE2);
await page.goto(server.PREFIX + '/consolelog.html');
await page.evaluate(() => window.__done = true);
await watchdog;
});

0
test/assets/empty2.html Normal file
View File

View File

@ -15,12 +15,12 @@
*/
const utils = require('./utils');
const {TimeoutError} = utils.requireRoot('Errors');
module.exports.addTests = function({testRunner, expect, puppeteer}) {
module.exports.addTests = function({testRunner, expect, puppeteer, Errors}) {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
const {TimeoutError} = Errors;
describe('BrowserContext', function() {
it('should have default context', async function({browser, server}) {

View File

@ -15,19 +15,30 @@
*/
const utils = require('./utils');
const os = require('os');
module.exports.addTests = function({testRunner, expect}) {
module.exports.addTests = function({testRunner, expect, FFOX}) {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
describe('Keyboard', function() {
it('should type into the textarea', async({page, server}) => {
await page.goto(server.PREFIX + '/input/textarea.html');
const textarea = await page.$('textarea');
await textarea.type('Type in this text!');
expect(await page.evaluate(() => result)).toBe('Type in this text!');
it('should type into a textarea', async({page, server}) => {
await page.evaluate(() => {
const textarea = document.createElement('textarea');
document.body.appendChild(textarea);
textarea.focus();
});
const text = 'Hello world. I am the text that was typed!';
await page.keyboard.type(text);
expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe(text);
});
it('should press the metaKey', async({page}) => {
await page.evaluate(() => {
window.keyPromise = new Promise(resolve => document.addEventListener('keydown', event => resolve(event.key)));
});
await page.keyboard.press('Meta');
expect(await page.evaluate('keyPromise')).toBe(FFOX && os.platform() !== 'darwin' ? 'OS' : 'Meta');
});
it('should move with the arrow keys', async({page, server}) => {
await page.goto(server.PREFIX + '/input/textarea.html');

View File

@ -24,12 +24,36 @@ function dimensions() {
};
}
module.exports.addTests = function({testRunner, expect}) {
module.exports.addTests = function({testRunner, expect, FFOX}) {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
describe('Mouse', function() {
it('should click the document', async({page, server}) => {
await page.evaluate(() => {
window.clickPromise = new Promise(resolve => {
document.addEventListener('click', event => {
resolve({
type: event.type,
detail: event.detail,
clientX: event.clientX,
clientY: event.clientY,
isTrusted: event.isTrusted,
button: event.button
});
});
});
});
await page.mouse.click(50, 60);
const event = await page.evaluate(() => window.clickPromise);
expect(event.type).toBe('click');
expect(event.detail).toBe(1);
expect(event.clientX).toBe(50);
expect(event.clientY).toBe(60);
expect(event.isTrusted).toBe(true);
expect(event.button).toBe(0);
});
it('should resize the textarea', async({page, server}) => {
await page.goto(server.PREFIX + '/input/textarea.html');
const {x, y, width, height} = await page.evaluate(dimensions);
@ -74,17 +98,20 @@ module.exports.addTests = function({testRunner, expect}) {
await page.goto(server.PREFIX + '/input/scrollable.html');
await page.evaluate(() => document.querySelector('#button-3').addEventListener('mousedown', e => window.lastEvent = e, true));
const modifiers = {'Shift': 'shiftKey', 'Control': 'ctrlKey', 'Alt': 'altKey', 'Meta': 'metaKey'};
// In Firefox, the Meta modifier only exists on Mac
if (FFOX && os.platform() !== 'darwin')
delete modifiers['Meta'];
for (const modifier in modifiers) {
await page.keyboard.down(modifier);
await page.click('#button-3');
if (!(await page.evaluate(mod => window.lastEvent[mod], modifiers[modifier])))
fail(modifiers[modifier] + ' should be true');
throw new Error(modifiers[modifier] + ' should be true');
await page.keyboard.up(modifier);
}
await page.click('#button-3');
for (const modifier in modifiers) {
if ((await page.evaluate(mod => window.lastEvent[mod], modifiers[modifier])))
fail(modifiers[modifier] + ' should be false');
throw new Error(modifiers[modifier] + ' should be false');
}
});
it('should tween mouse movement', async({page, server}) => {

View File

@ -15,14 +15,24 @@
*/
const utils = require('./utils');
const {TimeoutError} = utils.requireRoot('Errors');
module.exports.addTests = function({testRunner, expect}) {
module.exports.addTests = function({testRunner, expect, Errors}) {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
const {TimeoutError} = Errors;
describe('Page.goto', function() {
it('should work', async({page, server}) => {
await page.goto(server.EMPTY_PAGE);
expect(page.url()).toBe(server.EMPTY_PAGE);
});
it('should work with redirects', async({page, server}) => {
server.setRedirect('/redirect/1.html', '/redirect/2.html');
server.setRedirect('/redirect/2.html', '/empty.html');
await page.goto(server.PREFIX + '/redirect/1.html');
expect(page.url()).toBe(server.EMPTY_PAGE);
});
it('should navigate to about:blank', async({page, server}) => {
const response = await page.goto('about:blank');
expect(response).toBe(null);
@ -523,4 +533,13 @@ module.exports.addTests = function({testRunner, expect}) {
await navigationPromise;
});
});
describe('Page.reload', function() {
it('should work', async({page, server}) => {
await page.goto(server.EMPTY_PAGE);
await page.evaluate(() => window._foo = 10);
await page.reload();
expect(await page.evaluate(() => window._foo)).toBe(undefined);
});
});
};

View File

@ -17,10 +17,6 @@ const fs = require('fs');
const path = require('path');
const utils = require('./utils');
const {waitEvent} = utils;
const {TimeoutError} = utils.requireRoot('Errors');
const DeviceDescriptors = utils.requireRoot('DeviceDescriptors');
const iPhone = DeviceDescriptors['iPhone 6'];
let asyncawait = true;
try {
@ -29,11 +25,14 @@ try {
asyncawait = false;
}
module.exports.addTests = function({testRunner, expect, headless}) {
module.exports.addTests = function({testRunner, expect, headless, Errors, DeviceDescriptors}) {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
const {TimeoutError} = Errors;
const iPhone = DeviceDescriptors['iPhone 6'];
describe('Page.close', function() {
it('should reject all promises when page is closed', async({context}) => {
const newPage = await context.newPage();

View File

@ -22,7 +22,7 @@ const {Matchers} = require('../utils/testrunner/');
const YELLOW_COLOR = '\x1b[33m';
const RESET_COLOR = '\x1b[0m';
module.exports.addTests = ({testRunner, product, puppeteer, defaultBrowserOptions}) => {
module.exports.addTests = ({testRunner, product, puppeteer, Errors, DeviceDescriptors, defaultBrowserOptions}) => {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
@ -52,6 +52,8 @@ module.exports.addTests = ({testRunner, product, puppeteer, defaultBrowserOption
FFOX,
CHROME,
puppeteer,
Errors,
DeviceDescriptors,
expect,
defaultBrowserOptions,
headless: !!defaultBrowserOptions.headless,

View File

@ -17,10 +17,11 @@
const utils = require('./utils');
const {waitEvent} = utils;
module.exports.addTests = function({testRunner, expect}) {
module.exports.addTests = function({testRunner, expect, puppeteer, Errors}) {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
const {TimeoutError} = Errors;
describe('Target', function() {
it('Browser.targets should return all of the targets', async({page, server, browser}) => {
@ -142,4 +143,25 @@ module.exports.addTests = function({testRunner, expect}) {
expect(page.target().opener()).toBe(null);
});
});
describe('Browser.waitForTarget', () => {
it('should wait for a target', async function({browser, server}) {
let resolved = false;
const targetPromise = browser.waitForTarget(target => target.url() === server.EMPTY_PAGE);
targetPromise.then(() => resolved = true);
const page = await browser.newPage();
expect(resolved).toBe(false);
await page.goto(server.EMPTY_PAGE);
const target = await targetPromise;
expect(await target.page()).toBe(page);
await page.close();
});
it('should timeout waiting for a non-existent target', async function({browser, server}) {
let error = null;
await browser.waitForTarget(target => target.url() === server.EMPTY_PAGE, {
timeout: 1
}).catch(e => error = e);
expect(error).toBeInstanceOf(TimeoutError);
});
});
};

View File

@ -53,6 +53,7 @@ beforeAll(async state => {
state.server.PREFIX = `http://localhost:${port}`;
state.server.CROSS_PROCESS_PREFIX = `http://127.0.0.1:${port}`;
state.server.EMPTY_PAGE = `http://localhost:${port}/empty.html`;
state.server.EMPTY_PAGE2 = `http://localhost:${port}/empty2.html`;
const httpsPort = port + 1;
state.httpsServer = await TestServer.createHTTPS(assetsPath, httpsPort);
@ -61,6 +62,7 @@ beforeAll(async state => {
state.httpsServer.PREFIX = `https://localhost:${httpsPort}`;
state.httpsServer.CROSS_PROCESS_PREFIX = `https://127.0.0.1:${httpsPort}`;
state.httpsServer.EMPTY_PAGE = `https://localhost:${httpsPort}/empty.html`;
state.httpsServer.EMPTY_PAGE2 = `https://localhost:${httpsPort}/empty2.html`;
});
afterAll(async({server, httpsServer}) => {
@ -89,6 +91,8 @@ if (process.env.BROWSER !== 'firefox') {
require('./puppeteer.spec.js').addTests({
product: 'Chromium',
puppeteer: utils.requireRoot('index'),
Errors: utils.requireRoot('Errors'),
DeviceDescriptors: utils.requireRoot('DeviceDescriptors'),
defaultBrowserOptions,
testRunner,
});
@ -100,6 +104,8 @@ if (process.env.BROWSER !== 'firefox') {
require('./puppeteer.spec.js').addTests({
product: 'Firefox',
puppeteer: require('../experimental/puppeteer-firefox'),
Errors: require('../experimental/puppeteer-firefox/Errors'),
DeviceDescriptors: utils.requireRoot('DeviceDescriptors'),
defaultBrowserOptions,
testRunner,
});

View File

@ -15,7 +15,6 @@
*/
const utils = require('./utils');
const {TimeoutError} = utils.requireRoot('Errors');
let asyncawait = true;
try {
@ -24,10 +23,11 @@ try {
asyncawait = false;
}
module.exports.addTests = function({testRunner, expect, product}) {
module.exports.addTests = function({testRunner, expect, product, Errors}) {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
const {TimeoutError} = Errors;
describe('Page.waitFor', function() {
it('should wait for selector', async({page, server}) => {
@ -200,6 +200,13 @@ module.exports.addTests = function({testRunner, expect, product}) {
await waitForFunction;
expect(fooFound).toBe(true);
});
it('should survive navigations', async({page, server}) => {
const watchdog = page.waitForFunction(() => window.__done);
await page.goto(server.EMPTY_PAGE);
await page.goto(server.PREFIX + '/consolelog.html');
await page.evaluate(() => window.__done = true);
await watchdog;
});
});
describe('Frame.waitForSelector', function() {
@ -241,7 +248,7 @@ module.exports.addTests = function({testRunner, expect, product}) {
it('Page.waitForSelector is shortcut for main frame', async({page, server}) => {
await page.goto(server.EMPTY_PAGE);
await utils.attachFrame(page, 'frame1', server.EMPTY_PAGE);
await utils.attachFrame(page, 'frame1', server.EMPTY_PAGE2);
const otherFrame = page.frames()[1];
const watchdog = page.waitForSelector('div');
await otherFrame.evaluate(addElement, 'div');
@ -251,8 +258,8 @@ module.exports.addTests = function({testRunner, expect, product}) {
});
it('should run in specified frame', async({page, server}) => {
await utils.attachFrame(page, 'frame1', server.EMPTY_PAGE);
await utils.attachFrame(page, 'frame2', server.EMPTY_PAGE);
await utils.attachFrame(page, 'frame1', server.EMPTY_PAGE2);
await utils.attachFrame(page, 'frame2', server.EMPTY_PAGE2);
const frame1 = page.frames()[1];
const frame2 = page.frames()[2];
const waitForSelectorPromise = frame2.waitForSelector('div');