Understand Java Interface using Selenium WebDriver
Last Updated :
24 Apr, 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
File Upload using Selenium WebDriver and Java Robot Class
File uploads are a common task in web automation, and handling them effectively can be crucial for testing file-related functionalities. While Selenium WebDriver provides a straightforward way to interact with file upload elements, sometimes the built-in methods fall short. In such cases, combining
3 min read
Selenium- WebDriver Vs RC Vs IDE
Selenium is a famous system for Automatic internet browsers, utilized widely for web application testing. Inside the Selenium structure, two significant parts have advanced throughout the long term: Selenium WebDriver and Selenium RC (Controller). Both fill a similar need for Automatic internet brow
4 min read
Selenium Webdriver Handling Checkbox using Java
A set of tools and libraries that allow the user to automate interactions with web browsers is known as Selenium. It is too diverse tool that can handle every operation of a webpage starting from opening the webpage to closing of webpage. In this article, we will see the various steps to handle the
6 min read
How to do session handling in Selenium Webdriver using Java?
In Selenium WebDriver, managing browser sessions is crucial for ensuring that each test runs independently and without interference. A browser session in Selenium is identified by a unique session ID, which helps track and manage the session throughout the test. Proper session handling in Selenium W
4 min read
How to Click on a Hyperlink Using Java Selenium WebDriver?
An open-source tool that is used to automate the browser is known as Selenium. Automation reduces human effort and makes the work comparatively easier. There are numerous circumstances in which the user wants to open a new page or perform a certain action with the click of the hyperlink. In this art
4 min read
How to Take a Screenshot in Selenium WebDriver Using Java?
Selenium WebDriver is a collection of open-source APIs used to automate a web application's testing. To capture a screenshot in Selenium, one must utilize the Takes Screenshot method. This notifies WebDriver that it should take a screenshot in Selenium and store it. Selenium WebDriver tool is used t
3 min read
How to Automate Click Using Selenium WebDriver?
Selenium is one of the most popular and powerful tools for automating web applications. Selenium is widely used for automating user interactions like filling out forms, clicking on a button, navigating to a web page, and many more. One of the most common tasks while working with Selenium is clicking
5 min read
Understanding execute async script in Selenium Using java
Understanding executeAsyncScript in Selenium Using Java is crucial for handling asynchronous JavaScript operations during test automation. The executeAsyncScript method allows Selenium to wait for certain operations, like AJAX calls or timeouts, to complete before proceeding. This is particularly us
2 min read
Key press in (Ctrl+A) Selenium WebDriver using java
Selenium WebDriver is a powerful tool for automating web applications, and it offers various ways to simulate user actions such as clicking, typing, and even pressing keyboard shortcuts. One of the common actions is selecting all text or elements on a page using the "Ctrl+A" key press. This is usefu
3 min read
Selenium WebDriver - Running test on Safari Browser using Java
Selenium WebDriver is a powerful tool for automating web application testing. It supports multiple browsers, including Safari. In this guide, we'll walk through the steps to set up and run tests on Safari using Java. This includes setting up the environment, writing basic tests, handling dynamic ele
4 min read