I am novice to Java or Selenium.
I just need help to understand one basic question.
Why we assign firefoxdriver instance to WebDriver? WebDriver driver=new Firef
WebDriver driver = new FirefoxDriver();
In the above statement, WebDriver
is an interface. An interface contains empty methods that have been defined but not implemented. These methods can be implemented by anyone as long as the method type and signatures are not 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
is a class that has been written specifically for the Firefox 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
.
So in the above statement, we are actually telling FirefoxDriver
class that "hey you can automate the various methods that you want on the Firefox 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
, which means that the object (driver) is of type WebDriver
but points to the memory allocation to all data and methods in FirefoxDriver
(and, as mentioned above, the FirefoxDriver
class already has the implemented version of methods in WebDriver
). So all good :)
By using this technique, we have made it easy for the tester to use any browser of his or her liking. For example, to automate on IE driver, one will have to simply write a statement like
WebDriver driver = new IEDriver(); //where IEDriver is the class written for IE