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
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();``
}