WebDriver vs ChromeDriver - java

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.

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.

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 {}

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();

This can be the simplest point:
ChromeDriver is only specific to Chrome Browser
WebDriver is global for all Browsers

Related

What is the difference between ChromeDriver driver = new ChromeDriver(); and WebDriver driver = new ChromeDriver();

What is the difference between:
ChromeDriver driver = new ChromeDriver ();
and
WebDriver driver = new ChromeDriver ();
Will I get same output if I use any of these codes in Selenium Java?
I didn't any difference in two codes so will my output also same if I use of these two?
ChromeDriver driver = new ChromeDriver();
When using:
ChromeDriver driver = new ChromeDriver();
The ChromeDriver instance will be only able to invoke and act on the methods implemented by ChromeDriver and supported by google-chrome 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 firefox, google-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();
Using WebDriver driver = new ChromeDriver(); you are creating an instance of the WebDriver interface and casting it to ChromeDriver Class. All the browser drivers like FirefoxDriver, ChromeDriver, InternetExplorerDriver, PhantomJSDriver, SafariDriver etc 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, PhantomJS, Safari.
WebDriver driver = new FirefoxDriver();
driver = new ChromeDriver();
driver = new FirefoxDriver();
driver = new SafariDriver();
The correct driver initialisation is the second one. Use this:
WebDriver driver = new ChromeDriver ();

Selenium opening browser but not loading websites in Java

I am using Selenium in a Burp plugin but I can't load pages with the get method. Browsers open correctly, both Firefox and Chrome, but they don't load the page. Chrome address bar shows "data;.", while Firefox has no text in it. I am using the last driver available, Chrome 81.0.4044.183 and the driver for this exact version, while Firefox is 76.0.1 and I am using GeckoDriver 0.24 (since 0.25+ have a known bug) and it works with the last version of Firefox.
The code is the following
void runBrowserAutomatization(File fileDriver, String seleniumTrack, boolean isHeadless) {
WebDriver driver;
if (gui.usedBrowser().toLowerCase().contains("chrome")) {
ChromeOptions options = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
options.setCapability(CapabilityType.PROXY, proxy);
options.setHeadless(isHeadless);
System.setProperty("webdriver.chrome.driver", fileDriver.getPath());
driver = new ChromeDriver(options);
} else if (gui.usedBrowser().toLowerCase().contains("firefox")) {
FirefoxOptions options = new FirefoxOptions();
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
options.setCapability(CapabilityType.PROXY, proxy);
options.setHeadless(isHeadless);
System.setProperty("webdriver.gecko.driver", fileDriver.getPath());
driver = new FirefoxDriver(options);
} else {
PrintMsg("No browser selected...");
return;
}
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.nytimes.com/");
driver.quit();
}
I may also think it is a Proxy misconfiguration, Burp certificate is installed in Firefox and Windows (where Chrome gets Certificate Authorities) but is not shown in the settings of the instance started by Selenium. Any help or suggestion is highly appreciated, thnaks.

Selenium - using WebDriver interface as type

Less of a concrete question but still valid I think.
Selenium tutorial presents an example of obtaining a driver:
WebDriver driver = new FirefoxDriver();
However, this prevents us from using browser-specific drivers' methods. In my case it was setProxy(). Hence, I used:
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setProxy("someproxy", 6666);
The question is: why would I limit myself to using methods only from the WebDriver interface, when I know which driver I want to use?
So:
Using the WebDriver interface as a type is better because it provides flexibility: different types of WebDrivers (such as FirefoxDriver or ChromeDriver etc.) could be required at different times.
Setting a proxy could be done with Proxy and DesiredCapabilities. To cite the Selenium Tutorial:
String PROXY = "localhost:8080";
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(PROXY)
.setFtpProxy(PROXY)
.setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(cap);

FirefoxDriver: how does the browser gets open automatically

Inside main method if I write WebDriver driver = new FirefoxDriver(); and execute the class how does the browser gets open automatically
public class Web{
public Static void main(String[] args){
WebDriver driver = new FirefoxDriver();
}
}
Firefox driver is included in the selenium-server-stanalone.jar available in the downloads. The driver comes in the form of an xpi (firefox extension) which is added to the firefox profile when you start a new instance of FirefoxDriver.
You can read up more on the Firefox Driver here.
If I misunderstood the question and you want to know how to open a browser and go to a URL, the below example will open the Firefox browser and go to http://www.google.com/
public class Web{
public Static void main(String[] args){
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
}
}
Selenium has quite extensive documentation on WebDriver available here. I suggest you read the documentation to get to know Selenium better!

How to set Google Chrome in WebDriver

I am trying to set Chrome as my browser for testing with Web-Driver and set the chromedriver.exe file properly but I am still getting the following error:
org.openqa.selenium.WebDriverException:
The path to the driver executable must be set by the webdriver.chrome.driver system property;
for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver.
The latest version can be downloaded from http://code.google.com/p/chromedriver/downloads/list
I have already checked the path of the driver but still i am getting same error.
I don't know where i have made a mistake.
Here is my code:
File file = new File("C:\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
Capability= DesiredCapabilities.chrome();
Capability.setBrowserName("chrome");
Capability.setPlatform(Platform.LINUX);
browser=new RemoteWebDriver(new URL(nodeURL),Capability);
browser.get(webUrl);
Please help me!!
Aditya,
As you said in your last comment that you are trying to access chrome of some other system so based on that you should keep your chrome driver in that system itself.
for example: if you are trying to access linux chrome from windows then you need to put your chrome driver in linux at some place and give permission as 777 and use below code at your windows system.
System.setProperty("webdriver.chrome.driver", "\\var\\www\\Jar\\chromedriver");
Capability= DesiredCapabilities.chrome(); Capability.setPlatform(org.openqa.selenium.Platform.ANY);
browser=new RemoteWebDriver(new URL(nodeURL),Capability);
This is working code of my system.
I'm using this since the begin and it always work. =)
System.setProperty("webdriver.chrome.driver", "C:\\pathto\\my\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
For Mac -Chrome browser
public class MultipleBrowser {
public WebDriver driver= null;
String browser="mozilla";
String url="https://www.omnicard.com";
#BeforeMethod
public void LaunchBrowser() {
if(browser.equalsIgnoreCase("mozilla"))
driver= new FirefoxDriver();
else if(browser.equalsIgnoreCase("safari"))
driver= new SafariDriver();
else if(browser.equalsIgnoreCase("chrome"))
System.setProperty("webdriver.chrome.driver","/Users/mhossain/Desktop/chromedriver");
driver= new ChromeDriver();
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
driver.navigate().to(url);
//driver.manage().deleteAllCookies();
}
It was giving Illegal Exception.
My workaround with code:
public void dofirst(){
System.setProperty("webdriver.chrome.driver","D:\\Softwares\\selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.facebook.com");
}
Mac OS:
You have to install ChromeDriver first:
brew cask install chromedriver
It will be copied to /usr/local/bin/chromedriver. Then you can use it in java code classes.
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver","Absolute path of Chrome driver");
driver =new ChromeDriver();
baseUrl = "URL/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

Categories

Resources