What is the difference between ChromeDriver and WebDriver in selenium?
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.
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, Edge, Opera, Safari as follows:
WebDriver driver = new FirefoxDriver();
// or
WebDriver driver = new ChromeDriver();
// or
WebDriver driver = new InternetExplorerDriver();
// or
WebDriver driver = new EdgeDriver();
// or
WebDriver driver = new OperaDriver();
// or
WebDriver driver = new SafariDriver();
Trivia
As per current scenario, we have to instantiate the implementations of WebDriver Interface directly. As per the current practice we write our Automated Test Script against this interface so that in future we may swap in a more fully featured Browser when there is a requirement for one.
What is the difference between ChromeDriver driver = new ChromeDriver(); and WebDriver driver = new ChromeDriver();
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 google-chrome 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 firefox, google-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.
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();
WebDriver vs ChromeDriver
Satish's answer is correct but in more layman's terms, ChromeDriver
is specifically and only a driver for Chrome. WebDriver
is a more generic driver that can be used for many different browsers... IE, Chrome, FF, etc.
If you only cared about Chrome, you might create a driver using
ChromeDriver driver = new ChromeDriver();
If you want to create a function that returns a driver for a specified browser, you could do something like the below.
public static WebDriver startDriver(Browsers browserType)
{
switch (browserType)
{
case FIREFOX:
...
return new FirefoxDriver();
case CHROME:
...
return new ChromeDriver();
case IE32:
...
return new InternetExplorerDriver();
case IE64:
...
return new InternetExplorerDriver();
default:
throw new InvalidParameterException("Unknown browser type");
}
}
public enum Browsers
{
CHROME, FIREFOX, IE32, IE64;
}
... and then call it like...
WebDriver driver = startDriver(Browsers.FIREFOX);
driver.get("http://www.google.com");
and depending on what browser you specify, that browser will be launched and navigate to google.com.
What is benefit of using ChromeDriver over WebDriver if we are using only Chrome Browser in our Selenium Automation Script
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?
Related Topics
Failed to Resolve Com.Google.Android.Gms Play-Services-Auth:11.4.0
Parsing Query Strings on Android
Must Override a Superclass Method' Errors After Importing a Project into Eclipse
Execution Failed For Task :App:Compiledebugjavawithjavac in Android Studio
Set Imageview Width and Height Programmatically
Difference Between Jsonobject and Jsonarray
How to Ping External Ip from Java Android
Error: Could Not Find or Load Main Class
Running Jmap Getting Unable to Open Socket File
How to Execute Bash Command With Sudo Privileges in Java
Growing Resident Memory Usage (Rss) of Java Process
React on Global Hotkey in a Java Program on Windows/Linux/Mac
Gradle Finds Wrong Java_Home Even Though It's Correctly Set
How to Stop Repeated Keypressed()/Keyreleased() Events in Swing