Class BaseBrowserlessTest

java.lang.Object
com.vaadin.browserless.BaseBrowserlessTest

public abstract class BaseBrowserlessTest extends Object
Base class for browserless tests. Provides methods to set up and clean a mocked Vaadin environment. The class allows scan classpath for routes and error views. Subclasses should typically restrict classpath scanning to a specific packages for faster bootstrap, by using ViewPackages annotation. If the annotation is not present a full classpath scan is performed For internal use only. May be renamed or removed in a future release.
Since:
1.0
See Also:
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Creates the test base; subclasses are instantiated by the test engine.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T extends com.vaadin.flow.component.Component>
    ComponentQuery<T>
    $(Class<T> componentType)
    Deprecated, for removal: This API element is subject to removal in a future version.
    since 1.1, for removal in 26.0; use find(Class) instead.
    <T extends com.vaadin.flow.component.Component>
    ComponentQuery<T>
    $(Class<T> componentType, com.vaadin.flow.component.Component fromThis)
    Deprecated, for removal: This API element is subject to removal in a future version.
    since 1.1, for removal in 26.0; use find(Class, Component) instead.
    <T extends com.vaadin.flow.component.Component>
    ComponentQuery<T>
    $view(Class<T> componentType)
    Deprecated, for removal: This API element is subject to removal in a future version.
    since 1.1, for removal in 26.0; use findInView(Class) instead.
    protected final Set<Class<?>>
    Gets the lookup services to register, combining the ones required by the framework integration, the ones provided by the deprecated lookupServices() hook, and the ones declared by the test configuration.
    protected final Set<Class<?>>
    Same as allLookupServices(), but for a configuration that has already been resolved.
    protected void
    Tears down mocked Vaadin.
    protected Routes
    Equivalent to discoverRoutes(scanPackages()).
    protected static Routes
    discoverRoutes(Set<String> packageNames)
    Discover and return Routes for mocked Vaadin core system.
    <T extends com.vaadin.flow.component.Component>
    ComponentQuery<T>
    find(Class<T> componentType)
    Gets a query object for finding a component inside the UI
    <T extends com.vaadin.flow.component.Component>
    ComponentQuery<T>
    find(Class<T> componentType, com.vaadin.flow.component.Component fromThis)
    Gets a query object for finding a component nested inside the given component.
    <T extends com.vaadin.flow.component.Component>
    ComponentQuery<T>
    findInView(Class<T> componentType)
    Gets a query object for finding a component inside the current view
    void
    fireShortcut(com.vaadin.flow.component.Key key, com.vaadin.flow.component.KeyModifier... modifiers)
    Simulates a keyboard shortcut performed on the browser.
    protected Set<Class<?>>
    Gets the services implementations required by the framework integration to initialize the Vaadin Lookup, for example the Spring or Quarkus lookup initializers.
    com.vaadin.flow.component.HasElement
    Get the current view instance that is shown on the ui.
    Gets the JavaScript sent to the browser of the current UI since the UI was created, including what the first view sent while it was navigated to.
    protected void
    Registers the signals test environment, so that signal-backed components work inside the mocked environment.
    protected void
    Create mocked Vaadin core obects, such as session, servlet populated with Routes, UI etc. for testing and find testers for the components.
    protected static <T extends ComponentTester<Y>, Y extends com.vaadin.flow.component.Component>
    T
    internalWrap(Class<T> wrap, Y component)
    Wraps the component in the given tester type, preferring a more specific tester registered through @Tests when one exists.
    protected static <T extends ComponentTester<Y>, Y extends com.vaadin.flow.component.Component>
    T
    internalWrap(Y component)
    Equivalent to TesterRegistry.wrap(component).
    protected Set<Class<?>>
    Deprecated.
    since 25.3, declare the services with BrowserlessTestConfig.lookupServices() or by overriding testConfiguration() instead.
    <T extends com.vaadin.flow.component.Component>
    T
    navigate(Class<T> navigationTarget)
    Navigate to the given view class if it is registered.
    <C, T extends com.vaadin.flow.component.Component & com.vaadin.flow.router.HasUrlParameter<C>>
    T
    navigate(Class<T> navigationTarget, C parameter)
    Navigate to view with url parameter.
    <T extends com.vaadin.flow.component.Component>
    T
    navigate(Class<T> navigationTarget, Map<String,String> parameters)
    Navigate to view corresponding to the given navigation target with the specified route parameters.
    <T extends com.vaadin.flow.component.Component>
    T
    navigate(String location, Class<T> expectedTarget)
    Navigate to given location string.
    com.vaadin.flow.component.HasElement
    Simulates the user reloading the page (pressing F5): the current UI is detached and a fresh one is created in the same Vaadin session, then the current location is rendered again.
    <T extends com.vaadin.flow.component.Component>
    T
    reload(Class<T> expectedTarget)
    Simulates a page reload (see reload()) and verifies the resulting view is of the expected type.
    protected static void
    Simulates a server round-trip: runs the tasks queued with UI.access() and the queued Signals effects, such as those of a signal changed on another thread, runs the beforeClientResponse callbacks and flushes pending component changes, as a real client response would.
    protected final boolean
    Processes all pending Signals tasks with a default max wait time of 100 milliseconds.
    protected final boolean
    runPendingSignalsTasks(long maxWaitTime, TimeUnit unit)
    Processes all pending Signals tasks, waiting up to the specified timeout for tasks to arrive.
    protected Set<String>
    Collects the packages to scan for routes, from the @ViewPackages annotation on the test class.
    protected void
    Scan testers and populate testers map with them.
    <T extends ComponentTester<Y>, Y extends com.vaadin.flow.component.Component>
    T
    test(Class<T> tester, Y component)
    Wrap component in given ComponentTester.
    <T extends ComponentTester<Y>, Y extends com.vaadin.flow.component.Component>
    T
    test(Y component)
    Wrap component with ComponentTester best matching component type.
    Gets the custom Vaadin configuration, such as application properties and feature flags, to apply to the mock Vaadin environment.
    protected abstract String
    Gets the name of the Test Engine that is able to run the base class implementation.

    Methods inherited from class java.lang.Object

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

    • BaseBrowserlessTest

      protected BaseBrowserlessTest()
      Creates the test base; subclasses are instantiated by the test engine.
  • Method Details

    • discoverRoutes

      protected Routes discoverRoutes()
      Equivalent to discoverRoutes(scanPackages()).
      Returns:
      the routes found in the scanned packages
    • discoverRoutes

      protected static Routes discoverRoutes(Set<String> packageNames)
      Discover and return Routes for mocked Vaadin core system.
      Parameters:
      packageNames - the packages to scan for routes
      Returns:
      Routes
      See Also:
    • initVaadinEnvironment

      protected void initVaadinEnvironment()
      Create mocked Vaadin core obects, such as session, servlet populated with Routes, UI etc. for testing and find testers for the components.
    • allLookupServices

      protected final Set<Class<?>> allLookupServices()
      Gets the lookup services to register, combining the ones required by the framework integration, the ones provided by the deprecated lookupServices() hook, and the ones declared by the test configuration. For internal use only.
      Returns:
      the set of services implementation classes, never null.
      Since:
      25.3
    • allLookupServices

      protected final Set<Class<?>> allLookupServices(BrowserlessConfiguration configuration)
      Same as allLookupServices(), but for a configuration that has already been resolved. testConfiguration() is an overridable hook, so an override rebuilding the configuration on every call would otherwise do the work twice, and a non deterministic one could register services that do not belong to the configuration actually applied to the environment. Resolve the configuration once and pass it to both this method and MockVaadin.setup(). For internal use only.
      Parameters:
      configuration - the configuration to read the lookup services from, not null
      Returns:
      the set of services implementation classes, never null.
      Since:
      25.3
    • initSignalsSupport

      protected void initSignalsSupport()
      Registers the signals test environment, so that signal-backed components work inside the mocked environment.
    • scanTesters

      protected void scanTesters()
      Scan testers and populate testers map with them. The test method can find appropriate test based on testers map.
      See Also:
    • scanPackages

      protected Set<String> scanPackages()
      Collects the packages to scan for routes, from the @ViewPackages annotation on the test class.
      Returns:
      the packages to scan, empty to scan the whole classpath
    • cleanVaadinEnvironment

      protected void cleanVaadinEnvironment()
      Tears down mocked Vaadin.
    • lookupServices

      @Deprecated(since="25.3") protected Set<Class<?>> lookupServices()
      Deprecated.
      since 25.3, declare the services with BrowserlessTestConfig.lookupServices() or by overriding testConfiguration() instead. Overrides of this method are still honored.
      Gets the services implementations to be used to initialized Vaadin Lookup. Default implementation returns an empty Set. Override this method to provide custom Vaadin services, such as InstantiatorFactory, ResourceProvider, etc. Since 25.3 the services required by the Spring and Quarkus integrations are contributed by frameworkLookupServices() instead, and are always registered. An override of this method therefore adds to them and can no longer replace one of them, for example to swap the Spring SpringSecurityRequestCustomizer; override frameworkLookupServices() in a framework specific base class for that.
      Returns:
      set of services implementation classes, never null.
    • frameworkLookupServices

      protected Set<Class<?>> frameworkLookupServices()
      Gets the services implementations required by the framework integration to initialize the Vaadin Lookup, for example the Spring or Quarkus lookup initializers. These services are always registered, regardless of the test configuration, so that a test customizing testConfiguration() cannot accidentally break the framework integration. Meant to be overridden by framework specific base classes only; tests should declare their own services with BrowserlessTestConfig.lookupServices().
      Returns:
      set of services implementation classes, never null.
      Since:
      25.3
    • testConfiguration

      protected BrowserlessConfiguration testConfiguration()
      Gets the custom Vaadin configuration, such as application properties and feature flags, to apply to the mock Vaadin environment. Default implementation returns the configuration declared by the BrowserlessTestConfig annotations present on the test class and on the current test method, if any. Override this method to provide the configuration programmatically, potentially on top of the declared one.
       @Override
       protected BrowserlessConfiguration testConfiguration() {
           return BrowserlessConfiguration.builder()
                   .withConfiguration(super.testConfiguration())
                   .withFeatureFlags("myExperimentalFeature").build();
       }
       
      Returns:
      the configuration to apply, never null.
      Since:
      25.3
      See Also:
    • reload

      public com.vaadin.flow.component.HasElement reload()
      Simulates the user reloading the page (pressing F5): the current UI is detached and a fresh one is created in the same Vaadin session, then the current location is rendered again. Session-scoped state (session attributes, security context) survives. Views annotated with @PreserveOnRefresh keep their component instance and state; other views are recreated.
      Returns:
      the view shown after the reload
      Since:
      25.4
    • reload

      public <T extends com.vaadin.flow.component.Component> T reload(Class<T> expectedTarget)
      Simulates a page reload (see reload()) and verifies the resulting view is of the expected type.
      Type Parameters:
      T - the view type
      Parameters:
      expectedTarget - the expected view class after reload
      Returns:
      the view shown after the reload
      Since:
      25.4
    • fireShortcut

      public void fireShortcut(com.vaadin.flow.component.Key key, com.vaadin.flow.component.KeyModifier... modifiers)
      Simulates a keyboard shortcut performed on the browser.
      Parameters:
      key - Primary key of the shortcut. This must not be a KeyModifier.
      modifiers - Key modifiers. Can be empty.
    • getExecutedJavaScript

      public ExecutedJavaScript getExecutedJavaScript()
      Gets the JavaScript sent to the browser of the current UI since the UI was created, including what the first view sent while it was navigated to. Each UI has its own capture: after reload(), or in a new window, this returns the capture of the new UI, which starts empty. Like any ExecutedJavaScript, querying it sends whatever is still pending first. To look at the JavaScript of a single step, start a separate capture with ExecutedJavaScript.capture() before it. This capture runs for as long as the UI lives and is read-only: it cannot be stopped.
      Returns:
      the JavaScript sent to the browser of the UI, not null
    • getCurrentView

      public com.vaadin.flow.component.HasElement getCurrentView()
      Get the current view instance that is shown on the ui.
      Returns:
      current view
    • internalWrap

      protected static <T extends ComponentTester<Y>, Y extends com.vaadin.flow.component.Component> T internalWrap(Y component)
      Equivalent to TesterRegistry.wrap(component).
      Type Parameters:
      T - the item type
      Y - the component type
      Parameters:
      component - the component to wrap
      Returns:
      a tester for the given component
    • internalWrap

      protected static <T extends ComponentTester<Y>, Y extends com.vaadin.flow.component.Component> T internalWrap(Class<T> wrap, Y component)
      Wraps the component in the given tester type, preferring a more specific tester registered through @Tests when one exists.
      Type Parameters:
      T - the tester type
      Y - the component type
      Parameters:
      wrap - the tester type the caller asked for
      component - the component to wrap
      Returns:
      a tester for the given component
    • test

      public <T extends ComponentTester<Y>, Y extends com.vaadin.flow.component.Component> T test(Y component)
      Wrap component with ComponentTester best matching component type.
      Type Parameters:
      T - tester type
      Y - component type
      Parameters:
      component - component to get test wrapper for
      Returns:
      component in wrapper with test helpers
    • test

      public <T extends ComponentTester<Y>, Y extends com.vaadin.flow.component.Component> T test(Class<T> tester, Y component)
      Wrap component in given ComponentTester.
      Type Parameters:
      T - tester type
      Y - component type
      Parameters:
      tester - test wrapper to use
      component - component to wrap
      Returns:
      initialized test wrapper for component
    • find

      public <T extends com.vaadin.flow.component.Component> ComponentQuery<T> find(Class<T> componentType)
      Gets a query object for finding a component inside the UI

      The query walks the server-side component tree. A component that another component renders per item, such as the component a ComponentRenderer column renders for a grid row, is rendered into the column and not into the tree, and the content of an overlay, such as a context menu, is attached only while the overlay is open. Neither is reachable this way, and the lookup returns an empty result rather than failing, so reach those components through the owning component tester instead: GridTester.getCellComponent(row, column) for grid cells, ContextMenuTester.open() and then clickItem(...) for a context menu, and GridTester.contextMenu(row).open() for a grid context menu.

      Type Parameters:
      T - the type of the component(s) to search for
      Parameters:
      componentType - the type of the component(s) to search for
      Returns:
      a query object for finding components
      Since:
      1.1
    • find

      public <T extends com.vaadin.flow.component.Component> ComponentQuery<T> find(Class<T> componentType, com.vaadin.flow.component.Component fromThis)
      Gets a query object for finding a component nested inside the given component.

      Searches the same server-side component tree as find(Class), see there for what that tree does not contain.

      Type Parameters:
      T - the type of the component(s) to search for
      Parameters:
      componentType - the type of the component(s) to search for
      fromThis - component used as starting element for search.
      Returns:
      a query object for finding components
      Since:
      1.1
    • findInView

      public <T extends com.vaadin.flow.component.Component> ComponentQuery<T> findInView(Class<T> componentType)
      Gets a query object for finding a component inside the current view

      Searches the same server-side component tree as find(Class), see there for what that tree does not contain.

      Type Parameters:
      T - the type of the component(s) to search for
      Parameters:
      componentType - the type of the component(s) to search for
      Returns:
      a query object for finding components
      Since:
      1.1
    • $

      @Deprecated(since="1.1", forRemoval=true) public <T extends com.vaadin.flow.component.Component> ComponentQuery<T> $(Class<T> componentType)
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 1.1, for removal in 26.0; use find(Class) instead.
      Gets a query object for finding a component inside the UI.
      Type Parameters:
      T - the type of the component(s) to search for
      Parameters:
      componentType - the type of the component(s) to search for
      Returns:
      a query object for finding components
    • $

      @Deprecated(since="1.1", forRemoval=true) public <T extends com.vaadin.flow.component.Component> ComponentQuery<T> $(Class<T> componentType, com.vaadin.flow.component.Component fromThis)
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 1.1, for removal in 26.0; use find(Class, Component) instead.
      Gets a query object for finding a component nested inside the given component.
      Type Parameters:
      T - the type of the component(s) to search for
      Parameters:
      componentType - the type of the component(s) to search for
      fromThis - component used as starting element for search.
      Returns:
      a query object for finding components
    • $view

      @Deprecated(since="1.1", forRemoval=true) public <T extends com.vaadin.flow.component.Component> ComponentQuery<T> $view(Class<T> componentType)
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 1.1, for removal in 26.0; use findInView(Class) instead.
      Gets a query object for finding a component inside the current view.
      Type Parameters:
      T - the type of the component(s) to search for
      Parameters:
      componentType - the type of the component(s) to search for
      Returns:
      a query object for finding components
    • roundTrip

      protected static void roundTrip()
      Simulates a server round-trip: runs the tasks queued with UI.access() and the queued Signals effects, such as those of a signal changed on another thread, runs the beforeClientResponse callbacks and flushes pending component changes, as a real client response would. Dialogs that are no longer opened are removed from the UI, as after a lookup.

      Only tasks that are already queued are run, so a test of a background thread must first wait for that thread to call access() or to change the signal; runPendingSignalsTasks(long, TimeUnit) waits for Signals tasks that have not arrived yet. If a queued UI.access() task fails, its exception is rethrown wrapped in an ExecutionException. If a Signals task fails, its exception is thrown as is and the round trip stops there, leaving the remaining tasks queued, as with runPendingSignalsTasks().

    • runPendingSignalsTasks

      protected final boolean runPendingSignalsTasks()
      Processes all pending Signals tasks with a default max wait time of 100 milliseconds. This is a convenience method for tests that need to wait for asynchronous Signal effects to complete.

      When Signals are triggered from background threads or non-UI contexts, their effects are enqueued to simulate asynchronous processing. This method allows tests to flush and execute all such pending tasks synchronously, ensuring deterministic behavior in unit tests. Tasks that are already queued are also run by every round trip, such as roundTrip() or a component lookup; this method is needed to wait for tasks of background work that has not finished yet, or to run the queue without a round trip.

      If any VaadinSession lock is held by the current thread, it is temporarily released during the wait to allow background threads to acquire the lock and enqueue tasks.

      Confirmation of a write to a shared signal (for example SharedValueSignal or SharedListSignal) is dispatched through the same queue. The new value is visible immediately through peek(), but the SignalOperation returned by the write only completes once the queued confirmation task has been run, by this method or by the next round trip. Blocking on operation.result().get() without draining the queue first never completes, because the confirmation task can only run on the test thread that drains the queue:

      
       var operation = tickets.insertLast("a ticket");
       runPendingSignalsTasks();
       assertTrue(operation.result().join().successful());
       
      Returns:
      true if any pending Signals tasks were processed.
      See Also:
    • runPendingSignalsTasks

      protected final boolean runPendingSignalsTasks(long maxWaitTime, TimeUnit unit)
      Processes all pending Signals tasks, waiting up to the specified timeout for tasks to arrive. This method is essential for testing asynchronous Signal effects triggered from background threads or non-UI contexts.

      When Signals are triggered from background threads or non-UI contexts, their effects are enqueued to simulate asynchronous processing. This method allows tests to flush and execute all such pending tasks synchronously, ensuring deterministic behavior in unit tests.

      The timeout applies only to waiting for the first task to arrive. Once the first task is found, all remaining tasks in the queue are processed immediately without additional waiting. If any VaadinSession lock is held by the current thread, it is temporarily released during the wait to allow background threads to acquire the lock and enqueue tasks.

      Parameters:
      maxWaitTime - the maximum time to wait for the first task to arrive in the given time unit. If <= 0, returns immediately if no tasks are available.
      unit - the time unit of the timeout value
      Returns:
      true if any pending Signals tasks were processed.
      See Also:
    • testingEngine

      protected abstract String testingEngine()
      Gets the name of the Test Engine that is able to run the base class implementation. The Test Engine name is reported in the exception thrown when the Vaadin environment is not set up correctly.
      Returns:
      name of the Test Engine.