16

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
  • 3,591
  • 5
  • 32
  • 67
  • @Raedwald how is this a duplicate? Yours is asking about purely interfaces; a generality. Mine is asking about two different, specific objects. – 8protons Oct 08 '19 at 17:48

4 Answers4

21

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.

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • 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 Dec 12 '18 at 18:30
  • 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`. – SiKing Dec 12 '18 at 18:36
  • 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 Dec 12 '18 at 18:58
  • 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. – JeffC Dec 12 '18 at 18:59
20

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 {}
Satish Gupta
  • 1,447
  • 1
  • 12
  • 14
1

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();
quit
  • 282
  • 2
  • 9
0

This can be the simplest point:

  • ChromeDriver is only specific to Chrome Browser
  • WebDriver is global for all Browsers
Ishita Shah
  • 3,955
  • 2
  • 27
  • 51