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.
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 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.
ChromeDriver is a separate executable that Selenium WebDriver uses to control Chrome.
Using WebDriver driver = new ChromeDriver(); you are creating an instance of the WebDriver interface and casting it to ChromeDriver Class.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With