fix(firefox): setContent to properly listen for lifecycle events (#219)

This commit is contained in:
Dmitry Gozman 2019-12-12 20:11:33 -08:00 committed by GitHub
parent ee1f4784c6
commit 90f0b8c2b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 6 deletions

View file

@ -9,7 +9,7 @@
"main": "index.js",
"playwright": {
"chromium_revision": "719491",
"firefox_revision": "1004",
"firefox_revision": "1005",
"webkit_revision": "1032"
},
"scripts": {

View file

@ -29,7 +29,7 @@ import * as network from '../network';
import { BrowserContext, BrowserInterface } from '../browserContext';
export class Browser extends EventEmitter implements BrowserInterface {
private _connection: Connection;
_connection: Connection;
_defaultViewport: types.Viewport;
private _process: import('child_process').ChildProcess;
private _closeCallback: () => Promise<void>;

View file

@ -124,6 +124,8 @@ export class ExecutionContextDelegate implements js.ExecutionContextDelegate {
}
async releaseHandle(handle: js.JSHandle): Promise<void> {
if (!handle._remoteObject.objectId)
return;
await this._session.send('Runtime.disposeObject', {
executionContextId: this._executionContextId,
objectId: handle._remoteObject.objectId,

View file

@ -301,13 +301,26 @@ export class FrameManager extends EventEmitter implements PageDelegate {
return watcher.navigationResponse();
}
async setFrameContent(frame: frames.Frame, html: string) {
async setFrameContent(frame: frames.Frame, html: string, options: frames.NavigateOptions = {}) {
const {
waitUntil = (['load'] as frames.LifecycleEvent[]),
timeout = this._page._timeoutSettings.navigationTimeout(),
} = options;
const context = await frame._utilityContext();
frame._firedLifecycleEvents.clear();
await context.evaluate(html => {
document.open();
document.write(html);
document.close();
}, html);
const watcher = new frames.LifecycleWatcher(frame, waitUntil, timeout);
const error = await Promise.race([
watcher.timeoutOrTerminationPromise,
watcher.lifecyclePromise,
]);
watcher.dispose();
if (error)
throw error;
}
setExtraHTTPHeaders(extraHTTPHeaders: network.Headers): Promise<void> {

View file

@ -528,6 +528,24 @@ module.exports.addTests = function({testRunner, expect, headless, playwright, FF
const result = await page.content();
expect(result).toBe(expectedOutput);
});
it('should not confuse with previous navigation', async({page, server}) => {
const imgPath = '/img.png';
let imgResponse = null;
server.setRoute(imgPath, (req, res) => imgResponse = res);
let loaded = false;
// Trigger navigation which might resolve next setContent call.
page.evaluate(url => window.location.href = url, server.EMPTY_PAGE);
const contentPromise = page.setContent(`<img src="${server.PREFIX + imgPath}"></img>`).then(() => loaded = true);
await server.waitForRequest(imgPath);
expect(loaded).toBe(false);
for (let i = 0; i < 5; i++)
await page.evaluate('1'); // Roundtrips to give setContent a chance to resolve.
expect(loaded).toBe(false);
imgResponse.end();
await contentPromise;
});
it('should work with doctype', async({page, server}) => {
const doctype = '<!DOCTYPE html>';
await page.setContent(`${doctype}<div>hello</div>`);
@ -541,7 +559,7 @@ module.exports.addTests = function({testRunner, expect, headless, playwright, FF
const result = await page.content();
expect(result).toBe(`${doctype}${expectedOutput}`);
});
it.skip(FFOX)('should respect timeout', async({page, server}) => {
it('should respect timeout', async({page, server}) => {
const imgPath = '/img.png';
// stall for image
server.setRoute(imgPath, (req, res) => {});
@ -549,7 +567,7 @@ module.exports.addTests = function({testRunner, expect, headless, playwright, FF
await page.setContent(`<img src="${server.PREFIX + imgPath}"></img>`, {timeout: 1}).catch(e => error = e);
expect(error).toBeInstanceOf(playwright.errors.TimeoutError);
});
it.skip(FFOX)('should respect default navigation timeout', async({page, server}) => {
it('should respect default navigation timeout', async({page, server}) => {
page.setDefaultNavigationTimeout(1);
const imgPath = '/img.png';
// stall for image
@ -558,7 +576,7 @@ module.exports.addTests = function({testRunner, expect, headless, playwright, FF
await page.setContent(`<img src="${server.PREFIX + imgPath}"></img>`).catch(e => error = e);
expect(error).toBeInstanceOf(playwright.errors.TimeoutError);
});
it.skip(FFOX)('should await resources to load', async({page, server}) => {
it('should await resources to load', async({page, server}) => {
const imgPath = '/img.png';
let imgResponse = null;
server.setRoute(imgPath, (req, res) => imgResponse = res);