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:
- We can not create an object of interface, i.e. we can not instantiate an interface.
- Interface has abstract methods means they do not have body, only declaration.
- We can pass reference of any super class or interface to a sub class object.
- 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.
- 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.
Let’s understand above diagram in details:
- 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.
- 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.
- Then we have browser specific driver classes like ChromeDriver(), EdgeDriver(), FirefoxDriver() etc.
Q 1: Why do we upcast any browser driver class object to WebDriver?
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:
- You can launch any browser through single browser factory method. (We will see soon how)
- It is a good programming practice to upcast the object to maximum level possible keeping in mind that you should not loose important functionalities.
- 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.
- 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.
- Remember, there is no such mandatory rule that you need to upcast or downcast. It is just way of proper programming.
i.e. no need to do downcasting in below case:
FirefoxDriver driver2= new FirefoxDriver();
But, if we write as below:
WebDriver driver= new FirefoxDriver();
- WebElement is an interface which extends SearchContext and TakesScreenshot interfaces. It has many useful abstarct methids like click(), sendKeys(), isSelected() etc.
- 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.
- 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.