Class BrowserlessClassExtension

java.lang.Object
com.vaadin.browserless.BrowserlessClassExtension
All Implemented Interfaces:
GeneratedLocators, Locators, TesterWrappers, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.Extension

public class BrowserlessClassExtension extends Object implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback
JUnit 5 extension for browserless Vaadin testing with per-class lifecycle.

The Vaadin environment is initialized once before all tests in the class and torn down after all tests. Use as a static field with @RegisterExtension:

 
 class MyStatefulTest {
     @RegisterExtension
     static BrowserlessClassExtension ext = new BrowserlessClassExtension()
             .withViewPackages(MyView.class);

     @BeforeAll
     static void setup() {
         ext.navigate(MyView.class);
     }

     @Test
     void testA() {
         /* same session */ }

     @Test
     void testB() {
         /* same session */ }
 }
 
 

For a fresh environment per test method, use BrowserlessExtension instead.

Since:
1.1
See Also:
  • Constructor Details

    • BrowserlessClassExtension

      public BrowserlessClassExtension()
      Creates a new extension with per-class lifecycle.
  • Method Details

    • withViewPackages

      public BrowserlessClassExtension withViewPackages(Class<?>... classes)
      Adds packages to scan for @Route-annotated views, derived from the given classes' packages.
      Parameters:
      classes - classes whose packages should be scanned
      Returns:
      this extension instance
    • withViewPackages

      public BrowserlessClassExtension withViewPackages(String... packages)
      Adds packages to scan for @Route-annotated views.
      Parameters:
      packages - package names to scan
      Returns:
      this extension instance
    • withServices

      public BrowserlessClassExtension withServices(Class<?>... serviceClasses)
      Adds Vaadin Lookup service implementation classes.
      Parameters:
      serviceClasses - service implementation classes to register
      Returns:
      this extension instance
    • withComponentTesterPackages

      public BrowserlessClassExtension withComponentTesterPackages(String... packages)
      Adds extra packages to scan for ComponentTester implementations.
      Parameters:
      packages - package names to scan for testers
      Returns:
      this extension instance
    • withComponentTesterPackages

      public BrowserlessClassExtension withComponentTesterPackages(Class<?>... classes)
      Adds the packages of the given classes to the set of packages to scan for ComponentTester implementations.
      Parameters:
      classes - classes whose packages should be scanned for testers
      Returns:
      this extension instance
    • withApplicationProperty

      public BrowserlessClassExtension withApplicationProperty(String name, String value)
      Sets a Vaadin application property (init parameter) for the tests using this extension.
      Parameters:
      name - the property name
      value - the property value
      Returns:
      this extension instance
      Since:
      25.3
    • withApplicationProperties

      public BrowserlessClassExtension withApplicationProperties(Map<String,String> properties)
      Sets Vaadin application properties (init parameters) for the tests using this extension.
      Parameters:
      properties - the properties to set
      Returns:
      this extension instance
      Since:
      25.3
    • withFeatureFlags

      public BrowserlessClassExtension withFeatureFlags(String... featureIds)
      Enables the given Vaadin feature flags for the tests using this extension.
      Parameters:
      featureIds - the identifiers of the features to enable
      Returns:
      this extension instance
      Since:
      25.3
    • withFeatureFlags

      public BrowserlessClassExtension withFeatureFlags(Feature... features)
      Enables the given Vaadin feature flags for the tests using this extension.
      Parameters:
      features - the features to enable
      Returns:
      this extension instance
      Since:
      25.3
    • withFeatureFlag

      public BrowserlessClassExtension withFeatureFlag(String featureId, boolean enabled)
      Enables or disables the given Vaadin feature flag for the tests using this extension.
      Parameters:
      featureId - the identifier of the feature
      enabled - true to enable the feature, false to disable it
      Returns:
      this extension instance
      Since:
      25.3
    • withFeatureFlag

      public BrowserlessClassExtension withFeatureFlag(Feature feature, boolean enabled)
      Enables or disables the given Vaadin feature flag for the tests using this extension.
      Parameters:
      feature - the feature
      enabled - true to enable the feature, false to disable it
      Returns:
      this extension instance
      Since:
      25.3
    • withConfiguration

      public BrowserlessClassExtension withConfiguration(BrowserlessConfiguration configuration)
      Applies the given custom Vaadin configuration to the tests using this extension.

      The configuration wins over the one declared by a BrowserlessTestConfig annotation on the test class. Since the Vaadin environment is shared by all the tests in the class, a BrowserlessTestConfig annotation on a test method is not supported.

      Parameters:
      configuration - the configuration to apply
      Returns:
      this extension instance
      Since:
      25.3
    • isClassScoped

      protected boolean isClassScoped()
      Tells whether this extension creates a single Vaadin environment shared by all the tests in the class, rather than one per test method.
      Returns:
      true if the Vaadin environment is scoped to the test class, false otherwise
    • beforeAll

      public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext ctx)
      Specified by:
      beforeAll in interface org.junit.jupiter.api.extension.BeforeAllCallback
    • afterAll

      public void afterAll(org.junit.jupiter.api.extension.ExtensionContext ctx)
      Specified by:
      afterAll in interface org.junit.jupiter.api.extension.AfterAllCallback
    • addViewPackages

      protected void addViewPackages(Class<?>... classes)
      Adds packages to scan for @Route-annotated views, derived from the given classes' packages.
      Parameters:
      classes - classes whose packages should be scanned
    • addViewPackages

      protected void addViewPackages(String... packages)
      Adds packages to scan for @Route-annotated views.
      Parameters:
      packages - package names to scan
    • addServices

      protected void addServices(Class<?>... serviceClasses)
      Adds Vaadin Lookup service implementation classes.
      Parameters:
      serviceClasses - service implementation classes to register
    • addComponentTesterPackages

      protected void addComponentTesterPackages(String... packages)
      Adds extra packages to scan for ComponentTester implementations.
      Parameters:
      packages - package names to scan for testers
    • addComponentTesterPackages

      protected void addComponentTesterPackages(Class<?>... classes)
      Adds the packages of the given classes to the set of packages to scan for ComponentTester implementations.
      Parameters:
      classes - classes whose packages should be scanned for testers
    • addApplicationProperty

      protected void addApplicationProperty(String name, String value)
      Sets a Vaadin application property (init parameter) for the tests using this extension.
      Parameters:
      name - the property name
      value - the property value
    • addApplicationProperties

      protected void addApplicationProperties(Map<String,String> properties)
      Sets Vaadin application properties (init parameters) for the tests using this extension.
      Parameters:
      properties - the properties to set
    • addFeatureFlags

      protected void addFeatureFlags(String... featureIds)
      Enables the given Vaadin feature flags for the tests using this extension.
      Parameters:
      featureIds - the identifiers of the features to enable
    • addFeatureFlags

      protected void addFeatureFlags(Feature... features)
      Enables the given Vaadin feature flags for the tests using this extension.
      Parameters:
      features - the features to enable
    • addFeatureFlag

      protected void addFeatureFlag(String featureId, boolean enabled)
      Enables or disables the given Vaadin feature flag for the tests using this extension.
      Parameters:
      featureId - the identifier of the feature
      enabled - true to enable the feature, false to disable it
    • addFeatureFlag

      protected void addFeatureFlag(Feature feature, boolean enabled)
      Enables or disables the given Vaadin feature flag for the tests using this extension.
      Parameters:
      feature - the feature
      enabled - true to enable the feature, false to disable it
    • addConfiguration

      protected void addConfiguration(BrowserlessConfiguration configuration)
      Applies the given configuration on top of the one built so far for the tests using this extension.

      The configuration wins over the one declared by a BrowserlessTestConfig annotation on the test class, but loses against the one declared on the test method.

      Parameters:
      configuration - the configuration to apply
    • doInit

      protected void doInit(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext ctx)
      Sets up the mocked Vaadin environment for a test, with the configuration built by this extension merged with the BrowserlessTestConfig annotations of the test.

      When the test instance is a BaseBrowserlessTest, the environment is initialized through it; otherwise this extension sets it up on its own. Call doCleanup() afterwards to tear it down, also when this method throws.

      Parameters:
      testInstance - the test instance, or null when the extension runs without one
      ctx - the JUnit extension context of the test or test class
    • doCleanup

      protected void doCleanup()
      Tears down the mocked Vaadin environment set up by doInit(Object, ExtensionContext). Does nothing when there is nothing to tear down.
    • find

      public <T extends Component> ComponentQuery<T> find(Class<T> type)
      Gets a query object for finding components of the given type in 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 - component type
      Parameters:
      type - component type to search for
      Returns:
      a component query
    • find

      public <T extends Component> ComponentQuery<T> find(Class<T> type, Component fromThis)
      Gets a query object for finding components nested inside a given component.

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

      Type Parameters:
      T - component type
      Parameters:
      type - component type to search for
      fromThis - starting component for search
      Returns:
      a component query scoped to the given component
    • findInView

      public <T extends Component> ComponentQuery<T> findInView(Class<T> type)
      Gets a query object for finding components 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 - component type
      Parameters:
      type - component type to search for
      Returns:
      a component query scoped to the current view
    • getCurrentView

      public HasElement getCurrentView()
      Gets the current view instance shown in the UI.
      Returns:
      the current view
    • reload

      public 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 survives. Views annotated with @PreserveOnRefresh keep their component instance and state; other views are recreated.
      Returns:
      the view shown after the reload
    • reload

      public <T extends 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
    • roundTrip

      public void roundTrip()
      Simulates a server round-trip: runs the tasks queued with UI.access(), 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(). If a queued task fails, its exception is rethrown wrapped in an ExecutionException.

    • runPendingSignalsTasks

      public boolean runPendingSignalsTasks()
      Processes all pending Signals tasks with a default max wait of 100 milliseconds.

      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. Blocking on operation.result().get() without draining the queue first never completes, because the confirmation task can only run on the thread that calls this method:

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

      public boolean runPendingSignalsTasks(long maxWaitTime, TimeUnit unit)
      Processes all pending Signals tasks, waiting up to the specified timeout for tasks to arrive.
      Parameters:
      maxWaitTime - maximum time to wait for the first task
      unit - time unit for the timeout
      Returns:
      true if any pending Signals tasks were processed
    • fireShortcut

      public void fireShortcut(Key key, KeyModifier... modifiers)
      Simulates a keyboard shortcut performed on the browser.
      Parameters:
      key - primary key of the shortcut
      modifiers - key modifiers
    • activateLocatorContext

      public void activateLocatorContext()
      Description copied from interface: Locators
      Hook for context-bound implementations to install Vaadin thread-locals before a locator is built. Default is a no-op.
      Specified by:
      activateLocatorContext in interface GeneratedLocators
      Specified by:
      activateLocatorContext in interface Locators