QaMantra: Why we assign Browser instance to webdriver interface

Why we assign Browser instance to webdriver interface

WebDriver driver = new FirefoxDriver();

WebDriver driver = new ChromeDriver();

WebDriver driver = new IEDriver();


In the above statement, WebDriver is an interface. An interface contains empty functions that have been defined but not implemented. These functions can be implemented by anyone as long as the method type and signatures can not be violated.

                             Therefore, an interface is also known as contract, because you can use an interface as you like but you cannot change the way it has been defined. And, since it has empty methods you won't actually need to instantiate it and so you cannot instantiate it.

FirefoxDriver/ChromeDriver/IEDriver and so on browser Driver is a class that has been written specifically for the Firefox/Chrome/IE and many browser. It has methods that are implemented and it can be instantiated. It can perform all functions (or methods) on the Firefox browser as defined in the interface WebDriver.


WebDriver API Architecture


So in the above statement, we are actually telling FirefoxDriver/ChromeDriver/IEDriver class that " you can automate the various methods that you want on the Firefox/Chrome/IE browser but you need to stick to the contract defined in WebDriver".

                                   So we declare a reference variable of type WebDriver and then use it to instantiate FirefoxDriver/ChromeDriver/IEDriver, which means that the object (driver) is of type WebDriver but points to the memory allocation to all data and methods in FirefoxDriver/ChromeDriver/IEDriver (and, as mentioned above, the FirefoxDriver/ChromeDriver/IEDriver class already has the implemented version of methods in WebDriver)

Continue Reading

No comments: