In selenium, WebDriver is an interface that declares all selenium WebDriver methods with no implementation. It contains all the selenium method signatures or abstract methods like findElement ( ), get ( ), quit ( ) with no implementation.
This is because every browser has its logic to perform browser actions like how to launch the browser, how to close the browser, and handling different types of elements. This logic is written in other programming languages, so, Selenium has made WebDriver an interface. Each browser vendor (Mozilla, Edge, IE, Chrome, etc.) can provide the implementation classes. You have separate class files for “FireFoxDriver” implemented by Mozilla, “ChromeDrive” implemented by Google, etc.
This gives a lot of flexibility.
- If the browser implementation changes, then the onus is on the browser vendor to change the WebDriver implementation
- We need not have to write different automation scripts for different browsers, as the methods or contracts are the same for all browsers. (and these contracts are implemented by different browser vendors).
- This way of implementing any tool or software is one of the best design principles and allows for easy maintenance of the test automation tools code (not test automation scripts)
Note:
To put it simply, an Interface is a contract or a container that stores the method’s signature to be implemented in the code segment. Note that the interface does not implement any method. It contains only method signature or abstract methods, or bodyless methods. So we cannot use the interface until another class implements it. The classes that implement an interface’s abstract methods are known as implementation classes.