Docs

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. For Java EE/CDI injection, use the CDI setup; for Quarkus, use the Quarkus setup. BrowserlessTest alone creates the Vaadin environment, not an application dependency injection container.

Dependencies

Add the browserless-test-junit6 dependency with a test scope. Assuming you have imported the Vaadin Bill-of-Materials (BOM) and have a Maven project, all you need is:

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

No other test framework dependencies are required.

Writing Tests

Create this view, then place its test in the same Java package under src/test/java:

Source code
HelloWorldView.java
@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:

Source code
Java
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());
    }

}

Use navigate(), find(), and test() for interactions as shown above.

Use an Extension Instead of a Base Class

If your test already extends another class, register an instance of BrowserlessExtension. Use the same dependency as above.

Source code
Java
@ViewPackages(classes = CartView.class)
class CartViewTest {

    @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());
    }
}

Use 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 ID cart-size. For lifecycle and configuration options, see JUnit 6 Extensions.

Run the Test

Run the test from your IDE or with mvn test. Continue with Test User Interactions.

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

Updated