When we first start with selenium automation our very first line code comes as :
WebDriver driver = new FireFoxDriver
Simple answer to this is WebDriver is an Interface,and
we are defining a reference variable(driver) whose type is an interface .Now
any object we assign to it must be an instance of a class(FireFoxDriver) that
implements the interface."
Note : Firefox driver is included in the
selenium-server-stanalone.jar available in the downloads. The driver comes in
the form of an xpi (firefox extension) which is added to the firefox profile
when you start a new instance of FirefoxDriver.
WHAT IS AN INTERFACE
Interface is like a blueprint of Class. It contains
variables and body less methods(Abstract methods), where we just declare
methods but we implement them inside the class which inherit Interface.
EXAMPLE
package com.package1;
public interface WebDriver {
public void getUrl(String Url);
}
Above example just contain the
structure of a get method, now a class which
would implement this interface need to define the method.
Now we will create a class which will implement the interface
Now we will create a class which will implement the interface
package com.package1;
public class FirefoxDriver implements WebDriver{
@Override
public void getUrl(String Url) {
System.out.println("Url is
:"
+ Url);
}
}
Class that implements an interface must
implement all the methods declared in the interface. Now our FirefoxDriver
class should implement all the methods declared inside an WebDriver interface,
same is the case with ChromeDriver or IEDriver classes.
package com.package1;
import
org.testng.annotations.Test;
public class ImplementationClass
{
@Test
public void test(){
WebDriver
driver=new FirefoxDriver();
driver.getUrl("http://facebook.com");
}
}
So from here we have understood that “Webdriver” is an
interface and “FirefoxDriver” is an
implementation class of the interface.
INTERFACE INSTANCE
We can create a reference variable of an interface but we
can't instantiate any interface since it is just a contract to be implemented
in a Class.
WebDriver driver = New WebDriver()
To understand the concept of WebDriver driver=new
WebDriver() , you have to know the concept behind the anonymous inner class
using Interface.
package com.package1;
public interface WebDriver {
void eat();
}
package com.package1;
public class ImplementationClass
{
public static void main(String[] args) {
WebDriver
driver= new WebDriver() {
@Override
public void eat() {
System.out.println("Nice
Fruits");
}
};
driver.eat();
}
}
Output here is : nice fruits
INTERNAL WORKING OF THE CODE
It performs two main tasks behind this code:
WebDriver driver= new
WebDriver(){
void eat(){
System.out.println("nice fruits");}
};
A class is created but its name is decided by the compiler
which implements the Eatable interface and provides the implementation of the
eat() method.
An object of Anonymous class is created that is referred by
p reference variable of Eatable type.
INTERNAL CLASS GENERATED BY THE COMPILER
import java.io.PrintStream;
static class TestAnonymousInner1$1 implements WebDriver
{
TestAnonymousInnerClass(){}
void eat(){System.out.println("nice
fruits");}
}
So coming on to our topic : WebDriver driver = new
WebDriver();
This means that we are simultaneously creating an
anonymous class that implements the WebDriver interface and also creating an
instance of that anonymous class.
1 comment:
https://gist.github.com/anonymous/3936cf81c6ec5c6d845f3480e89dcf60
Post a Comment