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, including newly opened ones: the snapshot is neither saved nor restored on a same-user window switch, so it is touched only on a user-switch. Consequently a logout performed in one window leaves the user logged out in every other window — to authenticate again, create a fresh user context with BrowserlessApplicationContext.newUser() rather than reusing this one.

Since:
1.1
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.

      A window opened after the user has logged out shares the user's logged-out security state: it does not restore the pre-logout snapshot. To log in again, create a fresh user context with BrowserlessApplicationContext.newUser() instead of reusing this one.

      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