If you have given Selenium interviews, you must have faced the question which signifies the title of this post. Being a Selenium enthusiast, you must know the logic behind writing WebDriver driver = new ChromeDriver() or new FirefoxDriver() or new IEDriver(), etc. If it’s an authentic question then it will be asked everywhere, even in normal conversation among new Selenium enthusiasts and you should be capable enough to answer this.
One thing you should keep in mind that if I am writing WebDriver driver = new ChromeDriver() as the title of this post, then it doesn’t mean that this post is only applicable to ChromeDriver, instead, it is applicable to other driver classes as well, like FirefoxDriver(), SafariDriver(), IEDriver(), etc.
We will decode this code today. 😀
Recommended Readings: Must read changes in Selenium 4 Alpha Release
Interview Questions Related to WebDriver and ChromeDriver or FirefoxDriver initializations
Let’s discuss some of the interview questions generally asked by interviewer related to the above context.
1. Can we use WebDriver driver = new WebDriver()?
2. Can we use ChromeDriver driver = new ChromeDriver()?
3. Why do we only use WebDriver driver = new ChromeDriver()?
4. What are the significances of RemoteWebDriver in Selenium?
For more Selenium interview questions click on below link:
These are the general questions asked by people. Once you finish reading this article, you will get the answer to all the questions listed above so I would ask you to pay attention here and read this article thoroughly.
I will start with basic related java topics here so that a novice reader will not get confused. So let’s start with the interface and I assume you are well aware of the class in java.
What is an interface in Java?
An interface in java is the blueprint of the class. It is used to achieve 100% abstraction as well as a way to multiple inheritances in java. The interface has static constants and default, static and private methods. It doesn’t hold the method body, thus, the methods are implemented in a different class.
The interface in java declared with a keyword called interface. There can be a nested interface as well. If any class wants to use the methods of the interface then that class have to implement the interface, whereas, one interface can easily extends another interface, hence, interface develops IS-A relationship.
What is Polymorphism in Java?
Polymorphism in Java means a single method will be used in different ways. Polymorphism can be achieved either at compile time or at runtime through method overloading and method overriding.
These two concepts are important if you want to understand why do we use WebDriver driver = new ChromeDriver().
Structure of WebDriver driver = new ChromeDriver()
Look at the above image. We see that Selenium starts fetching WebElements from SearchContext interface. This interface has findElement(s) abstract methods, which are further defined in RemoteWebDriver class. So WebDriver interface at first extends SearchContext interface, just to identify the WebElements to perform operations. Further, WebDriver has methods like get(URL), getCurrentUrl(), getTitle(), etc, which is mainly used at the top level of the automation project. Next, there is a class called RemoteWebDriver which has methods and definition to manage sessions, URL, Capabilities, and expansion of the abstracts methods of the WebDriver interface. So basically RemoteWebDriver has the implementation of all the abstract methods of the SearchContext interface and WebDriver interface.
Finally, RemoteWebDriver is extended by the classes of the native browser like ChromeDriver(), FirefoxDriver(), SafariDriver(), etc.
Let’s discuss the major differences between RemoteWebDriver and WebDriver.
Differences between RemoteWebDriver and WebDriver
WebDriver is an interface and as per the concept of the interface, we can’t keep the body of the methods written inside it. Hence, we use RemoteWebDriver class to implement all the methods written inside WebDriver interface, like findElement(), switchTo(), get(), findElements(), etc.
Every browser has the separate definition to perform testing in their browser, which basically brings the concept to have separate bindings and classes for each browser like ChromeDriver(), FIrefoxDriver(), etc. These native browser’s class extends RemoteWebDriver. We can easily run tests in our local machine by creating the instance of these classes. But, when we want to run our tests remotely then we can’t use these native browser’s class. In such a case, we have to configure and parameterize the RemoteWebDriver constructor.
We require a Selenium standalone server to perform our tests on the remote server. Let’s take Grid as of now, so we should have URL of the grid passed in the RemoteWebDriver constructor and capabilities will define the browsers at the grid.
If we code against the WebDriver interface then it will be helpful to separately allocate the session ids in case of parallel execution of the tests. In a nutshell, we can say that the RemoteWebDriver is the concrete implementation of the WebDriver interface and all the URLs will be routed via RemoteWebDriver only.
Why can’t we use WebDriver driver = new WebDriver()?
If we go back to the first class of Java then it reminds us that we create the object of the classes. Objects are similar to real-time objects, hence, the above definition is not possible as WebDriver is not a class, rather, it’s an interface. Therefore, we can’t create an object of the WebDriver.
Definition of ChromeDriver driver = new ChromeDriver()
This definition is logically correct, but technically not advisable for test automation project. We already knew that ChromeDriver or any other native browser’s driver class extends RemoteWebDriver, hence, implements all the methods of the WebDriver. So the test will run successfully on the Chrome browser. But, when we want to perform cross-browser testing then in that it will require object instantiation of a different browser. Thus, session handling will be difficult and cross-browser testing won’t be applicable in that case. Overall, testing will be inefficient.
Advisable- WebDriver driver = new ChromeDriver()
This is the advisable way and it resolves all the problems of session handling and cross-browser testing. Here we are creating an object of the ChromeDriver() by taking reference to the WebDriver interface.
We are just assigning one session per WebDriver reference, so, here reference of WebDriver is casting different browser’s class like ChromeDriver, FirefoxDriver, IEDriver, SafariDriver, etc. This technique makes browser switching easier because we are sharing the same reference of the WebDriver with different browsers and here benefit is that all the browsers are extending RemoteWebDriver.
Third party browsers and APIs basically implements methods written inside WebDriver, hence, it is the whim of the browser’s developers how they are declaring those methods. We can conclude that we are performing runtime polymorphism here with methods written inside WebDriver interface.
See image below which lists all the methods written inside ChromeDriver and WebDriver separately.
This was all about the most discussed topic. Leave your doubts in the comment section below and do not forget to join our Facebook group for quick updates.
Amazing Avinash Thanks for the article
You’re welcome 🙂
Nice blog. Really helpful 👍. Thanks.
Thank you.. Keep visiting. 🙂
Nice article, very helpful for people who are new to selenium.
awesome explanation. Thanks Avinash!!