What's the difference between WebDriver and ChromeDriver?
Asked Answered
M

5

17

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.

Milburn answered 7/9, 2016 at 20:26 Comment(1)
the question format is a part of solution that's why you need title, and formatting to your questionSclerotomy
H
22

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.

Hacksaw answered 8/9, 2016 at 4:31 Comment(4)
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")).Guacin
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.Guacin
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.Hacksaw
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.Hacksaw
A
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 {}
Adulthood answered 7/9, 2016 at 20:40 Comment(0)
M
12

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:

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.

Missy answered 3/1, 2018 at 16:0 Comment(0)
D
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();
Daedal answered 31/8, 2018 at 12:37 Comment(0)
J
0

This can be the simplest point:

  • ChromeDriver is only specific to Chrome Browser
  • WebDriver is global for all Browsers
Jute answered 12/12, 2018 at 5:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.