mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
feat: browser.waitForTarget (#3356)
This adds `browser.waitForTarget` and `browserContext.waitForTarget`. It also fixes a flaky test that was incorrectly expecting targets to appear instantly.
This commit is contained in:
committed by
Andrey Lushnikov
parent
07febb637c
commit
6ac66c3547
@@ -16,6 +16,7 @@
|
||||
|
||||
const utils = require('./utils');
|
||||
const puppeteer = utils.requireRoot('index');
|
||||
const {TimeoutError} = utils.requireRoot('Errors');
|
||||
|
||||
module.exports.addTests = function({testRunner, expect}) {
|
||||
const {describe, xdescribe, fdescribe} = testRunner;
|
||||
@@ -79,6 +80,24 @@ module.exports.addTests = function({testRunner, expect}) {
|
||||
]);
|
||||
await context.close();
|
||||
});
|
||||
it('should wait for a target', async function({browser, server}) {
|
||||
const context = await browser.createIncognitoBrowserContext();
|
||||
let resolved = false;
|
||||
const targetPromise = context.waitForTarget(target => target.url() === server.EMPTY_PAGE);
|
||||
targetPromise.then(() => resolved = true);
|
||||
const page = await context.newPage();
|
||||
expect(resolved).toBe(false);
|
||||
await page.goto(server.EMPTY_PAGE);
|
||||
const target = await targetPromise;
|
||||
expect(await target.page()).toBe(page);
|
||||
await context.close();
|
||||
});
|
||||
it('should timeout waiting for a non-existent target', async function({browser, server}) {
|
||||
const context = await browser.createIncognitoBrowserContext();
|
||||
const error = await context.waitForTarget(target => target.url() === server.EMPTY_PAGE, {timeout: 1}).catch(e => e);
|
||||
expect(error).toBeInstanceOf(TimeoutError);
|
||||
await context.close();
|
||||
});
|
||||
it('should isolate localStorage and cookies', async function({browser, server}) {
|
||||
// Create two incognito contexts.
|
||||
const context1 = await browser.createIncognitoBrowserContext();
|
||||
|
||||
@@ -121,7 +121,7 @@ module.exports.addTests = function({testRunner, expect}) {
|
||||
server.waitForRequest('/one-style.css')
|
||||
]);
|
||||
// Connect to the opened page.
|
||||
const target = context.targets().find(target => target.url().includes('one-style.html'));
|
||||
const target = await context.waitForTarget(target => target.url().includes('one-style.html'));
|
||||
const newPage = await target.page();
|
||||
// Issue a redirect.
|
||||
serverResponse.writeHead(302, { location: '/injectedstyle.css' });
|
||||
|
||||
Reference in New Issue
Block a user