why we assign firefoxdriver instance to webdriver

前端 未结 8 1620
时光说笑
时光说笑 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条回答
  • 2021-02-06 17:13
    package test.a1;
    
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    
    public class testDriver {
    
        public static void main(String[] args) 
        {
            // TODO Auto-generated method stub
            String browser="FireFox";
            if(browser=="Chrome")
            {
                System.setProperty("webdriver.chrome.driver",
                "C:\\Users\\LL\\chromedriver_win32\\chromedriver.exe");
    
                ChromeDriver driver = new ChromeDriver();
                driver.get("https://www.gmail.com");
            }
            else if(browser=="IE")
            {
    
                System.setProperty("webdriver.ie.driver","Drivers\\IEDriverServer.exe");
    
                InternetExplorerDriver driver = new InternetExplorerDriver();
                driver.get("https://www.gmail.com");
            }
            else if(browser=="FireFox")
            {
    
                FirefoxDriver driver = new FirefoxDriver();
                driver.get("https://www.gmail.com");
            }
        }
    }
    

    We can do this ..so kindly let know why ..we are doing this Webdriver driver=new FirefoxDriver();.

    0 讨论(0)
  • 2021-02-06 17:14

    Webdriver is an interface, not a class. We create a Webdriver reference driver and assign it to an object of class FirefoxDriver. To perform the testing on Firefox, make an object of class FirefoxDriver. Likewise, to test on chrome, use Chromedriver class' object and assign it to Webdriver. Webdriver is an interface which is implemented by both FirefoxDriver class and ChromeDriver class(and classes for other browsers like IE, Safari). Objects of only those classes can be assigned to an interface reference which implement that interface(which in this case is Webdriver interface)

    0 讨论(0)
  • 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();``
    

    }

    0 讨论(0)
  • 2021-02-06 17:18

    WebDriver is an interface and FirefoxDriver is a class which implements this WebDriver contract. See Selenium doc enter link description here

    Now we can create a reference variable of an interface but we can't instantiate any interface since it is just a contract to be implemented.But we can assign the instance of a class (In this case FirefoxDriver ) to its Parents(WebDriver).

    We can assign child classes to its parents(It may be a class or an interface to which it extends/implements).

    Now The reason behind doing WebDriver driver=new FirefoxDriver() is just to create an abstraction to the client(Java Program) because you can use Any Driver class according to browser.

    0 讨论(0)
  • 2021-02-06 17:23

    It will make use of Interface Concept.See the below code.

     FirefoxDriver  f = new FirefoxDriver();
     ChromeDriver c = new ChromeDriver();
    

    To load a page using chrome,

    c.get("google.com");

    If I want to use same call with firefox, I need to comment the above line and need to write a new line like this

    //c.get("google.com");
    f.get("google.com");
    

    This will go on increasing , ending up with specific code for each instance of driver. But if I assign the the driver instance to WebDriver interface variable, I can write single set of code which will work for all drivers.

     WebDriver d;
        d  = FireFoxDriver();
        //just replace the above line with d = ChromeDriver() or InternetExplorer();
        d.get("google.com");
        d.getTitle();
        d.close();
    
    0 讨论(0)
  • 2021-02-06 17:25

    When creating a new instance of browser new ChromeBrowser(); ChromeBrowser(ChromeDriver.exe) is running on local machine and will die with the instance. So the browser restarts with each instance creation. To avoid restart of browser multiple times, Webdriver takes responsibility by referring Browser instance and controls browser not to be restarted for each instance creation.

    WebDriver driver = new ChromeDriver();
    

    From the documentation;

    A WebDriver implementation that controls a Chrome browser running on the local machine. This class is provided as a convenience for easily testing the Chrome browser. The control server which each instance communicates with will live and die with the instance. To avoid unnecessarily restarting the ChromeDriver server with each instance, use a RemoteWebDriver coupled with the desired ChromeDriverService, which is managed separately.

    0 讨论(0)
提交回复
热议问题