When we work with Selenium 3.x
, geckodriver
and Mozilla Firefox Browser
through Selenium-Java
bindings we need to configure the Test Environment
through System.setProperty
line. Find the details along with your Answer below.
Your Question have 2 parts so I will answer both of them in parts:
1. Do I need to use the below code every time in each and every Program:
System.setProperty("webdriver.gecko.driver","<path to geckodriver.exe>");
WebDriver driver = new FirefoxDriver();
Answer:
Yes
Explaination:
Whenever we need to execute a program (Selenium-Java based) it is mandatory we explicitly mention the type of driver (gecko
, chrome
, ie
) which we are trying to use in our program in the form of "webdriver.gecko.driver"
. Along with it, we also need to explicitly mention the absolute path of the driver (gecko
, chrome
, ie
) binary (.exe
) in the form of "<path to geckodriver.exe>"
. Next we are using the WebDriver
interface and casting the WebDriver
instance to FirefoxDriver
.
2. Do I need to use the below code every time when i want to launch Firefox:
System.setProperty("webdriver.gecko.driver","<path to geckodriver.exe>");
Answer:
No
Explaination:
Once we configure the WebDriver
instance i.e. the driver
through DesiredCapabilities
class, the driver is able to carry the same configuration till its life time which is controled through your Automation Script. So until and unless we explicitly call quit()
method through the driver, the driver instance remains active and carries the configuration. So, within your program no matter how many time you choose to close the browser instance by calling close()
method, you can always mention driver = new FirefoxDriver();
to open a new browser session again and again with the stored configuration within the driver
.
An Example:
package demo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Driver_Close_Initiate
{
static WebDriver driver;
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability("marionette", true);
driver = new FirefoxDriver(dc);
driver.get("https://google.com");
driver.close();
driver = new FirefoxDriver(dc);
driver.get("https://facebook.com");
driver.quit();
}
}