Tuesday, 21 June 2016

Headless execution using HTML Unit in Selenium

Headless mean without any user interface. HtmlUnit allows you to execute tests in Headless manner. You will not see browser opening the web pages or performing any operation like clicking buttons or entering data. All these operations happen in memory. This kind of execution is useful if you want to test the functionality. Also headless execution is much faster than normal execution. Downside is that you will not be able to test how the web page actually looks like on specific browser.

You will have to add below dependency in your project.

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>

By default, JavaScript is not enabled. You need to use below line to execute JavaScript.
((HtmlUnitDriver)driver).setJavascriptEnabled(true);

Here is the complete code to execute selenium tests using HtmlUnitDriver.

package htmlunit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

/**
 * Created by Sagar on 21-06-2016.
 */
public class testHtmlUnit {
    public static void main(String[] args) {
        WebDriver driver = new HtmlUnitDriver();
        ((HtmlUnitDriver) driver).setJavascriptEnabled(true);
        driver.get("http://www.softpost.org");
        driver.close();
        driver.quit();
    }
}


What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Buy Best Selenium Books

Contributors