Using Headless Mode
Headless mode is a very useful way to run Firefox for automated testing with Selenium WebDriver. In headless mode, Firefox runs as normal only you don't see the UI components. This makes Firefox faster and tests run more efficiently, especially in the CI (Continuous Integration) environment.
We can run Selenium tests in headless mode by configuring the FirefoxOptions class, as shown in the following code snippet:
@BeforeMethod
public void setup() {
System.setProperty("webdriver.gecko.driver",
"./src/test/resources/drivers/geckodriver 2");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setHeadless(true);
driver = new FirefoxDriver(firefoxOptions);
driver.get("http://demo-store.seleniumacademy.com/");
}
In the preceding code, we first created an instance of the FirefoxOptions class, called the setHeadless() method, that passes the value as true to launch the Firefox browser in headless mode. You will see a long message indicating the browser instance has been launched in headless mode, as shown in the following console output:
1532194389309 geckodriver INFO geckodriver 0.21.0
1532194389317 geckodriver INFO Listening on 127.0.0.1:21734
1532194390907 mozrunner::runner INFO Running command: "/Applications/Firefox.app/Contents/MacOS/firefox-bin" "-marionette" "-headless" "-foreground" "-no-remote" "-profile" "/var/folders/zr/rdwhsjk54k5bj7yr34rfftrh0000gn/T/rust_mozprofile.DmJCQRKVVRs6"
*** You are running in headless mode.
During the execution, you will not see the Firefox window on the screen but the test will be executed in headless mode.