About Me

Page views

Powered by Blogger.

Followers

Thursday 7 May 2015


So What is a headless browser?
Headless browser is a term used to define browser simulation programs which do not have a GUI. These programs behave just like a browser but don’t show any GUI. Famous ones are HtmlUnit and the NodeJs headless browsers. There are a good number of more browsers too.

use of Headless browsers :
1. You have a central build tool which does not have any browser installed on it. So to do the basic level of sanity tests after every build you may use the headless browser to run your tests.
2. You want to write a crawler program that goes through different pages and collects data, headless browser will be your choice. Because you really don’t care about opening a browser. All you need is to access the webpages.
3. You would like to simulate multiple browser versions on the same machine. In that case you would want to use a headless browser, because most of them support simulation of different versions of browsers. We will come to this point soon.      

Selenium support for headless browser
Selenium supports headless testing using its class called HtmlUnitDriver. This class internally uses HtmlUnit headless browser. HtmlUnit is a pure Java implementation so you will not find this tutorial to be focused on java bindings Quick start ! You can create a HtmlUnitWebDriver like this
HtmlUnitDriver unitDriver = new HtmlUnitDriver();
Practice Yourself

public static void main(String[] args) {
        // Declaring and initialising the HtmlUnitWebDriver
        HtmlUnitDriver unitDriver = new HtmlUnitDriver();
        // open google.com webpage
        unitDriver.get("http://google.com");
        System.out.println("Title of the page is -> " + unitDriver.getTitle());
        // find the search edit box on the google page
        WebElement searchBox = unitDriver.findElement(By.name("q"));
        // type in Selenium
        searchBox.sendKeys("Selenium");
        // find the search button
        WebElement button = unitDriver.findElement(By.name("gbqfba"));
        // Click the button
        button.click();
          System.out.println("Title of the page is -> " + unitDriver.getTitle());
    }

Pay attention to before using headless browser
Headless browsers are simulation programs, they are not your real browsers. Most of these headless browsers have evolved enough to simulate, to a pretty close approximation, like a real browser. Still you would not want to run all your tests in a headless browser. JavaScript is one area where you would want to be really careful before using a Headless browser. JavaScript are implemented differently by different browsers. Although JavaScript is a standard but each browser has its own little differences in the way that they have implemented JavaScript. This is also true in case of headless browsers also. For example HtmlUnit headless browser uses the Rihno JavaScript engine which not being used by any other browser.

Java script support in HtmlUnitWebDriver
1. By passing the java script enable flag in constructor
HtmlUnitDriver unitDriver = new HtmlUnitDriver(true);
2. By specifying it using the setJavascriptEnabled method
HtmlUnitDriver unitDriver = new HtmlUnitDriver();
unitDriver.setJavascriptEnabled(true);


Once you have html unit webdriver set for Java script you can execute any script exactly like you would do in any other browser object 
// Declaring and initialising the HtmlUnitWebDriver
HtmlUnitDriver unitDriver = new HtmlUnitDriver();
unitDriver.setJavascriptEnabled(true);
unitDriver.get("http://google.com");
String domainName = (String) unitDriver.executeScript("return document.domain");
System.out.println("Domain name is " + domainName);

0 comments:

Post a Comment

Popular Posts

Copyright © Learn Selenium Yourself | Powered by Blogger
Design by Duan Zhiyan | Blogger Theme by NewBloggerThemes.com