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.

Understand Java Interface using Selenium WebDriver

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”);

}

}

Similar Reads

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

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

Conclusion

...