Why we write WebDriver driver = new ChromeDriver(); in Selenium Web Driver?

Monil Joshi
3 min readSep 24, 2021

--

In the previous article we saw, what are related interfaces and classes in Selenium Web Driver.

You can read that blog here

What is WebDriver interface & hierarchy of related interfaces and Classes

In our script first line was

System.setProperty(“webdriver.chrome.driver”,”<ChromeDriver_Path>/chromedriver”);
WebDriver driver = new ChromeDriver();

In this article we are going to see why we write like the way we have written and not

WebDriver driver = new WebDriver();
OR
ChromeDriver driver = new ChromeDriver();

WebDriver driver= new WebDriver();
As in the previous blog, we have seen WebDriver is an interface. We cannot create Object of an Interface. If we write code like this we will get compile time error

ChromeDriver driver = new ChromeDriver();
driver.get("https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/WebDriver.html");

Technically this will work. So you can start writing your code with this snippet.

But when we automate any web application, we must cover automation framework for all the supported browsers. It is called as cross browser testing

If you start your script with above statement you are creating instance of ChromeDriver class. As per the java concept if you create an object using New keyword it will initiate constructor of that particular class. We have object of ChromeDriver class and it will only work with Chrome browser

This will create a problem if you want to perform cross browser testing like writing script for different browsers like Firefox, Edge etc. You will have to create another instance of another browser using New keyword like

FirefoxDriver driver_1= new FirefoxDriver();

Instead we can create reference variable of WebDriver interface and cast it to browser class as below

WebDriver driver = new ChromeDriver();

Now you can write your script for any browser. You can simple change the definition statement as

WebDriver driver = new FirefoxDriver();

In this case script will execute without any issue. This driver instance has access to almost all the methods of WebDriver. This happens because every browser extends RemoteWebDriver and RemoteWebDriver implements WebDriver interface.

One more advantage of doing it is we can use same instance of driver with different browsers by quitting earlier session in same script. Below code will work absolutely fine

WebDriver driver;
//Use driver instance to execute test on Chrome browser
System.setProperty(“webdriver.chrome.driver”,”<ChromeDriver_Path>/chromedriver”);
driver = new ChromeDriver();
//test code
driver.quit()
//Use same driver instance to execute test on Firefox browser
System.setProperty("webdriver.gecko.driver", "C:\\Drivers\\geckodriver.exe");
driver = new FirefoxDriver();
driver.quit()

Now you might have one question in your mind? If we are restricted to use method implemented inside WebDriver interface what if we want to use browser specific methods that means method present inside Browser Driver class

We can still use those methods whenever it required for execution on respective browser only. We can do it by casting web driver instance to the browser class driver. For eg. We have launchApp() present only inside ChromeDriver class.

So if we use this method in our code first it will give us compile time error.

Compile Time Error

Complete code will look like this,

WebDriver driver;
//Use driver instance to execute test on Chrome browser
System.setProperty(“webdriver.chrome.driver”,”<ChromeDriver_Path>/chromedriver”);
driver = new ChromeDriver();
//test code
driver.launchApp("")
;driver.quit()

On mouse hover it will give us suggestion to cast driver to ChromeDriver for this particular step

Once we click on suggestion it will cast driver to ChromeDriver class

Complete code will look like this

WebDriver driver;
System.setProperty(“webdriver.chrome.driver”,”<ChromeDriver_Path>/chromedriver”);
driver = new ChromeDriver();
((ChromeDriver) driver).launchApp("");
driver.quit();

Summary is, it is better to create reference of WebDriver interface and cast it to browser driver class than creating separate object of each class

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Monil Joshi
Monil Joshi

Written by Monil Joshi

I am having an 8+ years of experience in software testing. Working with Web, Mobile and API technologies. Visit my GIT https://github.com/monilj

Responses (4)

Write a response