Hello Welcome to Selenium tutorial, today we will see how to perform Headless browser Testing using Selenium HtmlUnitDriver.
Headless refer which we can’t see on UI.
If you have ever worked on Firefox, Chrome, IE browser in Selenium you might have faced so many issues like xpath is changing everytime when a new feature is getting added.Using these browsers, some common issue which I faced during my daily automation like Sync issues and speed as well. Some browser takes the time to load in my experience I faced performance issues with IE browser.
To increase the speed of your test script i.e. performance of your script we can try some testcases using Headless browser Testing using Selenium.
What is Headless testing/Headless browser in Automation?
Advantage and Disadvantage of headless browsers or Why should I use this?
How to perform Headless testing in Selenium Webdriver using HTMLUnitDriver.
What are the common issues or limitation while working with Headless browsers?
So let us discuss each topic separately.
What is Headless testing/Headless browser in Automation?
Ans-A browser, which does not have any GUI it means which runs in the background. If you run your programs in Firefox, Chrome, IE and the different browser then you can see how the browser is behaving but in headless browsers, you cannot .
Advantage and Disadvantage of headless browsers or Why should I use this?
Ans-One of the most Important advantage is Performance.
1-When we run your test case using headless browsers then you will get the result just in seconds, you will see the result in below program.
2-When we have to run a test case to create some sample test data or just you have to verify some messages and functionality then you can try headless browsers.
3- When we have to run the test case on the remote machine or server, which does not have any browser, but still you have to execute test case then you can try with headless browsers again.
I hope you get the clear picture of this so let us start with some program and output as well.
There is so many headless browsers available in the market, which does the same like Nodejs etc.
When you build your test case using Jenkins then also it runs in Headless mode.
Here is the quick video for Headless testing using HTMLUNITDriver
Headless browser Testing using Selenium
To implement Headless testing selenium have inbuilt class known as
HtmlUnitDriver like other browsers like FirefoxDriver, ChromeDriver etc.
You can find the official https://code.google.com/p/selenium/wiki/HtmlUnitDriver
Before moving to the program, you should have the setup ready. If you have not done setup then no worry use download and Install
Java, Eclipse, Selenium Webdriver Jars, Latest Release
Program-Headless browser Testing using Selenium
package com.bog.htmldemo; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.htmlunit.HtmlUnitDriver; public class HtmlDemoProgram1 { public static void main(String[] args) throws InterruptedException { // Declaring and initialize HtmlUnitWebDriver WebDriver driver = new HtmlUnitDriver(); // open facebook webpage driver.get("http://www.facebook.com"); // Print the title System.out.println("Title of the page "+ driver.getTitle()); // find the username field WebElement username = driver.findElement(By.id("email")); // enter username username.sendKeys("mukeshotwani.50@gmail.com"); // find the password field WebElement password = driver.findElement(By.id("pass")); // Click the loginbutton password.sendKeys("pjs@903998"); // find the Sign up button WebElement Signup_button = driver.findElement(By.id("loginbutton")); // Click the loginbutton Signup_button.click(); // wait for 5 second to login Thread.sleep(5000); // You will get new title after login System.out.println("After login title is = " + driver.getTitle()); }}
Output
Title of the page Welcome to Facebook – Log In, Sign Up or Learn More
After login, title is = Facebook
In above program, you will notice it will take only 3-4 seconds to give you the result but if try the same in other browsers it will take 15-20 seconds.
Javascript supports in the HtmlUnitDriver
All the browser which we are using they use separate javascript engine, but HTMLUnitDriver use Rhino engine so if you will test some javascript applications then chances are high you will get diff results.
By default, JavaScript is disabled in HTMLUnitDriver so you have to enable it while writing the script.
Two ways to enable javascript in HTMLUnitDriver
First- setJavascriptEnabled method to true
WebDriver driver = new HtmlUnitDriver(); driver.setJavascriptEnabled(true);
Second- While initializing browser itself you can enable.
HtmlUnitDriver driver = new HtmlUnitDriver(true);
Thanks for visiting my blog. Keep in touch.
Have a nice day 🙂
Kavya Chitra says
Hi Mukesh,
I am using HtmlUnit for headless browser, it is not detecting some Elements like Button. Whereas with WebDriver the test is executing. Plz help me on this.
Mukesh Otwani says
Can you try Phantom JS ?
Hari says
Hi,
Nice article and it is very useful for all selenium testers.
I executed my code as per your suggetions with HtmlUnitDriver. Thank you very much
Mukesh Otwani says
Hi Hari,
Great…:)
Sue says
Selenium3.01 does not have the htmlunitdriver.jar , where can i find the jar?
Mukesh Otwani says
Hi Sue,
HtmlUnitDriver was a part of Selenium main distribution package prior to Selenium version 2.53. From 2.53 version, you need to add it separately.
Lae says
Hi Mukesh,
On you script everything appears fine. But, on my package Test;. It’s seem to have an error. It wanted for me to import The type org.openqa.selenium.HasInputDevices cannot be resolved. It is indirectly referenced from required .class .
Now, what is that?
Mukesh Otwani says
Hi Lae,
I have not used HasInputDevices in my code.
Suman says
Hi Mukesh,
I am using Angular JS application and I am trying to fetch the data from table then it is showing error message saying that “Element not visible”.
Can you please help me on this.
Mukesh Otwani says
Hi Suman,
Below post will guide you http://learn-automation.com/solve-elementnotvisibleexception-in-selenium-webdriver/
Naresh says
Hi Mukesh,
I am using phantomjs for Headless browser testing.
Q: How handle the alert message in headless browser ?
Mukesh Otwani says
Hi Naresh,
You can handle using normal way http://learn-automation.com/handle-alert-in-selenium-webdriver/
Rajkumar S says
Nice Article Mukesh, it is to the point and simple and straight.
Mukesh Otwani says
Thanks Rajkumar, For headless kindly visit phantomjS as well.
Naveen says
Hi Mukesh,
When I tried to print result page in google using following code.it is not printing any value. please suggest any changes it current code
DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
capabilities.setBrowserName(BrowserType.FIREFOX);
HtmlUnitDriver driver = new HtmlUnitDriver(capabilities);
driver = new HtmlUnitDriver(capabilities);
driver.setJavascriptEnabled(false);
driver.get(“http://www.google.com”);
WebElement element = driver.findElement(By.name(“q”));
element.sendKeys(“uber”);
element.submit();
WebElement webElement = driver.findElement(By.xpath(“/html”));
// WebElement webElement = driver.findElement(By.xpath(“/html”));
String str=webElement.getAttribute(“outerHTML”);
// String str= webElement.getAttribute(“outerHTML”);
System.out.println(str);
output:
null
Thanks
Naveen
Mukesh Otwani says
Hey Naveen, its working from my side. Which Selenium version u r using?
Pradeep says
I am getting the same results. basically returns null.
I am using following dependencies:
org.seleniumhq.selenium
selenium-server
3.0.0-beta3
org.seleniumhq.selenium
selenium-java
3.0.0-beta3
org.seleniumhq.selenium
selenium-support
3.0.0-beta2
org.seleniumhq.selenium
selenium-htmlunit-driver
2.52.0
Please suggest….
Mukesh Otwani says
Hi Pradeep,
Remove all and use only
org.seleniumhq.selenium
selenium-java
2.53.1
Cyril says
Hey, I’ve Selenium-Java-2.53.1 but HtmlUnitDriver is not recognized. Can you help me ?
Mukesh Otwani says
Hey Cycil,
Kindly use selenium server standalone 2.53.1 and then try the same program http://selenium-release.storage.googleapis.com/index.html?path=2.53/
Kindly check and let me know.
Cyril says
http://image.noelshack.com/fichiers/2016/41/1476102828-bugunitdriver.png
On this link you will see the rendering my program
Mukesh Otwani says
Hey Cyril,
You can download below jar to make your program work https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-htmlunit-driver/2.52.0
Arpitha says
hi Mukesh,
I am facing problem in headless browser testing. The application is built using Angular JS, when i do the headless testing for the application – the script will launch the browser but it fails to load the page, as i can see in the logs “unable to locate web element “. Could you please help me in resolving this.
Mukesh Otwani says
Hi Arpitha,
Use protractor for Angular JS application
Swatik Panda says
Hi MUkesh,
Thanks for this fine article on Headless browser .
I am getting following error
driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_38);
((HtmlUnitDriver)driver).setJavascriptEnabled(true);
Output:
———-
Mar 28, 2016 11:38:38 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Invalid base url: “/”, ignoring it
Mar 28, 2016 11:38:39 AM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError
SEVERE: runtimeError: message=[An invalid or illegal selector was specified (selector: ‘*,:x’ error: Invalid selector: *:x).] sourceName=[https://stage.xyz.net/sf/js/jquery/jquery.js] line=[4] lineSource=[null] lineOffset=[0]
Mar 28, 2016 11:38:39 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Invalid base url: “/”, ignoring it
Mar 28, 2016 11:38:39 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Invalid base url: “/”, ignoring it
Mar 28, 2016 11:38:40 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Invalid base url: “/”, ignoring it
Mar 28, 2016 11:38:40 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Invalid base url: “/”, ignoring it
Mar 28, 2016 11:38:40 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Invalid base url: “/”, ignoring it
Note : I have disabled all the listners in xml .
Mukesh Otwani says
Hi Swatik,
It seems some dependency is missing.
Please add all jars and from seleniumhq and run test again.
Tony says
any limitation of HTMLunitTest Driver it may be? i.e. support the same behavior of IE, Firefox or Chrome? thanks the sharing.
Mukesh Otwani says
Hi Tony, it works for simple scenario. Try Phantom JS that will work like other browsers.
Poojitha says
Hi Mukesh,
Great work and good explanation looking forward for any additional information
Mukesh Otwani says
Hi Poojitha,
Thank you 🙂 Keep visiting
surekha says
Please Help me how to use htmlunitdriver using python language.
Mukesh Otwani says
Hi Surekha,
I have not worked on python so not sure on this 🙁
Nitin says
Yo Man! You Rock My World!~! Am Ain’t MJ!! 😉 🙂
Mukesh Otwani says
Thanks Nitin
Madiraju Krishna Chaitanya says
Hi Mukesh Ji,Nice Program.I want to highlight a small correction in the comments section in the above program that you have written.In the Line Number: 33 you have given it as “// Click the loginbutton” for the password being entered code, written below(Line Number: 34).Please correct the comment something as “enter password”. The rest of the is working fine, as designed.Thank You for your valuable time and for sharing your knowledge and experience.
Mukesh Otwani says
Thanks Chaitanya ji I will make the changes 🙂
Madiraju Krishna Chaitanya says
Hi Mukesh Ji, Nice Article.It was very informative.I could grasp the concept very easily by following the video and also the code provided.Thank You.Please keep up the Good Work.
Mukesh Otwani says
Hi Chaitanya,
Thank you your comments always keep motivating me 🙂 Please share with others too 🙂