diff --git a/vaadin-testbench-core-junit5/src/test/java/com/vaadin/testbench/TestBenchDriverTest.java b/vaadin-testbench-core-junit5/src/test/java/com/vaadin/testbench/TestBenchDriverTest.java index d52b9ca09..3de00e9e1 100644 --- a/vaadin-testbench-core-junit5/src/test/java/com/vaadin/testbench/TestBenchDriverTest.java +++ b/vaadin-testbench-core-junit5/src/test/java/com/vaadin/testbench/TestBenchDriverTest.java @@ -64,7 +64,8 @@ public void testTestBenchDriverActsAsProxy() { mockDriver.quit(); Mockito.when(((JavascriptExecutor) mockDriver) - .executeScript(Mockito.anyString())).thenReturn(true); + .executeAsyncScript(Mockito.anyString(), Mockito.any())) + .thenReturn(true); TargetLocator mockTargetLocator = Mockito.mock(TargetLocator.class); Mockito.when(mockDriver.switchTo()).thenReturn(mockTargetLocator); @@ -103,8 +104,8 @@ public void testDisableWaitForVaadin() { FirefoxDriver mockFF = Mockito.mock(FirefoxDriver.class); Mockito.when(mockFF.getCapabilities()).thenReturn(mockCapabilities); - Mockito.when( - mockFF.executeScript(contains("clients[client].isActive()"))) + Mockito.when(mockFF.executeAsyncScript( + contains("clients[client].isActive()"), Mockito.any())) .thenReturn(true); WebElement mockElement = Mockito.mock(WebElement.class); Mockito.when(mockFF.findElement(isA(By.class))).thenReturn(mockElement); diff --git a/vaadin-testbench-core-junit5/src/test/java/com/vaadin/testbench/commands/TestBenchCommandExecutorTest.java b/vaadin-testbench-core-junit5/src/test/java/com/vaadin/testbench/commands/TestBenchCommandExecutorTest.java index 55ed15852..90f2dca4e 100644 --- a/vaadin-testbench-core-junit5/src/test/java/com/vaadin/testbench/commands/TestBenchCommandExecutorTest.java +++ b/vaadin-testbench-core-junit5/src/test/java/com/vaadin/testbench/commands/TestBenchCommandExecutorTest.java @@ -18,7 +18,9 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.openqa.selenium.Capabilities; +import org.openqa.selenium.JavascriptException; import org.openqa.selenium.OutputType; +import org.openqa.selenium.ScriptTimeoutException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.remote.RemoteWebDriver; @@ -235,8 +237,8 @@ private WebDriver mockScreenshotDriver(int nrScreenshotsGrabbed, "cursor-bottom-edge-off.png"); Mockito.when(driver.getScreenshotAs(OutputType.BYTES)) .thenReturn(screenshotBytes); - Mockito.when( - driver.executeScript(Mockito.contains("window.Vaadin.Flow"))) + Mockito.when(driver.executeAsyncScript( + Mockito.contains("window.Vaadin.Flow"), Mockito.any())) .thenReturn(Boolean.TRUE); if (expectGetCapabilities) { Capabilities mockedCapabilities = Mockito.mock(Capabilities.class); @@ -303,10 +305,68 @@ public void testTotalTimeSpentServicingRequests() { } + @Test + public void waitForVaadin_idle_asksBrowserOnce() { + FirefoxDriver driver = Mockito.mock(FirefoxDriver.class); + Mockito.when(driver.executeAsyncScript(Mockito.contains("flow.ready("), + Mockito.any())).thenReturn(Boolean.TRUE); + + newCommandExecutor(driver).waitForVaadin(); + + Mockito.verify(driver).executeAsyncScript( + Mockito.contains("flow.ready("), + // Flow's own timeout must end the call before the default + // WebDriver script timeout of 30s does + Mockito.longThat(timeout -> timeout > 0 && timeout < 30000)); + } + + @Test + public void waitForVaadin_notIdleOrInterrupted_asksAgain() { + FirefoxDriver driver = Mockito.mock(FirefoxDriver.class); + Mockito.when( + driver.executeAsyncScript(Mockito.anyString(), Mockito.any())) + .thenReturn(Boolean.FALSE) + .thenThrow(new ScriptTimeoutException("script timeout"), + new JavascriptException( + "document unloaded while waiting for result"), + new JavascriptException("Document was unloaded")) + .thenReturn(Boolean.TRUE); + + newCommandExecutor(driver).waitForVaadin(); + + Mockito.verify(driver, Mockito.times(5)) + .executeAsyncScript(Mockito.anyString(), Mockito.any()); + } + + @Test + public void waitForVaadin_scriptError_isNotRetried() { + FirefoxDriver driver = Mockito.mock(FirefoxDriver.class); + JavascriptException error = new JavascriptException( + "TypeError: clients[client].isActive is not a function"); + Mockito.when( + driver.executeAsyncScript(Mockito.anyString(), Mockito.any())) + .thenThrow(error); + TestBenchCommandExecutor tbce = newCommandExecutor(driver); + + JavascriptException thrown = Assertions + .assertThrows(JavascriptException.class, tbce::waitForVaadin); + + Assertions.assertSame(error, thrown); + Mockito.verify(driver).executeAsyncScript(Mockito.anyString(), + Mockito.any()); + } + + private TestBenchCommandExecutor newCommandExecutor(FirefoxDriver driver) { + TestBenchCommandExecutor tbce = new TestBenchCommandExecutor(null, + null); + tbce.setDriver(TestBench.createDriver(driver, tbce)); + return tbce; + } + private FirefoxDriver mockJSExecutor(boolean forcesSync) { FirefoxDriver jse = Mockito.mock(FirefoxDriver.class); - Mockito.when(jse - .executeScript(Mockito.contains("window.Vaadin.Flow.client"))) + Mockito.when(jse.executeAsyncScript( + Mockito.contains("window.Vaadin.Flow"), Mockito.any())) .thenReturn(Boolean.TRUE); Mockito.when(jse.executeScript(Mockito.contains("getProfilingData()"))) .thenReturn(Arrays.asList(1000L, 2000L, 3000L)); diff --git a/vaadin-testbench-core/src/test/java/com/vaadin/testbench/TestBenchDriverTest.java b/vaadin-testbench-core/src/test/java/com/vaadin/testbench/TestBenchDriverTest.java index 56bfb7e26..bb5ac724d 100644 --- a/vaadin-testbench-core/src/test/java/com/vaadin/testbench/TestBenchDriverTest.java +++ b/vaadin-testbench-core/src/test/java/com/vaadin/testbench/TestBenchDriverTest.java @@ -65,7 +65,8 @@ public void testTestBenchDriverActsAsProxy() { mockDriver.quit(); Mockito.when(((JavascriptExecutor) mockDriver) - .executeScript(Mockito.anyString())).thenReturn(true); + .executeAsyncScript(Mockito.anyString(), Mockito.any())) + .thenReturn(true); TargetLocator mockTargetLocator = Mockito.mock(TargetLocator.class); Mockito.when(mockDriver.switchTo()).thenReturn(mockTargetLocator); @@ -103,8 +104,8 @@ public void testDisableWaitForVaadin() { FirefoxDriver mockFF = Mockito.mock(FirefoxDriver.class); Mockito.when(mockFF.getCapabilities()).thenReturn(mockCapabilities); - Mockito.when( - mockFF.executeScript(contains("clients[client].isActive()"))) + Mockito.when(mockFF.executeAsyncScript( + contains("clients[client].isActive()"), Mockito.any())) .thenReturn(true); WebElement mockElement = Mockito.mock(WebElement.class); Mockito.when(mockFF.findElement(isA(By.class))).thenReturn(mockElement); diff --git a/vaadin-testbench-core/src/test/java/com/vaadin/testbench/commands/TestBenchCommandExecutorTest.java b/vaadin-testbench-core/src/test/java/com/vaadin/testbench/commands/TestBenchCommandExecutorTest.java index 68ea1514b..c0d45f9a0 100644 --- a/vaadin-testbench-core/src/test/java/com/vaadin/testbench/commands/TestBenchCommandExecutorTest.java +++ b/vaadin-testbench-core/src/test/java/com/vaadin/testbench/commands/TestBenchCommandExecutorTest.java @@ -17,7 +17,9 @@ import org.junit.Test; import org.mockito.Mockito; import org.openqa.selenium.Capabilities; +import org.openqa.selenium.JavascriptException; import org.openqa.selenium.OutputType; +import org.openqa.selenium.ScriptTimeoutException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.remote.RemoteWebDriver; @@ -32,6 +34,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; public class TestBenchCommandExecutorTest { @@ -216,8 +220,8 @@ private WebDriver mockScreenshotDriver(int nrScreenshotsGrabbed, "cursor-bottom-edge-off.png"); Mockito.when(driver.getScreenshotAs(OutputType.BYTES)) .thenReturn(screenshotBytes); - Mockito.when( - driver.executeScript(Mockito.contains("window.Vaadin.Flow"))) + Mockito.when(driver.executeAsyncScript( + Mockito.contains("window.Vaadin.Flow"), Mockito.any())) .thenReturn(Boolean.TRUE); if (expectGetCapabilities) { Capabilities mockedCapabilities = Mockito.mock(Capabilities.class); @@ -284,10 +288,68 @@ public void testTotalTimeSpentServicingRequests() { } + @Test + public void waitForVaadin_idle_asksBrowserOnce() { + FirefoxDriver driver = Mockito.mock(FirefoxDriver.class); + Mockito.when(driver.executeAsyncScript(Mockito.contains("flow.ready("), + Mockito.any())).thenReturn(Boolean.TRUE); + + newCommandExecutor(driver).waitForVaadin(); + + Mockito.verify(driver).executeAsyncScript( + Mockito.contains("flow.ready("), + // Flow's own timeout must end the call before the default + // WebDriver script timeout of 30s does + Mockito.longThat(timeout -> timeout > 0 && timeout < 30000)); + } + + @Test + public void waitForVaadin_notIdleOrInterrupted_asksAgain() { + FirefoxDriver driver = Mockito.mock(FirefoxDriver.class); + Mockito.when( + driver.executeAsyncScript(Mockito.anyString(), Mockito.any())) + .thenReturn(Boolean.FALSE) + .thenThrow(new ScriptTimeoutException("script timeout"), + new JavascriptException( + "document unloaded while waiting for result"), + new JavascriptException("Document was unloaded")) + .thenReturn(Boolean.TRUE); + + newCommandExecutor(driver).waitForVaadin(); + + Mockito.verify(driver, Mockito.times(5)) + .executeAsyncScript(Mockito.anyString(), Mockito.any()); + } + + @Test + public void waitForVaadin_scriptError_isNotRetried() { + FirefoxDriver driver = Mockito.mock(FirefoxDriver.class); + JavascriptException error = new JavascriptException( + "TypeError: clients[client].isActive is not a function"); + Mockito.when( + driver.executeAsyncScript(Mockito.anyString(), Mockito.any())) + .thenThrow(error); + TestBenchCommandExecutor tbce = newCommandExecutor(driver); + + JavascriptException thrown = assertThrows(JavascriptException.class, + tbce::waitForVaadin); + + assertSame(error, thrown); + Mockito.verify(driver).executeAsyncScript(Mockito.anyString(), + Mockito.any()); + } + + private TestBenchCommandExecutor newCommandExecutor(FirefoxDriver driver) { + TestBenchCommandExecutor tbce = new TestBenchCommandExecutor(null, + null); + tbce.setDriver(TestBench.createDriver(driver, tbce)); + return tbce; + } + private FirefoxDriver mockJSExecutor(boolean forcesSync) { FirefoxDriver jse = Mockito.mock(FirefoxDriver.class); - Mockito.when(jse - .executeScript(Mockito.contains("window.Vaadin.Flow.client"))) + Mockito.when(jse.executeAsyncScript( + Mockito.contains("window.Vaadin.Flow"), Mockito.any())) .thenReturn(Boolean.TRUE); Mockito.when(jse.executeScript(Mockito.contains("getProfilingData()"))) .thenReturn(Arrays.asList(1000L, 2000L, 3000L)); diff --git a/vaadin-testbench-integration-tests-junit5/src/test/java/com/vaadin/tests/WaitForVaadinIT.java b/vaadin-testbench-integration-tests-junit5/src/test/java/com/vaadin/tests/WaitForVaadinIT.java index 75d0ff20c..338448be5 100644 --- a/vaadin-testbench-integration-tests-junit5/src/test/java/com/vaadin/tests/WaitForVaadinIT.java +++ b/vaadin-testbench-integration-tests-junit5/src/test/java/com/vaadin/tests/WaitForVaadinIT.java @@ -8,10 +8,7 @@ */ package com.vaadin.tests; -import java.lang.reflect.Field; - import org.junit.jupiter.api.Assertions; -import org.openqa.selenium.JavascriptExecutor; import com.vaadin.flow.component.Component; import com.vaadin.testUI.PageObjectView; @@ -46,9 +43,10 @@ public void waitForVaadin_activeConnector_waitsUtilReady() { openTestURL(); assertDevServerIsNotLoaded(); getCommandExecutor().executeScript( - "window.Vaadin.Flow.clients[\"blocker\"] = {isActive: () => true};"); - setWaitForVaadinLoopHook(500, - "window.Vaadin.Flow.clients[\"blocker\"] = {isActive: () => false};"); + "window.Vaadin.Flow.clients[\"blocker\"] = {isActive: () => true};" + + "setTimeout(function() {" + + " window.Vaadin.Flow.clients[\"blocker\"] = {isActive: () => false};" + + "}, 500);"); getCommandExecutor().waitForVaadin(); assertClientIsActive(); } @@ -82,9 +80,10 @@ public void waitForVaadin_devModeNotReady_waitsUntilReady() { openTestURL(); assertDevServerIsNotLoaded(); getCommandExecutor().executeScript( - "window.Vaadin = {Flow: {devServerIsNotLoaded: true}};"); - setWaitForVaadinLoopHook(500, - "window.Vaadin.Flow.devServerIsNotLoaded = false;"); + "window.Vaadin = {Flow: {devServerIsNotLoaded: true}};" + + "setTimeout(function() {" + + " window.Vaadin.Flow.devServerIsNotLoaded = false;" + + "}, 500);"); getCommandExecutor().waitForVaadin(); assertDevServerIsNotLoaded(); } @@ -123,28 +122,4 @@ private void assertExecutionBlocked(Runnable command) { "Unexpected execution time, waiting time = " + timeout); } - private void setWaitForVaadinLoopHook(long timeout, - String scriptToRunAfterTimeout) { - long systemCurrentTimeMillis = System.currentTimeMillis(); - setWaitForVaadinLoopHook(() -> { - if (System.currentTimeMillis() - - systemCurrentTimeMillis > timeout) { - ((JavascriptExecutor) getCommandExecutor().getDriver() - .getWrappedDriver()) - .executeScript(scriptToRunAfterTimeout); - setWaitForVaadinLoopHook(null); - } - }); - } - - private void setWaitForVaadinLoopHook(Runnable action) { - try { - Field field = getCommandExecutor().getClass() - .getDeclaredField("waitForVaadinLoopHook"); - field.setAccessible(true); - field.set(getCommandExecutor(), action); - } catch (NoSuchFieldException | IllegalAccessException e) { - throw new RuntimeException(e); - } - } } diff --git a/vaadin-testbench-integration-tests/src/test/java/com/vaadin/tests/WaitForVaadinIT.java b/vaadin-testbench-integration-tests/src/test/java/com/vaadin/tests/WaitForVaadinIT.java index 7dbb88a5e..55a0b02c7 100644 --- a/vaadin-testbench-integration-tests/src/test/java/com/vaadin/tests/WaitForVaadinIT.java +++ b/vaadin-testbench-integration-tests/src/test/java/com/vaadin/tests/WaitForVaadinIT.java @@ -8,11 +8,8 @@ */ package com.vaadin.tests; -import java.lang.reflect.Field; - import org.junit.Assert; import org.junit.Test; -import org.openqa.selenium.JavascriptExecutor; import com.vaadin.flow.component.Component; import com.vaadin.testUI.PageObjectView; @@ -46,9 +43,10 @@ public void waitForVaadin_activeConnector_waitsUtilReady() { openTestURL(); assertDevServerIsNotLoaded(); getCommandExecutor().executeScript( - "window.Vaadin.Flow.clients[\"blocker\"] = {isActive: () => true};"); - setWaitForVaadinLoopHook(500, - "window.Vaadin.Flow.clients[\"blocker\"] = {isActive: () => false};"); + "window.Vaadin.Flow.clients[\"blocker\"] = {isActive: () => true};" + + "setTimeout(function() {" + + " window.Vaadin.Flow.clients[\"blocker\"] = {isActive: () => false};" + + "}, 500);"); getCommandExecutor().waitForVaadin(); assertClientIsActive(); } @@ -83,9 +81,10 @@ public void waitForVaadin_devModeNotReady_waitsUntilReady() { openTestURL(); assertDevServerIsNotLoaded(); getCommandExecutor().executeScript( - "window.Vaadin = {Flow: {devServerIsNotLoaded: true}};"); - setWaitForVaadinLoopHook(500, - "window.Vaadin.Flow.devServerIsNotLoaded = false;"); + "window.Vaadin = {Flow: {devServerIsNotLoaded: true}};" + + "setTimeout(function() {" + + " window.Vaadin.Flow.devServerIsNotLoaded = false;" + + "}, 500);"); getCommandExecutor().waitForVaadin(); assertDevServerIsNotLoaded(); } @@ -125,28 +124,4 @@ private void assertExecutionBlocked(Runnable command) { timeout >= BLOCKING_EXECUTION_TIMEOUT); } - private void setWaitForVaadinLoopHook(long timeout, - String scriptToRunAfterTimeout) { - long systemCurrentTimeMillis = System.currentTimeMillis(); - setWaitForVaadinLoopHook(() -> { - if (System.currentTimeMillis() - - systemCurrentTimeMillis > timeout) { - ((JavascriptExecutor) getCommandExecutor().getDriver() - .getWrappedDriver()) - .executeScript(scriptToRunAfterTimeout); - setWaitForVaadinLoopHook(null); - } - }); - } - - private void setWaitForVaadinLoopHook(Runnable action) { - try { - Field field = getCommandExecutor().getClass() - .getDeclaredField("waitForVaadinLoopHook"); - field.setAccessible(true); - field.set(getCommandExecutor(), action); - } catch (NoSuchFieldException | IllegalAccessException e) { - throw new RuntimeException(e); - } - } } diff --git a/vaadin-testbench-shared/src/main/java/com/vaadin/testbench/commands/TestBenchCommandExecutor.java b/vaadin-testbench-shared/src/main/java/com/vaadin/testbench/commands/TestBenchCommandExecutor.java index aeaaef709..babd05e51 100644 --- a/vaadin-testbench-shared/src/main/java/com/vaadin/testbench/commands/TestBenchCommandExecutor.java +++ b/vaadin-testbench-shared/src/main/java/com/vaadin/testbench/commands/TestBenchCommandExecutor.java @@ -14,11 +14,14 @@ import java.net.InetAddress; import java.net.UnknownHostException; import java.util.List; +import java.util.Locale; import org.openqa.selenium.Dimension; import org.openqa.selenium.HasCapabilities; +import org.openqa.selenium.JavascriptException; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.Point; +import org.openqa.selenium.ScriptTimeoutException; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.HttpCommandExecutor; @@ -49,28 +52,53 @@ private static Logger getLogger() { private boolean enableWaitForVaadin = true; private boolean autoScrollIntoView = true; private boolean nativeClick = Parameters.isNativeClick(); + + /** + * Calls back with {@code true} when Vaadin is idle and {@code false} when + * it should be asked again. + *

+ * When the page provides {@code window.Vaadin.Flow.ready()}, Flow decides + * what idle means and the script only returns once Flow is idle or the + * given timeout has passed. Otherwise, e.g. on older Flow versions or while + * the dev server is starting, the state is checked once and returned + * immediately. + */ // @formatter:off - String WAIT_FOR_VAADIN_SCRIPT = - "if (document.readyState != 'complete') {" - + " return false;" + private static final String WAIT_FOR_VAADIN_SCRIPT = + "var callback = arguments[arguments.length - 1];" + + "var flow = window.Vaadin && window.Vaadin.Flow;" + + "if (flow && typeof flow.ready === 'function') {" + + " flow.ready({ timeout: arguments[0] }).then(" + + " function() { callback(true); }," + + " function() { callback(false); });" + + " return;" + "}" - + "if (window.Vaadin && window.Vaadin.Flow && window.Vaadin.Flow.devServerIsNotLoaded) {" - + " return false;" - + "} else if (window.Vaadin && window.Vaadin.Flow && window.Vaadin.Flow.clients) {" - + " var clients = window.Vaadin.Flow.clients;" + + "if (document.readyState != 'complete') {" + + " callback(false);" + + "} else if (flow && flow.devServerIsNotLoaded) {" + + " callback(false);" + + "} else if (flow && flow.clients) {" + + " var clients = flow.clients;" + " for (var client in clients) {" + " if (clients[client].isActive()) {" - + " return false;" + + " callback(false);" + + " return;" + " }" + " }" - + " return true;" + + " callback(true);" + "} else {" - + " return true;" + + " callback(true);" + "}"; // @formatter:on - // A hook for testing purposes - private Runnable waitForVaadinLoopHook; + private static final long WAIT_FOR_VAADIN_TIMEOUT_MS = 40000; + + /** + * The longest a single call to {@code window.Vaadin.Flow.ready()} may wait. + * Kept below the default WebDriver script timeout of 30s so that Flow's own + * timeout ends the call rather than the driver. + */ + private static final long FLOW_READY_TIMEOUT_MS = 10000; public TestBenchCommandExecutor(ImageComparison imageComparison, ReferenceNameGenerator referenceNameGenerator) { @@ -111,6 +139,10 @@ public String getRemoteControlName() { /** * Block until Vaadin reports it has finished processing server messages. + *

+ * When the page provides {@code window.Vaadin.Flow.ready()}, the waiting + * happens in the browser and costs a single WebDriver call. Otherwise the + * state is polled until Vaadin is idle. */ public void waitForVaadin() { if (!enableWaitForVaadin) { @@ -118,24 +150,54 @@ public void waitForVaadin() { return; } - long timeoutTime = System.currentTimeMillis() + 40000; - Boolean finished = false; - while (System.currentTimeMillis() < timeoutTime && !finished) { - if (waitForVaadinLoopHook != null) { - waitForVaadinLoopHook.run(); + // Must use the wrapped driver here to avoid calling waitForVaadin + // again + JavascriptExecutor executor = (JavascriptExecutor) getDriver() + .getWrappedDriver(); + long timeoutTime = System.currentTimeMillis() + + WAIT_FOR_VAADIN_TIMEOUT_MS; + while (System.currentTimeMillis() < timeoutTime) { + long flowReadyTimeout = Math.min(FLOW_READY_TIMEOUT_MS, + timeoutTime - System.currentTimeMillis()); + if (isVaadinIdle(executor, flowReadyTimeout)) { + return; } - // Must use the wrapped driver here to avoid calling waitForVaadin - // again - finished = (Boolean) ((JavascriptExecutor) getDriver() - .getWrappedDriver()).executeScript(WAIT_FOR_VAADIN_SCRIPT); - if (finished == null) { - // This should never happen but according to - // https://dev.vaadin.com/ticket/19703, it happens - getLogger().debug( - "waitForVaadin returned null, this should never happen"); - finished = false; + } + } + + private boolean isVaadinIdle(JavascriptExecutor executor, + long flowReadyTimeout) { + Object finished; + try { + finished = executor.executeAsyncScript(WAIT_FOR_VAADIN_SCRIPT, + flowReadyTimeout); + } catch (ScriptTimeoutException e) { + getLogger().debug("Waiting for Vaadin was interrupted", e); + return false; + } catch (JavascriptException e) { + if (!isPageUnloaded(e)) { + throw e; } + // The page was replaced while waiting, e.g. by a navigation or by + // the dev server finishing its startup + getLogger().debug("Waiting for Vaadin was interrupted", e); + return false; } + if (finished == null) { + // This should never happen but according to + // https://dev.vaadin.com/ticket/19703, it happens + getLogger().debug( + "waitForVaadin returned null, this should never happen"); + } + return Boolean.TRUE.equals(finished); + } + + private static boolean isPageUnloaded(JavascriptException e) { + // e.g. "document unloaded while waiting for result" (Chrome) or + // "Document was unloaded" (Firefox) + String message = e.getRawMessage(); + return message != null + && message.toLowerCase(Locale.ROOT).contains("unloaded"); } @Override