Selenium Webdriver Interface & Classes Hierarchy In Details: As we all know that by using selenium suite we can automate web-based applications and for automation selenium webDriver developers have provided so many classes and interfaces by implementing that in our application we can able to automate our web application.
If you are writing your script in Java language then during the automation script preparation, you have implemented or come across the java concepts like Up-casting and down-casting. I came across so many testers that they can write the test script, but when you ask them about the inner implementation or what’s happening when you write some specific code, then they are not able to answer that. So it’s a suggestion from my end that when you have written something then be aware of what concepts you have used here and what you have to implement there. If you know those things, then you can handle all types of situations easily.
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 are going to discuss up-casting and down-casting and also the Selenium Webdriver Interface hierarchy too. Before starting the explain, let me share you few things or recall about interfaces which can help you to understand the things easily:
Read Also: Selenium Webdriver Interview Questions
- As we all know that 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. To use subclass methods, we must downcast the object to a subclass object.
- 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
Let us explain the above hierarchy in details:
- SearchContext is the topmost interface of Webdriver which contains only two abstract method findElement() and findElements(). These two methods don’t have a method body.
- WebDriver also is an interface which extends SearchContext interface which has also so many abstract methods like close(), get(java.lang.String URL), quite(), navigate(), switchTo() and other so many methods for more details you can visit this URL
- The next one is RemoteWebDriver, which is a fully implemented class where all abstract methods of WebDriver and SearchContext interface implemented. Also, two other interfaces JavascriptExecutor and TakesScreenshot abstract methods are implemented in RemoteWebDriver class.
- And Finally, browser-specific driver classes available like FirefoxDriver, ChromeDriver, IEDriver, SafariDriver, etc.
WebElement Hierarchy
- The webElement interface extends two other interfaces like SearchContext and TakesScreenshot interfaces. the webElement interface has so many useful methods that are frequently used during the automation. those methods like clear(), click(), getText(), submit() etc.
- RemoteWebELement is a class which implements all the abstract methods of the webElement interface
- When you try to search an element in DOM then you are going to use findElement() and findElements() then the remoteWebelement class is up-casting to webElement interface, and if you perform any operation like click() or submit(), then the overridden method of the remoteWebelement class is executed.
If you saw in this post, we are mainly discussing two main concepts of JAVA like up-casting and down-casting which used in the selenium. you may face some interview questions on these two topics so lets us discuss some of the interview questions on those topics so that you can easily give the solution to those questions if you face those in the near feature.
Read Also: Selenium WebDriver Commands
Selenium Webdriver Interface Interview Questions: Why we do Up-casting browser driver class object to Webdriver but why we are 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:
- If we write like above then by a single driver, we can able to launch any browser.
- From the good programming, viewpoint its always a good practice to up-cast the object up to maximum level but keeping one thing in mind that we are not losing any important functionalities.
- If we up-cast to SearchContext then as you saw in the above picture, it has only two methods that are findElement() and findElements(). So if we try to access other important methods, then we need to down-casting.
- Because of that, we are Up-casting up to WebDriver only because if we up-cast to WebDriver then we are not facing any problems in access the important methods.
Please write comments if you find anything incorrect, or you want to share more information about this topic discussed above then you can use our contact us page.
Thanks for this type of explanation for interview point of view.
this is really an useful post
Very good explanation
Thank you for this
very useful and understandable information. Thank you 🙂
Very good explanation.
Thank you
Awesome explanation.
Thank you.
EdgeDriver also extends the class Chrominum class