Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Feb 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions experimental/puppeteer-firefox/test/assets/consolelog.html
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>console.log test</title>
</head>
<body>
<script>
console.log('yellow')
</script>
</body>
</html>
4 changes: 2 additions & 2 deletions experimental/puppeteer-firefox/test/ignorehttpserrors.spec.js
Expand Up @@ -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;
Expand All @@ -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 => {
Expand Down
2 changes: 1 addition & 1 deletion experimental/puppeteer-firefox/test/keyboard.spec.js
Expand Up @@ -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)));
});
Expand Down
10 changes: 5 additions & 5 deletions experimental/puppeteer-firefox/test/launcher.spec.js
Expand Up @@ -15,22 +15,22 @@
*/
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;

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,
Expand All @@ -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
});
Expand Down
9 changes: 4 additions & 5 deletions experimental/puppeteer-firefox/test/page.spec.js
Expand Up @@ -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;
});
});

Expand Down
77 changes: 38 additions & 39 deletions experimental/puppeteer-firefox/test/puppeteer.spec.js
Expand Up @@ -28,38 +28,37 @@ 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};

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({testRunner, expect, product, puppeteer});
require('./ignorehttpserrors.spec.js').addTests({testRunner, expect, product, puppeteer});
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 => {
await state.browser.close();
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 => {
Expand All @@ -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);
});
});
});
Expand Down
6 changes: 3 additions & 3 deletions experimental/puppeteer-firefox/test/target.spec.js
Expand Up @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions experimental/puppeteer-firefox/test/waittask.spec.js
Expand Up @@ -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;
});
Expand Down
Empty file added test/assets/empty2.html
Empty file.
4 changes: 2 additions & 2 deletions test/browsercontext.spec.js
Expand Up @@ -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}) {
Expand Down
25 changes: 18 additions & 7 deletions test/keyboard.spec.js
Expand Up @@ -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');
Expand Down
33 changes: 30 additions & 3 deletions test/mouse.spec.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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}) => {
Expand Down
23 changes: 21 additions & 2 deletions test/navigation.spec.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
});
};