Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy

Hello Folks,

Selenium automates browser and to achieve this, Selenium people have provided many classes and interfaces.

During learning selenium, many times we will use up-casting and down-casting concepts of JAVA. Many people know how to perform some operations like opening any desired browser through single method, running java script etc but they are not able to answer why did they do up-casting and down-casting.

In this post, I will explain hierarchy of Selenium classes and interfaces.

Let’s start with some basic concepts of java:

  1. We can not create an object of interface, i.e. we can not instantiate an interface.
  2. Interface has abstract methods means they do not have body, only declaration.
  3. We can pass reference of any super class or interface to a sub class object.
  4. When we do up-casting, we restrict object from using sub class methods. To use sub class methods, we must down cast the object to sub class object.
  5. When we up cast an object and try to access a method of sub class,  and that method is overridden in sub class, remember overridden method of sub class will be called without down casting object. It is major concept on which selenium works.

WebDriver hierarchy:

Let’s understand above diagram in details:

  1. SearchContext is a top most interface which has only two methods names findElement() and findElements(). These methods will be abstract as SearchContext is an interface.
  2. WebDriver is also an interface which extends SearchContext. WebDriver has many abstarct methods like get(String url), close(), quit() , getWindowHandle etc. WebDriver has nested interfaces names Window, Navigation, Timeouts etc. These nested interfaces are used to perform/group specific operation like getPosition(), back(), forward() etc.
  3. RemoteWebDriver is a fully implemented class which implements Webdriver, JavascriptExecutor and TakesScreenshot. Fully implemented class means it defined body for all inherited methods.
  4. Then we have browser specific driver classes like ChromeDriver(), EdgeDriver(), FirefoxDriver() etc.

#InterviewQuestion:

Q 1: Why do we upcast any browser driver class object to WebDriver? OR

Why do we not upcast to SearchContext or RemoteWebDriver?

A: In selenium, maximum time when we create an object of any browser driver class, we do upcast.

E.g. WebDriver driver= new FirefoxDriver();

Reasons are as below:

  1. You can launch any browser through single browser factory method. (We will see soon how)
  2. It is a good programming practice to upcast the object to maximum level possible keeping in mind that you should not loose important functionalities.
  3. If we upcast object to SearchContext, it will not be so feasible as it has only two methods. To use other important methods, we need to do down casting.
  4. Keeping 2nd point in mind, We can upcast till WebDriver as we are not making it difficult to use important methods because of major concept of overriding in java. So, we do not upcast to RemoteWebDriver.
  5. Remember, there is no such mandatory rule that you need to upcast or downcast. It is just way of proper programming.

Q 2: Why do we downcast object execute javascript?

A: We do not downcast always to run javascript until object is upcasted to WebDriver.

i.e. no need to do downcasting in below case:

FirefoxDriver driver2= new FirefoxDriver();

We can run javascript directly because execute() method will be visible/accessible to driver2 object.

But, if we write as below:

WebDriver driver= new FirefoxDriver();

We must need to downcast it to JavaScriptExecutor to make execute() method visible to object.

WebElement hierachy:

  1. WebElement is an interface which extends SearchContext and TakesScreenshot interfaces. It has many useful abstarct methids like click(), sendKeys(), isSelected() etc.
  2. RemoteWebELement is a fully implemented class as it implements WebElement interface. In fact, findElement() and findElements() of SearchContext interface have been defined properly in this class only.
  3. When we search for some element using findElement or findElements() method, a type of RemoteWebElement class is up casted to WebElement. And when we perform any action on webelement like click(), method defined in RemoteWebElemnt class will be executed because of overriding concept.

I hope, This post will be very useful as it clears why we do up casting and down casting while creating script. Now you must be able to answer.

#HappyLearning

Thanks. Bye.