Automation, Automation Framework, Browser, Functional Testing, Headless browser, Selenium, System Testing, Testing, Web Automation, Web Testing

How to run Selenium tests in headless mode.

Quick, pace, speed are some synonyms which describes us. We , humans, wants pace in each and every act of ours. We want to speed up things as much as we can do. So we try experimenting things as in we try to search for betterment of work, as we are growing in automating things, replacing human acts to programs or machinery.

If I talk about selenium, then the major time which is spent in framework is at execution. And if I go one step more deeper then the initial point where the execution takes time is when it is trying to open browser be it Chrome, IE or any other. So just to wipe out this pain point there is one such browser which works in headless mode, and is compatible with selenium as well.

How can we use it?
Lets talk in code.
private WebDriver driver;
private String baseUrl = "http://www.google.com";
@Before
public void setUp() throws Exception {
System.out.println("Begin Setup");
driver = new HtmlUnitDriver();
driver.get(baseUrl);
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
}

What this piece of code will do is, it will create a webdriver instance for you. And as mentioned the instance would be of HTML UNIT DRIVER.

Lets understand this line-by-line.

private WebDriver driver;

This is when you are making a driver instance.

private String baseUrl = “http://www.google.com”

Here you are calling a web URL, which would act as the base URL for your whole class.

driver = new HtmlUnitDriver();

This is when you are mentioning the name of the browser you want to work with. And other functionality will work as is.

Advantage

Main advantage is you need not to install any driver for this in your OS. It works faster than the usual browsers like Chrome, IE, FF, etc.

Advertisements

2 thoughts on “How to run Selenium tests in headless mode.”

    1. Hi Shailendra,
      HTML driver is not that different from any other browser in terms of usage, so if you want to click, or do some other activity then you need to follow the below code:-
      public static void main(final String[] args) {
      // Create a new instance of the html unit driver
      final WebDriver driver = new HtmlUnitDriver();
      ((HtmlUnitDriver)driver).setJavascriptEnabled(true);

      // Add a URl you want to visit Google
      driver.get(“http://www.google.com”);
      // Find the text input element by its name
      final WebElement element = driver.findElement(By.name(“q”));
      element.submit(); //use submit
      element.click(); //use click

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s