Why WebDriver Is An Interface?

Hello Folks,

We will see a very important topic as well as frequently asked interview question:

“Why WebDriver is an interface?”.

Let’s start this post with a small story.

There are three “builders” and all builders do the same work such as building a house, interior designing, wooden works etc. But all three builders have unique style of get work done which are not known to outsiders/world.

There is an entrepreneur who wants to start a “construction service provider” company and went to talk to these three builders as they are very famous. All three builders were ready to be part of the entrepreneur ‘s company but they kept below conditions:

  1. We will not share our techniques or logic used for construction with you.
  2. You just need to provide us order(What needs to be done) and your work will be done.
  3. Our one “agent”  will be with you and every order should come from him. You are not allowed to enter our premises.

The entrepreneur  accepted all terms and conditions set by builders and started company. Company grown like any thing and earn a lot of profits. Story over!!

Now relate the story with respect to this post:

  1. Builders are browsers.
  2. Entrepreneur is WebDriver.
  3. Agent is browser driver class.

Every browser has their own logic to perform browser’s actions such as Launching a browser, closing a browser, loading URL, handling different type of web elements. Same operation is done in different ways by browsers.

Since browsers can not share there logic with Selenium developers, browsers develop an agent called browser drivers (ChromeDriver, FirefoxDriver etc) and gave to Selenium developers. This browser drivers works as interface between browser and selenium. Now browser and selenium are able to communicate to each others.

Selenium developer created a list of actions like (launching, loading browser, click, type etc) and gave to browsers to handle them whenever these methods are called on browsers. Since, selenium developers does not know logic and logic will not be same for all browsers, they created WebDriver as an interface. Interface is a type in Java which has only abstract method i.e methods without body/logic.

RemoteWebDriver class implements WebDriver interface which is extended by Browser drivers . Any browser driver class can override implementation given in RemoteWebDriver.

Now you want to work on any browser just create an object of respective browser driver class and start working on it.

If we try to understand this in pure Java logic, it will be as below:

“If we know logic for some methods only, create class as an Abstract and mark method as abstract if you do not know logic. If you do not know logic of any method, create an Interface.”

For more proper concept, every browser driver class extends RemoteWebDriver which implements WebDriver interface.

if you see source codes of Selenium, you will see RemoteWebDriver class has logic for all methods of WebDriver interface and browser driver classes like ChromeDriver extends and defines its own method. They don’t override any method of RemoteWebDriver.

You can conclude from above point that Webdriver could be defined as a Class or abstract class aa well. But as per javadoc:

“There are a number of situations in software engineering when it is important for disparate groups of programmers to agree to a “contract” that spells out how their software interacts. Each group should be able to write their code without any knowledge of how the other group’s code is written. Generally speaking, interfaces are such contracts.“

So for the best architecture, WebDriver is an interface.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning