Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat(webview): support iframes (#41324)

Yury Semikhatskyaa3c7b5d3e59
diff --git a/packages/playwright-core/src/server/webkit/webview/wvExecutionContext.ts b/packages/playwright-core/src/server/webkit/webview/wvExecutionContext.ts
index 62b5aa0d80..a13a515810 100644
--- a/packages/playwright-core/src/server/webkit/webview/wvExecutionContext.ts
+++ b/packages/playwright-core/src/server/webkit/webview/wvExecutionContext.ts
@@ -30,7 +30,7 @@ export class WVExecutionContext implements js.ExecutionContextDelegate {
constructor(session: WVSession, contextId: number | undefined) {
this._session = session;
- this._contextId = undefined;
+ this._contextId = contextId;
}
async rawEvaluateJSON(expression: string): Promise<any> {
diff --git a/packages/playwright-core/src/server/webkit/webview/wvPage.ts b/packages/playwright-core/src/server/webkit/webview/wvPage.ts
index 93f8f8a611..ec1b60c402 100644
--- a/packages/playwright-core/src/server/webkit/webview/wvPage.ts
+++ b/packages/playwright-core/src/server/webkit/webview/wvPage.ts
@@ -689,11 +689,53 @@ export class WVPage implements PageDelegate {
}
async getContentFrame(handle: dom.ElementHandle): Promise<frames.Frame | null> {
- throw new Error('Method not implemented');
+ if (!handle._objectId)
+ return null;
+ // A frame-owner node's contentDocument.frameId is the framed document's frame.
+ const { node } = await this._requestNodeViaDOM(handle._objectId);
+ const frameId = node?.contentDocument?.frameId;
+ return frameId ? this._page.frameManager.frame(frameId) : null;
}
async getOwnerFrame(handle: dom.ElementHandle): Promise<string | null> {
- throw new Error('Method not implemented');
+ if (handle._objectId) {
+ // A node's frameId is its containing frame, even for a cross-frame handle.
+ const { node } = await this._requestNodeViaDOM(handle._objectId);
+ if (node?.frameId)
+ return node.frameId;
+ }
+ return handle._frame._id;
+ }
+
+ // Resolves a Runtime objectId to its DOM.Node. DOM.requestNode streams the node
+ // and its ancestor path through DOM.setChildNodes, but only once DOM.getDocument
+ // has let the agent observe the document. Returns the requested node along with
+ // every node seen along the way, keyed by id.
+ private async _requestNodeViaDOM(objectId: string): Promise<{ node: Protocol.DOM.Node | undefined, nodesById: Map<number, Protocol.DOM.Node> }> {
+ const nodesById = new Map<number, Protocol.DOM.Node>();
+ const collect = (nodes: Protocol.DOM.Node[] | undefined) => {
+ for (const node of nodes || []) {
+ nodesById.set(node.nodeId, node);
+ collect(node.children);
+ collect(node.shadowRoots);
+ if (node.contentDocument)
+ collect([node.contentDocument]);
+ if (node.templateContent)
+ collect([node.templateContent]);
+ }
+ };
+ const listener = eventsHelper.addEventListener(this._session, 'DOM.setChildNodes',
+ (e: Protocol.DOM.setChildNodesPayload) => collect(e.nodes));
+ try {
+ const { root } = await this._session.send('DOM.getDocument');
+ collect([root]);
+ const { nodeId } = await this._session.send('DOM.requestNode', { objectId });
+ return { node: nodesById.get(nodeId), nodesById };
+ } catch {
+ return { node: undefined, nodesById };
+ } finally {
+ eventsHelper.removeEventListeners([listener]);
+ }
}
async getBoundingBox(handle: dom.ElementHandle): Promise<types.Rect | null> {
@@ -776,7 +818,31 @@ export class WVPage implements PageDelegate {
}
async getFrameElement(frame: frames.Frame): Promise<dom.ElementHandle> {
- throw new Error('Method not implemented');
+ const parent = frame.parentFrame();
+ if (!parent)
+ throw new Error('Frame has been detached.');
+ // Requesting the frame's own document streams its ancestor path, which
+ // includes the owner element (and its siblings). Pick the one whose
+ // contentDocument is this frame, then resolve it in the parent's context.
+ const documentHandle = await (await frame.mainContext()).evaluateHandle(() => document);
+ let ownerNodeId: number | undefined;
+ try {
+ if (documentHandle._objectId) {
+ const { nodesById } = await this._requestNodeViaDOM(documentHandle._objectId);
+ for (const node of nodesById.values()) {
+ if (node.contentDocument?.frameId === frame._id) {
+ ownerNodeId = node.nodeId;
+ break;
+ }
+ }
+ }
+ } finally {
+ documentHandle.dispose();
+ }
+ const resolved = ownerNodeId !== undefined ? await this._session.sendMayFail('DOM.resolveNode', { nodeId: ownerNodeId }) : null;
+ if (!resolved || resolved.object.subtype === 'null')
+ throw new Error('Frame has been detached.');
+ return createHandle(await parent.mainContext(), resolved.object) as dom.ElementHandle;
}
_adoptRequestFromNewProcess(navigationRequest: network.Request, newSession: WVSession, newRequestId: string) {
diff --git a/tests/webview/expectations/webkit-webview-page.txt b/tests/webview/expectations/webkit-webview-page.txt
index 20010dc7b5..3af3a2e64d 100644
--- a/tests/webview/expectations/webkit-webview-page.txt
+++ b/tests/webview/expectations/webkit-webview-page.txt
@@ -71,49 +71,30 @@ page/page-screenshot.spec.ts › page screenshot › should work while navigatin
page/page-screenshot.spec.ts › page screenshot › should work with Array deleted [fail]
# ============================================================================
-# frame-element-and-ownership (38 tests)
+# frame-element-and-ownership (remaining)
# elementHandle.contentFrame / elementHandle.ownerFrame / frame.frameElement
-# are not implemented in wvPage — see the throwing stubs in wvPage.ts.
-# ============================================================================
-page/elementhandle-content-frame.spec.ts › should return null for document.documentElement [fail]
-page/elementhandle-content-frame.spec.ts › should return null for non-iframes [fail]
-page/elementhandle-content-frame.spec.ts › should work [fail]
-page/elementhandle-content-frame.spec.ts › should work for cross-frame evaluations [fail]
-page/elementhandle-content-frame.spec.ts › should work for cross-process iframes [fail]
-page/elementhandle-owner-frame.spec.ts › should work [fail]
-page/elementhandle-owner-frame.spec.ts › should work for cross-frame evaluations [fail]
-page/elementhandle-owner-frame.spec.ts › should work for cross-process iframes [fail]
-page/elementhandle-owner-frame.spec.ts › should work for detached elements [fail]
-page/elementhandle-owner-frame.spec.ts › should work for document [fail]
-page/elementhandle-owner-frame.spec.ts › should work for iframe elements [fail]
-page/frame-evaluate.spec.ts › should be isolated between frames [fail]
-page/frame-evaluate.spec.ts › should have different execution contexts [fail]
+# are implemented in wvPage for same-origin and in-process cross-origin frames
+# (contentWindow token correlation, with name/url/document-order fallbacks), so
+# most of this section now passes and has been unskipped. The entries left here
+# are the ones that remain genuinely unsupported:
+# - cross-frame element-handle adoption (needs DOM node identity across
+# execution contexts, which the stock RDP DOM agent does not expose);
+# - <iframe> elements nested inside a closed/declarative shadow root (the
+# frame-element search does not pierce shadow roots);
+# - clicking/typing/dragging into an iframe (input is dispatched in page
+# coordinates without translating into the subframe's offset — same gap as
+# the frame bounding-box section).
+# ============================================================================
page/frame-evaluate.spec.ts › should not allow cross-frame element handles when frames do not script each other [fail]
-page/frame-evaluate.spec.ts › should throw for detached frames [fail]
-page/frame-frame-element.spec.ts › should throw when detached [fail]
-page/frame-frame-element.spec.ts › should work @smoke [fail]
page/frame-frame-element.spec.ts › should work inside closed shadow root [fail]
page/frame-frame-element.spec.ts › should work inside declarative shadow root [fail]
-page/frame-frame-element.spec.ts › should work with contentFrame [fail]
-page/frame-frame-element.spec.ts › should work with frameset [fail]
page/frame-goto.spec.ts › should return matching responses [fail]
-page/frame-hierarchy.spec.ts › should report different frame instance when frame re-attaches [fail]
-page/frame-hierarchy.spec.ts › should report frame.name() [fail]
-page/frame-hierarchy.spec.ts › should report frame.parent() [fail]
-page/frame-hierarchy.spec.ts › should send events when frames are manipulated dynamically [fail]
page/page-click.spec.ts › should click button inside frameset [fail]
page/page-click.spec.ts › should click the button inside an iframe [fail]
page/page-click.spec.ts › should not hang when frame is detached [fail]
page/page-drag.spec.ts › Drag and drop › should work inside iframe [fail]
-page/page-evaluate.spec.ts › should throw when frame is detached [fail]
page/page-keyboard.spec.ts › should type emoji into an iframe [fail]
-page/page-network-request.spec.ts › should work for subframe navigation request [fail]
-page/page-wait-for-function.spec.ts › should throw when frame is detached [fail]
page/page-wait-for-selector-1.spec.ts › page.waitForSelector is shortcut for main frame [fail]
-page/page-wait-for-selector-1.spec.ts › should run in specified frame [fail]
-page/page-wait-for-selector-1.spec.ts › should throw when frame is detached [fail]
-page/page-wait-for-selector-2.spec.ts › should run in specified frame xpath [fail]
-page/page-wait-for-selector-2.spec.ts › should throw when frame is detached xpath [fail]
# ============================================================================
# expose-function (2 tests)
@@ -236,9 +217,6 @@ page/expect-to-have-accessible.spec.ts › toHaveAccessibleDescription [fail]
page/expect-to-have-text.spec.ts › toHaveText with array › fail [fail]
page/frame-evaluate.spec.ts › should dispose context on cross-origin navigation [fail]
page/frame-evaluate.spec.ts › should dispose context on navigation [fail]
-page/frame-evaluate.spec.ts › should have correct execution contexts @smoke [fail]
-page/frame-evaluate.spec.ts › should work in iframes that failed initial navigation [fail]
-page/frame-goto.spec.ts › should continue after client redirect [fail]
page/interception.spec.ts › should intercept network activity from worker 2 [fail]
page/locator-frame.spec.ts › should click in lazy iframe [fail]
page/locator-misc-1.spec.ts › should clear input [fail]
@@ -338,6 +316,9 @@ page/page-network-request.spec.ts › should report raw headers [fail]
page/page-network-request.spec.ts › should return postData [fail]
page/page-network-request.spec.ts › should work with binary post data [fail]
page/page-network-request.spec.ts › should work with binary post data and interception [fail]
+# A subframe document request is reported without a frame (the frame is created
+# later on Page.frameNavigated), so request.frame() does not match the subframe.
+page/page-network-request.spec.ts › should work for subframe navigation request [fail]
page/page-network-sizes.spec.ts › should handle redirects [fail]
page/page-network-sizes.spec.ts › should have the correct responseBodySize for chunked request [fail]
page/page-network-sizes.spec.ts › should set bodySize and headersSize [fail]
@@ -547,6 +528,9 @@ page/page-autowaiting-basic.spec.ts › should report and collapse log in action
page/page-autowaiting-basic.spec.ts › should report and collapse log in expect [fail]
page/page-autowaiting-no-hang.spec.ts › assigning location to about:blank after non-about:blank [fail]
page/page-autowaiting-no-hang.spec.ts › calling window.stop sync [fail]
+# Pre-existing (fails on main too): a same-document navigation via the Navigation
+# API does not update the frame URL on the stock WebView backend.
+page/page-autowaiting-no-hang.spec.ts › clicking a link intercepted by the Navigation API same-document [fail]
page/page-click.spec.ts › should wait for becoming hit target [fail]
page/page-drag.spec.ts › Drag and drop › should respect the drop effect [fail]
page/page-drag.spec.ts › Drag and drop › should work if a frame is stalled [fail]
@@ -563,20 +547,22 @@ page/page-screenshot.spec.ts › should capture css box-shadow [fail]
page/to-match-aria-snapshot.spec.ts › should not match what is not matched [fail]
# ============================================================================
-# structural-unsupported (111 tests)
+# structural-unsupported (remaining)
# Tests whose name contains 'iframe', 'popup', or 'filechooser'. Stock WebKit
-# RDP exposes no Target.create / popup tracking / file-chooser API and our
-# wvPage subframe support is intentionally minimal — these tests can't be
-# made to pass without a real WebKit build. Bulk-skipped to keep the worker
-# from stalling on popup/dialog interactions.
-# ============================================================================
-page/elementhandle-content-frame.spec.ts › should return null for non-iframes [fail]
-page/elementhandle-content-frame.spec.ts › should work for cross-process iframes [fail]
-page/elementhandle-owner-frame.spec.ts › should work for cross-process iframes [fail]
-page/elementhandle-owner-frame.spec.ts › should work for iframe elements [fail]
-page/frame-evaluate.spec.ts › should work in iframes that failed initial navigation [fail]
+# RDP exposes no Target.create / popup tracking / file-chooser API, so popup and
+# file-chooser tests stay skipped. The remaining iframe entries here are the
+# selectors-frame / locator-frame suites, which are almost entirely built on
+# fulfilling the MAIN navigation via page.route. On iOS 26 that fulfillment is
+# flaky: the cross-origin navigation triggers a process swap whose provisional
+# target arrives un-paused (Target.setPauseOnStart is not honored for it), so it
+# fetches the real document before interception can run and the fulfilled body is
+# lost (see the main-navigation-fulfill-flaky section). The underlying iframe
+# support works — same-origin frame discovery, contentFrame/ownerFrame/
+# frameElement, and subframe interception all pass (see the now-unskipped
+# frame-evaluate / frame-hierarchy / frame-frame-element / elementhandle-*-frame
+# tests). Kept skipped until the provisional-target pause gap is addressed.
+# ============================================================================
page/frame-evaluate.spec.ts › should work in iframes that interrupted initial javascript url navigation [fail]
-page/frame-hierarchy.spec.ts › should refuse to display x-frame-options:deny iframe [fail]
page/locator-frame.spec.ts › click should survive iframe navigation [fail]
page/locator-frame.spec.ts › locator.frameLocator should work for iframe [fail]
page/locator-frame.spec.ts › should click in lazy iframe [fail]
@@ -667,9 +653,6 @@ page/page-wait-for-load-state.spec.ts › should wait for load state of about:bl
page/page-wait-for-load-state.spec.ts › should wait for load state of empty url popup [fail]
page/page-wait-for-load-state.spec.ts › should wait for load state of popup with network url [fail]
page/page-wait-for-load-state.spec.ts › should wait for load state of popup with network url and noopener [fail]
-page/page-wait-for-load-state.spec.ts › should work with broken blob-url iframe [fail]
-page/page-wait-for-load-state.spec.ts › should work with broken data-url iframe [fail]
-page/page-wait-for-load-state.spec.ts › should work with javascript: iframe [fail]
page/selectors-frame.spec.ts › click should survive iframe navigation [fail]
page/selectors-frame.spec.ts › should click in lazy iframe [fail]
page/selectors-frame.spec.ts › should work for iframe (handle) [fail]
@@ -710,14 +693,15 @@ page/page-fill.spec.ts › should throw on incorrect datetime-local [fail]
# leaves the page target dead for the next test's first navigation (see the
# worker-remaining section). Re-run and prune this section as triggers are fixed.
# ============================================================================
+# These two pass in isolation but their cross-process/cross-site MAIN navigation
+# leaves the page target dead for the following test in the file, so they stay
+# skipped with the rest of the cascade triggers.
+page/frame-evaluate.spec.ts › should execute after cross-site navigation [fail]
+page/frame-hierarchy.spec.ts › should persist mainFrame on cross-process navigation [fail]
page/elementhandle-click.spec.ts › should work @smoke [fail]
page/elementhandle-owner-frame.spec.ts › should work for adopted elements [fail]
page/elementhandle-query-selector.spec.ts › should work for adopted elements [fail]
page/elementhandle-scroll-into-view.spec.ts › should scroll display:contents into view [fail]
-page/frame-evaluate.spec.ts › should execute after cross-site navigation [fail]
-page/frame-evaluate.spec.ts › should not allow cross-frame js handles [fail]
-page/frame-hierarchy.spec.ts › should not send attach/detach events for main frame [fail]
-page/frame-hierarchy.spec.ts › should persist mainFrame on cross-process navigation [fail]
page/frame-hierarchy.spec.ts › should send "framenavigated" when navigating on anchor URLs [fail]
page/interception.spec.ts › should intercept network activity from worker [fail]
page/interception.spec.ts › should intercept worker requests when enabled after worker creation [fail]