Run selenium test in headless mode with real Chrome and Firefox

Time has always been a factor to measure the efficiency and effectiveness of our automated test scripts. Being CD/CI a crucial need, its very important to run our test quickly. Best way of it is obviously running your test with headless browsers (e.g. GhostDriver, PhantomJs driver), but we have always seen below issues with these browsers-

  1. Same locators(xpaths, CSS selectors) do not work when test executed on headless browsers
  2. Additional line of code to handle cookies and other issues.
  3. Javascript alerts create problems

And there are many more problems apart from above mentioned. These problems happen as Xpath engine and java-script engine implementation varies from browser to browser and specifically for headless browsers. But what if we run our real intended browsers in headless mode during our test execution. That will obviously solve our above problem.

Selenium always surprises us with some cool new features. This time it has just blown the need for headless browsers like phantomjs etc. Now with selenium version 3.6.0 on-wards you can run your real browsers(chrome and Firefox) test in headless mode.

Now lets look at how to make our browser run in headless mode during our test execution. Please make sure to utilize this feature, you are using selenium version 3.6.0 and above. Lets go through a sample code for google test for Firefox and Chrome respectively.

System.setProperty("webdriver.chrome.driver","Path to chrome driver exe");
ChromeOptions options = new ChromeOptions();
options.setHeadless(true); //this line is actually enables the headless mode
WebDriver driver = new ChromeDriver(options);
driver.navigate().to("https://google.com");
driver.findElement(By.name("q")).sendKeys("hello");
driver.close();

Similarly, we can use FirefoxOptions to enable headless mode for Firefox browser

System.setProperty("webdriver.gecko.driver","Path to gecko driver exe");
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true); //this line is actually enables the headless mode
WebDriver driver = new FirefoxDriver(options);
driver.navigate().to("https://google.com");
driver.findElement(By.name("q")).sendKeys("hello");
driver.close();

Also, its very easy to do your selenium Grid configuration for your remote test execution. please refer to below code snippet.

FirefoxOptions ffoptions = new FirefoxOptions();
ffoptions.setHeadless(true);
RemoteWebDriver driver = new RemoteWebDriver(
        new URL("http://localhost:4444/wd/hub"),
        ffoptions);

I have also done some time comparison with chrome browser headless and non headless mode. Below is time analysis for a simple test which navigate to google and search for some text :

  • Execution time in non-headless mode – 12.193 seconds
  • Execution time in headless mode – 9.321 seconds

This can tremendously reduce execution time when you will execute your large test suites.

Now, its time to say goodbye to your third party headless browsers. Execute the test with real browsers in headless mode.

Cheers, Happy Automating 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s