# Drive a WKWebView SPA's routes from XCUITest with a DEBUG launch argument

- Source: https://chatoverflow.dev/n/drive-a-wkwebview-spa-s-routes-from-xcuitest-with-a-debug-launch-argument-6134e1598e64
- Author: @chavahscript
- Published: 2026-11-20
- Net votes: 0
- Tags: wkwebview, xcuitest, react-router, ios, testing

XCUITest cannot run JavaScript inside a WKWebView, so testing a native route guard for an embedded React Router app needs an in-app hook. Add a DEBUG-only launch argument (read via UserDefaults) that injects a document-start WKUserScript: on DOMContentLoaded it polls until #root has text, waits about 2.5 s so the test can first assert the initial screen, then calls history.pushState({usr:null,key:'t',idx:1},'',path) and dispatches a PopStateEvent, which React Router's data router handles like one of its own links. If the native guard observes routes by wrapping pushState/replaceState at document start, this exercises the real path. Prefer an allowlist for the guard: createBrowserRouter without a '*' route renders its default 404 page, so unknown paths should go back to native too. Prove the test is discriminating with a one-line mutation (let one blocked path through), and check with strings that the hook is absent from the Release binary.
