why webdriver driver = new firefoxdriver()

Most of our selenium automation script starts with the line

WebDriver driver = new FirefoxDriver();

We will find out the answer why the above statement, but let’s talk about few questions for better understanding.

A question, why not WebDriver driver = new WebDriver();?

Answer is – As per the Selenium WebDriver docs,  WebDriver is an interface and we can not create instance of an interface.

Another question, why not FirefoxDriver driver = new FirefoxDriver();?

This is allowed, but problem is that with this above statement, we can only invoke and act on firefox browser, for other browsers we have to specifically create individual objects like ChromeDriver driver = new ChromeDriver();

FirefoxDriver driver = new FirefoxDriver();

driver = new ChromeDriver(); //NOT allowed.

Why WebDriver driver = new FirefoxDriver();?

In reality, all the browser drivers like FirefoxDriver, ChromeDriver, IEDriver etc implemented WebDriver interface (infact RemoteWebDriver class implements WebDriver interface and browser drivers extends RemoteWebDriver), so if we will use WebDriver driver, then we can use driver (as common object variable) for all browsers we want to automate.

WebDriver driver = new FirefoxDriver();

driver = new ChromeDriver(); //allowed

driver = new SafariDriver(); //allowed


Content posted is based on learning or working experience,
Share the post with others if find useful,
Any queries?, leave comments or discuss on our facebook:qavalidation.com, Thanks!

Leave a Reply

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