Understand Java Interface using Selenium WebDriver
Last Updated :
23 Jul, 2025
Java Interface is a blueprint of the class that contains static constants and abstract methods. It cannot have a method body. The interface is a mechanism to achieve abstraction. This article focuses on discussing the Selenium WebDriver Interface.
Example:
public interface Animal
{
void printAnimalName();
void printAnimalColor();
void printAnimalWeight();
}
In the above example, an Interface “Animal” is created and three non-implemented methods are declared. Now, it’s just a blueprint, there is no implementation. Any class that implements this interface must implement the non-implemented methods, or Java will throw an error. Here is what the implemented class looks like. In the below class “Cat”, we have implemented the three non-implemented methods.
public class Cat implements Animal
{
@Override
public void printAnimalName()
{
System.out.println("Cat");
}
@Override
public void printAnimalColor()
{
System.out.println("Black");
}
@Override
public void printAnimalWeight()
{
System.out.println("50");
}
}
Need for an Interface in Java
Selenium supports various web browsers for automation such as Chrome, Firefox, Safari, Edge, etc. It doesn’t depend on what browser we are choosing for our automation, we must have some common methods available to work with these browsers. For example:
- Load the URL: get() or navigate()
- Find a WebElement: findElement()
- Get the current URL: getCurrentURL()
- Close the browser: close() or quit()
We have different classes available to open different browsers, such as:
Now, let’s assume four developers are writing the code for these four classes, the developer who is writing code for ChromeDriver gives the method name to load the URL as getURL(), whereas another developer writing code for EdgeDriver gives the method name as loadURL(). Try to understand the problem for us as end users, we have to remember three different methods to load the URL, do you think it’s a good practice? Of course not, that’s when the importance of the Interface comes into the picture.
How does Selenium use Java Interface?
To overcome this problem, Selenium Developers have created an Interface called “WebDriver” and defined all the must-have methods inside it without implementing it.
- Any class implementing the WebDriver Interface now has to use and implement the same set of methods, or Java will throw an error.
- As a result, whether it’s Chrome, Edge, Firefox, or Safari, all the implementing classes must have the same set of methods with the same name.
This is how the Interface achieves abstraction, i.e. hiding all the implementing parts for various browsers, getting the method name, and using it in your code.
In the below image, the WebDriver interface and its methods are shown in the centre. These methods are not implemented yet. The classes are mentioned in the circle that implement the WebDriver interface.
WebDriver InterfaceBelow is the Java program to implement interface:
Java
// Java program to implement
// Interface
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class InterfaceExample {
public static void main(String args[])
throws InterruptedException
{
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(
"https://demo.automationtesting.in/Register.html");
WebElement element = driver.findElement(
By.xpath("//input[@placeholder='First Name']"));
element.sendKeys("Selenium");
driver.quit();
}
}
Explanation:
- WebDriver driver = new ChromeDriver(): Here WebDriver is an Interface and ChromeDriver is the implementing class.
- driver.get(): get() method is declared in WebDriver Interface. Hence the ChromeDriver class must define the method get(), which is used to open a URL on the Chrome Browser. Similarly, FirefoxDriver implements the method get() to open a URL on the Firefox Browser.
- driver.findElement(): findElement() is another method that is declared in the WebDriver interface and all the classes that implement the WebDriver interface must define its definition. The same goes for driver.quit() as well.
- element.sendKeys(): Enter a value in the text field.
Output:
OutputConclusion
While learning Java Interface we always tend to see some common and easy examples either from blogs or from books which are good for beginners. But as we progress in our careers, we should understand the real-life applications of Java Interface. Selenium is a popular tool today in the Test Automation Industry. From this article, we have learned how Selenium WebDriver uses the Java Interface concept internally and what is the importance of Java Interface. Without the Selenium WebDriver interface, it's tough to manage multiple browser-implementing classes to have similar features.
Similar Reads
Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp
10 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing,
3 min read
Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
8 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa
15+ min read
Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le
4 min read
Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ
8 min read
SDLC MODELS
TYPES OF TESTING