Class BrowserlessClassExtension

java.lang.Object
com.vaadin.browserless.BrowserlessClassExtension
All Implemented Interfaces:
com.vaadin.browserless.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.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new extension with per-class lifecycle.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
     
    protected void
     
    protected void
    addServices(Class<?>... serviceClasses)
     
    protected void
    addViewPackages(Class<?>... classes)
     
    protected void
    addViewPackages(String... packages)
     
    void
    afterAll(org.junit.jupiter.api.extension.ExtensionContext ctx)
     
    void
    beforeAll(org.junit.jupiter.api.extension.ExtensionContext ctx)
     
    protected void
     
    protected void
    doInit(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext ctx)
     
    <T extends com.vaadin.flow.component.Component>
    com.vaadin.browserless.ComponentQuery<T>
    find(Class<T> type)
    Gets a query object for finding components of the given type in the UI.
    <T extends com.vaadin.flow.component.Component>
    com.vaadin.browserless.ComponentQuery<T>
    find(Class<T> type, com.vaadin.flow.component.Component fromThis)
    Gets a query object for finding components nested inside a given component.
    <T extends com.vaadin.flow.component.Component>
    com.vaadin.browserless.ComponentQuery<T>
    findInView(Class<T> type)
    Gets a query object for finding components 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.
    com.vaadin.flow.component.HasElement
    Gets the current view instance shown in the UI.
    <T extends com.vaadin.flow.component.Component>
    T
    navigate(Class<T> target)
    Navigates to the given view class.
    <C, T extends com.vaadin.flow.component.Component & com.vaadin.flow.router.HasUrlParameter<C>>
    T
    navigate(Class<T> target, C parameter)
    Navigates to the given view class with a URL parameter.
    <T extends com.vaadin.flow.component.Component>
    T
    navigate(Class<T> target, Map<String,String> parameters)
    Navigates to the given view class with route parameters.
    <T extends com.vaadin.flow.component.Component>
    T
    navigate(String location, Class<T> expectedTarget)
    Navigates to the given location string and verifies the expected target.
    void
    Simulates a server round-trip, flushing pending component changes.
    boolean
    Processes all pending Signals tasks with a default max wait of 100 milliseconds.
    boolean
    runPendingSignalsTasks(long maxWaitTime, TimeUnit unit)
    Processes all pending Signals tasks, waiting up to the specified timeout for tasks to arrive.
    Adds the packages of the given classes to the set of packages to scan for ComponentTester implementations.
    Adds extra packages to scan for ComponentTester implementations.
    withServices(Class<?>... serviceClasses)
    Adds Vaadin Lookup service implementation classes.
    withViewPackages(Class<?>... classes)
    Adds packages to scan for @Route-annotated views, derived from the given classes' packages.
    withViewPackages(String... packages)
    Adds packages to scan for @Route-annotated views.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.vaadin.browserless.TesterWrappers

    popoverFor, popoverFor, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test, test
  • 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
    • 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)
    • 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 com.vaadin.flow.component.Component> com.vaadin.browserless.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 com.vaadin.flow.component.Component> com.vaadin.browserless.ComponentQuery<T> find(Class<T> type, com.vaadin.flow.component.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 com.vaadin.flow.component.Component> com.vaadin.browserless.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 com.vaadin.flow.component.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(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
      modifiers - key modifiers