[BUG] page.frames() is not accurate
Context:
- Playwright Version: ^1.0.1
- Operating System: Windows
- Node version: 12
- Browser: Chromium
When iframes are 'destroyed' in browser, page.frames() still return them which raise following errorstack:
Error Message: Error: Protocol error (Runtime.callFunctionOn): Session closed. Most likely the iframe has been closed.
Stacktrace:
Error: Protocol error (Runtime.callFunctionOn): Session closed. Most likely the iframe has been closed.
at CRSession.send (node_modules\playwright-core\lib\chromium\crConnection.js:127:19)
at CRSession.<anonymous> (node_modules\playwright-core\lib\helper.js:64:31)
at CRExecutionContext.evaluate (node_modules\playwright-core\lib\chromium\crExecutionContext.js:70:83)
at async FrameManager.waitForSignalsCreatedBy (node_modules\playwright-core\lib\frames.js:81:28)
at async FrameExecutionContext._doEvaluateInternal (node_modules\playwright-core\lib\dom.js:42:16)
at async RerunnableTask.rerun (node_modules\playwright-core\lib\frames.js:743:23)
-- ASYNC --
at Frame.<anonymous> (node_modules\playwright-core\lib\helper.js:63:23)
Unfortunatly i have no example to provide :(
But i checked that frame are really not exists anymore using window.frames in my console which returns 0 when playwright returns 5 sized array elements
How do you detect on the playwright side that an iframe has been destroyed? Are you listening to framedetached event and one doesn't come or are you using another signal?
i am not listening at any event i just execute a page.frames() function which is return array with 5 elements when my chrome console tell me there is no iframe in my page :(
There maybe some delay before the event propagates from the browser to playwright, it shouldn't be too long but if you e.g. do a click() and then immediately check page.frames() in your code it may not yet be updated. Seeing your code snippet would be helpful. In any case if you expect an iframe to be "destroyed" you wait for 'framedetached' event. After it has fired the frame should not be in page.frames().
I listened to framedetached event as you said. Here is the new details:
- Before the action i ve got 5 length array when i do page.frames() and window.frames length is 4 => that's OK
- 1 framedetached event is received
- Then when i go again a page.frames i got a 4 length array and window.frames now returns 0
- No extra framedetached event are received after that
- No extra framedetached event are received after that
This sounds like a bug in playwright that framedetached event is not fired when iframes are removed. Would you be able to upload a code snippet where this is happening? If uploading a snippet is problematic could you give us more details about how the action actually removes the frames?
When all 4 iframes are deleted from the page you should receive 4 framedetached events like in the following snippet:
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
let allRemovedCallback;
const allRemoved = new Promise(fulfill => allRemovedCallback = fulfill);
let detachedEventsCount = 0;
page.on('framedetached', frame => {
console.log('Frame removed: ' + frame.name());
++detachedEventsCount;
if (detachedEventsCount == 4) {
console.log('All iframes removed');
allRemovedCallback();
}
});
const attachFrame = async (name) => {
await page.evaluate(async ({ name }) => {
const frame = document.createElement('iframe');
frame.name = name;
document.body.appendChild(frame);
return frame;
}, { name });
}
for (let i = 0; i < 4; i++) {
await attachFrame('f_' + i);
}
console.log(`page.frames().length=${page.frames().length}`);
for (let i = 0; i < 4; i++) {
console.log(`removing iframe ${i}...`);
page.evaluate(() => document.body.firstElementChild.remove());
console.log(`page.frames().length=${page.frames().length}`);
}
await allRemoved;
console.log(`page.frames().length=${page.frames().length}`);
await browser.close();
})();
@sarut0bi I don't think there's much we can do to help here if there's no reproduction test case :( Any chance you can come up with a repro?
i can share example website and steps to reproduce privately with one of you guys?
That'd be great, can you send instructions to the email in my GitHub profile?
Note that window.frames does not report iframes that are created in the shadow dom (see https://github.com/w3c/webcomponents/issues/145). Just something to look at - could be totally unrelated.
@sarut0bi thanks for sharing detailed steps privately and helping us debug this. The issue is that the iframes that were not removed are out-of-process iframes (OOPIFs), i.e. they are rendered in a process separate from the main page. They require special treatment and we've fixed several bugs related to OOPIFs since 1.0.2 which will be available with the next release. For now as a workaround you can use playwright@next. Please let us know if it still doesn't work for you.
It works flawlessly in playwright@next :) Thx for good support and reactivity!