why we assign firefoxdriver instance to webdriver

前端 未结 8 1623
时光说笑
时光说笑 2021-02-06 16:52

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

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-06 17:17

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

    }

提交回复
热议问题