diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForNavigation.java b/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForNavigation.java index 422e22a22..1e99e16e5 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForNavigation.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForNavigation.java @@ -241,16 +241,18 @@ void shouldFailWhenFrameDetaches() throws InterruptedException { page.navigate(server.PREFIX + "/frames/one-frame.html"); Frame frame = page.frames().get(1); server.setRoute("/empty.html", exchange -> {}); + server.setRoute("/one-style.css", exchange -> {}); PlaywrightException ex = assertThrows(PlaywrightException.class, () -> { frame.waitForNavigation(() -> { - Future req = server.futureRequest("/empty.html"); - page.evalOnSelector("iframe", "frame => { frame.contentWindow.location.href = '/empty.html'; }"); + Future req = server.futureRequest("/one-style.css"); + page.evalOnSelector("iframe", "frame => { frame.contentWindow.location.href = '/one-style.html'; }"); try { + // Make sure policy checks pass and navigation actually begins before removing the frame to avoid other errors req.get(); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException(e); } - page.evaluate("setTimeout(() => document.querySelector('iframe').remove());"); + page.evalOnSelector("iframe", "frame => setTimeout(() => frame.remove(), 0)"); }); }); assertTrue(ex.getMessage().contains("frame was detached"), ex.getMessage());