Hello Folks,

In this post, we will learn about a frequently asked interview question in selenium that “Why we upcast a browser driver class object to WebDriver type?”.

OR,

Why do we write as below:

WebDriver driver= new FirefoxDriver();
WebDriver driver= new ChromeDriver ();

Why not as below:

FirefoxDriver driver= new FirefoxDriver();
ChromeDriver driver= new ChromeDriver ();

Let’s discuss about this.

First of all, all above statements are correct. It is not mandatory to up cast a browser driver class object to WebDriver or any higher class/interface in hierarchy.

Now question is if it is not mandatory, why we do so?

We should be aware hierarchy of Selenium webdriver classes and interfaces. You can learn about it from here.

WebDriver Interface is super interface (Not super most) of every browser driver classes like ChromeDriver , FirefoxDriver etc. And as per java concept, a super class/interface reference can hold a its sub class object but vice versa is not possible.

We will see two approaches here:

  1. When we do not upcast to WebDriver
  2. When we upcast to WebDriver

When we do not upcast to WebDriver:

Suppose, you need to create a base class which need to be extended in every class and perform some actions. User should be able to launch any browser.

You will create a base class as below:

You will create test scripts as below:

Disadvantages:

  • In the same style you need to create other browser driver scripts and use proper reference variable of browser type.
  • In this approach, if a new browser comes in market, you need to change Base class because we need to define a new reference variable of type new browser class.
  • Modification is not easy because it require changes at framework level.
  • You can not use methods defined in above classes in hierarchy.
  • It will problem to maintain a same reference of browser driver. You might get null pointer exception.

When we upcast to WebDriver:

We will create a very simple base class as below:

We will create scripts as below:

In this approach, you can use same reference variable of type WebDriver to launch any browser.

Advatnages:

  • If you compare both approach, you will understand that we upcast for better architecture , best coding practice, ease of modification and enhancements. There is no hard core rule that we need to do it.
  • We can launch any browser using same reference type.
  • If new browser comes in market, there is no need to do any changes in framework (Base class).
  • You can use methods of classes in hierarchy which requires downcasting. For ex: To take screenshot and execute java script.
  • We achieve run time polymorphism here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

12 thoughts on “Why Do We Up cast Browser Driver Class Object To WebDriver?

  1. Hi Amod, Thanks for your valuable inputs – I thinks your detailed information is worth reading on each topic.

    Currently I am stuck on this and if you mind letting me know. Here it is –

    In Selenium – we have findElement() method that returns webElement which is an interface. So we usually store it under Webelement type of variable. I know at a very abstract level that this variable is referring to some object and I am not able to understanding the architecture as how we we are making the interface variable point to an object and hence achieving this execution.

    It will be of great help if you can provide some code example on same.

    Looking forward for your response.

    1. Hello Pradeep,

      Interface is a type which you can say a referenced variable. When you have a class named “Demo” and you write Demo d, it means you created a referenced variable of type Demo. findElement method returns something which can be stored in Type WebElement. And an interface type can hold a class object if there is relationship between them.

      1. Thanks Pramod for response, well this is what my question is – What java concept is applied here such that we are making a method returning an interface (Findelement method in this case) – and in turn that interface reference variable points to class object. What phenomena is this?

          1. Thanks Amod – But I would like to make a request to cover it in detail if it is possible at your end in your future topics. This help help a lot.

            Thanks Again !

  2. Well thats good topic and had same question in mind! Thanks for clarifying the concept! I had a doubt on Javascript executor.Why is it required?

Leave a Reply