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.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through 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

18 thoughts on “Why WebDriver Is An Interface?

  1. Amod your posts are really wonderful with an in dept understanding..Loved reading the post after 9 hours of my regular office work..:)

  2. Just a question.. How would the class, Remotewebdriver which has implementations of interface driver would have been implemented by Selenium team, since the implementation’s is more like “entrepreneur” and would not have logic for browsers.. or remotewebdriver would have been implemented by browser folks (Guess its incorrect) or something to do with JSON wire protocol

    1. It will be api. Just think yatra.com they show you flights of all flight companies..yatra don’t have code. They use api given by flight companies

      1. Thanks Buddy!! Keep up with your insightful posts. I mean intstead of all websites offering similar things, yours is good in sense you dig deep and offer indepth explanation.

  3. Hi Amod, saw internal implementation of different classes like firefoxdriver,chromedriver etc and there are no methods implemented like get,close,quit(),getPageSource() in the browser driver classes. All these methods are implemented in RemoteWebDriver class only. so browser driver classes are for opening the respective browsers?

    1. Hello,
      Actually RemoteWebDriver is fully implemented class of WebDriver interface and all other browser driver class extent RemoteWebDriver. If any browser driver wants to override any method they can do. You can launch browser using RemoteWebDriver class also where you need to pass browser type. And at high level, RemoteWebDriver is used if you want to run scripts on grid and Specific browser driver classes for local run.

      1. so all browser driver classes are using same implementation of the methods of webdriver. There are no separate implementations of different methods in different browser classes. Then webdriver could have been a normal class or abstract class because implementation of methods of webdriver are only once in RemoteWebdriver class and not in specific browser classes.

        1. There will be no problem if WebDriver is a class with implementation of methods. Any other class can inherit and override it. But to follow better architecture it is defined in the way.

        2. If you go for interface java doc, they clearly says:
          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.

      2. Thanks Amod it’s very helpful.

        One doubt, not sure if it is valid. Is RemoteWebDriver is adapter class since it’s implementing all the methods of webdriver interface.

Leave a Reply