Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebDriver vs ChromeDriver [duplicate]

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.

like image 218
8protons Avatar asked Sep 07 '16 20:09

8protons


People also ask

What is difference between ChromeDriver and WebDriver?

WebDriver is an open source tool for automated testing of web apps across many browsers. It provides capabilities for navigating to web pages, user input, JavaScript execution, and many more. ChromeDriver is a standalone server which implements WebDriver's wire protocol for Chromium.

Why do we use WebDriver instead of ChromeDriver?

Why can WebDriver be used instead of FirefoxDriver and ChromeDriver? Because, in comparison with FirefoxDriver() and ChromeDriver() which are classes so objects can be created for them, WebDriver is an interface. An interface is just a template that is implemented by a class.

Is Chrome and ChromeDriver same?

ChromeDriver is a separate executable that Selenium WebDriver uses to control Chrome.

What happens if you write WebDriver driver new ChromeDriver?

Using WebDriver driver = new ChromeDriver(); you are creating an instance of the WebDriver interface and casting it to ChromeDriver Class.


1 Answers

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.

like image 168
JeffC Avatar answered Oct 19 '22 20:10

JeffC