Skip to content

feat: add window.Vaadin.Flow.ready() for testing tools - #24240

Draft
Artur- wants to merge 14 commits into
mainfrom
feature/when-ready
Draft

Artur- wants to merge 14 commits into
mainfrom
feature/when-ready

Conversation

@Artur-

@Artur- Artur- commented May 1, 2026 •

Copy link
Copy Markdown
Member

Review risk: two-way door — adds a new client-side JS API (window.Vaadin.Flow.ready()) and a window event; existing behavior of Flow pages is unchanged and nothing depends on the API until a testing tool opts in.

Summary

Adds window.Vaadin.Flow.ready({ timeout }), a Promise-based "wait until Flow is idle" API that testing tools (TestBench, Playwright, …) can use instead of polling window.Vaadin.Flow.clients[*].isActive() from outside the browser. The readiness rules live in Flow, so every tool gets the same definition of "idle".

What changed

  • ready() is documented in ready.js and inlined into the bootstrap script of both bootstrap paths (IndexHtmlRequestHandler and BootstrapHandler), so it exists before any client module has loaded. It resolves once document.readyState is complete, the dev server is loaded and every registered client reports isActive() === false. A page with no Flow clients counts as idle, matching what TestBench has always done. It rejects after the timeout (default 30s).
  • ready() is event-driven: the client dispatches a vaadin-flow-activity-changed window event whenever isActive() may have changed (request ended, deferred command queue drained, client published, Flow.ts navigation finished). ready() re-checks on that event and on readystatechange; a 500ms interval is kept only as a safety net.
  • The event is dispatched in a separate task, not a microtask, so listeners never see the momentary idle state in the middle of a promise chain (e.g. between Flow initialization and the first route navigation). Resolving there would let a test continue before the view is rendered.
  • When a postponed back/forward navigation proceeds, Flow stays active while the browser moves to the target entry, so ready() does not resolve before that view is shown.
  • The dev-mode "not ready" page sets ready: false, so callers fall back to their own handling while the dev server starts.
  • No whenReady(callback) adapter: the Promise API is enough for callers.

Test summary

  • Browser tests for ready() itself, evaluated joined to one line as the server inlines it (so a // comment fails them): resolves immediately when idle or when there are no clients, waits for the document to finish loading, resolves on the activity event without waiting for the safety-net interval, keeps waiting while the dev server is not loaded, rejects on timeout.
  • Client tests that the request tracker, the deferred-command scheduler (only once its queue is empty), publishClient and Flow.loadingFinished() notify, that notifications are coalesced, and that a momentary idle state inside a promise chain is not reported (fails with microtask dispatch). Flow stays active until a postponed back/forward navigation that proceeds has shown its view.
  • Bootstrap tests check that the inline script defines ready() after window.Vaadin.Flow is initialised, and that the {{READY}} placeholder is substituted in the web component bootstrap.
  • Verified end to end with the companion TestBench change (feat: delegate waitForVaadin readiness checks to Flow testbench#2160) against a production build: 74 browser ITs green across repeated runs. Before the separate-task dispatch, ready() resolved before the first view was rendered in 36 of 60 page loads; with it, 0 of 60, the same as the existing polling.

API Changes

  • New public constant com.vaadin.flow.server.BootstrapHandler.READY_JS (the inlined script, shared by both bootstrap paths).
  • New client JS API window.Vaadin.Flow.ready(options?: { timeout?: number }): Promise<void> and window event vaadin-flow-activity-changed.

🤖 Generated with Claude Code

Define ready() as the canonical Promise-based "wait until Flow is
idle" API and whenReady(callback) as a thin callback adapter for
TestBench-style usage. Both are inlined into the bootstrap HTML so
they are available on every Flow page in dev and production mode.

ready() resolves once document.readyState is complete,
window.Vaadin.Flow.devServerIsNotLoaded is falsy, at least one client
with isActive() has been registered, and all such clients report
inactive. The "at least one client" requirement avoids a false
positive before Flow has bootstrapped. ready() takes an optional
{ timeout } (default 30s) and rejects on timeout instead of polling
forever. whenReady(callback) invokes the callback either when ready()
resolves or after logging the rejection on timeout, so the callback
contract holds either way.

The dev-mode-not-ready page sets ready and whenReady to false as
not-ready sentinels for TestBench.
@github-actions

github-actions Bot commented May 1, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

 1 667 files  ± 0   1 755 suites  +2   2h 17m 33s ⏱️ + 9m 18s
12 954 tests +14  12 885 ✅ +14  69 💤 ±0  0 ❌ ±0 
13 588 runs  +14  13 519 ✅ +14  69 💤 ±0  0 ❌ ±0 

Results for commit d11e1e2. ± Comparison against base commit a59da97.

♻️ This comment has been updated with latest results.

Artur- added 2 commits May 1, 2026 22:09
The leading guard comment in whenReady.js was breaking the
startsWith assertion. Move the warning about disallowed // comments
onto the WHEN_READY_JS field's javadoc, where future maintainers of
the inlining are more likely to see it, and relax the test to use
contains so unrelated changes to whenReady.js do not break it.
@sonarqubecloud

sonarqubecloud Bot commented May 2, 2026

Copy link
Copy Markdown

@Artur-
Artur- marked this pull request as draft May 4, 2026 06:37
totally-not-ai Bot and others added 3 commits October 14, 2026 16:05
The client now dispatches a vaadin-flow-activity-changed window event
whenever the result of isActive() may have changed: when a request ends,
when the deferred command queue drains, when a client is published and
when a Flow.ts navigation finishes. ready() listens for it (and for
readystatechange) instead of polling every 50ms, with a slow interval
kept only as a safety net.

ready() now also treats a page without any Flow client as idle, matching
what TestBench has always done, so it does not hang until the timeout on
pages that never start a Flow client. The whenReady(callback) adapter is
removed; ready() returns a Promise, which is enough for any caller.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Dispatching in a microtask let ready() observe a momentary idle state
in the middle of a promise chain, e.g. between Flow finishing its
initialization and navigating to the first route, and resolve before
the view was rendered. Dispatching in a separate task means listeners
only see the state between tasks, which is also what a polling caller
such as TestBench has always observed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@totally-not-ai totally-not-ai Bot changed the title feat: add window.Vaadin.Flow.ready() and whenReady(callback) feat: add window.Vaadin.Flow.ready() for testing tools Oct 14, 2026
totally-not-ai Bot and others added 3 commits October 19, 2026 13:14
# Conflicts:
#	flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java
Evaluate ready.js in ReadyTests the way the server inlines it, joined
to one line, so a // comment breaks the tests. Add tests for waiting on
document.readyState, for loadingFinished() and publishClient()
notifying activity changes, and for the {{READY}} substitution in the
web component bootstrap. Pin the deferred command count condition in
TrackingSchedulerTests and the script order in IndexHtmlRequestHandler
tests.

Document the ready() contract in ready.js, keeping the comment free of
< since the script is inlined into a script element.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@totally-not-ai

Copy link
Copy Markdown
Contributor

Fixed: removed the unthrown ServiceException from the throws clause of writeBootstrapPage_noExportChunk (the reported line), and also from the new writeBootstrapPage_readyScriptInlined test, which had copied the same signature.

totally-not-ai Bot and others added 2 commits October 19, 2026 16:10
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@totally-not-ai

Copy link
Copy Markdown
Contributor

Fixed: added a reason to the @Disabled annotations on writeBootstrapPage_noExportChunk (the reported test) and its sibling writeBootstrapPage_withExportChunk. Both have been disabled since the switch to Vite: they assert vaadin-export-*.cache.js chunks, which only the webpack build produced, and WebComponentBootstrapHandler no longer handles export chunks.

@sonarqubecloud

Copy link
Copy Markdown

# Conflicts:
#	flow-server/src/test/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandlerTest.java
# Conflicts:
#	flow-client/src/test/frontend/FlowTests.ts
When a postponed back/forward navigation proceeds, Flow waits for the
browser to move to the target entry before it shows the view. serverPaused
had already reported Flow idle, so Vaadin.Flow.ready() could resolve
during that move, before the view was shown. Mark Flow active again until
completeNavigation runs.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant