Make Selenium Easy

Why Should We Upcast Browser Driver Class Object To WebDriver?

In interview a frequently asked interview question is “Why do we upcast browser class object to WebDriver? or why we do not upcast to RemoteWebDriver class instead of WebDriver?” and we give all sort of answers.

First of all this question is asked with assumption that we do not have any option except upcast. Is it so? Absolutely not. It completely up to you whether you upcast or not and if you do then to which level? Question should be “Why should we upcast it or what are advantages if you upcast?

Before we start thinking this particular topic only with respect to Selenium WebDriver-Java, let’s learn core Java concepts i.e. Inheritance and upcast.

I define an Interface in Java as “A contract. I want something to do but I do not know how to do the same. I need a class or multiple classes to achieve that.“. This may not should exactly with purpose of Interface but at high level this we do.

“Amod” ( My name ) wants to perform swapping of two numbers in Java but he does not know the logic to do. So he has created an interface as below:-

Amod has a best friend “John” who has average programming knowledge and promises to give implementation of Amod’s method. Amod is so happy now. John gives implementation as below:-

Now Amod creates a usage utility to run his incomplete method as below:-

Output :-

Amod is happy now and everywhere he is appreciating John. One day he met his best mentor Mukesh who is a great developer. He says to Amod that he can provide a more optimal and memory efficient implementation of swapping method. Amod is more happy now and asked for the implementation from Mukesh. Mukesh gives implementation as below:-

Now Amod creates a new usage utility class so that he can use Mukesh’s implementation.

As Amod is using Mukesh’s implementation it makes John sad. He says to Amod, he is not doing good with him. Amod does not want to make sad any one. He is in tension now what to do. He is also thinking if there is another new implementation given by someone else again he needs to create a new usage class and same problem may arise again.

He found a solution in core concept of Interface, Inheritance and runtime method overriding as below:-

Amod has created a reference of his Interface as whatAmodWantsButDontKnowHowToDo. Since both John and Mukesh’s implementation class implement Amod’s interface, whatAmodWantsButDontKnowHowToDo can hold any object of implemented class. If there is further new implementations of Amod’s method, he does not need to create a new class anymore. When John’s comes to meet Amod then he sees his implementation is being used and same for Mukesh. ( Please assume the game :-p Hypothetical statement ).

Story over but did you get the point of story?

Implementation Classes of WebDriver Interface

Above classes in image are directly or indirectly implementations of WebDriver interface. In fact RemoteWebDriver is direct implemented class of WebDriver and remaining browser classes extends RemoteWebDriver. There is another update I can see in Selenium 4 that, a new class ChromiumDriver is introduced which extends RemoteWebDriver and ChromeDriver class extends ChromiumDriver now.

We know WebDriver is an interface and these implemented class complete it. Many implemented classes represent a browser. You can related WebDriver interface as WhatAmodWantsButDontKnowHowToDo interface and ChromeDriver , FirefoxDriver etc as MukeshKnowsOptimalImplementation and JohnKnowsImplementation.

Let’s talk with respect to Selenium WebDriver now

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 for chrome browser as below:-

Similarly you need to do for other browsers as well. If a new browser comes in market, you need to add in base class first.

When we upcast to WebDriver:

We will create a very simple base class as below:

We will create scripts as below:

In above approach, you do not need to change in base class if new browser comes. You have flexibility of using any browser with same reference.

Above question will not be answered completely if I do not tell you :-

  1. Why not upcast to SearchContext which is supermost interface?
  2. Why not upcast to RemoteWebDriver?

SearchContext is a parents interface of WebDriver and consists of only two methods findElement(By by) and findElements(By by). Obviosuly you can upcast to SearchContext but you need to downcast it to desired browser driver reference even to use a get() method. SearchContext reference will not have visibility of methods of implemented class.

Now the last question, why not to upcast to RemoteWebDriver when all browser classes extends it. It is again a valid question and answer is you can do. It will not make much difference but what if a new class comes in place of RemoteWebDriver?? Your existing script may not be upgraded. Anyway chances of getting a replacement of RemoteWebDriver is very very less but still. Upcast to WebDriver and keep framework supportable for all browsers and remote executions as well.

You can download/clone above sample project from here.

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

Find all Selenium related post here, all API manual and automation related posts here and find frequently asked Java Programs here.

Many other topics you can navigate through menu.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

19 thoughts on “Why Should We Upcast Browser Driver Class Object To WebDriver?

  1. Appreciate your efforts and details explaination given.Can you create some blog series for Appium as well

    Regards,
    Bhushan

  2. Hey, could you please explain the statement: “It will problem to maintain a same reference of browser driver. You might get null pointer exception.?”

  3. In the first part of the above question I see a line under the Dis Advantages – *You can not use methods defined in above classes in hierarchy.*

    I would like to know in which context the above line is said, because when a class is extended, we can always use the methods defined in above classes or from the interfaces in hierarchy.

    1. In case of multilevel inheritance, you may have different implementation of same method in two classes. If you don’t upcast, always last overridden method will be called.

  4. 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 !

  5. 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

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.