{"record":{"id":"5d42c1637f1d3a45","repo":"karatelabs/karate","slug":"failed-to-create-isolated-world-for-frame","errorCode":null,"errorMessage":"failed to create isolated world for frame {}: {}","messagePattern":"failed to create isolated world for frame (.+?): (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java","lineNumber":2545,"sourceCode":"        }\n        // Last resort: create an isolated world if the main world context never arrived.\n        // This is less ideal (page-set variables won't be visible) but allows basic DOM access.\n        if (!frameContexts.containsKey(frameId)) {\n            logger.warn(\"frame context not received via event, falling back to isolated world: {}\", frameId);\n            try {\n                CdpResponse response = cdp.method(\"Page.createIsolatedWorld\")\n                        .param(\"frameId\", frameId)\n                        .send();\n                Integer contextId = response.getResult(\"executionContextId\");\n                if (contextId != null) {\n                    frameContexts.put(frameId, contextId);\n                    // Complete the readiness future too, so a concurrent waiter on the\n                    // same frame observes the isolated world instead of timing out.\n                    completeFrameContext(frameId, contextId);\n                    logger.debug(\"created isolated world for frame {}: contextId={}\", frameId, contextId);\n                }\n            } catch (Exception e) {\n                logger.warn(\"failed to create isolated world for frame {}: {}\", frameId, e.getMessage());\n            }\n        }\n\n        // Verify the frame context is alive and ready for JS execution\n        // This is critical for flaky test prevention - the context may exist but\n        // the frame's document might not be ready yet (still loading)\n        waitForFrameContextReady(frameId);\n    }\n\n    /**\n     * Wait for an OOPIF's document to leave the 'loading' state. cdp.sessionId is\n     * already routed to the OOPIF session when this is called, so a contextId-less\n     * Runtime.evaluate hits the OOPIF's default (main world) execution context.\n     *\n     * <p>When {@code expectedUrl} is non-empty, also require {@code document.URL} to\n     * contain it. {@code window.location.href} updates as soon as a navigation is\n     * committed in the browsing context — that's what {@code switchFrame} matches on\n     * — but {@code document.URL} only updates when the new {@link org.w3c.dom.Document}","sourceCodeStart":2527,"sourceCodeEnd":2563,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java#L2527-L2563","documentation":"After the event wait and the Page.createIsolatedWorld fallback both fail, CdpDriver logs that it could not create the isolated world for the frame and swallows the exception. Subsequent script evaluation against that frame will likely fail (no execution context). This indicates the CDP command itself errored — usually the frame no longer exists or the CDP session is unhealthy.","triggerScenarios":"ensureFrameContext() runs Page.createIsolatedWorld and the send() throws — frame detached/navigated between the call, invalid frameId, target crashed, or CDP connection dropped.","commonSituations":"Frames removed mid-interaction (ad iframes, SPA route changes), browser/tab closed while test was switching frames, or flaky CDP websocket during heavy parallel load.","solutions":["Re-locate and re-switch into the frame after it stabilizes — the old frameId is stale; retry the whole switchFrame.","Add a wait for the frame to exist before switching (locator-based wait).","Check browser health: if the tab crashed, re-launch the driver.","Reduce parallelism or increase CDP timeouts if websocket congestion causes dropped sessions."],"exampleFix":"// before\ndriver.switchFrame(\"iframe#ad\"); // frame may vanish\n// after\nif (driver.exists(\"iframe#ad\")) {\n    driver.switchFrame(\"iframe#ad\");\n}","handlingStrategy":"retry","validationCode":"if (driver.exists(\"iframe#ad\")) { driver.switchFrame(\"iframe#ad\"); }","typeGuard":null,"tryCatchPattern":"try {\n    driver.switchFrame(frameLocator);\n} catch (RuntimeException e) {\n    // frame may have detached; re-check existence and retry once\n    if (driver.exists(frameLocator)) { driver.switchFrame(frameLocator); }\n}","preventionTips":["Check frame existence before switching","Avoid switching into frames known to be short-lived (ads, toasts)","Keep the browser session healthy; restart on repeated CDP failures"],"tags":["cdp","iframe","isolated-world","frame-detached"],"backgroundTag":"iframe-execution-context-not-ready","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}