What Is HtmlUnitDriver?
HtmlUnitDriver is headless browser you could launch with Selenium Web Driver, meaning that it’s a browser without a user interface. Such browsers have many advantages, like taking less resources, since they don’t have to render the web elements visually on the screen. Although some pages may not render correctly in such browsers, they tend to work well most of the time.
In this tutorial, we will look on the various ways to add HtmlUnitDriver to your project, and how to launch it with various configurations. It’s worth noting that HtmlUnitDriver is one of multple ways of launching headless browser. We will look at other ways to launch these browsers thorough the series. ☇
Quickly go to:-
Advantages Of HtmlUnitDriver
- Lightweight & runs faster for most of the tests.
- Takes less recourses, since it won’t need to render the GUI.
- Can run more instances using the same resources, compared to browsers with GUI.
- Supports Java script
- Can run known browsers, such as Firefox, Chrome & Edge.
Disadvantages Of HtmlUnitDriver
- Not having a rendered GUI can cause issues sometimes.
- Sometimes it’s hard to fix its issues, specially with our inability to see the screen.
- Codes written for web driver with GUI may not always work on HtmlUnitDriver right away.
Adding HtmlUnitDriver To Your Project
To use the HTML Unit browser, simply add the Maven or Gradle dependency to your project. On Maven, you simply add the following dependency to your pom.xml file:-
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-htmlunit-driver</artifactId> <version>2.52.0</version> </dependency>
If you use Gradle, add the following dependency to your build.gradle file:-
compile group: 'org.seleniumhq.selenium', name: 'selenium-htmlunit-driver', version: '2.52.0'
Launching Htmlunitdriver Using Java
Once we added HtmlUnitDriver to our project, We can launch browser instances with it, which is done by creating an instance of the class HtmlUnitDriver. We can interact with it like any other WebDriver instance. For a starter, we can use the getTitle() function to get the page title & print it to console, since that’s the easiest way to make sure the page was loaded correctly:-
WebDriver HTMLUnitdriver = new HtmlUnitDriver(); HTMLUnitdriver.get("http://yahoo.com/"); String PageTitle = HTMLUnitdriver.getTitle(); System.out.println(PageTitle);
Htmlunitdriver Options
You can specify what browser HTML Unit launches in the constructor. We do that for Firefox here:-
WebDriver HTMLUnitdriver = new HtmlUnitDriver(BrowserVersion.FIREFOX);
HTML Unit has JavaScript is disabled by default, but you can enable it in the constructor:-
WebDriver HTMLUnitdriver = new HtmlUnitDriver(true);
You can enable JavaScript & specify a browser at the same time in the constructor:-
WebDriver HTMLUnitdriver = new HtmlUnitDriver(BrowserVersion.FIREFOX, true);
And Finally
I hope you liked my tutorial has helped you learn more about HtmlUnitDriver, and see you again in another one.
See Also:-
-
- Introduction to selenium web driver series. What is it? And what browsers & programming languages it supports – Tech Fairy
- What are 60% Keyboards (65%)? And what are their advantages & disadvantages – Tech Fairy
- 40% Keyboards Form Factor, What are they? And why would you use one? – Tech Fairy
- Why ThinkPad laptops are popular, and what are their advantages?
- What are business-grade laptops? What are their advantages? And which one to buy?
- Java VS Python VS C# detailed comparison, which language to learn first?
- Do you need an Expensive motherboard for gaming?
- What is the meaning of the different USB port colors? (blue, teal blue, yellow, red, black)
- Why motherboards & laptops still come with USB 2.0 ports When USB 3.0 Is Backward Compatible?
- USB 2.0 VS USB 3.0 Comparison: What are the differences between the two ports?