fix(frameElement): should work for frames inside closed shadow root (#17055)

This commit is contained in:
Dmitry Gozman 2022-09-06 08:52:25 -07:00 committed by GitHub
parent 85698f51c7
commit b734f36f8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 20 deletions

View File

@ -571,17 +571,14 @@ export class FFPage implements PageDelegate {
const parent = frame.parentFrame();
if (!parent)
throw new Error('Frame has been detached.');
const info = this._page.parseSelector('frame,iframe');
const handles = await this._page.selectors._queryAll(parent, info);
const items = await Promise.all(handles.map(async handle => {
const frame = await handle.contentFrame().catch(e => null);
return { handle, frame };
}));
const result = items.find(item => item.frame === frame);
items.map(item => item === result ? Promise.resolve() : item.handle.dispose());
if (!result)
const context = await parent._mainContext();
const result = await this._session.send('Page.adoptNode', {
frameId: frame._id,
executionContextId: ((context as any)[contextDelegateSymbol] as FFExecutionContext)._executionContextId
});
if (!result.remoteObject)
throw new Error('Frame has been detached.');
return result.handle;
return context.createHandle(result.remoteObject) as dom.ElementHandle;
}
}

View File

@ -988,17 +988,14 @@ export class WKPage implements PageDelegate {
const parent = frame.parentFrame();
if (!parent)
throw new Error('Frame has been detached.');
const info = this._page.parseSelector('frame,iframe');
const handles = await this._page.selectors._queryAll(parent, info);
const items = await Promise.all(handles.map(async handle => {
const frame = await handle.contentFrame().catch(e => null);
return { handle, frame };
}));
const result = items.find(item => item.frame === frame);
items.map(item => item === result ? Promise.resolve() : item.handle.dispose());
if (!result)
const context = await parent._mainContext();
const result = await this._session.send('DOM.resolveNode', {
frameId: frame._id,
executionContextId: ((context as any)[contextDelegateSymbol] as WKExecutionContext)._contextId
});
if (!result || result.object.subtype === 'null')
throw new Error('Frame has been detached.');
return result.handle;
return context.createHandle(result.object) as dom.ElementHandle;
}
_onRequestWillBeSent(session: WKSession, event: Protocol.Network.requestWillBeSentPayload) {

View File

@ -55,3 +55,23 @@ it('should throw when detached', async ({ page, server }) => {
const error = await frame1.frameElement().catch(e => e);
expect(error.message).toContain('Frame has been detached.');
});
it('should work inside closed shadow root', async ({ page, server, browserName }) => {
await page.goto(server.EMPTY_PAGE);
await page.setContent(`
<div id=framecontainer>
</div>
<script>
const iframe = document.createElement('iframe');
iframe.setAttribute('name', 'myframe');
iframe.setAttribute('srcdoc', 'find me');
const div = document.getElementById('framecontainer');
const host = div.attachShadow({ mode: 'closed' });
host.appendChild(iframe);
</script>
`);
const frame = page.frame({ name: 'myframe' });
const element = await frame.frameElement();
expect(await element.getAttribute('name')).toBe('myframe');
});