Skip to content

Commit

Permalink
chore: attempted fix
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Dec 8, 2022
1 parent 090dd80 commit 46ef46a
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 48 deletions.
40 changes: 20 additions & 20 deletions packages/puppeteer-core/src/common/Debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,23 @@ export const debug = (prefix: string): ((...args: unknown[]) => void) => {
/**
* @internal
*/
let capturedLogs: string[] = [];
/**
* @internal
*/
let captureLogs = false;
/**
* @internal
*/
export function setLogCapture(value: boolean): void {
capturedLogs = [];
captureLogs = value;
}
/**
* @internal
*/
export function getCapturedLogs(): string[] {
return capturedLogs;
}
let capturedLogs: string[] = [];
/**
* @internal
*/
let captureLogs = false;

/**
* @internal
*/
export function setLogCapture(value: boolean): void {
capturedLogs = [];
captureLogs = value;
}

/**
* @internal
*/
export function getCapturedLogs(): string[] {
return capturedLogs;
}
10 changes: 9 additions & 1 deletion packages/puppeteer-core/src/common/Frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ import {getQueryHandlerAndSelector} from './QueryHandler.js';
import {EvaluateFunc, HandleFor, NodeFor} from './types.js';
import {importFS} from './util.js';

import {debug} from './Debug.js';

const log = debug('puppeteer:frame ');

/**
* @public
*/
Expand Down Expand Up @@ -392,17 +396,21 @@ export class Frame {
waitUntil,
timeout
);
const navResponse = watcher.navigationResponse();
const error = await Promise.race([
watcher.timeoutOrTerminationPromise(),
watcher.sameDocumentNavigationPromise(),
watcher.newDocumentNavigationPromise(),
]);
log('LIFECYCLE WATCHER RESOLVED');
try {
if (error) {
throw error;
}
return await watcher.navigationResponse();
log('LIFECYCLE WATCHER wait for navigationResponse');
return await navResponse;
} finally {
log('LIFECYCLE WATCHER DISPOSED');
watcher.dispose();
}
}
Expand Down
37 changes: 15 additions & 22 deletions packages/puppeteer-core/src/common/FrameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export class FrameManager extends EventEmitter {
*/
_frameTree = new FrameTree();

#frameNavigatedReceived = new Set<string>();

get timeoutSettings(): TimeoutSettings {
return this.#timeoutSettings;
}
Expand Down Expand Up @@ -103,6 +105,7 @@ export class FrameManager extends EventEmitter {
this.#onFrameAttached(session, event.frameId, event.parentFrameId);
});
session.on('Page.frameNavigated', event => {
this.#frameNavigatedReceived.add(event.frame.id);
this.#onFrameNavigated(event.frame);
});
session.on('Page.navigatedWithinDocument', event => {
Expand Down Expand Up @@ -142,15 +145,15 @@ export class FrameManager extends EventEmitter {
const result = await Promise.all([
client.send('Page.enable'),
client.send('Page.getFrameTree'),
client.send('Page.setLifecycleEventsEnabled', {enabled: true}),
client.send('Runtime.enable').then(() => {
return this.#createIsolatedWorld(client, UTILITY_WORLD_NAME);
})
]);

const {frameTree} = result[1];
this.#handleFrameTree(client, frameTree);
await Promise.all([
client.send('Page.setLifecycleEventsEnabled', {enabled: true}),
client.send('Runtime.enable').then(() => {
return this.#createIsolatedWorld(client, UTILITY_WORLD_NAME);
}),
// TODO: Network manager is not aware of OOP iframes yet.
client === this.#client
? this.#networkManager.initialize()
Expand Down Expand Up @@ -210,15 +213,6 @@ export class FrameManager extends EventEmitter {
this.initialize(target._session());
}

onDetachedFromTarget(target: Target): void {
const frame = this.frame(target._targetId);
if (frame && frame.isOOPFrame()) {
// When an OOP iframe is removed from the page, it
// will only get a Target.detachedFromTarget event.
this.#removeFramesRecursively(frame);
}
}

#onLifecycleEvent(event: Protocol.Page.LifecycleEventEvent): void {
const frame = this.frame(event.frameId);
if (!frame) {
Expand Down Expand Up @@ -249,15 +243,13 @@ export class FrameManager extends EventEmitter {
session: CDPSession,
frameTree: Protocol.Page.FrameTree
): void {
log('Handling frame tree', frameTree.frame.id);
if (frameTree.frame.parentId) {
this.#onFrameAttached(
session,
frameTree.frame.id,
frameTree.frame.parentId
);
log('Handling frame tree', frameTree.frame.id, frameTree.frame.url);
if (!this.#frameNavigatedReceived.has(frameTree.frame.id)) {
this.#onFrameNavigated(frameTree.frame);
} else {
this.#frameNavigatedReceived.delete(frameTree.frame.id);
}
this.#onFrameNavigated(frameTree.frame);

if (!frameTree.childFrames) {
log('Finished handling frame tree, no child', frameTree.frame.id);
return;
Expand All @@ -281,6 +273,7 @@ export class FrameManager extends EventEmitter {
// If an OOP iframes becomes a normal iframe again
// it is first attached to the parent page before
// the target is removed.
log('Updating client in #onFrameAttached')
frame.updateClient(session);
}
return;
Expand All @@ -293,7 +286,7 @@ export class FrameManager extends EventEmitter {

async #onFrameNavigated(framePayload: Protocol.Page.Frame): Promise<void> {
const frameId = framePayload.id;
log('#onFrameNavigated', frameId);
log('#onFrameNavigated', frameId, framePayload.url);
const isMainFrame = !framePayload.parentId;

let frame = this._frameTree.getById(frameId);
Expand Down
6 changes: 6 additions & 0 deletions packages/puppeteer-core/src/common/LifecycleWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import {HTTPRequest} from './HTTPRequest.js';
import {HTTPResponse} from './HTTPResponse.js';
import {NetworkManagerEmittedEvents} from './NetworkManager.js';
import {CDPSessionEmittedEvents} from './Connection.js';
import {debug} from './Debug.js';

const log = debug('puppeteer:lifecyleWatcher ');

/**
* @public
*/
Expand Down Expand Up @@ -109,6 +113,7 @@ export class LifecycleWatcher {
waitUntil: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[],
timeout: number
) {
log('LifeCycleWacher created for frame ', frame._id);
if (Array.isArray(waitUntil)) {
waitUntil = waitUntil.slice();
} else if (typeof waitUntil === 'string') {
Expand Down Expand Up @@ -281,6 +286,7 @@ export class LifecycleWatcher {
}

#checkLifecycleComplete(): void {
log('LifeCycleWacher checkLifecycleComplete', this.#frame._lifecycleEvents, this.#expectedLifecycle, this.#swapped);
// We expect navigation to commit.
if (!checkLifecycle(this.#frame, this.#expectedLifecycle)) {
return;
Expand Down
3 changes: 0 additions & 3 deletions packages/puppeteer-core/src/common/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,6 @@ export class CDPPage extends Page {

#onDetachedFromTarget = (target: Target) => {
const sessionId = target._session()?.id();

this.#frameManager.onDetachedFromTarget(target);

const worker = this.#workers.get(sessionId!);
if (!worker) {
return;
Expand Down
1 change: 1 addition & 0 deletions test/src/mocha-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ export const describeWithDebugLogs = (
setLogCapture(false);
});

// eslint-disable-next-line mocha/no-exclusive-tests
return describe.only(description, body);
};

Expand Down
14 changes: 12 additions & 2 deletions test/src/oopif.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ describeWithDebugLogs('OOPIF', function () {
});

afterEach(async () => {
await context.close();
await context?.close();
});

after(async () => {
await browser.close();
await browser?.close();
});

it('should treat OOP iframes and normal iframes the same', async () => {
Expand Down Expand Up @@ -132,11 +132,13 @@ describeWithDebugLogs('OOPIF', function () {

const [frame1, frame2] = await Promise.all([frame1Promise, frame2Promise]);

log('FRAMES RESOLVED');
expect(
await frame1.evaluate(() => {
return document.location.href;
})
).toMatch(/one-frame\.html$/);
log('AFTER EVALUATE1');
expect(
await frame2.evaluate(() => {
return document.location.href;
Expand Down Expand Up @@ -174,16 +176,20 @@ describeWithDebugLogs('OOPIF', function () {
await utils.attachFrame(page, 'frame1', server.EMPTY_PAGE);

const frame = await framePromise;
log('FRAME FOUND');
expect(frame.isOOPFrame()).toBe(false);
const nav = frame.waitForNavigation();
await utils.navigateFrame(
page,
'frame1',
server.CROSS_PROCESS_PREFIX + '/empty.html'
);
log('WAITING FOR NAV TO CROSS_PROCESS_PREFIX ');
await nav;
log('NAVIGATED TO CROSS_PROCESS_PREFIX ');
expect(frame.isOOPFrame()).toBe(true);
await utils.detachFrame(page, 'frame1');
log('NAVIGATED DETACHED');
expect(page.frames()).toHaveLength(1);
});

Expand Down Expand Up @@ -217,17 +223,21 @@ describeWithDebugLogs('OOPIF', function () {
'frame1',
server.CROSS_PROCESS_PREFIX + '/empty.html'
);
log('AFTER ATTACH');
const frame = await framePromise;
log('BEFORE EVALUATE1');
await frame.evaluate(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
_test = 'Test 123!';
});
log('BEFORE EVALUATE2');
const result = await frame.evaluate(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return window._test;
});
log('AFTER EVALUATE2');
expect(result).toBe('Test 123!');
});
it('should provide access to elements', async () => {
Expand Down

0 comments on commit 46ef46a

Please sign in to comment.