Open In App

Why Upcasting is Required in Selenium Webdriver?

Improve
Improve
Like Article
Like
Save
Share
Report

Selenium is the software that is highly used in the field of testing a website. In the testing field, writing programming is known as Scripting. Selenium helps to write the scripts. Typecasting is one of the most important concepts which basically deals with the conversion of one data type to another data type implicitly or explicitly. The article focuses on discussing the need for typecasting in the selenium web driver.

What is Upcasting?

Typecasting is a very common practice that involves converting one data type to another data type. Often a float datatype is converted to the integer data type using the typecasting model. Typecasting model has two different submodels. One is the Upcasting and another is the downcasting.  

In Java programming language, typecasting can be done in objects also. One object can be typecasted to another object. This is a special feature of the Java programming language. There are two object models in Java. One is the Parent object and another is the Child object. The typecasting between these two objects is known as Upcasting and Downcasting. 

  1. Upcasting: Upcasting is the typecasting of a child object to a parent object. Upcasting can be done implicitly. Upcasting gives us the flexibility to access the parent class members but it is not possible to access all the child class members using this feature. Instead of all the members, we can access some specified members of the child class. For instance, we can access the overridden methods.
  2. Downcasting: Similarly, downcasting means the typecasting of a parent object to a child object. Downcasting cannot be implicit.

In selenium, the Upcasting method is used. 

Upcasting

 

Example: In Selenium scripts, it is required to add the location of the WebDriver to the script. After providing the location, upcasting is done to upcast the driver child object to the parent object.

Below is the example code to show upcasting:

Java




// Java program to implement
// upcasting
import java.io.*;
import java.lang.Thread;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
// Class
public class ChromeHomePage {
 
    // Main Driver
    public static void main(String[] args)
    {
 
        // Try Block TO Use sleep() Method
        try {
 
            // String To Store Home Page URL
            String baseUrl = "https://www.google.com/";
            // Implementation of SetProperty Method (Adding Location Of WebDriver)
            System.setProperty("webdriver.edge.driver","test/resources/chromedriver.exe");
             
              /* Important Upcasted Line Of Selenium WebDriver*/
            Webdriver driver =new Chromedriver();
           
            // Calling Home Page Method
            driver.get(baseUrl);
            // 2 Seconds Delay
            Thread.sleep(2000);
            // Closing The Opened Window Method
            driver.quit();
        }
        // Catch Block To Handle Exceptions
        catch (Exception e) {
            // Display Exceptions On Console
            System.out.println(e);
        }
    }
}


Explanation: 

Webdriver driver = new Chromedriver();

Here,

  1. WebDriver: WebDriver is the interface in the Java programming language. The interface is an abstract type in Java programming language. It is being used to describe the behaviors of the classes that are going to be implemented. Normally, the interface is declared with the interface keyword. In the selenium, the declaration is already done. So, there is no need to declare it more.
  2. driver: This is the local variable declared. 
  3. new: The new keyword is used to implement an instance of an object. The object that has the constructor function in it. Calling a new keyword with the constructor function helps to create a new empty object.
  4. ChromeDriver(): This is an inbuilt constructor in the Selenium scripting language. But this is also used as a function. As Java programming language has the rule that Class Name will always equal the Constructor Name.

An object of a ChromeDriver class is created by calling the ChromeDriver constructor. This will open the chrome browser in Selenium. The ChromeDriver instance is stored in the driver variable. WebDriver is the datatype for the variable driver. 

Why WebDriver datatype is used instead of ChromeDriver?

Webdriver Hierarchy is a complex structure. This is the backbone of the Selenium programming language. The below image illustrates the web driver interface structure:

Web driver hierarchy

 

  • The uppermost part of the Webdriver Hierarchy is the SearchContext. This is the interface. In the SearchContext, there are some abstract methods are present. They are the findElement() and findElements(). 
  • Under the SearchContext, there is another interface WebDriver. There are some abstract methods are present. These methods can be accessible from the WebDriver interface. Like there are quit(), close(), getString(), etc. Along with that, there are several other Nested Interfaces are present. Like, there are Windows, Navigation, Option, etc. are present. 
  • Under WebDriver, there is a class RemoteWebDriver. This is the first class in the Webdriver Hierarchy from the upside down. There is nothing more special methods present. This act as the parent class.
  • Under the RemoteWebDriver, the browser drivers are present. Like, there are ChromeDriver, FirefoxDriver, SafariDriver and many more are present. They are the child class of the RemoteWebDriver class. 

In the above line, as the ChromeDriver is typecasted with the WebDriver interface, so it is considered as Upcasting. As the child object or class ChromeDriver is typecasted with the higher class or parent class WebDriver, it is known as the Upcasting method. 

Why Upcasting is Required In Selenium Webdriver?

  • If testers only use the ChromeDriver on both sides of the equation, then three will be an issue. As ChromeDriver itself cannot create operations on the browser. As it doesn’t have any abstract methods to operate the browser. This is only a simple child class. So, we have to do the upcasting to the WebDriver. As in the Webdriver Hierarchy, the WebDriver interface has the largest abstract functions. It has also some nested methods present that help a lot to handle the browser. For all of these reasons, there is a need to upcast the ChromeDriver to the WebDriver.
  • ChromeDriver is typecasted to WebDriver instead of SearchContext because more benefits will be received from the WebDriver interface, instead of the SeachContext interface. As in the SearchContext interface, there are only two abstract methods. Whereas in WebDriver, there are lots of abstract methods present. This helps to operate the browser easily. So, the upcasting is only done with the WebDriver interface.


Last Updated : 16 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads