i

Selenium Step By Step Guide

HtmlUnitDriver and Handling Captchas

We usually do the Selenium test automation on popular web browsers like IE, Chrome, Firefox, Safari. Validating the tests in web browser is clearly visible to the user during the automation run. User can see the test running in browser UI smoothly. But in some cases, the user needs to run the test cases without any GUI, and this means test run should not be visible to the user on any browser, but the test gets executed successfully.

We can achieve this by using "Headless Browsers". A headless browser does not have any GUI, and it works in the background. There are different types of the headless driver available to handle this kind of situation. The most widely used headless driver is "HtmlUnitDriver”.

We can use the “HtmlUnitDriver" latest maven dependencies to get its libraries into our project. We can paste below the dependency in the POM.xml file.

 

  org.seleniumhq.selenium

  htmlunit-driver

  2.34.0

 

There are various dependencies available on maven platform but try to use which comes with htmlunit-driver .

Once the jar is loaded to the project, we can start instantiating the HtmlUnitDriver object to load the URL without any browser UI. Let us see the complete code of the same:

package SeleniumTest.SeleniumTest;

 

import org.openqa.selenium.Alert;

import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.WebDriverWait;

 

 

 

public class HeadlessBrowser

{

    public static void main( String[] args )

    {

        System.setProperty("webdriver.chrome.driver","C:\\Users\\AC42610\\chromedriver.exe");

        WebDriver driver=new HtmlUnitDriver();

        driver.get("https://selflearning.io/");

        System.out.println(driver.getTitle());

 

    }

}

This code prints the console with the text "Self Learning | Online Tutorials | Mock Test | Old Papers | Study Material | Quiz | Video Lectures".

 

Handling Captchas:

To be very frank on this, Captcha should not be automated at all. CAPTCHA is a backronym for "Completely Automated Public Turing test to tell Computers and Human Apart”.

This means that Captcha is a protection against any automated software to avoid the automated loading of web pages. If we do automation of Captcha, then the purpose of having a captcha is itself worthless.

Now the question comes, how to handle this situation for testing such scenarios?

We can have a set of workarounds to handle this situation:

  1. Ask the Development team to disable the Captcha from the Test Environment. Once tested, we can ask rollback for this change.
  2. Or else we can ask the development team to make a temporary change of generating a same predefined text in the captcha section in Test Environment. Once tested, we can ask rollback for this change.
  3. Or else we can adjust out test script to enter Captcha manually while the automated test case is running.

There could be a variety of workaround to handle this situation, but there is one thing that must be taken care that the whole concept of having Captcha is to prevent the web page from any automated tool. So, if we are using point a or b for handling this situation, we must take care to get the rollback to original code to make sure the correct code pushed to production.