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

View File

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

View File

@ -75,12 +75,11 @@ module.exports.addTests = function({testRunner, expect, product}) {
await newPage.close(); await newPage.close();
expect(newPage.isClosed()).toBe(true); expect(newPage.isClosed()).toBe(true);
}); });
it('should emit the close event', async({browser}) => { it('should work with page.close', async function({ page, context, server }) {
const newPage = await browser.newPage(); const newPage = await context.newPage();
let gotClosedEvent = false; const closedPromise = new Promise(x => newPage.on('close', x));
newPage.on('close', () => gotClosedEvent = true);
await newPage.close(); 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) toBeGolden: GoldenUtils.compare.bind(null, GOLDEN_DIR, OUTPUT_DIR)
}); });
const defaultBrowserOptions = {
beforeAll(async state => {
state.defaultBrowserOptions = {
handleSIGINT: false, handleSIGINT: false,
executablePath: product === 'chromium' ? process.env.CHROME : process.env.FFOX, executablePath: product === 'chromium' ? process.env.CHROME : process.env.FFOX,
dumpio: !!process.env.DUMPIO, dumpio: !!process.env.DUMPIO,
args: product === 'chromium' ? ['--no-sandbox'] : [], 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;
});
require('./launcher.spec.js').addTests({testRunner, expect, product, puppeteer}); const testOptions = {testRunner, expect, product, puppeteer, defaultBrowserOptions};
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', () => { describe('Browser', () => {
beforeAll(async state => { beforeAll(async state => {
state.browser = await puppeteer.launch(state.defaultBrowserOptions); state.browser = await puppeteer.launch(defaultBrowserOptions);
}); });
afterAll(async state => { afterAll(async state => {
@ -58,8 +57,8 @@ module.exports.addTests = ({testRunner, product, puppeteer}) => testRunner.descr
state.browser = null; state.browser = null;
}); });
require('./browser.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./browser.spec.js').addTests(testOptions);
require('./browsercontext.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./browsercontext.spec.js').addTests(testOptions);
describe('Page', () => { describe('Page', () => {
beforeEach(async state => { beforeEach(async state => {
@ -73,31 +72,31 @@ module.exports.addTests = ({testRunner, product, puppeteer}) => testRunner.descr
state.page = null; state.page = null;
}); });
require('./page.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./page.spec.js').addTests(testOptions);
require('./evaluation.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./evaluation.spec.js').addTests(testOptions);
require('./navigation.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./navigation.spec.js').addTests(testOptions);
require('./dialog.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./dialog.spec.js').addTests(testOptions);
require('./frame.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./frame.spec.js').addTests(testOptions);
require('./jshandle.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./jshandle.spec.js').addTests(testOptions);
require('./elementhandle.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./elementhandle.spec.js').addTests(testOptions);
require('./target.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./target.spec.js').addTests(testOptions);
require('./waittask.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./waittask.spec.js').addTests(testOptions);
require('./queryselector.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./queryselector.spec.js').addTests(testOptions);
require('./emulation.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./emulation.spec.js').addTests(testOptions);
require('./screenshot.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./screenshot.spec.js').addTests(testOptions);
// Input tests // Input tests
require('./keyboard.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./keyboard.spec.js').addTests(testOptions);
require('./mouse.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./mouse.spec.js').addTests(testOptions);
require('./click.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./click.spec.js').addTests(testOptions);
require('./type.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./type.spec.js').addTests(testOptions);
require('./hover.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./hover.spec.js').addTests(testOptions);
// Browser-specific page tests // Browser-specific page tests
if (product === 'firefox') if (product === 'firefox')
require('./firefoxonly.spec.js').addTests({testRunner, expect, product, puppeteer}); require('./firefoxonly.spec.js').addTests(testOptions);
else 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', () => { describe('Browser.waitForTarget', () => {
it('should wait for a target', async function({browser, server}) { it('should wait for a target', async function({browser, server}) {
let resolved = false; 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); targetPromise.then(() => resolved = true);
const page = await browser.newPage(); const page = await browser.newPage();
expect(resolved).toBe(false); expect(resolved).toBe(false);
await page.goto(server.EMPTY_PAGE2); await page.goto(server.EMPTY_PAGE);
const target = await targetPromise; const target = await targetPromise;
expect(await target.page()).toBe(page); expect(await target.page()).toBe(page);
await page.close(); await page.close();
}); });
it('should timeout waiting for a non-existent target', async function({browser, server}) { it('should timeout waiting for a non-existent target', async function({browser, server}) {
let error = null; let error = null;
await browser.waitForTarget(target => target.url() === server.EMPTY_PAGE2, { await browser.waitForTarget(target => target.url() === server.EMPTY_PAGE, {
timeout: 1 timeout: 1
}).catch(e => error = e); }).catch(e => error = e);
expect(error).toBeInstanceOf(TimeoutError); expect(error).toBeInstanceOf(TimeoutError);

View File

@ -123,11 +123,9 @@ module.exports.addTests = function({testRunner, expect, product}) {
await watchdog; await watchdog;
}); });
it('should survive navigations', async({page, server}) => { it('should survive navigations', async({page, server}) => {
const watchdog = page.waitForFunction(() => { const watchdog = page.waitForFunction(() => window.__done);
return window.__done;
});
await page.goto(server.EMPTY_PAGE); 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 page.evaluate(() => window.__done = true);
await watchdog; await watchdog;
}); });

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

View File

@ -15,12 +15,12 @@
*/ */
const utils = require('./utils'); 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 {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner; const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
const {TimeoutError} = Errors;
describe('BrowserContext', function() { describe('BrowserContext', function() {
it('should have default context', async function({browser, server}) { it('should have default context', async function({browser, server}) {

View File

@ -15,19 +15,30 @@
*/ */
const utils = require('./utils'); 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 {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner; const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
describe('Keyboard', function() { describe('Keyboard', function() {
it('should type into the textarea', async({page, server}) => { it('should type into a textarea', async({page, server}) => {
await page.goto(server.PREFIX + '/input/textarea.html'); await page.evaluate(() => {
const textarea = document.createElement('textarea');
const textarea = await page.$('textarea'); document.body.appendChild(textarea);
await textarea.type('Type in this text!'); textarea.focus();
expect(await page.evaluate(() => result)).toBe('Type in this text!'); });
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}) => { it('should move with the arrow keys', async({page, server}) => {
await page.goto(server.PREFIX + '/input/textarea.html'); 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 {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner; const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
describe('Mouse', function() { 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}) => { it('should resize the textarea', async({page, server}) => {
await page.goto(server.PREFIX + '/input/textarea.html'); await page.goto(server.PREFIX + '/input/textarea.html');
const {x, y, width, height} = await page.evaluate(dimensions); 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.goto(server.PREFIX + '/input/scrollable.html');
await page.evaluate(() => document.querySelector('#button-3').addEventListener('mousedown', e => window.lastEvent = e, true)); await page.evaluate(() => document.querySelector('#button-3').addEventListener('mousedown', e => window.lastEvent = e, true));
const modifiers = {'Shift': 'shiftKey', 'Control': 'ctrlKey', 'Alt': 'altKey', 'Meta': 'metaKey'}; 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) { for (const modifier in modifiers) {
await page.keyboard.down(modifier); await page.keyboard.down(modifier);
await page.click('#button-3'); await page.click('#button-3');
if (!(await page.evaluate(mod => window.lastEvent[mod], modifiers[modifier]))) 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.keyboard.up(modifier);
} }
await page.click('#button-3'); await page.click('#button-3');
for (const modifier in modifiers) { for (const modifier in modifiers) {
if ((await page.evaluate(mod => window.lastEvent[mod], modifiers[modifier]))) 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}) => { it('should tween mouse movement', async({page, server}) => {

View File

@ -15,14 +15,24 @@
*/ */
const utils = require('./utils'); 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 {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner; const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
const {TimeoutError} = Errors;
describe('Page.goto', function() { 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}) => { it('should navigate to about:blank', async({page, server}) => {
const response = await page.goto('about:blank'); const response = await page.goto('about:blank');
expect(response).toBe(null); expect(response).toBe(null);
@ -523,4 +533,13 @@ module.exports.addTests = function({testRunner, expect}) {
await navigationPromise; 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 path = require('path');
const utils = require('./utils'); const utils = require('./utils');
const {waitEvent} = utils; const {waitEvent} = utils;
const {TimeoutError} = utils.requireRoot('Errors');
const DeviceDescriptors = utils.requireRoot('DeviceDescriptors');
const iPhone = DeviceDescriptors['iPhone 6'];
let asyncawait = true; let asyncawait = true;
try { try {
@ -29,11 +25,14 @@ try {
asyncawait = false; asyncawait = false;
} }
module.exports.addTests = function({testRunner, expect, headless}) { module.exports.addTests = function({testRunner, expect, headless, Errors, DeviceDescriptors}) {
const {describe, xdescribe, fdescribe} = testRunner; const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner; const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
const {TimeoutError} = Errors;
const iPhone = DeviceDescriptors['iPhone 6'];
describe('Page.close', function() { describe('Page.close', function() {
it('should reject all promises when page is closed', async({context}) => { it('should reject all promises when page is closed', async({context}) => {
const newPage = await context.newPage(); const newPage = await context.newPage();

View File

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

View File

@ -17,10 +17,11 @@
const utils = require('./utils'); const utils = require('./utils');
const {waitEvent} = utils; const {waitEvent} = utils;
module.exports.addTests = function({testRunner, expect}) { module.exports.addTests = function({testRunner, expect, puppeteer, Errors}) {
const {describe, xdescribe, fdescribe} = testRunner; const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner; const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
const {TimeoutError} = Errors;
describe('Target', function() { describe('Target', function() {
it('Browser.targets should return all of the targets', async({page, server, browser}) => { 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); 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.PREFIX = `http://localhost:${port}`;
state.server.CROSS_PROCESS_PREFIX = `http://127.0.0.1:${port}`; state.server.CROSS_PROCESS_PREFIX = `http://127.0.0.1:${port}`;
state.server.EMPTY_PAGE = `http://localhost:${port}/empty.html`; state.server.EMPTY_PAGE = `http://localhost:${port}/empty.html`;
state.server.EMPTY_PAGE2 = `http://localhost:${port}/empty2.html`;
const httpsPort = port + 1; const httpsPort = port + 1;
state.httpsServer = await TestServer.createHTTPS(assetsPath, httpsPort); state.httpsServer = await TestServer.createHTTPS(assetsPath, httpsPort);
@ -61,6 +62,7 @@ beforeAll(async state => {
state.httpsServer.PREFIX = `https://localhost:${httpsPort}`; state.httpsServer.PREFIX = `https://localhost:${httpsPort}`;
state.httpsServer.CROSS_PROCESS_PREFIX = `https://127.0.0.1:${httpsPort}`; state.httpsServer.CROSS_PROCESS_PREFIX = `https://127.0.0.1:${httpsPort}`;
state.httpsServer.EMPTY_PAGE = `https://localhost:${httpsPort}/empty.html`; state.httpsServer.EMPTY_PAGE = `https://localhost:${httpsPort}/empty.html`;
state.httpsServer.EMPTY_PAGE2 = `https://localhost:${httpsPort}/empty2.html`;
}); });
afterAll(async({server, httpsServer}) => { afterAll(async({server, httpsServer}) => {
@ -89,6 +91,8 @@ if (process.env.BROWSER !== 'firefox') {
require('./puppeteer.spec.js').addTests({ require('./puppeteer.spec.js').addTests({
product: 'Chromium', product: 'Chromium',
puppeteer: utils.requireRoot('index'), puppeteer: utils.requireRoot('index'),
Errors: utils.requireRoot('Errors'),
DeviceDescriptors: utils.requireRoot('DeviceDescriptors'),
defaultBrowserOptions, defaultBrowserOptions,
testRunner, testRunner,
}); });
@ -100,6 +104,8 @@ if (process.env.BROWSER !== 'firefox') {
require('./puppeteer.spec.js').addTests({ require('./puppeteer.spec.js').addTests({
product: 'Firefox', product: 'Firefox',
puppeteer: require('../experimental/puppeteer-firefox'), puppeteer: require('../experimental/puppeteer-firefox'),
Errors: require('../experimental/puppeteer-firefox/Errors'),
DeviceDescriptors: utils.requireRoot('DeviceDescriptors'),
defaultBrowserOptions, defaultBrowserOptions,
testRunner, testRunner,
}); });

View File

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