Search code examples
javaseleniumselenium-webdriverinterfacewebdriver

What is the difference between ChromeDriver driver = new ChromeDriver(); and WebDriver driver = new ChromeDriver();


What is the difference between:

ChromeDriver driver = new ChromeDriver ();

and

WebDriver driver = new ChromeDriver ();

Will I get same output if I use any of these codes in Selenium Java?

I didn't any difference in two codes so will my output also same if I use of these two?


Solution

  • ChromeDriver driver = new ChromeDriver();

    When using:

    ChromeDriver driver = new ChromeDriver();
    

    The ChromeDriver instance will be only able to invoke and act on the methods implemented by ChromeDriver and supported by only. To act with other browsers we have to specifically create individual objects as below :

    • FirefoxDriver driver = new FirefoxDriver();
    • InternetExplorerDriver driver = new InternetExplorerDriver();

    WebDriver Interface

    From Selenium perspective, the WebDriver Interface is similar like a agreement which the 3rd party Browser Vendors like , , , , etc have to adhere and implement the same. This would in-turn help the end-users to use the exposed APIs to write a common code and implement the functionalities across all the available Browsers without any change.


    WebDriver driver = new ChromeDriver();

    Using WebDriver driver = new ChromeDriver(); you are creating an instance of the WebDriver interface and casting it to ChromeDriver Class. All the browser drivers like FirefoxDriver, ChromeDriver, InternetExplorerDriver, PhantomJSDriver, SafariDriver etc implemented the WebDriver interface (actually the RemoteWebDriver class implements WebDriver Interface and the Browser Drivers extends RemoteWebDriver). So if we use WebDriver driver, then we can use the already initialized driver (as common object variable) for all browsers we want to automate e.g. Mozilla, Chrome, InternetExplorer, PhantomJS, Safari.

    WebDriver driver = new FirefoxDriver();
    driver = new ChromeDriver();
    driver = new FirefoxDriver();
    driver = new SafariDriver();