WebDriver vs ChromeDriver
Solution 1
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.
Solution 2
WebDriver is an interface, while ChromeDriver is a class which implements WebDriver interface. Actually ChromeDriver extends RemoteWebDriver which implements WebDriver. Just to add Every WebDriver like ChromeDriver, FirefoxDriver, EdgeDriver are supposed to implement WebDriver.
Below are the signatures of ChromeDriver and RemoteDriver classes
public class ChromeDriver extends RemoteWebDriver
implements LocationContext, WebStorage {}
public class RemoteWebDriver implements WebDriver, JavascriptExecutor,
FindsById, FindsByClassName, FindsByLinkText, FindsByName,
FindsByCssSelector, FindsByTagName, FindsByXPath,
HasInputDevices, HasCapabilities, TakesScreenshot {}
Solution 3
WebDriver is an interface
ChromeDriver is an implementation of the WebDriver interface
https://docs.oracle.com/javase/tutorial/java/concepts/interface.html
There is no difference in usage:
ChromeDriver driver = new ChromeDriver();
or
WebDriver driver = new ChromeDriver();
Comments
-
8protons almost 2 years
In Selenium 2 - Java, what's the difference between
ChromeDriver driver = new ChromeDriver();
and
WebDriver driver = new ChromeDriver();
? I've seen both of these used in various tutorials, examples, etc and am not sure about the difference between utilizing the
ChromeDriver
vsWebDriver
objects.-
8protons over 4 years@Raedwald how is this a duplicate? Yours is asking about purely interfaces; a generality. Mine is asking about two different, specific objects.
-
-
SiKing over 5 yearsIMHO: This answer could be improved by highlighting the fact that you could select the browser at runtime. Change
startDriver(Browsers)
tostartDriver(String)
, and call itstartDriver(System.getenv("BROWSER"))
. -
SiKing over 5 yearsSorry if I am picky. :) Also:
ChromeDriver
extends bothJavascriptExecutor
andTakesScreenshot
classes (via extendingRemoteWebDriver
), whichWebDriver
does not. So if you use those functions and onlyChromeDriver
, you do not need to recast yourdriver
. -
JeffC over 5 yearsYour first comment doesn't change my answer. You could just as easily pull the browser from a config file, convert it to an enum, and pass it to
startDriver()
. I do some variation of that with the few suites that I've written in the last few years... but passing in as string is just as valid. I tend towards using enums when it makes sense because it reduces the string processing and bugs associated with typos, etc. when strings are using vs enums. -
JeffC over 5 yearsYour second comment is a valid point but that wasn't the direction I was taking my answer. Satish's existing answer already touched on that... but probably could have expanded upon it more as a more substantial difference, depending on how often you might use JSE or take a screenshot.