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 FirefoxDriver()
I know that this is kind of Late binding in Java, because we can assign IEDriver or some other instance to WebDriver at later point of time.
Question1: But this applices to classes, right?
Question2: WebDriver is an interface, then can we create an object instance of an interface?
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
The Answer is simple,
WebDriver is an interface which has a common behavior and we upcast so that the same behavior can be used across the Classes. Example:
Interface: Consider WebDriver has the behavior of Switch
public interface WebDriver
{
void on();
void off();
int voltage=220;;
}
Class1: Consider this as ChromeDriver class
public class ChromeDriver implements WebDriver {
@Override
public void on() {
System.out.println("ChromeDriver On");
}
@Override
public void off() {
System.out.println("ChromeDriver Off");
}
} Class 2: Consider this class as FireFoxDriver class
public class FireFoxDriver implements WebDriver {
@Override
public void on() {
System.out.println("FireFoxDriver On");
}
@Override
public void off() {
System.out.println("FireFoxDriver Off");
}
}
Now Consider Runner Class:
//Here to change the implementation we just need to change the object
//Whether you need to access Mozilla or Chrome
public class Runner {
Webdriver driver = new ChromeDriver();//new FireFoxDriver();
driver.on();
driver.off();``
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With