Skip to content

feat: delegate waitForVaadin readiness checks to Flow - #2160

Draft
Artur- wants to merge 9 commits into
mainfrom
wait-for-vaadin-in-flow
Draft

Artur- wants to merge 9 commits into
mainfrom
wait-for-vaadin-in-flow

Conversation

@Artur-

@Artur- Artur- commented Mar 3, 2026 •

Copy link
Copy Markdown
Member

Review risk: two-way door — changes how waitForVaadin() waits (one async script instead of a sync polling loop); if wrong, tests could continue before Vaadin is idle or wait too long, but the change is isolated to one method and reverts cleanly.

Summary

waitForVaadin() delegates the waiting to Flow's window.Vaadin.Flow.ready() (vaadin/flow#24240) when the page provides it, so Flow alone defines what "idle" means and a wait costs a single WebDriver call. With older Flow versions the existing readiness check is still used.

What changed

  • waitForVaadin() now runs one executeAsyncScript per attempt. If window.Vaadin.Flow.ready is a function, the script awaits it (with a 10s chunk, below the default 30s script timeout) and returns true when it resolves. Otherwise it runs the same checks as before (document.readyState, devServerIsNotLoaded, clients[*].isActive()) and returns immediately, so the loop polls as before.
  • A ScriptTimeoutException, or a JavascriptException caused by the page being unloaded while waiting (e.g. a navigation), is treated as "not idle yet" and retried until the 40s overall timeout. Any other script error is rethrown immediately, as before.
  • Removes the waitForVaadinLoopHook test hook; the integration tests now flip the blocking state with setTimeout in the page instead.
  • Behavioral note: the wait script now goes through executeAsyncScript instead of executeScript, also on the fallback path. Tests that mock the driver need to stub executeAsyncScript.

Test summary

  • Unit tests: an idle page needs a single browser call, with a ready timeout below the default script timeout; "not idle" results, script timeouts and page-unload errors are retried until idle; other script errors are rethrown without retrying.
  • Integration tests (WaitForVaadinIT, element and query ITs) pass against both the published Flow snapshot (fallback path) and Flow with ready() (delegated path).
  • Benchmark: 74 browser ITs, production build, local ChromeDriver, sum of suite times, 3 runs at 0ms and 2 runs at 40ms added per-command latency:
Setup 0ms 40ms WebDriver script calls
main + published Flow 43.4s 58.6s ~2,350–2,400
this PR + published Flow 42.8s 57.7s ~2,310–2,580
this PR + Flow with ready() 42.3s 57.8s 2,215, the same every run

The suite is ~2.5% faster, and with ready() the number of WebDriver calls drops by ~7% and no longer varies between runs. These ITs rarely wait on slow server round trips, so most waits already return on the first call. The saving grows with views that keep the client busy longer.

🤖 Generated with Claude Code

@Artur-
Artur- force-pushed the wait-for-vaadin-in-flow branch 2 times, most recently from 2c0976c to 6cfbe71 Compare March 3, 2026 16:09
@Artur- Artur- changed the title feat: replace waitForVaadin polling loop with single async call feat: delegate waitForVaadin readiness checks to Flow Mar 3, 2026
@github-actions

github-actions Bot commented Mar 3, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

  192 files  ±0    192 suites  ±0   12m 29s ⏱️ +57s
1 388 tests +3  1 381 ✅ +3   7 💤 ±0  0 ❌ ±0 
1 855 runs  +6  1 845 ✅ +6  10 💤 ±0  0 ❌ ±0 

Results for commit b380d1d. ± Comparison against base commit b12c6a8.

♻️ This comment has been updated with latest results.

Comment on lines +64 to +65
getCommandExecutor()
.executeScript("window.Vaadin.Flow.whenReady = false;");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this setting the test always fails because waitForVaadin almost immediately returns, since there's nothing waiting for the timeout expected by assertExecutionBlocked

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcollovati Good catch — the trigger was wrong. waitForVaadin never looked at whenReady, so nothing was blocking.

Both dev-mode tests now set devServerIsNotLoaded again, which is what the dev server actually sets and what the test names describe, so there is a real reason for waitForVaadin to keep waiting. waitForVaadin_devModeNotReady_waitsUntilReady clears it from a setTimeout and asserts the end state, so the waitForVaadinLoopHook reflection hook stays removed.

The absence of Flow.ready deliberately does not block any more — that is the non-Flow-page case, and it has to return immediately. waitForVaadin_noFlow_returnsImmediately is restored to guard it: without that, a page without Flow burned the whole 40s budget on every command and then continued as if Vaadin were idle.

One more thing in this file: BLOCKING_EXECUTION_TIMEOUT was 40000, exactly equal to the full waitForVaadin budget, so assertExecutionBlocked could only ever pass by a hair. Lowered to 20000 to match the JUnit 4 copy.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcollovati Right, whenReady = false only made the script skip the Flow path and report idle straight away, so nothing ever blocked. That setup is gone in the current version of the branch. The blocking tests now go back to the original states that really keep Vaadin busy: Flow.clients with an always-active client, and devServerIsNotLoaded: true. The ...UntilReady variants flip those states with an in-page setTimeout instead of the old reflection-based loop hook. With today's Flow (no Flow.ready() yet) this runs the fallback poll loop, which keeps returning "not idle" until the 40s budget is spent, so assertExecutionBlocked holds. I ran WaitForVaadinIT (JUnit 5) locally against the branch head and all 7 tests pass, including both ..._waits cases.

@Artur-
Artur- force-pushed the wait-for-vaadin-in-flow branch from b8748ef to 17f911b Compare May 2, 2026 05:52
Artur- added 2 commits May 2, 2026 08:55
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.
@Artur-
Artur- force-pushed the wait-for-vaadin-in-flow branch from 17f911b to e389e12 Compare May 2, 2026 05:55
totally-not-ai Bot and others added 4 commits September 18, 2026 06:03
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) <noreply@anthropic.com>
…path

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) <noreply@anthropic.com>
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 <noreply@anthropic.com>
totally-not-ai Bot and others added 3 commits October 14, 2026 17:03
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 <noreply@anthropic.com>
# Conflicts:
#	vaadin-testbench-shared/src/main/java/com/vaadin/testbench/commands/TestBenchCommandExecutor.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants