Why WebDriver Is An Interface not a class or an abstract class?

RemoteWebDriver

Why WebDriver is an interface not a class or an abstract class?” This is a frequently asked interview question as well as an important concept of Selenium WebDriver.

Anyway the answer of this question depends on how much you understand Interface and Abstract class concepts in Java and how and when to use.

There are plenty of posts on Difference between Abstract class and Interface, but the actual question “When to use what?” remains confusing for many specially for testers like us.

I follow a simple understanding about when to use Interface and abstract class that “If you do not know anything and dependent on other’s help then use an Interface. If you do know something but still need help of others, use an Abstract class.“.

Usage of Interfaces and Abstract classes is needed for better architectural design for better scalability, maintainability and less painful to make a bigger change.

Let’s discuss Why Selenium WebDriver developers made WebDriver 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, furniture works , paintings etc. But all three builders have their own style of performing same works which are not known to outsiders/world.

There is an entrepreneur who wants to start a “Construction service provider” company and wants to onboard three builders discussed above. 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 style of getting work done with you. You just need to give the tasks to do and do not care how we will do that.
  2. We may not know all the terms used in each others business. So there will be a mediator between us who understands everything of both sides. Each communication should go through mediator so that we no need to invest time on understanding each other’s terms.

The entrepreneur  accepts all terms and conditions given by builders and launches company. Story over!!

The more or less same story with Selenium WebDriver as well. The purpose of Selenium WebDriver to automate browsers but they do not know how each browser works internally.

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 operations are performed in different ways by different browsers.

Without knowing internal working mechanisms of browsers, Selenium WebDriver developers can not write codes to perform actions. If they get to know the internal mechanism some how, it will be difficult to manage when internal mechanism changes and this should not break flow for other browsers.

So to make it simple, a contract is placed between Selenium WebDriver developers and browsers and that contract is in form of WebDriver. A WebDriver consist of all related basic methods which could be performed on a browser.

Official Java doc of WebDriver summarize it very well as :-

The main interface to use for testing, which represents an idealised web browser. The methods in this interface fall into three categories:

  • Control of the browser itself
  • Selection of WebElements
  • Debugging aids

Key methods are get(String), which is used to load a new web page, and the various methods similar to findElement(By), which is used to find WebElements.

Currently, you will need to instantiate implementations of this interface directly. It is hoped that you write your tests against this interface so that you may “swap in” a more fully featured browser when there is a requirement for one.

RemoteWebDriver is direct implemented class of WebDriver which are furher extended by browser Specific classes like ChromeDriver, FirefoxDriver etc. There is a misunderstanding that RemoteWebDriver has all the logic to perform actions on browser and browser specific classes like ChromeDriver gives missing implementations. It is not correct. In fact there is no implemented classes of WebDriver interface provided by browsers. Everything is done via browser executable servers.

All implementations of WebDriver that communicate with the browser, or a RemoteWebDriver server shall use a common wire protocol called JSON Wire Protocol. This wire protocol defines a RESTful web service using JSON over HTTP. So the role of RemoteWebDriver to construct a request for specific browser o perform. All request are sent to browser specific servers i.e. executable files.

WebDriver could be an abstract class as well but idea behind making WebDriver is an interface for better architectural design. I have come across below lines form some source :-

“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

21 thoughts on “Why WebDriver Is An Interface not a class or an abstract class?

  1. Great work !! Its very easy to understand the concept through real time examples. Glad that I came across this content

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

    1. Each WebService is an API but every API is not a webservice.
      When API connect over HTTP network then it is called WebService.
      For instance :
      Suppose you have make out a simple calculator jar .
      And locally when you are passing parameters and operations. It is doing something. Its an API.
      But when you expose this API over to the network and other API’s are interacting over it. Then it becomes a Web Service.

      Fingers crossed I have brought some clearity.

      1. You are correct. API – Application Programming Interface – How it communicates to system that is secondary. Yeah but an API over a network is a web service.

  3. 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.

  4. 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. RemoteWebDriver prepares request to hit browser servers. A request is sent to server to perform specific operations on browser.

      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 to Amod Mahajan Cancel reply

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