What is benefit of using ChromeDriver over WebDriver if we are using only Chrome Browser in our Selenium Automation Script

I am using WebDriver and ChromeDriver variables in following manner:

  • Line1 :

    WebDriver driver1 = new ChromeDriver();
    
  • Line2:

    ChromeDriver driver2 = new ChromeDriver();
    

My script is limited to run test cases only in Chrome Driver, so I will never use any other browser for my testing purpose. So, If I choose Line2 in my code instead of Line1, will that make any difference in terms of any functionality or benefits.(Looking for any additional functionalities which ChromDriver Class can provide in addition to existing methods of WebDriver Interface).


Solution 1:

ChromeDriver driver = new ChromeDriver();

If you use ChromeDriver driver = new ChromeDriver(); the ChromeDriver instance which will get created through that we will be only able to invoke and act on the methods implemented by ChromeDriver and supported by Chrome Browser 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 Mozilla, Chrome, Internet Explorer, Safari, 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.


Why WebDriver driver = new ChromeDriver();

Through WebDriver driver = new ChromeDriver(); we 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();

This question

Now, as you mentioned will never use any other browser for my testing purpose you can fall back on ChromDriver Class. But at this point it would be worth to mention that ChromeDriver is a standalone server which implements WebDriver's wire protocol for Chromium. Chromium team is in the process of implementing and moving to the W3C standard.

So moving forward, ChromeDriver, GeckoDriver, IEDriverServer, OperaDriver, SafariDriver each variant will be compliant with W3C standard only.

You can find a relevant discussion in what is the difference between ChromeDriver and WebDriver in selenium?