Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take two weeks Trial! Try Now

Headless Browser Testing with Selenium

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 the best Software Testing Services Provider, we like to share a dive deep into this topic.

What Is Headless Browser?

A headless browser 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.

headless browser

The Need for Headless Browser Testing

Drawbacks of Headless Browser Testing

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:

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. The Chromeoptions are used not only to run our tests in headless mode but also to set the various properties/contents of the Chrome driver.

The headless Chrome also helps us with:

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 the 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 the 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:

headless mode

Conclusion

The purpose of the article is to show you how to run our Selenium Test Automation Services in headless mode, which means running our test case on a browser that does not have 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 includes a little modification in your existing test script. Good Luck, Happy Testing!!!!

Software Development Team
Need Software Development Team?
captcha
🙌

Thank you!
We will contact soon.

Oops! Something went wrong.

Recent Blogs

Categories

NSS Note
Some of our clients
team