3

Why we always do:

WebDriver driver = new ChromeDriver();

Not this:

ChromeDriver driver= new ChromeDriver();

Any special reason, except object size ?

1

5 Answers 5

6

We generally do it this way because usually we want to be able to run our tests on multiple browsers. If we declare the driver as a specific driver type, we are then anchored to only that driver. This is not a problem if you only ever need to test on say Chrome for example. But what if you later want your tests to also be able to work with IE, Opera, Firefox, etc.?

These are extended classes of the WebDriver interface.

If your main tests and other classes define the commonly shared driver as simply WebDriver instead of specifically being tied to ChromeDriver, then the same tests can be run without change to the test code itself simply by initializing the shared driver object with a different driver extended class.

4

Absolutely right, Good explanation provided by @Bill Hileman, I was already upvoted it.

In addition to clear this doubt you need to understand What is WebDriver and ChromeDriver/Firefoxdriver.

ChromeDriver driver= new ChromeDriver();

With the use of above line, The script works perfectly on chrome browser but things are getting complicated in future if I want run my same script on Firefox or IE or some different browser.

How can we modify the same script to work for multiple browsers (Selenium Grid) instead of having a script for each browsers like ChromeDriver, FirefoxDriver, IEDriver?

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. It specifies what fields and methods the class should have but without providing more details.
The classes that implement the interface will provide the methods implementations.

So WebDriver is an interface and FirefoxDriver and ChromeDriver classes that implement the WebDriver interface.

Keep in mind, Just method declarations and no method implementations.

What about FirefoxDriver()?

FirefoxDriver()/ChromeDriver() is not really implementing the WebDriver interface.

It is inheriting instead from the RemoteDriver class which implements the WebDriver interface.

Below is some content of the RemoteDriver class:

public class RemoteWebDriver implements WebDriver, JavascriptExecutor,FindsById, FindsByClassName, FindsByLinkText, FindsByName, FindsByCssSelector, FindsByTagName, FindsByXPath,
HasInputDevices, HasCapabilities, TakesScreenshot {

private static final Logger logger = Logger.getLogger(RemoteWebDriver.class.getName());
private Level level = Level.FINE;

private ErrorHandler errorHandler = new ErrorHandler();
private CommandExecutor executor;
private Capabilities capabilities;
private SessionId sessionId;
private FileDetector fileDetector = new UselessFileDetector();
private ExecuteMethod executeMethod;

public void get(String url) {
execute(DriverCommand.GET, ImmutableMap.of("url", url));
}

public String getTitle() {
Response response = execute(DriverCommand.GET_TITLE);
Object value = response.getValue();
return value == null ? "" : value.toString();
}

public String getCurrentUrl() {
Response response = execute(DriverCommand.GET_CURRENT_URL);
if (response == null || response.getValue() == null) {
throw new WebDriverException("Remote browser did not respond to getCurrentUrl");
} else {
return response.getValue().toString();
}
}

Lots of method implementations are available in RemoteDriver.

This is why, we could not create the WebDriver object, because WebDriver is an interface and not a class.

It is possible to create an object for an interface and instantiate it using any of the classes that implements the interface.

like this: WebDriver driver = new FirefoxDriver(); OR

WebDriver driver = new ChromeDriver();

By using above code, your scripts are now flexible and can use any WebDriver object which is required to invoke particular browser.

Here reference variable of type WebDriver allows us to assign the driver object to different browser specific drivers. Thus allowing multi-browser testing by assigning the driver object to any of the desired browser.

0

This is what is called Polymorphism. Either you are using your first or second code line the object size will remain the same because ChromeDriver object is being placed in memory in both the cases.

The pattern you're talking about is worth using if you are going to pass that driver to some functionality that can work with the only WebDriver implementations (and knows nothing about more specific implementations like ChromeDriver) or if your code supports several browsers and does some tricky initialisation of the particular browser type (and hence using some particular WebDriver implementation).

You should also remember that in the case of WebDriver driver = new ChromeDriver(); you would not be able to invoke the methods of ChomeDriver unless you use the explicit type casting because there are no Chrome-specific methods described in WebDriver. Due to the same reason you would not be able to pass your driver to any method that expects any specific browser implementation without a direct type casting. Below is the example of available methods for a simple WebDriver and for ChromeDriver. It can be seen that ChromeDriver has a lot more available methods than WebDriver defines.

enter image description here

Nevertheless since ChromeDriver implements WebDriver interface it can be used in Selenium framework thanks to polymorphism conception.

0
package test.a1;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class testDriver {

public static void main(String[] args) 
{
    // TODO Auto-generated method stub
    String browser="FireFox";
    if(browser=="Chrome")
    {
        System.setProperty("webdriver.chrome.driver",
        "C:\\Users\\LL\\chromedriver_win32\\chromedriver.exe");

        ChromeDriver driver = new ChromeDriver();
        driver.get("https://www.gmail.com");
    }
    else if(browser=="IE")
    {

        System.setProperty("webdriver.ie.driver","Drivers\\IEDriverServer.exe");

        InternetExplorerDriver driver = new InternetExplorerDriver();
        driver.get("https://www.gmail.com");
    }
    else if(browser=="FireFox")
    {

        FirefoxDriver driver = new FirefoxDriver();
        driver.get("https://www.gmail.com");
    }
}

} We can do this ..so kindly let know why ..we are doing this Webdriver driver=new FirefoxDriver();

0

We use Webdriver driver = new Chromedriver (); For invoking chrome browser

So for Chrome you will use Webdriver driver = new Chromedriver();

For Firefox

Webdriver driver = new Firefoxdriver()

You can use respective browser name But you will only able to work with single browser only whereas Webdriver is an interface which will provide you the liberty to deal with multiple browser specific classes

Like Chromedriver driver = new Chromedriver ();

But once you create the driver reference of Chromedriver you cannot use same driver reference name for Firefox you need to write

Firefoxdriver driver1= new Firefoxdriver();

and then again you need to write the separate piece of code for driver1

So to avoid that we need to use Webdriver interface for cross browser compatibility

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.