Headless browser testing in selenium using HTMLUnitDriver

Knoldus Blog Audio
Reading Time: 3 minutes

Selenium is being used by many major organizations across the world. Using selenium we can automate the GUI and see it run in real-time. But what if we just want to execute our tests without actually opening the web browser and the website or the web application? In this blog, we will see how we can do that using Headless browser testing in selenium using HTMLUnitDriver.

Why Headless browser testing?

  • Headless browser testing is useful when we integrate our tests with the CI/CD pipelines where the test scripts run automatically without any human intervention.
  • In this scenario, the web browser doesn’t need to be launched because the CI/CD pipelines are run automatically at the night or in early the morning when no one is there to see the execution.
  • When we run our tests on web browsers it requires resources (RAM, GPU) to be available for executing the tests.
  • Even if we are running a single test on a single browser it requires a substantial amount of resources. And if we run our tests in parallel then it may result in slower execution of tests.
  • To overcome this issue we can execute our tests in a headless browser where the execution takes place without actually opening the browser. Which is much less hungry for system resources.
  • The results are almost the same but without utilising the GPU of the system.

Advantages Headless browser testing

  • The execution time is much less as compared to normal test execution. This saves a lot of time when the number of tests is more.
  • Headless mode is also useful when integrating your tests with the CI/CD pipelines, for automated execution of the tests. As it does not require the GUI for the execution of the tests.
  • The overall performance of the tests is also better, as the overhead of opening the web browser is removed.

Disadvantages Headless browser testing

  • We cannot always use headless mode for execution. As to check the overall user experience from the end-users point of view we need to execute the tests the normal way.
  • If the tests are failing because of some issues, it is difficult to debug the issues in headless mode. Normal execution is required in these scenarios.
  • In some cases, we might encounter a bug that is only reproducible in headless mode. These types of bugs will have not have much impact on the product.

Setting up the project and dependencies.

  • Create a new maven project using the IDE
  • Go to the POM.xml file and add the following dependencies.
<dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>htmlunit-driver</artifactId>
            <version>2.47.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.14.0</version>
        </dependency>
    </dependencies>
  • Create a new java class file end enter the following code.
public class SeleniumHeadlessTest {
    private static Logger log =  LogManager.getLogger(SeleniumHeadlessTest.class);
    @Test
    public void HeadlessTest() {
        WebDriver driver = new HtmlUnitDriver(true);
        //Initialize webdriver
        driver.get("https://login.salesforce.com/?locale=in");
        //Enter the URL to hit.
        log.debug(driver.getTitle());
        //Print the title of the webpage
        log.debug(driver.getCurrentUrl());
        //Print the URL of the current webpage
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        //Add wait for uninterrupted test execution in case of long load times
        driver.findElement(By.cssSelector("#username")).sendKeys("sample@gmail.com");
        //Find and enter text in username field
        driver.findElement(By.cssSelector("#password")).sendKeys("password");
        //Find and enter text in password field
        driver.findElement(By.cssSelector("#Login")).click();
        //Find and hit login button
        log.debug(driver.findElement(By.id("error")).getText());
        //Print error message after unsuccessful login attempt
        driver.close();
        //Close the web browser
    }
}

References

Techhub template


Knoldus-blog-footer-image

Written by 

Ankur is a QA Consultant having experience of more than 1.5 years. He is familiar with the core concepts of manual and automation, postman and Newman are his expertise. He is always eager to learn new and advance concepts in order to expand his horizon and apply them in project development with his existing knowledge. His hobbies include watching web series and getting to know about all the latest gadgets that come in the market.

Leave a Reply