Introduction:
This is more of an Object Oriented Programming interview question rather than a Selenium WebDriver question.
First of all, there is absolutely no problem with a browser driver object being referenced by RemoteWebDriver instance variable. There will never be any problem with the automation testing framework script, until and unless Selenium decides to deprecate RemoteWebDriver (very unlikely).
Now let's quickly see the Selenium WebDriver hierarchy.
SearchContext interface : It has 2 methods - findElement(By locator) and findElements(By locator).
WebDriver interface : It extends SearchContext interface. So it has to override both the SearchContext methods. Along with that, it provides its own abstract methods.
WebElement interface : It extends SearchContext interface. So it automatically overrides both of its parent interface's methods. Along with that, it provides its own abstract methods.
RemoteWebDriver class : It implements WebDriver interface. So it overrides all the methods of WebDriver and SearchContext interfaces.
RemoteWebDriver : the 1st Concrete class in WebDriver API hierarchy :
π‘
A concrete class is a class in which, all methods, including overridden and its own methods, are implemented.
All methods from its parent and grandparent interfaces are implemented in RemoteWebDriver class. This is the concept of Inheritance in Object Oriented Programming. RemoteWebDriver and subsequent child classes such as ChromeDriver, EdgeDriver etc. are essentially inheriting properties of WebDriver. The properties are declared in WebDriver interface. How these properties should work are defined in RemoteWebDriver class.
Using RemoteWebDriver instead of WebDriver :
The way we use the WebDriver API in our automation testing framework, there is no fundamental difference in WebDriver and RemoteWebDriver functionality-wise.
RemoteWebDriver driver1 = new ChomeDriver();
driver1.get("https://google.com");
//or,
WebDriver driver2 = new ChromeDriver();
driver2.get("https://google.com");
Here, both driver1 and driver2 will have same methods available to them. It won't affect any functionalities within the framework.
Why we don't write RemoteWebDriver = new ChromeDriver() :
According to object oriented programming, we should,
"code to interfaces"
That means, we should be able to maintain abstraction in our code as much as we can. Even though there is nothing wrong with writing RemoteWebDriver = new ChromeDriver(), we are exposing the implemented class here, rather than hiding it.
For better readability, we write WebDriver driver = new ChromeDriver(). We are concerned about what a method does, rather than how it does it. This level of abstraction can be achieved by creating driver object referenced by instance of the interface.
Thanks and Happy learning!
Top comments (0)