From 69c434af752b8e60f466463918453ff905038d30 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Sat, 2 Feb 2019 18:49:12 -0700 Subject: [PATCH] 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. --- .../test/assets/consolelog.html | 11 +++ .../test/ignorehttpserrors.spec.js | 4 +- .../puppeteer-firefox/test/keyboard.spec.js | 2 +- .../puppeteer-firefox/test/launcher.spec.js | 10 +-- .../puppeteer-firefox/test/page.spec.js | 9 +-- .../puppeteer-firefox/test/puppeteer.spec.js | 77 +++++++++---------- .../puppeteer-firefox/test/target.spec.js | 6 +- .../puppeteer-firefox/test/waittask.spec.js | 6 +- test/assets/empty2.html | 0 test/browsercontext.spec.js | 4 +- test/keyboard.spec.js | 25 ++++-- test/mouse.spec.js | 33 +++++++- test/navigation.spec.js | 23 +++++- test/page.spec.js | 9 +-- test/puppeteer.spec.js | 4 +- test/target.spec.js | 24 +++++- test/test.js | 6 ++ test/waittask.spec.js | 17 ++-- 18 files changed, 185 insertions(+), 85 deletions(-) create mode 100644 experimental/puppeteer-firefox/test/assets/consolelog.html create mode 100644 test/assets/empty2.html diff --git a/experimental/puppeteer-firefox/test/assets/consolelog.html b/experimental/puppeteer-firefox/test/assets/consolelog.html new file mode 100644 index 00000000000..7fa1b211a4d --- /dev/null +++ b/experimental/puppeteer-firefox/test/assets/consolelog.html @@ -0,0 +1,11 @@ + + + + console.log test + + + + + diff --git a/experimental/puppeteer-firefox/test/ignorehttpserrors.spec.js b/experimental/puppeteer-firefox/test/ignorehttpserrors.spec.js index d5ea924d25f..64e6e61bcbb 100644 --- a/experimental/puppeteer-firefox/test/ignorehttpserrors.spec.js +++ b/experimental/puppeteer-firefox/test/ignorehttpserrors.spec.js @@ -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 => { diff --git a/experimental/puppeteer-firefox/test/keyboard.spec.js b/experimental/puppeteer-firefox/test/keyboard.spec.js index 9a7b5653504..66e054f6d62 100644 --- a/experimental/puppeteer-firefox/test/keyboard.spec.js +++ b/experimental/puppeteer-firefox/test/keyboard.spec.js @@ -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))); }); diff --git a/experimental/puppeteer-firefox/test/launcher.spec.js b/experimental/puppeteer-firefox/test/launcher.spec.js index f96fb5565df..da874933d12 100644 --- a/experimental/puppeteer-firefox/test/launcher.spec.js +++ b/experimental/puppeteer-firefox/test/launcher.spec.js @@ -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 }); diff --git a/experimental/puppeteer-firefox/test/page.spec.js b/experimental/puppeteer-firefox/test/page.spec.js index 9e6e8ff3597..3a26a9b7135 100644 --- a/experimental/puppeteer-firefox/test/page.spec.js +++ b/experimental/puppeteer-firefox/test/page.spec.js @@ -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; }); }); diff --git a/experimental/puppeteer-firefox/test/puppeteer.spec.js b/experimental/puppeteer-firefox/test/puppeteer.spec.js index 3acf97a84fb..7f1159a4a05 100644 --- a/experimental/puppeteer-firefox/test/puppeteer.spec.js +++ b/experimental/puppeteer-firefox/test/puppeteer.spec.js @@ -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); }); }); }); diff --git a/experimental/puppeteer-firefox/test/target.spec.js b/experimental/puppeteer-firefox/test/target.spec.js index 8d9bd32c97f..35b9fe7d59f 100644 --- a/experimental/puppeteer-firefox/test/target.spec.js +++ b/experimental/puppeteer-firefox/test/target.spec.js @@ -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); diff --git a/experimental/puppeteer-firefox/test/waittask.spec.js b/experimental/puppeteer-firefox/test/waittask.spec.js index b7592fa3da3..f3b3c5da7c0 100644 --- a/experimental/puppeteer-firefox/test/waittask.spec.js +++ b/experimental/puppeteer-firefox/test/waittask.spec.js @@ -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; }); diff --git a/test/assets/empty2.html b/test/assets/empty2.html new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/browsercontext.spec.js b/test/browsercontext.spec.js index 6107b886a70..54ed4afab00 100644 --- a/test/browsercontext.spec.js +++ b/test/browsercontext.spec.js @@ -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}) { diff --git a/test/keyboard.spec.js b/test/keyboard.spec.js index a1b1886c332..dad6c9a9f5c 100644 --- a/test/keyboard.spec.js +++ b/test/keyboard.spec.js @@ -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'); diff --git a/test/mouse.spec.js b/test/mouse.spec.js index 018b0857353..5cf6df56c7f 100644 --- a/test/mouse.spec.js +++ b/test/mouse.spec.js @@ -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}) => { diff --git a/test/navigation.spec.js b/test/navigation.spec.js index 77643d6cc6e..a938473346b 100644 --- a/test/navigation.spec.js +++ b/test/navigation.spec.js @@ -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); + }); + }); }; diff --git a/test/page.spec.js b/test/page.spec.js index fc1b4781d46..91ef072dc03 100644 --- a/test/page.spec.js +++ b/test/page.spec.js @@ -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(); diff --git a/test/puppeteer.spec.js b/test/puppeteer.spec.js index 70f8380027b..3198a5aeb67 100644 --- a/test/puppeteer.spec.js +++ b/test/puppeteer.spec.js @@ -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, diff --git a/test/target.spec.js b/test/target.spec.js index 44e80ffe794..5951f909b12 100644 --- a/test/target.spec.js +++ b/test/target.spec.js @@ -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); + }); + }); }; diff --git a/test/test.js b/test/test.js index a6eab7faf4a..1b2ede24fd4 100644 --- a/test/test.js +++ b/test/test.js @@ -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, }); diff --git a/test/waittask.spec.js b/test/waittask.spec.js index b9b840a7bc9..e65caa5edea 100644 --- a/test/waittask.spec.js +++ b/test/waittask.spec.js @@ -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');