WebDriver vs ChromeDriver

15,076

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();
Share:
15,076
8protons
Author by

8protons

Just trying to figure things out.

Updated on June 07, 2022

Comments

  • 8protons
    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 vs WebDriver objects.

    • 8protons
      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
    SiKing over 5 years
    IMHO: This answer could be improved by highlighting the fact that you could select the browser at runtime. Change startDriver(Browsers) to startDriver(String), and call it startDriver(System.getenv("BROWSER")).
  • SiKing
    SiKing over 5 years
    Sorry if I am picky. :) Also: ChromeDriver extends both JavascriptExecutor and TakesScreenshot classes (via extending RemoteWebDriver), which WebDriver does not. So if you use those functions and only ChromeDriver, you do not need to recast your driver.
  • JeffC
    JeffC over 5 years
    Your 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
    JeffC over 5 years
    Your 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.