Wednesday, March 15, 2017

Launch Firefox browser with Gecko Driver and Selenium 3

As Selenium 3 has launched, firefox browser initialisation became the most common problem among the Selenium testers. Selenium 3 does not support default Firefox with in it.


What is Gecko Driver

Gecko Driver is the link between your tests in Selenium and the Firefox browser. GeckoDriver is a proxy for using W3C WebDriver-compatible clients to interact with Gecko-based browsers i.e. Mozilla Firefox in this case. As Selenium 3 will not have any native implementation of FF, we have to direct all the driver commands through Gecko Driver.



Marionette (the next generation of FirefoxDriver) is turned on by default from Selenium 3. Even if you are working with older versions of Firefox browser, Selenium 3 expects you to set path to the driver executable by the webdriver.gecko.driver.

Click here For more details on Marionette

Lets take example

Code Sample without Gecko Driver and Selenium 3

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestSelenium3 {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
}
}

"java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property;" 





So to Run above code you need to first download Gecko Driver from Github.
Download Gecko Driver

After download extract zip file to some specific folder and copy that path. Now in selenium code set the system property for that Gecko Driver and give path actual Gecko driver as below.


System.setProperty("webdriver.gecko.driver"
geckodriverpath+"geckodriver.exe");
DesiredCapabilities capabilities=DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capabilities);

So new selenium code is as below.



import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestSelenium3 {
public static void main(String[] args) {

System.setProperty("webdriver.gecko.driver","/Users/username/Downloads/geckodriver"); // Set Gecko driver path
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("https://www.google.com");
}
}

Above solution will work for you. Happy testing 😎😎😎.