Class BrowserlessExtension

java.lang.Object
com.vaadin.browserless.BrowserlessExtension
All Implemented Interfaces:
TesterWrappers, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension

public class BrowserlessExtension extends Object implements org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback
JUnit 5 extension for browserless Vaadin testing with per-method lifecycle.

A fresh Vaadin environment is initialized before each test method and torn down after. Use as an instance field with @RegisterExtension:

 
 @ViewPackages(classes = MyView.class)
 class MyTest {
     @RegisterExtension
     BrowserlessExtension ext = new BrowserlessExtension()
             .withServices(MyService.class);

     @Test
     void test() {
         MyView view = ext.navigate(MyView.class);
         ext.test(view.getButton()).click();
     }
 }
 
 

For a shared Vaadin environment across all tests in a class, use BrowserlessClassExtension instead.

See Also:
  • Constructor Details

    • BrowserlessExtension

      public BrowserlessExtension()
      Creates a new extension with per-method lifecycle.
  • Method Details

    • withViewPackages

      public BrowserlessExtension 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 BrowserlessExtension withViewPackages(String... packages)
      Adds packages to scan for @Route-annotated views.
      Parameters:
      packages - package names to scan
      Returns:
      this extension instance
    • withServices

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

      public BrowserlessExtension 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 BrowserlessExtension 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
    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext ctx)
      Specified by:
      beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
    • afterEach

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

      protected void addViewPackages(Class<?>... classes)
    • addViewPackages

      protected void addViewPackages(String... packages)
    • addServices

      protected void addServices(Class<?>... serviceClasses)
    • addComponentTesterPackages

      protected void addComponentTesterPackages(String... packages)
    • addComponentTesterPackages

      protected void addComponentTesterPackages(Class<?>... classes)
    • doInit

      protected void doInit(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext ctx)
    • doCleanup

      protected void doCleanup()
    • 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.
      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.
      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.
      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
    • roundTrip

      public void roundTrip()
      Simulates a server round-trip, flushing pending component changes.
    • runPendingSignalsTasks

      public boolean runPendingSignalsTasks()
      Processes all pending Signals tasks with a default max wait of 100 milliseconds.
      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