Confusion over the WebElement interface and it's methods

327 views
Skip to first unread message

farooq basha

unread,
Jan 22, 2017, 12:46:28 AM1/22/17
to Selenium Users
Hi All,

First of all I am quite new to selenium,I have a question which I think would be answered by the experts over here.

WebDriver obj = new ChromeDriver();

perfectly fine as WebDriver is the interface and it is implemented by many classes and ChromeDriver is one among them,so it's perfeclty valid to assign the reference variable to the implementing class and we can access the methods which are define in the interface.

but 

WebElement var= driver.findElement(By.id("value"));

var.method();

So I am confused with the above statement as "WebElement is the interface" and it's reference variable is not assigned to any of it's implementing class.

We are just assigning it to the other interface and we are able to access the methods of WebElement.

Can someone help me out with this.


Thanks in advance


Simon Stewart

unread,
Jan 23, 2017, 10:07:07 AM1/23/17
to seleniu...@googlegroups.com
This stuff is covered in many tutorials introducing programming languages. It might be useful to do some reading up on Java, and specifically its "type system". Java is a strongly, statically typed language.

You'll find that the ChromeDriver returns a "RemoteWebElement", which implements the "WebElement" interface. As a rule of thumb, it's best to refer to the least specific type that conveys meaning in your code (that is, always prefer to have variables have the type "WebElement" rather than "RemoteWebElement" in your code)

Simon

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/e2a5299b-580f-4695-ad1a-fe5c26321ae2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gopi

unread,
Jan 24, 2017, 12:50:59 AM1/24/17
to Selenium Users
A aa = new AImpl();
aa.a();

Here your reference variable is interface A type But actual Object is AImpl.

When you define a new interface, you are defining a new reference data type. You can use interface names anywhere you can use any other data type name. If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface.

If your audience is also interested in Selenium Automation Testing, they can take a look here: Selenium Training 
Reply all
Reply to author
Forward
0 new messages