Class BrowserlessUserContext

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    
    public class BrowserlessUserContext
     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 newUser rather than reusing this one.

    Since:

    1.1

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
      public final VaadinSession session
    • Constructor Summary

      Constructors 
      Constructor Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      VaadinSession getSession() Returns the Vaadin session associated with this user.
      BrowserlessUIContext newWindow() Creates a new window (UI instance) for this user.
      void close() Closes this user context and all its windows.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

    • Method Detail

      • getSession

         VaadinSession getSession()

        Returns the Vaadin session associated with this user.

        Returns:

        the Vaadin session

      • newWindow

         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 newUser instead of reusing this one.

        Returns:

        the new UI context

      • close

         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.