Class BrowserlessUserContext

java.lang.Object
com.vaadin.browserless.BrowserlessUserContext
All Implemented Interfaces:
AutoCloseable

public class BrowserlessUserContext extends Object implements AutoCloseable
User-level context for multi-user browserless testing.

Represents a single logical user with their own VaadinSession, HTTP request, and response. Each user context can have multiple windows (UI instances) via newWindow().

Instances of this class are thread-affine: they must be created, used, and closed on the same thread. The active context is held in a ThreadLocal and is not visible to other threads. This class is not safe for concurrent access from multiple threads.

Security context (if a SecurityContextHandler is configured on the parent BrowserlessApplicationContext) is initialised when this user is created and refreshed on user-switch (when activating a different user's window), capturing the outgoing user's live thread-local state at that moment. The new user's authentication is installed on the thread before SessionInit listeners fire, so listeners observe this user's identity — matching the Vaadin+Spring flow where the security filter chain runs before the servlet. The user's initial snapshot is captured after init fires, so any security mutation a listener performs persists into the snapshot.

The security snapshot is per-user, not per-window: all of a user's windows share one snapshot. Security-context mutations made while one window is active persist on the thread and remain visible to other windows of the same user; the snapshot is re-captured only on user-switch, so same-user window switches don't touch it.

See Also:
  • Method Details

    • newWindow

      public BrowserlessUIContext newWindow()
      Creates a new window (UI instance) for this user.

      The window is automatically activated (thread-locals set) and a new UI is created. If a route target for "" is registered, the UI will navigate to it.

      Returns:
      the new UI context
    • close

      public void close()
      Closes this user context and all its windows.

      Destroys the Vaadin session and clears associated state. If a window belonging to a different user is the active context on the calling thread when this method runs, that window is re-activated at the end so subsequent operations on it see a coherent thread-local state.

      Specified by:
      close in interface AutoCloseable
    • getSession

      public VaadinSession getSession()
      Returns the Vaadin session associated with this user.
      Returns:
      the Vaadin session