Neerhelst

Web automation in java with no browser

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.

<dependency>
    <groupId>net.sourceforge.htmlunit</groupId>
    <artifactId>htmlunit</artifactId>
    <version>2.42.0</version>
</dependency>

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");

        username.type("itsme");
        password.type("mypassword");

        HtmlPage page2 = button.click();
        System.out.println(page2.asXml());
    }

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

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.

About the author

I currently work as a Test Automation Consultant at b.ignited. Here I work for different clients in different industries to help them start and speed up their testing cycles

I’ve been testing software since 2000 when I became involved in testing telephone applications and hardware. Since then, I’ve been able to expand my experience by testing a variety of embedded, web, mobile and desktop applications. I have used various development methodologies from waterfall to agile.

I consider myself to be a lifelong learner.