As of now, you may have been running your browser automation test with Selenium, imagining each browser event occurring from your driver content. Typically, we utilize the command "WebDriver driver = new ChromeDriver();" to dispatch a computerized Chrome browser and to get a graphical UI in our framework. What if, we use the same command with just a little modification and when we run our Selenium script, our automated browser doesn’t even launch, however, our tests get fully executed?
Yes, this is possible. As a best Software Testing Services Provider, we like to share a dive deep into this topic.
What Is Headless Browser?
A headless browser is one that works like other browsers, but the only difference is they aren’t visual on a desktop which means there is no graphical user interface (GUI). A headless browser is used to automate the browser without launching the browser. While the tests are running, we could not see the browser but we can see the test results coming on the console.
The Need for Headless Browser Testing
- 1) Headless browser testing is generally faster when compared to actual UI testing since it doesn’t wait for the whole page to render before performing any action.
- 2) The test can even run when there is no real browser installed in the system.
- 3) In the case of parallel testing, if we launch multiple sessions at a time, the testing can be easily done without any human intervention. Equally, the user can perform his/her other tasks simultaneously.
- 4) Few Headless Browsers like Chrome and Firefox supports screenshot functionality.
- 5) In the case of Chrome and Firefox, we can temporarily switch off the headless mode for debugging of Selenium script.
Drawbacks of Headless Browser Testing
- 1) Since the UI isn't noticeable in Headless Browser Testing, the troubleshooting of Selenium content turns into somewhat troublesome.
- 2) Cosmetic bugs commonly known as GUI bugs are not identified.
Selenium Support for Headless Browsers
-
HtmlUnit
Written in Java, we can simulate various browser programs in headless mode. Selenium provides a predefined class “HtmlUnitDriver”. It supports the Htmlunit web browser which is a pure Java headless browser. It also supports HTTP and HTTPS protocols and can freely interact with web pages having submitted forms and click links.
Note: HtmlunitDriver is just not accessible in Selenium adaptation 3 or higher. To use this in your Selenium content, you need to download its jar file.
public class Apple1 {
public static void main(String args[]) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Lenovo-I7\\Desktop\\chromedriver.exe");
WebDriver driver = new HtmlUnitDriver();
driver.get("https://www.apple.com/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[@id=\'ac-globalnav\']/div/ul[2]/li[3]")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("Heading to iPad");
driver.findElement(
By.cssSelector("#chapternav > div > ul > li.chapternav-item.chapternav-item-ipad-air > a")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("Checking iPad Air");
driver.findElement(By.linkText("Why iPad")).click();
System.out.println("iPad information");
driver.quit();
}
-
Disadvantages of HtmlUnitDriver:
- 1. Debugging the script is too complex.
- 2. Doesn't support screenshot functionality
-
Headless Chrome
Before the dispatch of Chrome 59, the headless testing was uniquely on outsider headless browsers, for example, PhantomJS(formally deserted), HtmlUnit, and so on With a possibility for headless in Google Chrome, we get an opportunity to run our test in genuine browsers. To run our Selenium test in Chrome headless, Selenium gives a class called ChromeOptions. Chromeoptions isn't simply used to run our test in headless mode yet in addition to set different properties/contentions of Chrome driver.
The headless Chrome also helps us with:
- Full web crawling
- Taking Screenshot
- Gathering page information
- Handling complex websites
To run the test in headless mode, we just need to add a “--headless” flag using ChromeOptions.
Example:
public class Apple2 {
public static void main(String args[]) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Lenovo-I7\\Desktop\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("window-size=1400,800");
chromeOptions.addArguments("headless");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://www.apple.com/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[@id=\'ac-globalnav\']/div/ul[2]/li[3]")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("Heading to iPad");
driver.findElement(
By.cssSelector("#chapternav > div > ul > li.chapternav-item.chapternav-item-ipad-air > a")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("Checking iPad Air");
driver.findElement(By.linkText("Why iPad")).click();
System.out.println("iPad information");
driver.quit();
}
-
Firefox Headless Mode
With the release of Firefox 56 version, Mozilla offered the support of headless testing. Hence, even with this, we get a chance to run our browser automation test on a real browser. Similar to Chrome, we have FirefoxOptions class to run our test in Firefox headless mode. And, we would be using the same flag “--headless” provided by FirefoxBinary which is further used to handle the properties of Firefox driver.
public class Apple3 {
public static void main(String args[]) throws InterruptedException {
FirefoxBinary firefoxBinary = new FirefoxBinary();
firefoxBinary.addCommandLineOptions("--headless");
System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(firefoxBinary);
FirefoxDriver driver = new FirefoxDriver(firefoxOptions);
driver.get("https://www.apple.com/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[@id=\'ac-globalnav\']/div/ul[2]/li[3]")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("Heading to iPad");
driver.findElement(
By.cssSelector("#chapternav > div > ul > li.chapternav-item.chapternav-item-ipad-air > a")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("Checking iPad Air");
driver.findElement(By.linkText("Why iPad")).click();
System.out.println("iPad information");
driver.quit();
}
Output:
Conclusion:
The basic objective of this article is to show how to run our Selenium Test Automation Services in a headless mode which means executing our test cases on a browser that doesn’t have a GUI. The primary reason to run our tests in headless mode is to perform maximum execution in minimum time and to perform various other tasks on the same machine without unsettling the automated browsers. So, guys give it a try, this just includes little modification in your existing test script. Good Luck, Happy Testing!!!!