From d55218a38ee07c5b7d3319054fb29a2cf33b547a Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Fri, 1 May 2026 19:11:01 +0000 Subject: [PATCH 1/7] Delegate waitForVaadin readiness checks to Flow TestBench used to reimplement Flow's "am I idle?" logic in an inline JS poll loop that checked document.readyState, devServerIsNotLoaded, and iterated Flow.clients for isActive(). Flow now exposes that as a canonical Promise API (window.Vaadin.Flow.ready), so use it instead and let Flow own the readiness model. Keeps TestBench in sync as Flow's bootstrap evolves and removes a parallel implementation that could drift. waitForVaadin runs in two phases: a short Java-side poll for the ready function to be defined (survives dev server page reloads), then a single executeAsyncScript that calls Flow.ready({ timeout }) with the remaining budget. Both fulfilment and rejection resolve to the same callback so a JS-side timeout still releases the script. The waitForVaadinLoopHook reflection seam is gone; integration tests now drive readiness from JS (setTimeout flipping Flow.ready) and use Flow.ready = false as the not-ready sentinel, matching the dev-mode loading page. Drops waitForVaadin_noFlow_returnsImmediately since Flow is now required to be loaded. --- .../TestBenchCommandExecutorTest.java | 23 +++- .../TestBenchCommandExecutorTest.java | 23 +++- .../com/vaadin/tests/WaitForVaadinIT.java | 67 ++--------- .../com/vaadin/tests/WaitForVaadinIT.java | 67 ++--------- .../commands/TestBenchCommandExecutor.java | 112 ++++++++++++------ 5 files changed, 138 insertions(+), 154 deletions(-) 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 188865b82..7389664b3 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 @@ -11,6 +11,7 @@ import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; +import java.time.Duration; import java.util.Arrays; import org.junit.jupiter.api.Assertions; @@ -213,9 +214,7 @@ 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"))) - .thenReturn(Boolean.TRUE); + mockWaitForVaadin(driver); if (expectGetCapabilities) { Capabilities mockedCapabilities = Mockito.mock(Capabilities.class); Mockito.when(mockedCapabilities.getBrowserName()) @@ -283,6 +282,7 @@ public void testTotalTimeSpentServicingRequests() { private FirefoxDriver mockJSExecutor(boolean forcesSync) { FirefoxDriver jse = Mockito.mock(FirefoxDriver.class); + mockWaitForVaadin(jse); Mockito.when(jse .executeScript(Mockito.contains("window.Vaadin.Flow.client"))) .thenReturn(Boolean.TRUE); @@ -290,4 +290,21 @@ private FirefoxDriver mockJSExecutor(boolean forcesSync) { .thenReturn(Arrays.asList(1000L, 2000L, 3000L)); return jse; } + + private void mockWaitForVaadin(RemoteWebDriver driver) { + WebDriver.Options options = Mockito.mock(WebDriver.Options.class); + WebDriver.Timeouts timeouts = Mockito.mock(WebDriver.Timeouts.class); + Mockito.when(driver.manage()).thenReturn(options); + Mockito.when(options.timeouts()).thenReturn(timeouts); + Mockito.when(timeouts.getScriptTimeout()) + .thenReturn(Duration.ofSeconds(30)); + Mockito.when(timeouts.scriptTimeout(Mockito.any(Duration.class))) + .thenReturn(timeouts); + // Phase 1: sync check for Flow.ready returns true immediately + Mockito.when(driver.executeScript( + Mockito.contains("typeof window.Vaadin.Flow.ready"))) + .thenReturn(Boolean.TRUE); + Mockito.when(driver.executeAsyncScript(Mockito.contains("Flow.ready"), + Mockito.anyLong())).thenReturn(null); + } } 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..d415f6182 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 @@ -11,6 +11,7 @@ import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; +import java.time.Duration; import java.util.Arrays; import org.junit.Before; @@ -216,9 +217,7 @@ 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"))) - .thenReturn(Boolean.TRUE); + mockWaitForVaadin(driver); if (expectGetCapabilities) { Capabilities mockedCapabilities = Mockito.mock(Capabilities.class); Mockito.when(mockedCapabilities.getBrowserName()) @@ -286,6 +285,7 @@ public void testTotalTimeSpentServicingRequests() { private FirefoxDriver mockJSExecutor(boolean forcesSync) { FirefoxDriver jse = Mockito.mock(FirefoxDriver.class); + mockWaitForVaadin(jse); Mockito.when(jse .executeScript(Mockito.contains("window.Vaadin.Flow.client"))) .thenReturn(Boolean.TRUE); @@ -293,4 +293,21 @@ private FirefoxDriver mockJSExecutor(boolean forcesSync) { .thenReturn(Arrays.asList(1000L, 2000L, 3000L)); return jse; } + + private void mockWaitForVaadin(RemoteWebDriver driver) { + WebDriver.Options options = Mockito.mock(WebDriver.Options.class); + WebDriver.Timeouts timeouts = Mockito.mock(WebDriver.Timeouts.class); + Mockito.when(driver.manage()).thenReturn(options); + Mockito.when(options.timeouts()).thenReturn(timeouts); + Mockito.when(timeouts.getScriptTimeout()) + .thenReturn(Duration.ofSeconds(30)); + Mockito.when(timeouts.scriptTimeout(Mockito.any(Duration.class))) + .thenReturn(timeouts); + // Phase 1: sync check for Flow.ready returns true immediately + Mockito.when(driver.executeScript( + Mockito.contains("typeof window.Vaadin.Flow.ready"))) + .thenReturn(Boolean.TRUE); + Mockito.when(driver.executeAsyncScript(Mockito.contains("Flow.ready"), + Mockito.anyLong())).thenReturn(null); + } } 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..87d43a444 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; @@ -44,11 +41,11 @@ public void waitForVaadin_activeConnector_waits() { @BrowserTest 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(); } @@ -61,41 +58,23 @@ public void waitForVaadin_noConnectors_returnsImmediately() { assertExecutionNoLonger(() -> getCommandExecutor().waitForVaadin()); } - @BrowserTest - public void waitForVaadin_noFlow_returnsImmediately() { - openTestURL(); - - getCommandExecutor().executeScript("window.Vaadin.Flow = undefined;"); - assertExecutionNoLonger(() -> getCommandExecutor().waitForVaadin()); - } - @BrowserTest public void waitForVaadin_devModeNotReady_waits() { openTestURL(); - getCommandExecutor().executeScript( - "window.Vaadin = {Flow: {devServerIsNotLoaded: true}};"); + getCommandExecutor().executeScript("window.Vaadin.Flow.ready = false;"); assertExecutionBlocked(() -> getCommandExecutor().waitForVaadin()); } @BrowserTest public void waitForVaadin_devModeNotReady_waitsUntilReady() { openTestURL(); - assertDevServerIsNotLoaded(); - getCommandExecutor().executeScript( - "window.Vaadin = {Flow: {devServerIsNotLoaded: true}};"); - setWaitForVaadinLoopHook(500, - "window.Vaadin.Flow.devServerIsNotLoaded = false;"); + getCommandExecutor() + .executeScript("window._savedReady = window.Vaadin.Flow.ready;" + + "window.Vaadin.Flow.ready = false;" + + "setTimeout(function() {" + + " window.Vaadin.Flow.ready = window._savedReady;" + + "}, 500);"); getCommandExecutor().waitForVaadin(); - assertDevServerIsNotLoaded(); - } - - private void assertDevServerIsNotLoaded() { - Object devServerIsNotLoaded = executeScript( - "return window.Vaadin.Flow.devServerIsNotLoaded;"); - Assertions.assertTrue( - devServerIsNotLoaded == null - || devServerIsNotLoaded == Boolean.FALSE, - "devServerIsNotLoaded should be null or false"); } private void assertClientIsActive() { @@ -123,28 +102,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..f066d31fb 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; @@ -44,11 +41,11 @@ public void waitForVaadin_activeConnector_waits() { @Test 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(); } @@ -61,41 +58,23 @@ public void waitForVaadin_noConnectors_returnsImmediately() { assertExecutionNoLonger(() -> getCommandExecutor().waitForVaadin()); } - @Test - public void waitForVaadin_noFlow_returnsImmediately() { - openTestURL(); - - getCommandExecutor().executeScript("window.Vaadin.Flow = undefined;"); - assertExecutionNoLonger(() -> getCommandExecutor().waitForVaadin()); - } - @Test public void waitForVaadin_devModeNotReady_waits() { openTestURL(); - - getCommandExecutor().executeScript( - "window.Vaadin = {Flow: {devServerIsNotLoaded: true}};"); + getCommandExecutor().executeScript("window.Vaadin.Flow.ready = false;"); assertExecutionBlocked(() -> getCommandExecutor().waitForVaadin()); } @Test public void waitForVaadin_devModeNotReady_waitsUntilReady() { openTestURL(); - assertDevServerIsNotLoaded(); - getCommandExecutor().executeScript( - "window.Vaadin = {Flow: {devServerIsNotLoaded: true}};"); - setWaitForVaadinLoopHook(500, - "window.Vaadin.Flow.devServerIsNotLoaded = false;"); + getCommandExecutor() + .executeScript("window._savedReady = window.Vaadin.Flow.ready;" + + "window.Vaadin.Flow.ready = false;" + + "setTimeout(function() {" + + " window.Vaadin.Flow.ready = window._savedReady;" + + "}, 500);"); getCommandExecutor().waitForVaadin(); - assertDevServerIsNotLoaded(); - } - - private void assertDevServerIsNotLoaded() { - Object devServerIsNotLoaded = executeScript( - "return window.Vaadin.Flow.devServerIsNotLoaded;"); - Assert.assertTrue("devServerIsNotLoaded should be null or false", - devServerIsNotLoaded == null - || devServerIsNotLoaded == Boolean.FALSE); } private void assertClientIsActive() { @@ -125,28 +104,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 744e4dde7..c75daeab9 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 @@ -13,12 +13,14 @@ import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; +import java.time.Duration; import java.util.List; import org.openqa.selenium.Dimension; import org.openqa.selenium.HasCapabilities; 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; @@ -48,27 +50,19 @@ private static Logger getLogger() { private boolean enableWaitForVaadin = true; private boolean autoScrollIntoView = true; // @formatter:off - String WAIT_FOR_VAADIN_SCRIPT = - "if (document.readyState != 'complete') {" - + " return false;" - + "}" - + "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;" - + " for (var client in clients) {" - + " if (clients[client].isActive()) {" - + " return false;" - + " }" - + " }" - + " return true;" - + "} else {" - + " return true;" - + "}"; + private static final String READY_CHECK_SCRIPT = + "return typeof window.Vaadin !== 'undefined'" + + " && typeof window.Vaadin.Flow !== 'undefined'" + + " && typeof window.Vaadin.Flow.ready === 'function'"; + + private static final String WAIT_FOR_VAADIN_ASYNC_SCRIPT = + "var callback = arguments[arguments.length - 1];" + + "var timeout = arguments[0];" + + "window.Vaadin.Flow.ready({ timeout: timeout })" + + " .then(function() { callback(); })" + + " .catch(function() { callback(); });"; // @formatter:on - - // A hook for testing purposes - private Runnable waitForVaadinLoopHook; + private static final long WAIT_FOR_VAADIN_TIMEOUT_MS = 40000; public TestBenchCommandExecutor(ImageComparison imageComparison, ReferenceNameGenerator referenceNameGenerator) { @@ -109,31 +103,77 @@ public String getRemoteControlName() { /** * Block until Vaadin reports it has finished processing server messages. + *

+ * First waits for the dev server to start (if needed), then makes a single + * async call to {@code Flow.ready} which handles all remaining readiness + * checks. */ public void waitForVaadin() { if (!enableWaitForVaadin) { - // wait for vaadin is disabled, just return. 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 + WebDriver wrappedDriver = getDriver().getWrappedDriver(); + long deadline = System.currentTimeMillis() + WAIT_FOR_VAADIN_TIMEOUT_MS; + + if (!waitForDevServer(wrappedDriver, deadline)) { + return; + } + + // Single async call — Flow.ready handles all readiness checks + Duration originalTimeout = wrappedDriver.manage().timeouts() + .getScriptTimeout(); + try { + long remaining = deadline - System.currentTimeMillis(); + if (remaining <= 0) { + 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; + // Allow a small grace period over the JS-side timeout so the + // promise can reject and call back before WebDriver bails + wrappedDriver.manage().timeouts() + .scriptTimeout(Duration.ofMillis(remaining + 1000)); + ((JavascriptExecutor) wrappedDriver).executeAsyncScript( + WAIT_FOR_VAADIN_ASYNC_SCRIPT, remaining); + } catch (ScriptTimeoutException e) { + // Silent timeout + } finally { + wrappedDriver.manage().timeouts().scriptTimeout(originalTimeout); + } + } + + /** + * Polls until {@code Vaadin.Flow.ready} is a function, indicating the dev + * server has started and Flow is loaded. Uses Java-side polling so it + * survives page reloads during dev server startup. + * + * @return {@code true} if ready became available, {@code false} if the + * deadline was reached + */ + private boolean waitForDevServer(WebDriver wrappedDriver, long deadline) { + while (System.currentTimeMillis() < deadline) { + try { + Boolean ready = (Boolean) ((JavascriptExecutor) wrappedDriver) + .executeScript(READY_CHECK_SCRIPT); + if (Boolean.TRUE.equals(ready)) { + return true; + } + } catch (Exception e) { + // Page may be reloading, continue polling + } + long remaining = deadline - System.currentTimeMillis(); + if (remaining <= 0) { + return false; + } + try { + Thread.sleep(Math.min(500, remaining)); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return false; } } + return false; } @Override From e389e120d7aa0c7547500319aa715c9fe86f5f08 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Fri, 1 May 2026 19:19:03 +0000 Subject: [PATCH 2/7] Test using Flow when-ready snapshot for Vaadin.Flow.ready API --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index d21681006..1d8b4bc46 100644 --- a/pom.xml +++ b/pom.xml @@ -39,7 +39,7 @@ ${maven.compiler.release} 4.0.4 25.2-SNAPSHOT - 25.2-SNAPSHOT + 25.2.when-ready-SNAPSHOT 2.0.7 12.1.3 3.5.2 From 842027f409f806e503536139fa79db3359e8a80a Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Fri, 18 Sep 2026 06:03:50 +0000 Subject: [PATCH 3/7] fix: probe readiness synchronously before awaiting Flow.ready MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Delegating the whole readiness decision to `window.Vaadin.Flow.ready()` made `waitForVaadin()` depend on that function existing, so a page with no Flow — a plain login or error page, `about:blank`, or any Flow release that predates `ready()` — burned the full 40s budget and then returned as if Vaadin were idle. `TestBenchDriverTest.testDisableWaitForVaadin` went from 0.001s to 40.006s, and `WaitForVaadinIT` `.waitForVaadin_noConnectors_returnsImmediately` failed outright, because an unset `Vaadin.Flow.clients` is not "ready" for `Flow.ready()` either. Restore the synchronous probe as the fast path and use `Flow.ready()` only for the part it is actually good at: waiting. The probe answers, in one round trip, whether Vaadin is idle, and when it is not, whether `Flow.ready()` is available to await. Only then is the async call made, and its result is fed back into the loop. This keeps the win the async call was added for — a 500ms wait is 2 WebDriver calls instead of ~50 polls — without paying for it on the hot path, which is back to the single round trip it was before. Measured against a local chromedriver, an already-idle `waitForVaadin()` goes from 5 round trips (~17ms) back to 1 (~5ms), and it runs before nearly every element command, so that difference is multiplied by the latency to the grid. Also fixed along the way: - `manage().timeouts().getScriptTimeout()` is a default method on `WebDriver.Timeouts` that throws `UnsupportedCommandException` unless the driver implements it, and it was called outside the try/finally, so it took down every command for drivers that do not. That was the `testTestBenchDriverActsAsProxy` error. The session script timeout is no longer read or mutated at all; the browser-side wait is chunked to 10s, which stays below the 30s default. - A page load while the async script is pending aborts that script, and the resulting `ScriptTimeoutException` was swallowed as if Vaadin were idle. Verified against chromedriver: a reload 300ms into a pending call returns `script timeout` right away. The loop now re-probes, so a reload costs one chunk instead of silently skipping the wait. - A rejected `ready()` promise was indistinguishable from a resolved one. The callback reports the rejection, so the state is probed again and the reason is logged. - `clients[client].isActive()` was called unconditionally, which threw for a registered client that has no `isActive`. The integration tests drive the dev-server case through `devServerIsNotLoaded` again, which is what the dev server sets and what the test names describe, and `waitForVaadin_noFlow_returnsImmediately` is restored as the regression guard for the stall above. The `waitForVaadinLoopHook` reflection hook stays removed; `setTimeout` in the page does the same job. The JUnit 5 copy asserted "blocked" with a 40000ms threshold, exactly equal to the whole `waitForVaadin` budget, so it could only ever pass by a hair — it now uses 20000ms like the JUnit 4 copy. The unit test changes that stubbed `getScriptTimeout()` and the `Flow.ready` probe are reverted, since the pre-existing mocks match the restored script again. Co-authored-by: Claude Opus 5 (1M context) --- .../TestBenchCommandExecutorTest.java | 23 +-- .../TestBenchCommandExecutorTest.java | 23 +-- .../com/vaadin/tests/WaitForVaadinIT.java | 31 ++- .../com/vaadin/tests/WaitForVaadinIT.java | 28 ++- .../commands/TestBenchCommandExecutor.java | 193 ++++++++++++------ 5 files changed, 188 insertions(+), 110 deletions(-) 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 7389664b3..188865b82 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 @@ -11,7 +11,6 @@ import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; -import java.time.Duration; import java.util.Arrays; import org.junit.jupiter.api.Assertions; @@ -214,7 +213,9 @@ private WebDriver mockScreenshotDriver(int nrScreenshotsGrabbed, "cursor-bottom-edge-off.png"); Mockito.when(driver.getScreenshotAs(OutputType.BYTES)) .thenReturn(screenshotBytes); - mockWaitForVaadin(driver); + Mockito.when( + driver.executeScript(Mockito.contains("window.Vaadin.Flow"))) + .thenReturn(Boolean.TRUE); if (expectGetCapabilities) { Capabilities mockedCapabilities = Mockito.mock(Capabilities.class); Mockito.when(mockedCapabilities.getBrowserName()) @@ -282,7 +283,6 @@ public void testTotalTimeSpentServicingRequests() { private FirefoxDriver mockJSExecutor(boolean forcesSync) { FirefoxDriver jse = Mockito.mock(FirefoxDriver.class); - mockWaitForVaadin(jse); Mockito.when(jse .executeScript(Mockito.contains("window.Vaadin.Flow.client"))) .thenReturn(Boolean.TRUE); @@ -290,21 +290,4 @@ private FirefoxDriver mockJSExecutor(boolean forcesSync) { .thenReturn(Arrays.asList(1000L, 2000L, 3000L)); return jse; } - - private void mockWaitForVaadin(RemoteWebDriver driver) { - WebDriver.Options options = Mockito.mock(WebDriver.Options.class); - WebDriver.Timeouts timeouts = Mockito.mock(WebDriver.Timeouts.class); - Mockito.when(driver.manage()).thenReturn(options); - Mockito.when(options.timeouts()).thenReturn(timeouts); - Mockito.when(timeouts.getScriptTimeout()) - .thenReturn(Duration.ofSeconds(30)); - Mockito.when(timeouts.scriptTimeout(Mockito.any(Duration.class))) - .thenReturn(timeouts); - // Phase 1: sync check for Flow.ready returns true immediately - Mockito.when(driver.executeScript( - Mockito.contains("typeof window.Vaadin.Flow.ready"))) - .thenReturn(Boolean.TRUE); - Mockito.when(driver.executeAsyncScript(Mockito.contains("Flow.ready"), - Mockito.anyLong())).thenReturn(null); - } } 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 d415f6182..68ea1514b 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 @@ -11,7 +11,6 @@ import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; -import java.time.Duration; import java.util.Arrays; import org.junit.Before; @@ -217,7 +216,9 @@ private WebDriver mockScreenshotDriver(int nrScreenshotsGrabbed, "cursor-bottom-edge-off.png"); Mockito.when(driver.getScreenshotAs(OutputType.BYTES)) .thenReturn(screenshotBytes); - mockWaitForVaadin(driver); + Mockito.when( + driver.executeScript(Mockito.contains("window.Vaadin.Flow"))) + .thenReturn(Boolean.TRUE); if (expectGetCapabilities) { Capabilities mockedCapabilities = Mockito.mock(Capabilities.class); Mockito.when(mockedCapabilities.getBrowserName()) @@ -285,7 +286,6 @@ public void testTotalTimeSpentServicingRequests() { private FirefoxDriver mockJSExecutor(boolean forcesSync) { FirefoxDriver jse = Mockito.mock(FirefoxDriver.class); - mockWaitForVaadin(jse); Mockito.when(jse .executeScript(Mockito.contains("window.Vaadin.Flow.client"))) .thenReturn(Boolean.TRUE); @@ -293,21 +293,4 @@ private FirefoxDriver mockJSExecutor(boolean forcesSync) { .thenReturn(Arrays.asList(1000L, 2000L, 3000L)); return jse; } - - private void mockWaitForVaadin(RemoteWebDriver driver) { - WebDriver.Options options = Mockito.mock(WebDriver.Options.class); - WebDriver.Timeouts timeouts = Mockito.mock(WebDriver.Timeouts.class); - Mockito.when(driver.manage()).thenReturn(options); - Mockito.when(options.timeouts()).thenReturn(timeouts); - Mockito.when(timeouts.getScriptTimeout()) - .thenReturn(Duration.ofSeconds(30)); - Mockito.when(timeouts.scriptTimeout(Mockito.any(Duration.class))) - .thenReturn(timeouts); - // Phase 1: sync check for Flow.ready returns true immediately - Mockito.when(driver.executeScript( - Mockito.contains("typeof window.Vaadin.Flow.ready"))) - .thenReturn(Boolean.TRUE); - Mockito.when(driver.executeAsyncScript(Mockito.contains("Flow.ready"), - Mockito.anyLong())).thenReturn(null); - } } 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 87d43a444..bd9c1dea8 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 @@ -17,7 +17,7 @@ public class WaitForVaadinIT extends AbstractBrowserTB9Test { private static final long NON_BLOCKING_EXECUTION_TIMEOUT = 10000; - private static final long BLOCKING_EXECUTION_TIMEOUT = 40000; + private static final long BLOCKING_EXECUTION_TIMEOUT = 20000; @Override protected Class getTestView() { @@ -58,23 +58,42 @@ public void waitForVaadin_noConnectors_returnsImmediately() { assertExecutionNoLonger(() -> getCommandExecutor().waitForVaadin()); } + @BrowserTest + public void waitForVaadin_noFlow_returnsImmediately() { + openTestURL(); + + getCommandExecutor().executeScript("window.Vaadin.Flow = undefined;"); + assertExecutionNoLonger(() -> getCommandExecutor().waitForVaadin()); + } + @BrowserTest public void waitForVaadin_devModeNotReady_waits() { openTestURL(); - getCommandExecutor().executeScript("window.Vaadin.Flow.ready = false;"); + getCommandExecutor().executeScript( + "window.Vaadin = {Flow: {devServerIsNotLoaded: true}};"); assertExecutionBlocked(() -> getCommandExecutor().waitForVaadin()); } @BrowserTest public void waitForVaadin_devModeNotReady_waitsUntilReady() { openTestURL(); - getCommandExecutor() - .executeScript("window._savedReady = window.Vaadin.Flow.ready;" - + "window.Vaadin.Flow.ready = false;" + assertDevServerIsNotLoaded(); + getCommandExecutor().executeScript( + "window.Vaadin = {Flow: {devServerIsNotLoaded: true}};" + "setTimeout(function() {" - + " window.Vaadin.Flow.ready = window._savedReady;" + + " window.Vaadin.Flow.devServerIsNotLoaded = false;" + "}, 500);"); getCommandExecutor().waitForVaadin(); + assertDevServerIsNotLoaded(); + } + + private void assertDevServerIsNotLoaded() { + Object devServerIsNotLoaded = executeScript( + "return window.Vaadin.Flow.devServerIsNotLoaded;"); + Assertions.assertTrue( + devServerIsNotLoaded == null + || devServerIsNotLoaded == Boolean.FALSE, + "devServerIsNotLoaded should be null or false"); } private void assertClientIsActive() { 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 f066d31fb..955688a59 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 @@ -58,23 +58,41 @@ public void waitForVaadin_noConnectors_returnsImmediately() { assertExecutionNoLonger(() -> getCommandExecutor().waitForVaadin()); } + @Test + public void waitForVaadin_noFlow_returnsImmediately() { + openTestURL(); + + getCommandExecutor().executeScript("window.Vaadin.Flow = undefined;"); + assertExecutionNoLonger(() -> getCommandExecutor().waitForVaadin()); + } + @Test public void waitForVaadin_devModeNotReady_waits() { openTestURL(); - getCommandExecutor().executeScript("window.Vaadin.Flow.ready = false;"); + getCommandExecutor().executeScript( + "window.Vaadin = {Flow: {devServerIsNotLoaded: true}};"); assertExecutionBlocked(() -> getCommandExecutor().waitForVaadin()); } @Test public void waitForVaadin_devModeNotReady_waitsUntilReady() { openTestURL(); - getCommandExecutor() - .executeScript("window._savedReady = window.Vaadin.Flow.ready;" - + "window.Vaadin.Flow.ready = false;" + assertDevServerIsNotLoaded(); + getCommandExecutor().executeScript( + "window.Vaadin = {Flow: {devServerIsNotLoaded: true}};" + "setTimeout(function() {" - + " window.Vaadin.Flow.ready = window._savedReady;" + + " window.Vaadin.Flow.devServerIsNotLoaded = false;" + "}, 500);"); getCommandExecutor().waitForVaadin(); + assertDevServerIsNotLoaded(); + } + + private void assertDevServerIsNotLoaded() { + Object devServerIsNotLoaded = executeScript( + "return window.Vaadin.Flow.devServerIsNotLoaded;"); + Assert.assertTrue("devServerIsNotLoaded should be null or false", + devServerIsNotLoaded == null + || devServerIsNotLoaded == Boolean.FALSE); } private void assertClientIsActive() { 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 c75daeab9..dc7bcbf3e 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 @@ -13,11 +13,11 @@ import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; -import java.time.Duration; import java.util.List; 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; @@ -49,21 +49,67 @@ private static Logger getLogger() { private boolean enableWaitForVaadin = true; private boolean autoScrollIntoView = true; - // @formatter:off - private static final String READY_CHECK_SCRIPT = - "return typeof window.Vaadin !== 'undefined'" - + " && typeof window.Vaadin.Flow !== 'undefined'" - + " && typeof window.Vaadin.Flow.ready === 'function'"; - private static final String WAIT_FOR_VAADIN_ASYNC_SCRIPT = + /** + * Returned by {@link #WAIT_FOR_VAADIN_SCRIPT} when Vaadin is not idle but + * the page exposes {@code window.Vaadin.Flow.ready()}, so that the waiting + * itself can be delegated to the browser. + */ + private static final String AWAIT_FLOW_READY = "await-flow-ready"; + + /** + * Synchronous probe for the current readiness state. Returns {@code true} + * when Vaadin is idle, {@link #AWAIT_FLOW_READY} when Flow can be asked to + * report readiness asynchronously, and {@code false} when the state has to + * be probed again later. + *

+ * The {@code document.readyState} and {@code devServerIsNotLoaded} checks + * are kept here on purpose. This probe also runs on pages that have no Flow + * at all, and while the dev server is still starting there is no + * {@code window.Vaadin.Flow.ready()} to defer to yet. + */ + // @formatter:off + private static final String WAIT_FOR_VAADIN_SCRIPT = + "if (document.readyState != 'complete') {" + + " return false;" + + "}" + + "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;" + + " for (var client in clients) {" + + " if (clients[client].isActive && clients[client].isActive()) {" + + " return typeof window.Vaadin.Flow.ready === 'function'" + + " ? '" + AWAIT_FLOW_READY + "' : false;" + + " }" + + " }" + + " return true;" + + "} else {" + + " return true;" + + "}"; + + private static final String AWAIT_FLOW_READY_SCRIPT = "var callback = arguments[arguments.length - 1];" - + "var timeout = arguments[0];" - + "window.Vaadin.Flow.ready({ timeout: timeout })" - + " .then(function() { callback(); })" - + " .catch(function() { callback(); });"; + + "window.Vaadin.Flow.ready({ timeout: arguments[0] })" + + " .then(function() { callback(null); })" + + " .catch(function(error) {" + + " callback(error && error.message ? error.message : 'rejected');" + + " });"; // @formatter:on + private static final long WAIT_FOR_VAADIN_TIMEOUT_MS = 40000; + /** + * How long a single browser-side wait may last. Kept below the WebDriver + * default script timeout of 30s so the promise can settle and invoke the + * callback before the driver aborts the script, and short enough that a + * page load cancelling the pending script only costs one chunk before the + * state is probed again. + */ + private static final long AWAIT_FLOW_READY_CHUNK_MS = 10000; + + private static final long POLL_INTERVAL_MS = 100; + public TestBenchCommandExecutor(ImageComparison imageComparison, ReferenceNameGenerator referenceNameGenerator) { this.imageComparison = imageComparison; @@ -104,76 +150,105 @@ public String getRemoteControlName() { /** * Block until Vaadin reports it has finished processing server messages. *

- * First waits for the dev server to start (if needed), then makes a single - * async call to {@code Flow.ready} which handles all remaining readiness - * checks. + * A single synchronous probe decides whether there is anything to wait for. + * When there is, and the page exposes {@code window.Vaadin.Flow.ready()}, + * the waiting is delegated to the browser in one blocking call instead of + * being polled from the test JVM. */ public void waitForVaadin() { if (!enableWaitForVaadin) { + // wait for vaadin is disabled, just return. return; } // Must use the wrapped driver here to avoid calling waitForVaadin // again - WebDriver wrappedDriver = getDriver().getWrappedDriver(); + JavascriptExecutor executor = (JavascriptExecutor) getDriver() + .getWrappedDriver(); long deadline = System.currentTimeMillis() + WAIT_FOR_VAADIN_TIMEOUT_MS; - if (!waitForDevServer(wrappedDriver, deadline)) { - return; + while (System.currentTimeMillis() < deadline) { + Object state = executor.executeScript(WAIT_FOR_VAADIN_SCRIPT); + if (Boolean.TRUE.equals(state)) { + return; + } + if (AWAIT_FLOW_READY.equals(state) + && awaitFlowReady(executor, deadline)) { + return; + } + if (state == 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"); + } + if (!pauseBeforeNextProbe(deadline)) { + return; + } } + getLogger().debug("Vaadin was still not idle after {}ms, continuing", + WAIT_FOR_VAADIN_TIMEOUT_MS); + } - // Single async call — Flow.ready handles all readiness checks - Duration originalTimeout = wrappedDriver.manage().timeouts() - .getScriptTimeout(); + /** + * Lets Flow report readiness by awaiting {@code window.Vaadin.Flow.ready()} + * in the browser, so that waiting costs a single WebDriver call instead of + * one call per poll. + * + * @param executor + * the executor to run the script with + * @param deadline + * the time after which waiting must stop + * @return {@code true} if Flow reported it is idle, {@code false} if the + * state has to be probed again + */ + private boolean awaitFlowReady(JavascriptExecutor executor, long deadline) { + long chunk = Math.min(AWAIT_FLOW_READY_CHUNK_MS, + deadline - System.currentTimeMillis()); + if (chunk <= 0) { + return false; + } try { - long remaining = deadline - System.currentTimeMillis(); - if (remaining <= 0) { - return; + Object rejection = executor + .executeAsyncScript(AWAIT_FLOW_READY_SCRIPT, chunk); + if (rejection == null) { + return true; } - // Allow a small grace period over the JS-side timeout so the - // promise can reject and call back before WebDriver bails - wrappedDriver.manage().timeouts() - .scriptTimeout(Duration.ofMillis(remaining + 1000)); - ((JavascriptExecutor) wrappedDriver).executeAsyncScript( - WAIT_FOR_VAADIN_ASYNC_SCRIPT, remaining); + getLogger().debug("window.Vaadin.Flow.ready() was rejected: {}", + rejection); } catch (ScriptTimeoutException e) { - // Silent timeout - } finally { - wrappedDriver.manage().timeouts().scriptTimeout(originalTimeout); + // The pending script was aborted, e.g. by a page load or by a + // session script timeout shorter than the chunk + getLogger().debug( + "Awaiting window.Vaadin.Flow.ready() was interrupted", e); + } catch (JavascriptException e) { + // e.g. navigated to a page without Flow while the call was being + // set up + getLogger().debug("Could not call window.Vaadin.Flow.ready()", e); } + return false; } /** - * Polls until {@code Vaadin.Flow.ready} is a function, indicating the dev - * server has started and Flow is loaded. Uses Java-side polling so it - * survives page reloads during dev server startup. + * Waits for the poll interval without overrunning the deadline. * - * @return {@code true} if ready became available, {@code false} if the - * deadline was reached + * @param deadline + * the time after which waiting must stop + * @return {@code false} if waiting should stop */ - private boolean waitForDevServer(WebDriver wrappedDriver, long deadline) { - while (System.currentTimeMillis() < deadline) { - try { - Boolean ready = (Boolean) ((JavascriptExecutor) wrappedDriver) - .executeScript(READY_CHECK_SCRIPT); - if (Boolean.TRUE.equals(ready)) { - return true; - } - } catch (Exception e) { - // Page may be reloading, continue polling - } - long remaining = deadline - System.currentTimeMillis(); - if (remaining <= 0) { - return false; - } - try { - Thread.sleep(Math.min(500, remaining)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return false; - } + private boolean pauseBeforeNextProbe(long deadline) { + long remaining = Math.min(POLL_INTERVAL_MS, + deadline - System.currentTimeMillis()); + if (remaining <= 0) { + return false; + } + try { + Thread.sleep(remaining); + return true; + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return false; } - return false; } @Override From bfb8f75a86935d86aa0838d4b570511ea2a01495 Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Fri, 18 Sep 2026 06:14:55 +0000 Subject: [PATCH 4/7] refactor: keep the readiness probe authoritative and cover the async path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three follow-ups to the previous commit. `waitForVaadin()` returned as soon as `Flow.ready()` resolved, which quietly moved the definition of "idle" from the probe to Flow. The two are equivalent today — `ready()` reads the same `Vaadin.Flow.clients` map and the same `isActive()` — but nothing enforced that, so a future divergence would have changed what `waitForVaadin()` waits for without touching TestBench. A resolved promise now falls back into the loop and re-probes, so the probe is the single authority and `ready()` is purely an efficient sleep. Costs one extra round trip per completed wait (~5ms), only on calls that actually had to wait. `awaitFlowReady` deliberately tolerates a page load cancelling the pending script, but the synchronous probe on the very next iteration did not, so the same reload could propagate out of `waitForVaadin()` and abort the test instead. The probe now handles `JavascriptException` the same way. It deliberately does not catch `WebDriverException`: a dead session has to surface rather than be retried until the deadline. Neither the sentinel dispatch nor the failure handling was covered by a test, because the mocked drivers only ever report "idle". Added three to `TestBenchCommandExecutorTest` (both copies), each verified to fail when the behaviour it pins is removed: - `waitForVaadin_flowReadyAvailable_delegatesWaitToBrowser` — the sentinel dispatches to `executeAsyncScript`, and a resolved promise is confirmed by a second probe rather than trusted outright. - `waitForVaadin_awaitingFlowReadyDoesNotSucceed_keepsProbing` — a rejected promise, a `ScriptTimeoutException` and a `JavascriptException` are each retried, and none of them reach the caller. - `waitForVaadin_probeFailsWhilePageLoads_keepsProbing` — a probe that fails mid-navigation is retried. Re-checked end to end against chromedriver with Flow's `ready()` inlined in the page: idle page 1 round trip, a client that goes idle after 500ms completes in 530ms, a client that never goes idle blocks for the whole budget, and a reload mid-wait recovers in ~420ms. That last pair is what `waitForVaadin_activeConnector_waits` and `waitForVaadin_activeConnector_waitsUtilReady` rely on. Co-authored-by: Claude Opus 5 (1M context) --- cd.log | 4 + .../TestBenchCommandExecutorTest.java | 84 +++++++++++++++++++ .../TestBenchCommandExecutorTest.java | 84 +++++++++++++++++++ .../commands/TestBenchCommandExecutor.java | 29 ++++++- 4 files changed, 199 insertions(+), 2 deletions(-) create mode 100644 cd.log diff --git a/cd.log b/cd.log new file mode 100644 index 000000000..ef3acf566 --- /dev/null +++ b/cd.log @@ -0,0 +1,4 @@ +Starting ChromeDriver 152.0.7977.82 (d04cdb24d67b081f6cf80200ffc5233f44b61109-refs/branch-heads/7977@{#2322}) on port 9515 +Only local connections are allowed. +Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe. +ChromeDriver was started successfully on port 9515. 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 188865b82..577331433 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; @@ -36,6 +38,13 @@ public class TestBenchCommandExecutorTest { private static final String IMG_FOLDER = ImageComparisonTest.class .getPackage().getName().replace('.', '/'); + /** + * The value the readiness probe returns when Vaadin is not idle but the + * page exposes {@code window.Vaadin.Flow.ready()}. Mirrors the sentinel in + * {@link TestBenchCommandExecutor}. + */ + private static final String AWAIT_FLOW_READY = "await-flow-ready"; + @BeforeEach public void setUp() { } @@ -281,6 +290,81 @@ public void testTotalTimeSpentServicingRequests() { } + @Test + public void waitForVaadin_flowReadyAvailable_delegatesWaitToBrowser() { + FirefoxDriver driver = mockWaitForVaadinDriver(AWAIT_FLOW_READY, + Boolean.TRUE); + Mockito.when(driver.executeAsyncScript( + Mockito.contains("window.Vaadin.Flow.ready"), + Mockito.anyLong())).thenReturn(null); + + newCommandExecutor(driver).waitForVaadin(); + + Mockito.verify(driver).executeAsyncScript( + Mockito.contains("window.Vaadin.Flow.ready"), + Mockito.anyLong()); + // Flow resolving is not enough on its own: the synchronous probe has + // the final say on whether Vaadin is idle + Mockito.verify(driver, Mockito.times(2)) + .executeScript(Mockito.contains("window.Vaadin.Flow.clients")); + } + + @Test + public void waitForVaadin_awaitingFlowReadyDoesNotSucceed_keepsProbing() { + FirefoxDriver driver = mockWaitForVaadinDriver(AWAIT_FLOW_READY, + AWAIT_FLOW_READY, AWAIT_FLOW_READY, Boolean.TRUE); + // A rejected promise, a page load cancelling the pending script and + // navigating away from Flow must all be retried, and none of them may + // escape to the caller + Mockito.when(driver.executeAsyncScript(Mockito.anyString(), + Mockito.anyLong())) + .thenReturn("Vaadin.Flow.ready timed out after 10000ms") + .thenThrow(new ScriptTimeoutException("script timeout"), + new JavascriptException( + "window.Vaadin.Flow is undefined")); + + newCommandExecutor(driver).waitForVaadin(); + + Mockito.verify(driver, Mockito.times(3)) + .executeAsyncScript(Mockito.anyString(), Mockito.anyLong()); + Mockito.verify(driver, Mockito.times(4)) + .executeScript(Mockito.contains("window.Vaadin.Flow.clients")); + } + + @Test + public void waitForVaadin_probeFailsWhilePageLoads_keepsProbing() { + FirefoxDriver driver = Mockito.mock(FirefoxDriver.class); + Mockito.when(driver + .executeScript(Mockito.contains("window.Vaadin.Flow.clients"))) + .thenThrow(new JavascriptException("document is not defined")) + .thenReturn(Boolean.TRUE); + + newCommandExecutor(driver).waitForVaadin(); + + Mockito.verify(driver, Mockito.times(2)) + .executeScript(Mockito.contains("window.Vaadin.Flow.clients")); + } + + private TestBenchCommandExecutor newCommandExecutor(FirefoxDriver driver) { + TestBenchCommandExecutor tbce = new TestBenchCommandExecutor(null, + null); + tbce.setDriver(TestBench.createDriver(driver, tbce)); + return tbce; + } + + /** + * Mocks a driver whose readiness probe returns the given states in order, + * repeating the last one once they run out. + */ + private FirefoxDriver mockWaitForVaadinDriver(Object... probeStates) { + FirefoxDriver driver = Mockito.mock(FirefoxDriver.class); + Mockito.when(driver + .executeScript(Mockito.contains("window.Vaadin.Flow.clients"))) + .thenReturn(probeStates[0], + Arrays.copyOfRange(probeStates, 1, probeStates.length)); + return driver; + } + private FirefoxDriver mockJSExecutor(boolean forcesSync) { FirefoxDriver jse = Mockito.mock(FirefoxDriver.class); Mockito.when(jse 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..9bd31c7a9 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; @@ -39,6 +41,13 @@ public class TestBenchCommandExecutorTest { private static final String IMG_FOLDER = ImageComparisonTest.class .getPackage().getName().replace('.', '/'); + /** + * The value the readiness probe returns when Vaadin is not idle but the + * page exposes {@code window.Vaadin.Flow.ready()}. Mirrors the sentinel in + * {@link TestBenchCommandExecutor}. + */ + private static final String AWAIT_FLOW_READY = "await-flow-ready"; + @Before public void setUp() { } @@ -284,6 +293,81 @@ public void testTotalTimeSpentServicingRequests() { } + @Test + public void waitForVaadin_flowReadyAvailable_delegatesWaitToBrowser() { + FirefoxDriver driver = mockWaitForVaadinDriver(AWAIT_FLOW_READY, + Boolean.TRUE); + Mockito.when(driver.executeAsyncScript( + Mockito.contains("window.Vaadin.Flow.ready"), + Mockito.anyLong())).thenReturn(null); + + newCommandExecutor(driver).waitForVaadin(); + + Mockito.verify(driver).executeAsyncScript( + Mockito.contains("window.Vaadin.Flow.ready"), + Mockito.anyLong()); + // Flow resolving is not enough on its own: the synchronous probe has + // the final say on whether Vaadin is idle + Mockito.verify(driver, Mockito.times(2)) + .executeScript(Mockito.contains("window.Vaadin.Flow.clients")); + } + + @Test + public void waitForVaadin_awaitingFlowReadyDoesNotSucceed_keepsProbing() { + FirefoxDriver driver = mockWaitForVaadinDriver(AWAIT_FLOW_READY, + AWAIT_FLOW_READY, AWAIT_FLOW_READY, Boolean.TRUE); + // A rejected promise, a page load cancelling the pending script and + // navigating away from Flow must all be retried, and none of them may + // escape to the caller + Mockito.when(driver.executeAsyncScript(Mockito.anyString(), + Mockito.anyLong())) + .thenReturn("Vaadin.Flow.ready timed out after 10000ms") + .thenThrow(new ScriptTimeoutException("script timeout"), + new JavascriptException( + "window.Vaadin.Flow is undefined")); + + newCommandExecutor(driver).waitForVaadin(); + + Mockito.verify(driver, Mockito.times(3)) + .executeAsyncScript(Mockito.anyString(), Mockito.anyLong()); + Mockito.verify(driver, Mockito.times(4)) + .executeScript(Mockito.contains("window.Vaadin.Flow.clients")); + } + + @Test + public void waitForVaadin_probeFailsWhilePageLoads_keepsProbing() { + FirefoxDriver driver = Mockito.mock(FirefoxDriver.class); + Mockito.when(driver + .executeScript(Mockito.contains("window.Vaadin.Flow.clients"))) + .thenThrow(new JavascriptException("document is not defined")) + .thenReturn(Boolean.TRUE); + + newCommandExecutor(driver).waitForVaadin(); + + Mockito.verify(driver, Mockito.times(2)) + .executeScript(Mockito.contains("window.Vaadin.Flow.clients")); + } + + private TestBenchCommandExecutor newCommandExecutor(FirefoxDriver driver) { + TestBenchCommandExecutor tbce = new TestBenchCommandExecutor(null, + null); + tbce.setDriver(TestBench.createDriver(driver, tbce)); + return tbce; + } + + /** + * Mocks a driver whose readiness probe returns the given states in order, + * repeating the last one once they run out. + */ + private FirefoxDriver mockWaitForVaadinDriver(Object... probeStates) { + FirefoxDriver driver = Mockito.mock(FirefoxDriver.class); + Mockito.when(driver + .executeScript(Mockito.contains("window.Vaadin.Flow.clients"))) + .thenReturn(probeStates[0], + Arrays.copyOfRange(probeStates, 1, probeStates.length)); + return driver; + } + private FirefoxDriver mockJSExecutor(boolean forcesSync) { FirefoxDriver jse = Mockito.mock(FirefoxDriver.class); Mockito.when(jse 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 dc7bcbf3e..9c8f06974 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 @@ -168,13 +168,16 @@ public void waitForVaadin() { long deadline = System.currentTimeMillis() + WAIT_FOR_VAADIN_TIMEOUT_MS; while (System.currentTimeMillis() < deadline) { - Object state = executor.executeScript(WAIT_FOR_VAADIN_SCRIPT); + Object state = probe(executor); if (Boolean.TRUE.equals(state)) { return; } if (AWAIT_FLOW_READY.equals(state) && awaitFlowReady(executor, deadline)) { - return; + // Flow reported it is idle. Probe again instead of returning + // here, so that the probe stays the single authority on what + // counts as idle and Flow.ready() is only an efficient sleep. + continue; } if (state == null) { // This should never happen but according to @@ -190,6 +193,28 @@ && awaitFlowReady(executor, deadline)) { WAIT_FOR_VAADIN_TIMEOUT_MS); } + /** + * Runs {@link #WAIT_FOR_VAADIN_SCRIPT} to determine the current readiness + * state. + * + * @param executor + * the executor to run the script with + * @return the state reported by the script, or {@code false} if the script + * could not be run because a page load was in progress + */ + private Object probe(JavascriptExecutor executor) { + try { + return executor.executeScript(WAIT_FOR_VAADIN_SCRIPT); + } catch (JavascriptException e) { + // The page is most likely being replaced, which is expected while + // the dev server starts. Note that this deliberately does not + // catch WebDriverException: a broken session must still surface + // instead of being retried until the deadline. + getLogger().debug("Could not determine whether Vaadin is idle", e); + return false; + } + } + /** * Lets Flow report readiness by awaiting {@code window.Vaadin.Flow.ready()} * in the browser, so that waiting costs a single WebDriver call instead of From 7de4c6be374c86a3c68c349d664cfd414122302f Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Wed, 14 Oct 2026 16:58:43 +0000 Subject: [PATCH 5/7] refactor: let Flow's ready() own the waitForVaadin readiness logic waitForVaadin now makes a single async script call per attempt. When the page provides window.Vaadin.Flow.ready(), the script waits for the returned promise, so Flow alone decides what idle means and the wait costs one WebDriver call. Otherwise, e.g. with older Flow versions or while the dev server is starting, the script performs the existing readiness check and returns immediately, so it is polled as before. This replaces the synchronous probe, the "await-flow-ready" sentinel, the follow-up probe after ready() resolved and the poll sleep. Co-Authored-By: Claude Opus 5.5 --- .../vaadin/testbench/TestBenchDriverTest.java | 7 +- .../TestBenchCommandExecutorTest.java | 81 ++----- .../vaadin/testbench/TestBenchDriverTest.java | 7 +- .../TestBenchCommandExecutorTest.java | 81 ++----- .../commands/TestBenchCommandExecutor.java | 197 +++++------------- 5 files changed, 94 insertions(+), 279 deletions(-) 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 577331433..9643b20fb 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 @@ -38,13 +38,6 @@ public class TestBenchCommandExecutorTest { private static final String IMG_FOLDER = ImageComparisonTest.class .getPackage().getName().replace('.', '/'); - /** - * The value the readiness probe returns when Vaadin is not idle but the - * page exposes {@code window.Vaadin.Flow.ready()}. Mirrors the sentinel in - * {@link TestBenchCommandExecutor}. - */ - private static final String AWAIT_FLOW_READY = "await-flow-ready"; - @BeforeEach public void setUp() { } @@ -222,8 +215,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); @@ -291,58 +284,31 @@ public void testTotalTimeSpentServicingRequests() { } @Test - public void waitForVaadin_flowReadyAvailable_delegatesWaitToBrowser() { - FirefoxDriver driver = mockWaitForVaadinDriver(AWAIT_FLOW_READY, - Boolean.TRUE); - Mockito.when(driver.executeAsyncScript( - Mockito.contains("window.Vaadin.Flow.ready"), - Mockito.anyLong())).thenReturn(null); + 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("window.Vaadin.Flow.ready"), - Mockito.anyLong()); - // Flow resolving is not enough on its own: the synchronous probe has - // the final say on whether Vaadin is idle - Mockito.verify(driver, Mockito.times(2)) - .executeScript(Mockito.contains("window.Vaadin.Flow.clients")); - } - - @Test - public void waitForVaadin_awaitingFlowReadyDoesNotSucceed_keepsProbing() { - FirefoxDriver driver = mockWaitForVaadinDriver(AWAIT_FLOW_READY, - AWAIT_FLOW_READY, AWAIT_FLOW_READY, Boolean.TRUE); - // A rejected promise, a page load cancelling the pending script and - // navigating away from Flow must all be retried, and none of them may - // escape to the caller - Mockito.when(driver.executeAsyncScript(Mockito.anyString(), - Mockito.anyLong())) - .thenReturn("Vaadin.Flow.ready timed out after 10000ms") - .thenThrow(new ScriptTimeoutException("script timeout"), - new JavascriptException( - "window.Vaadin.Flow is undefined")); - - newCommandExecutor(driver).waitForVaadin(); - - Mockito.verify(driver, Mockito.times(3)) - .executeAsyncScript(Mockito.anyString(), Mockito.anyLong()); - Mockito.verify(driver, Mockito.times(4)) - .executeScript(Mockito.contains("window.Vaadin.Flow.clients")); + Mockito.contains("flow.ready("), Mockito.eq(10000L)); } @Test - public void waitForVaadin_probeFailsWhilePageLoads_keepsProbing() { + public void waitForVaadin_notIdleOrInterrupted_asksAgain() { FirefoxDriver driver = Mockito.mock(FirefoxDriver.class); - Mockito.when(driver - .executeScript(Mockito.contains("window.Vaadin.Flow.clients"))) - .thenThrow(new JavascriptException("document is not defined")) + Mockito.when( + driver.executeAsyncScript(Mockito.anyString(), Mockito.any())) + .thenReturn(Boolean.FALSE) + .thenThrow(new ScriptTimeoutException("script timeout"), + new JavascriptException("document unloaded")) .thenReturn(Boolean.TRUE); newCommandExecutor(driver).waitForVaadin(); - Mockito.verify(driver, Mockito.times(2)) - .executeScript(Mockito.contains("window.Vaadin.Flow.clients")); + Mockito.verify(driver, Mockito.times(4)) + .executeAsyncScript(Mockito.anyString(), Mockito.any()); } private TestBenchCommandExecutor newCommandExecutor(FirefoxDriver driver) { @@ -352,23 +318,10 @@ private TestBenchCommandExecutor newCommandExecutor(FirefoxDriver driver) { return tbce; } - /** - * Mocks a driver whose readiness probe returns the given states in order, - * repeating the last one once they run out. - */ - private FirefoxDriver mockWaitForVaadinDriver(Object... probeStates) { - FirefoxDriver driver = Mockito.mock(FirefoxDriver.class); - Mockito.when(driver - .executeScript(Mockito.contains("window.Vaadin.Flow.clients"))) - .thenReturn(probeStates[0], - Arrays.copyOfRange(probeStates, 1, probeStates.length)); - return driver; - } - 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 9bd31c7a9..37d16fe51 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 @@ -41,13 +41,6 @@ public class TestBenchCommandExecutorTest { private static final String IMG_FOLDER = ImageComparisonTest.class .getPackage().getName().replace('.', '/'); - /** - * The value the readiness probe returns when Vaadin is not idle but the - * page exposes {@code window.Vaadin.Flow.ready()}. Mirrors the sentinel in - * {@link TestBenchCommandExecutor}. - */ - private static final String AWAIT_FLOW_READY = "await-flow-ready"; - @Before public void setUp() { } @@ -225,8 +218,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); @@ -294,58 +287,31 @@ public void testTotalTimeSpentServicingRequests() { } @Test - public void waitForVaadin_flowReadyAvailable_delegatesWaitToBrowser() { - FirefoxDriver driver = mockWaitForVaadinDriver(AWAIT_FLOW_READY, - Boolean.TRUE); - Mockito.when(driver.executeAsyncScript( - Mockito.contains("window.Vaadin.Flow.ready"), - Mockito.anyLong())).thenReturn(null); + 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("window.Vaadin.Flow.ready"), - Mockito.anyLong()); - // Flow resolving is not enough on its own: the synchronous probe has - // the final say on whether Vaadin is idle - Mockito.verify(driver, Mockito.times(2)) - .executeScript(Mockito.contains("window.Vaadin.Flow.clients")); - } - - @Test - public void waitForVaadin_awaitingFlowReadyDoesNotSucceed_keepsProbing() { - FirefoxDriver driver = mockWaitForVaadinDriver(AWAIT_FLOW_READY, - AWAIT_FLOW_READY, AWAIT_FLOW_READY, Boolean.TRUE); - // A rejected promise, a page load cancelling the pending script and - // navigating away from Flow must all be retried, and none of them may - // escape to the caller - Mockito.when(driver.executeAsyncScript(Mockito.anyString(), - Mockito.anyLong())) - .thenReturn("Vaadin.Flow.ready timed out after 10000ms") - .thenThrow(new ScriptTimeoutException("script timeout"), - new JavascriptException( - "window.Vaadin.Flow is undefined")); - - newCommandExecutor(driver).waitForVaadin(); - - Mockito.verify(driver, Mockito.times(3)) - .executeAsyncScript(Mockito.anyString(), Mockito.anyLong()); - Mockito.verify(driver, Mockito.times(4)) - .executeScript(Mockito.contains("window.Vaadin.Flow.clients")); + Mockito.contains("flow.ready("), Mockito.eq(10000L)); } @Test - public void waitForVaadin_probeFailsWhilePageLoads_keepsProbing() { + public void waitForVaadin_notIdleOrInterrupted_asksAgain() { FirefoxDriver driver = Mockito.mock(FirefoxDriver.class); - Mockito.when(driver - .executeScript(Mockito.contains("window.Vaadin.Flow.clients"))) - .thenThrow(new JavascriptException("document is not defined")) + Mockito.when( + driver.executeAsyncScript(Mockito.anyString(), Mockito.any())) + .thenReturn(Boolean.FALSE) + .thenThrow(new ScriptTimeoutException("script timeout"), + new JavascriptException("document unloaded")) .thenReturn(Boolean.TRUE); newCommandExecutor(driver).waitForVaadin(); - Mockito.verify(driver, Mockito.times(2)) - .executeScript(Mockito.contains("window.Vaadin.Flow.clients")); + Mockito.verify(driver, Mockito.times(4)) + .executeAsyncScript(Mockito.anyString(), Mockito.any()); } private TestBenchCommandExecutor newCommandExecutor(FirefoxDriver driver) { @@ -355,23 +321,10 @@ private TestBenchCommandExecutor newCommandExecutor(FirefoxDriver driver) { return tbce; } - /** - * Mocks a driver whose readiness probe returns the given states in order, - * repeating the last one once they run out. - */ - private FirefoxDriver mockWaitForVaadinDriver(Object... probeStates) { - FirefoxDriver driver = Mockito.mock(FirefoxDriver.class); - Mockito.when(driver - .executeScript(Mockito.contains("window.Vaadin.Flow.clients"))) - .thenReturn(probeStates[0], - Arrays.copyOfRange(probeStates, 1, probeStates.length)); - return driver; - } - 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-shared/src/main/java/com/vaadin/testbench/commands/TestBenchCommandExecutor.java b/vaadin-testbench-shared/src/main/java/com/vaadin/testbench/commands/TestBenchCommandExecutor.java index 9c8f06974..2dcbb3c75 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 @@ -51,64 +51,51 @@ private static Logger getLogger() { private boolean autoScrollIntoView = true; /** - * Returned by {@link #WAIT_FOR_VAADIN_SCRIPT} when Vaadin is not idle but - * the page exposes {@code window.Vaadin.Flow.ready()}, so that the waiting - * itself can be delegated to the browser. - */ - private static final String AWAIT_FLOW_READY = "await-flow-ready"; - - /** - * Synchronous probe for the current readiness state. Returns {@code true} - * when Vaadin is idle, {@link #AWAIT_FLOW_READY} when Flow can be asked to - * report readiness asynchronously, and {@code false} when the state has to - * be probed again later. + * Calls back with {@code true} when Vaadin is idle and {@code false} when + * it should be asked again. *

- * The {@code document.readyState} and {@code devServerIsNotLoaded} checks - * are kept here on purpose. This probe also runs on pages that have no Flow - * at all, and while the dev server is still starting there is no - * {@code window.Vaadin.Flow.ready()} to defer to yet. + * 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 private static final String WAIT_FOR_VAADIN_SCRIPT = - "if (document.readyState != 'complete') {" - + " return false;" + "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 && clients[client].isActive()) {" - + " return typeof window.Vaadin.Flow.ready === 'function'" - + " ? '" + AWAIT_FLOW_READY + "' : false;" + + " if (clients[client].isActive()) {" + + " callback(false);" + + " return;" + " }" + " }" - + " return true;" + + " callback(true);" + "} else {" - + " return true;" + + " callback(true);" + "}"; - - private static final String AWAIT_FLOW_READY_SCRIPT = - "var callback = arguments[arguments.length - 1];" - + "window.Vaadin.Flow.ready({ timeout: arguments[0] })" - + " .then(function() { callback(null); })" - + " .catch(function(error) {" - + " callback(error && error.message ? error.message : 'rejected');" - + " });"; // @formatter:on private static final long WAIT_FOR_VAADIN_TIMEOUT_MS = 40000; /** - * How long a single browser-side wait may last. Kept below the WebDriver - * default script timeout of 30s so the promise can settle and invoke the - * callback before the driver aborts the script, and short enough that a - * page load cancelling the pending script only costs one chunk before the - * state is probed again. + * 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 AWAIT_FLOW_READY_CHUNK_MS = 10000; - - private static final long POLL_INTERVAL_MS = 100; + private static final long FLOW_READY_TIMEOUT_MS = 10000; public TestBenchCommandExecutor(ImageComparison imageComparison, ReferenceNameGenerator referenceNameGenerator) { @@ -150,10 +137,9 @@ public String getRemoteControlName() { /** * Block until Vaadin reports it has finished processing server messages. *

- * A single synchronous probe decides whether there is anything to wait for. - * When there is, and the page exposes {@code window.Vaadin.Flow.ready()}, - * the waiting is delegated to the browser in one blocking call instead of - * being polled from the test JVM. + * 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) { @@ -165,115 +151,36 @@ public void waitForVaadin() { // again JavascriptExecutor executor = (JavascriptExecutor) getDriver() .getWrappedDriver(); - long deadline = System.currentTimeMillis() + WAIT_FOR_VAADIN_TIMEOUT_MS; - - while (System.currentTimeMillis() < deadline) { - Object state = probe(executor); - if (Boolean.TRUE.equals(state)) { - return; - } - if (AWAIT_FLOW_READY.equals(state) - && awaitFlowReady(executor, deadline)) { - // Flow reported it is idle. Probe again instead of returning - // here, so that the probe stays the single authority on what - // counts as idle and Flow.ready() is only an efficient sleep. - continue; - } - if (state == 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"); - } - if (!pauseBeforeNextProbe(deadline)) { + 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; } } - getLogger().debug("Vaadin was still not idle after {}ms, continuing", - WAIT_FOR_VAADIN_TIMEOUT_MS); } - /** - * Runs {@link #WAIT_FOR_VAADIN_SCRIPT} to determine the current readiness - * state. - * - * @param executor - * the executor to run the script with - * @return the state reported by the script, or {@code false} if the script - * could not be run because a page load was in progress - */ - private Object probe(JavascriptExecutor executor) { + private boolean isVaadinIdle(JavascriptExecutor executor, + long flowReadyTimeout) { + Object finished; try { - return executor.executeScript(WAIT_FOR_VAADIN_SCRIPT); - } catch (JavascriptException e) { - // The page is most likely being replaced, which is expected while - // the dev server starts. Note that this deliberately does not - // catch WebDriverException: a broken session must still surface - // instead of being retried until the deadline. - getLogger().debug("Could not determine whether Vaadin is idle", e); + finished = executor.executeAsyncScript(WAIT_FOR_VAADIN_SCRIPT, + Math.max(flowReadyTimeout, 0)); + } catch (ScriptTimeoutException | JavascriptException 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; } - } - - /** - * Lets Flow report readiness by awaiting {@code window.Vaadin.Flow.ready()} - * in the browser, so that waiting costs a single WebDriver call instead of - * one call per poll. - * - * @param executor - * the executor to run the script with - * @param deadline - * the time after which waiting must stop - * @return {@code true} if Flow reported it is idle, {@code false} if the - * state has to be probed again - */ - private boolean awaitFlowReady(JavascriptExecutor executor, long deadline) { - long chunk = Math.min(AWAIT_FLOW_READY_CHUNK_MS, - deadline - System.currentTimeMillis()); - if (chunk <= 0) { - return false; - } - try { - Object rejection = executor - .executeAsyncScript(AWAIT_FLOW_READY_SCRIPT, chunk); - if (rejection == null) { - return true; - } - getLogger().debug("window.Vaadin.Flow.ready() was rejected: {}", - rejection); - } catch (ScriptTimeoutException e) { - // The pending script was aborted, e.g. by a page load or by a - // session script timeout shorter than the chunk + if (finished == null) { + // This should never happen but according to + // https://dev.vaadin.com/ticket/19703, it happens getLogger().debug( - "Awaiting window.Vaadin.Flow.ready() was interrupted", e); - } catch (JavascriptException e) { - // e.g. navigated to a page without Flow while the call was being - // set up - getLogger().debug("Could not call window.Vaadin.Flow.ready()", e); - } - return false; - } - - /** - * Waits for the poll interval without overrunning the deadline. - * - * @param deadline - * the time after which waiting must stop - * @return {@code false} if waiting should stop - */ - private boolean pauseBeforeNextProbe(long deadline) { - long remaining = Math.min(POLL_INTERVAL_MS, - deadline - System.currentTimeMillis()); - if (remaining <= 0) { - return false; - } - try { - Thread.sleep(remaining); - return true; - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return false; + "waitForVaadin returned null, this should never happen"); } + return Boolean.TRUE.equals(finished); } @Override From b380d1de0ccbac2fd8679fece0a234e081cad9c3 Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Wed, 14 Oct 2026 17:03:07 +0000 Subject: [PATCH 6/7] fix: only retry waitForVaadin when the page was unloaded A JavascriptException is also thrown for real errors in the wait script, which would then be retried silently until the 40s timeout. Only treat it as an interrupted wait when the page was unloaded and rethrow anything else, as before. Also restores integration test lines that the change does not need. Co-Authored-By: Claude Opus 5.5 --- .../TestBenchCommandExecutorTest.java | 23 ++++++++++++++++- .../TestBenchCommandExecutorTest.java | 25 ++++++++++++++++++- .../com/vaadin/tests/WaitForVaadinIT.java | 3 ++- .../com/vaadin/tests/WaitForVaadinIT.java | 2 ++ .../commands/TestBenchCommandExecutor.java | 19 ++++++++++++-- 5 files changed, 67 insertions(+), 5 deletions(-) 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 9643b20fb..de6744094 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 @@ -292,7 +292,10 @@ public void waitForVaadin_idle_asksBrowserOnce() { newCommandExecutor(driver).waitForVaadin(); Mockito.verify(driver).executeAsyncScript( - Mockito.contains("flow.ready("), Mockito.eq(10000L)); + 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 @@ -311,6 +314,24 @@ public void waitForVaadin_notIdleOrInterrupted_asksAgain() { .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); 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 37d16fe51..a78dde712 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 @@ -34,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 { @@ -295,7 +297,10 @@ public void waitForVaadin_idle_asksBrowserOnce() { newCommandExecutor(driver).waitForVaadin(); Mockito.verify(driver).executeAsyncScript( - Mockito.contains("flow.ready("), Mockito.eq(10000L)); + 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 @@ -314,6 +319,24 @@ public void waitForVaadin_notIdleOrInterrupted_asksAgain() { .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); 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 bd9c1dea8..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 @@ -17,7 +17,7 @@ public class WaitForVaadinIT extends AbstractBrowserTB9Test { private static final long NON_BLOCKING_EXECUTION_TIMEOUT = 10000; - private static final long BLOCKING_EXECUTION_TIMEOUT = 20000; + private static final long BLOCKING_EXECUTION_TIMEOUT = 40000; @Override protected Class getTestView() { @@ -41,6 +41,7 @@ public void waitForVaadin_activeConnector_waits() { @BrowserTest public void waitForVaadin_activeConnector_waitsUtilReady() { openTestURL(); + assertDevServerIsNotLoaded(); getCommandExecutor().executeScript( "window.Vaadin.Flow.clients[\"blocker\"] = {isActive: () => true};" + "setTimeout(function() {" 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 955688a59..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 @@ -41,6 +41,7 @@ public void waitForVaadin_activeConnector_waits() { @Test public void waitForVaadin_activeConnector_waitsUtilReady() { openTestURL(); + assertDevServerIsNotLoaded(); getCommandExecutor().executeScript( "window.Vaadin.Flow.clients[\"blocker\"] = {isActive: () => true};" + "setTimeout(function() {" @@ -69,6 +70,7 @@ public void waitForVaadin_noFlow_returnsImmediately() { @Test public void waitForVaadin_devModeNotReady_waits() { openTestURL(); + getCommandExecutor().executeScript( "window.Vaadin = {Flow: {devServerIsNotLoaded: true}};"); assertExecutionBlocked(() -> getCommandExecutor().waitForVaadin()); 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 2dcbb3c75..3086c2c4f 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,6 +14,7 @@ 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; @@ -167,8 +168,14 @@ private boolean isVaadinIdle(JavascriptExecutor executor, Object finished; try { finished = executor.executeAsyncScript(WAIT_FOR_VAADIN_SCRIPT, - Math.max(flowReadyTimeout, 0)); - } catch (ScriptTimeoutException | JavascriptException e) { + 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); @@ -183,6 +190,14 @@ private boolean isVaadinIdle(JavascriptExecutor executor, 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 public boolean compareScreen(String referenceId) throws IOException { return ScreenshotComparator.compareScreen(referenceId, From 5fd4ca58ad7815c6b89291b8a3b21a7c730dc562 Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:09:48 +0000 Subject: [PATCH 7/7] test: retry on the unload messages Chrome and Firefox actually send --- .../testbench/commands/TestBenchCommandExecutorTest.java | 6 ++++-- .../testbench/commands/TestBenchCommandExecutorTest.java | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) 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 f06542320..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 @@ -327,12 +327,14 @@ public void waitForVaadin_notIdleOrInterrupted_asksAgain() { driver.executeAsyncScript(Mockito.anyString(), Mockito.any())) .thenReturn(Boolean.FALSE) .thenThrow(new ScriptTimeoutException("script timeout"), - new JavascriptException("document unloaded")) + new JavascriptException( + "document unloaded while waiting for result"), + new JavascriptException("Document was unloaded")) .thenReturn(Boolean.TRUE); newCommandExecutor(driver).waitForVaadin(); - Mockito.verify(driver, Mockito.times(4)) + Mockito.verify(driver, Mockito.times(5)) .executeAsyncScript(Mockito.anyString(), Mockito.any()); } 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 a78dde712..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 @@ -310,12 +310,14 @@ public void waitForVaadin_notIdleOrInterrupted_asksAgain() { driver.executeAsyncScript(Mockito.anyString(), Mockito.any())) .thenReturn(Boolean.FALSE) .thenThrow(new ScriptTimeoutException("script timeout"), - new JavascriptException("document unloaded")) + new JavascriptException( + "document unloaded while waiting for result"), + new JavascriptException("Document was unloaded")) .thenReturn(Boolean.TRUE); newCommandExecutor(driver).waitForVaadin(); - Mockito.verify(driver, Mockito.times(4)) + Mockito.verify(driver, Mockito.times(5)) .executeAsyncScript(Mockito.anyString(), Mockito.any()); }