Selenium Webdriver Interface & Classes Hierarchy

Selenium Webdriver Interface & Classes Hierarchy In Details: As we all know, by using the Selenium suite, we can automate web-based applications. Automation selenium webDriver developers have provided so many classes and interfaces by implementing them in our application that we can automate our web application.

If you are writing your script in Java, you have implemented or encountered Java concepts like up-casting and down-casting during the automation script preparation. I came across many testers who can write the test script, but when you ask them about the inner implementation or what’s happening when you write some specific code, they cannot answer that. So it’s a suggestion from my end that when you have written something, be aware of what concepts you have used here and what you have to implement there. If you know those things, you can easily handle all situations.

Let’s take an example: Selenium Webdriver Interface.

Open different browsers using a single method, then he can write the below line of code, but when you told him to explain, most of them are not able to know about that

WebDriver driver=new FirefoxDriver();

So, in this post, we will discuss up-casting and down-casting and also the Selenium Webdriver Interface hierarchy. Before starting the explanation, let me share a few things or recall about interfaces that can help you to understand things easily:

  • As we all know, we cannot create an object or interface.
  • The interface has only a method declaration, and those methods don’t have a method body.
  • Passing of superclass references or interface to a subclass object is allowed.
  • When we do up-casting, we restrict objects from using subclass methods. We must downcast the object to a subclass object to use subclass methods.
  • When we upcast an object and try to access a method of the subclass, and that method is overridden in a subclass, remember the overridden method of the subclass will be called without a downcasting object. It is a major concept on which selenium works.

Selenium Webdriver Interface Hierarchy

Hierarchy of Selenium Classes and Interfaces
Hierarchy of Selenium Classes and Interfaces

Let us explain the above hierarchy in detail:

  • SearchContext is the topmost interface of Webdriver, which contains only two abstract methods: findElement() and findElements(). These two methods don’t have a method body.
  • WebDriver is also an interface that extends the SearchContext interface, which also has many abstract methods like close(), get(java.lang.String URL), quite(), navigate(), switchTo(), and other many methods for more details you can visit this URL
  • The next one is RemoteWebDriver, a fully implemented class where all abstract WebDriver and SearchContext interface methods are implemented. Also, two other interfaces, JavascriptExecutor and TakesScreenshot abstract methods, are implemented in the RemoteWebDriver class.
  • Finally, browser-specific driver classes like FirefoxDriver, ChromeDriver, IEDriver, SafariDriver, etc are available.

WebElement Hierarchy

WebElement Hierarchy
WebElement Hierarchy
  • The webElement interface extends two other interfaces: SearchContext and TakesScreenshot interfaces. the webElement interface has many useful methods frequently used during the automation. those methods like clear(), click(), getText(), submit() etc.
  • RemoteWebELement is a class that implements all the abstract methods of the webElement interface.
  • When you try to search for an element in the DOM, you will use findElement() and findElements(). Then the remoteWebelement class is up-casting to the webElement interface, and if you perform any operation like click() or submit(), then the overridden method of the remoteWebelement class is executed.

As you saw in this post, we are mainly discussing two main concepts of JAVA up-casting and down-casting, which are used in selenium. You may face some interview questions on these two topics, so let us discuss some of them so that you can easily give the solution to those questions if you face those in the near future.

Selenium Webdriver Interface Interview Questions: Why do we Up-casting browser driver class object to Webdriver, but why are we not up-casting into SearchContext or RemoteWebDriver?

Ans: When you are automating using selenium, then you may come across a frequently used statement where we are doing up-casting like:

WebDriver driver= new firefoxDriver();

Reasons:

  1. If we write like above, then by a single driver, we can launch any browser.
  2. From a good programming viewpoint, it’s always a good practice to up-cast the object to the maximum level but keep in mind that we are not losing any important functionalities.
  3. If we up-cast to SearchContext, then as you saw in the above picture, it has only two methods: findElement() and findElements(). So if we try to access other important methods, then we need to down-casting.
  4. Because of that, we are up-casting up to WebDriver only because if we are up-casting to WebDriver, we will not face any problems accessing the important methods.

Please write comments if you find anything incorrect, or if you want to share more information about this topic discussed above, you can use our Contact Us page.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

8 thoughts on “Selenium Webdriver Interface & Classes Hierarchy”

Leave a Comment