1

Why do we create a ChromeDriver instance and then assign it to a "WebDriver" interace reference.

Webdriver driver  = new ChromeDriver();

Why cant we create a Chromedriver , Firefoxdriver or IEDriver reference and assign it the object?

I am a complete newbie in Selenium and java?

1
  • 2
    What do you mean "can't"? Did you try it? This is more an issue of understanding OOP design and the SOLID principles than anything to do with QA: stackoverflow.com/q/383947/3001761
    – jonrsharpe
    Jul 26, 2019 at 7:28

5 Answers 5

5

Actually, you can.

 ChromeDriver x = new ChromeDriver();

is a perfect valid Java statement.

The point of using the interface WebDriver on the fact that all the API (methods) that you will be using are defined on the interface:

enter image description here

The classes ChromeDriver and Firefoxdriver implement these methods for the specificity of these browser. However, for your usage it is not necessary to know about each implementation, because they simply fulfill the interface (a contract) - it is a principle know as Abstraction in OOP.

It allows you to pass a concrete object (a ChromeDriver or a Firefoxdriver) throughout methods "disguised" as something more generic. If you actually need to change the object, you just need to act of its creation point and everything else will work fine at run-time. If you come to need something specific of ChromeDriver, you can cast the WebDriver to a ChromeDriver, but note that this operation is fragile to the change of the concrete object.

In a nutshell, in programming, always prefer to act on higher levels of abstraction, in order to be robust against changes in the lower levels.

1
ChromeDriver  cDriver = new ChromeDriver();
FirefoxDriver fDriver = new FirefoxDriver();
InternetExplorerDriver ieDriver = new InternetExplorerDriver();

Is a perfectly valid statement. But the problem is name(instance) of all the three drivers have to be different. They can't be re-used if you want to run the same test case against all the three browser. To avoid this problem we create an instance of WebDriver and initialize a it with driver of our choice so that the same object can be used across the framework.

Sample code where the same object can be used to run the test case across multiple browsers:

public void getBrowserInstance(String browserName) {

        WebDriver driver;

        switch (browserName) {
        case "chrome":
            driver = new ChromeDriver();
            break;

        case "firefox":
            driver = new FirefoxDriver();
            break;

        case "internetExplorer":
            driver = new InternetExplorerDriver();
            break;
        }

    }

Also go through the similar question already answered here

0

We can write:ChromeDriver driver=new ChromeDriver();,but we use to write:WebDriver driver = new ChromeDriver();.With the use of the second one, we can achieve Backward & Forward Compatibility, because Webdriver is an interface which is implemented by ChromeDriver class and other browser classes like Firefox, InternetExplorer, Safari, etc.

ChromeDriver cDriver = new ChromeDriver();

FirefoxDriver fDriver = new FirefoxDriver();

InternetExplorerDriver ieDriver = new InternetExplorerDriver();

The major drawback of using this: ChromeDriver driver=new ChromeDriver(); is we can't run our scripts on other browser by using same reference variable. To run the same code on different browser we need to re-initialize the reference variable.

0

Because Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Read about OOP to understand.

-1

In nutshell, to maintain the robustness of a framework towards any future changes, we follow one of the four fundamental principles of Object-Oriented Programming(OOPs) paradigm, the Abstraction.

You can read more @ https://stackify.com/oop-concept-abstraction/.

Thanks!

1
  • 2
    Please don't just link to a website. Your answer would be better if you summarized the part of the website that is relevant, linked as a summary, and let us know whether or not you have any affiliation with the site.
    – Kate Paulk
    Aug 1, 2019 at 18:01

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.