Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy

Posted on 05/06/2019 By admin

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.

Uncategorized

Post navigation

Previous Post: Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy
Next Post: Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy

Related Posts

image – Make Selenium Easy Uncategorized
sleepEx – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
March 2017 – Make Selenium Easy Uncategorized
Are You Mixing Waits In Selenium WebDriver? – A Bad Practise Uncategorized
Make Selenium Easy – Page 12 Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark