driver = new FirefoxDriver();

Understanding Webdriver driver = new FirefoxDriver();

Last updated on

Hello Friends! while doing UI automation using Selenium WebDriver, the very first line of code that we see to launch a browser is-

WebDriver driver = new FirefoxDriver();


Depending on the type of browser we want to launch the browser-specific driver changes to ChromeDriver(), InternetExplorerDriver(), etc. But have you ever noticed that why do we need to create a reference variable of type WebDriver? What if we directly instantiate the browser-specific driver like-

FirefoxDriver driver = new FirefoxDriver();


Is the above line of code incorrect? If not then what’s the benefit of creating WebDriver type reference variable. In this post, we will answer all these questions in detail. But first of all, let’s see what is WebDriver.

What is WebDriver?

WebDriver is an interface provided by Selenium WebDriver. As we know that interfaces in Java are the collection of constants and abstract methods(methods without any implementation). The WebDriver interface serves as a contract that each browser-specific implementation like ChromeDriver, FireFoxDriver must follow. The WebDriver interface declares methods like get(), navigate(), close(), sendKeys() etc. and the developers of the browser specific drivers implement these methods to get the stuff automated.
Take for example the ChromeDriver, it is developed by the guys from Chromium team, the developers of the Selenium project don’t have to worry about the implementation details of these drivers.

Is this correct – WebDriver driver = new WebDriver();?

No, the above statement is incorrect as WebDriver is an interface and in java we cannot instantiate an interface. Also, logically speaking, saying we want to launch WebDriver doesn’t make any sense as we haven’t specified any particualr browser that needs to be launched.

Is this correct – FirefoxDriver driver = new FirefoxDriver();?

Yes, it is perfectly correct. FirefoxDriver is an implementing class of WebDriver interface and the above statement will launch the Firefox browser.
Now, the question arises, why do we need to create a reference variable of type WebDriver?

Benefit of WebDriver driver = new FirefoxDriver();

Having a reference variable of type WebDriver allows us to assign the driver object to different browser specific drivers. Thus allowing multi-browser testing by assigning the driver object to any of the desired browser. For details check our tutorial on Multi browser testing using Selenium.

1 thought on “Understanding Webdriver driver = new FirefoxDriver();”

Leave a Comment