Docs

Documentation versions (currently viewingVaadin 25)
Documentation translations (currently viewingEnglish)

Set Up Browserless Tests in Plain Java

Configure browserless tests in a plain Java project using a base class or a JUnit extension.

Use this setup when the view and its collaborators can be constructed without a dependency injection container. Tests extend BrowserlessTest, which creates the Vaadin environment but no dependency injection container. For Java EE/CDI injection, use the CDI setup; for Quarkus, use the Quarkus setup.

Add Dependencies

Assuming you’ve imported the Vaadin Bill-of-Materials (BOM) and have a Maven project, add the browserless-test-junit6 dependency with a test scope:

Source code
XML
<dependency>
    <groupId>com.vaadin</groupId>
    <artifactId>browserless-test-junit6</artifactId>
    <scope>test</scope>
</dependency>

No other test framework dependencies are required.

Create a Test

Create this view:

Source code
HelloWorldView.java
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.Route;

@Route("")
public class HelloWorldView extends HorizontalLayout {

    TextField name;
    Button sayHello;

    public HelloWorldView() {
        name = new TextField("Your name");
        sayHello = new Button("Say hello");
        sayHello.addClickListener(e -> {
            Notification.show("Hello " + name.getValue());
        });
        add(name, sayHello);
    }
}

Create a test class that extends BrowserlessTest, in the same Java package as the view under src/test/java:

Source code
HelloWorldViewTest.java
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import com.vaadin.browserless.BrowserlessTest;
import com.vaadin.flow.component.notification.Notification;

class HelloWorldViewTest extends BrowserlessTest {

    @Test
    public void setText_clickButton_notificationIsShown() {
        final HelloWorldView helloView = navigate(HelloWorldView.class);

        test(helloView.name).setValue("Test");
        test(helloView.sayHello).click();

        Notification notification = find(Notification.class).single();
        Assertions.assertEquals("Hello Test", test(notification).getText());
    }

}

See How a Browserless Test Works for what each line of the test does.

Run the Test

Run the test from your IDE or with mvn test. The test passes when entering a name and clicking the button shows the expected notification.

Use an Extension Instead of a Base Class

If your test class already extends another class, register an instance of BrowserlessExtension instead. It’s included in the same dependency.

Source code
CartViewExtensionTest.java
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import com.vaadin.browserless.BrowserlessExtension;
import com.vaadin.browserless.ViewPackages;

@ViewPackages(classes = CartView.class)
class CartViewExtensionTest {

    @RegisterExtension
    BrowserlessExtension ext = new BrowserlessExtension();

    @Test
    void addItemToCart() {
        ext.navigate(CartView.class);
        ext.findButton().withText("Add to cart").click();

        Assertions.assertEquals("1 item",
                ext.findSpan().withId("cart-size").getText());
    }
}

Call the methods on the extension instance to navigate and interact with components. The example assumes a CartView with an Add to cart button and a span with the ID cart-size that shows the number of items. For lifecycle and configuration options, see JUnit 6 Extensions.

Next Steps

D68CAC9E-6131-45C9-84E6-6D1CA1E44E81