Selenium WebDriver, an interface or a class

As many of you know Selenium is the most popular open source tool for automating web applications. The Selenium – Java combination is the most popular among automation testers. In our free Java bootcamp (part of #testerbhicoder initiative) while we talk about interface, an often asked query is about what exactly is Selenium WebDriver. Is it a Class or an Interface. Obviously we need to understand the difference between what is a class and how is it different than an interface.

For all Selenium users if they reckon The initial block of code involves telling Selenium which browser it should launch.

So if we want to launch a Firefox browser we will write the following syntax in Java

WebDriver driver = new FirefoxDriver(); or WebDriver driver = new ChromeDriver();

We never write WebDriver driver = new WebDriver()

Since Java is based on Oops(Object oriented programming) concept we can clearly say that we have not created an object of WebDriver class. Rather the syntax WebDriver driver = new FirefoxDriver() is an syntax of an interface. 

Lets delve deeper into this..

One of the useful points about open source software like Selenium is that we can see the source code. So we downloaded the Selenium jar file and decompiled it to see all the class files. This is how it looked

 

 

When we opened the WebDriver class file we saw this and noted that it is defined as an abstract interface

 

 

Lets now understand what is an Interface

An Interface which looks like a class will contain Abstract methods (body less methods). So we cant create an object to interface but we can create classes where we can implement the abstract methods of the interface. The classes which implement the abstract methods of interface are known as implementation classes.

Obvious question is then why do we need an Interface which does not have any method body. Is it a waste of time? Its better to write classes for different browsers isn’t it?

We need to remember one thing , Selenium WebDriver’s architecture is designed in a way that it talks to browser in its native language. So if I want to write WebDriver code to work with Firefox then I will write code for Firefox, similarly I will have to write many class files for all the browsers. Even though I am successful in writing the code for all the browsers, by the time I release it to the market a new version of the same browser will come rendering all my efforts futile

So if I write an Interface with abstract methods I can send a message to all the browser companies i.e the third party companies to provide their implementation classes for my Interface. That’s where you have separate class files for FirefoxDriver , ChromeDriver etc. And these will implement the abstract methods of WebDriver interface in their way. 

 

Thus when we write WebDriver driver = new FirefoxDriver(); or WebDriver driver = new ChromeDriver() we are implementing rules of interface WebDriver over the third party browser class files Firefox and Chrome. FirefoxDriver() and ChromeDriver() methods are defined in the class files FirefoxDriver and ChromeDriver class files respectively. This is one of the simplest way to explain why WebDriver is an interface and not a class


Like this post? Share it with friends