Web automation in java with no browser

Web automation in java

Chrome has a headless option. In that case chrome launches without an interface. There is an other way to do web automation in java with no browser. I explain it.

The system under test

I was creating some tests for a new service. This service can be called with some API. So there is no need to test with selenium. We had a problem when the developers add more features to the system.

The feature we implement interacts with a very old part of the system. That part of the system has only a web page as interface. Our test calls a method on our new service with an API.

Diagram of the working system.

The API returns an ID. That ID is in fact received from the old system. The validation if the message that is created with the test is done in that old legacy system.

This means that the tests need to do some html calls to the old legacy system. Only then the test can see if the message is successfully processed. It is also the only way to check if the content of the message is correct.

htmlunit to rescue

The test need to do some html calls to the legacy system. Headless Chrome together with selenium is an option. I did not wanted to have some slow tests. (Yes selenium is slow). So I searched for alternatives. Then I found htmlunit. A java GUI less browser.

To start using the htmlunit package, just add the following dependency in your pom.xml file.


The implementation in the code looks a lot like selenium. For example to enter a user and password in a form on the login screen can be done like the following code:

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.*;
import java.io.IOException;

public class Example {
    public void login() throws IOException {
        WebClient webClient = new WebClient();
        final HtmlPage page = webClient.getPage("http://my.login.page/");
        HtmlForm form = page.getFormByName("Login");

        HtmlTextInput username = form.getInputByName("username");
        HtmlPasswordInput password = form.getInputByName("password");
        HtmlButton button = form.getButtonByName("submit_login");


        HtmlPage page2 = button.click();

    public static void main(String[] args) throws IOException {
        Example1 example = new Example1();

The reply is in this example printed out as plain xml text. You can check for other items on the page with the type HtmlPage that is received.

Web automation in java with no browser? It is possible with htmlunit. And the implementation is a lot faster than selenium. Try it to see for yourself.