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:
WebDriver hierarchy:
Let’s understand above diagram in details:
#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:
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:
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.
"How much Java I need to learn for selenium with Java binding?" is mostly asked question by a Professional who…
Hello Guys, You should not be able to type alphabets or special characters in a field which supposed to accept…
Hello Folks, Recently a guy asked me this question which he was asked in an interview in IBM. What the…
Hello Folks, As part of our API Testing series, we will see “Sending GET request with params in Postman”. In last…
We have learnt in previous posts regarding establishing relationship between test methods. You can go through them below: Dependency in…
In previous post, We have learnt to Establish dependency among test methods. In this post, we will see another concept…