+91 9790664230
kumar.arun211@gmail.com

Selenium headless mode tests on Chrome and Firefox

Blog to learn python and selenium

Selenium headless mode tests on Chrome and Firefox

In this short article lets experiment on how users can run Selenium headless tests on Chrome and Firefox browser headless mode.

Before Selenium headless browser usage, user should know browser options that Selenium provides. Below is brief introduction.

Options in Chrome and Firefox browser provides mechanism that we can use to customize and configure a Chrome browser and Firefox browser session.

In case of Chrome browser, users can create an instance of Options(python) or ChromeOptions(java), which has convenient methods for setting ChromeDriver specific capabilities. And this Options/ChromeOptions object can be passed into the ChromeDriver constructor.

Syntax: webdriver.Chrome(chrome_options=OptionsObject);

In case of Firefox browser, users can create an instance of Options(python) or FirefoxOptions(java), which has convenient methods for setting FirefoxDriver specific capabilities. And this Options/FirefoxOptions object can be passed into the FirefoxDriver constructor.

Syntax: webdriver.Firefox(firefox_options=OptionsObject);

While configuring driver sessions using options, we need to tell the driver instance to run in headless mode. The ‘headless’ option will tell to Google Chrome/Firefox to execute the browser in headless mode.

Chrome Example:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options();
options.add_argument("--headless");

driver = webdriver.Chrome(chrome_options=options);
driver.get("http://www.google.com/");
print driver.title;
driver.quit();

Firefox Example:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options();
options.add_argument("--headless");

driver = webdriver.Firefox(firefox_options=options);

driver.get("http://www.google.com/");
print driver.title;
driver.quit();

In both examples, we are invoking headless browsers i.e. no browser is actually displayed, loading URL and printing title of the browser window.

Headless running mode supports all the commands available in WebDriver. Apart from display, there is no difference in headless execution mode.

For example, taking screenshots would work with the same approach that we use it when we run our scripts in non-headless mode.

Before this support, third-party headless browsers like PhantomJS, SlimerJS, TrifleJS, Nightmare, and HTMLUnit are used to execute headless tests.

The “problem” is that these headless browsers just emulate some engines.

Note:

  • Passing capabilities to ChromeDriver constructor is Deprecated n WebDriver 3.6.0
  • Google Chrome shipping headless from version 59
  • Headless is available on Mac and Linux OS from Chrome 59. Windows support is coming soon!

This is all about starting with Headless Browser testing with Selenium using Chrome and Firefox. Please feel free to share your feedback with us using the comments section.

 

One Response

  1. Kashyap says:

    Hello,
    I was wondering how to achieve mouse hover event on headless browser for testing purpose

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2018 Copyrights. All Rights Reserved. Arunkumar Velusamy