Quick Contact

    WebDriver Interface
    Introduction

    Interface is like a blueprint of Class. It contains variables and body less methods i.e. Abstract methods. An object to interface can’t be create but classes can be created where the abstract methods of the interface can be implemented. The classes which implement the abstract methods of interface are known as implementation classes.

    The primary new feature in Selenium 2.0 is the integration of the WebDriver API. WebDriver is designed to provide a simpler, more concise programming interface in addition to addressing some limitations in the Selenium-RC API. Selenium-WebDriver was developed to better support dynamic web pages where elements of a page may change without the page itself being reloaded. WebDriver’s goal is to supply a well-designed object-oriented API that provides improved support for modern advanced web-app testing problems. Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation. How these direct calls are made, and the features they support depends on the browser you are using. Information on each ‘browser driver’ is provided later in this chapter.

    For those familiar with Selenium-RC, this is quite different from what you are used to. Selenium-RC worked the same way for each supported browser. It ‘injected’ javascript functions into the browser when the browser was loaded and then used its javascript to drive the AUT within the browser. WebDriver does not use this technique. Again, it drives the browser directly using the browser’s built in support for automation.

    WebDriver Architecture

    Selenium WebDriver’s architecture is designed in a way that it talks to the browser in its native language. In order to write WebDriver code to work with Firefox, code for Firefox need to be written. If an Interface with abstract methods can be written then a message can be send to all the browser companies i.e. the third party companies to provide their implementation classes for an Interface. To implement the abstract methods of WebDriver interface in their way, there are separate class files for FirefoxDriver ,ChromeDriver.

    WebDriver driver = new FirefoxDriver(); or WebDriver driver = new ChromeDriver()

    Above will be implementing rules of interface WebDriver over the third party browser class files Firefox and Chrome. FirefoxDriver() and ChromeDriver() methods are defined in the class files FirefoxDriver and ChromeDriver class files respectively.

    Example of the structure of a get method

    Public Interface WebDriver()
    {
    
    
    int iNum = 10;
    public void get(String URL);
    
    
    }
    
    

    Example of a class which would implement the interface

    Public class FirefoxDriver implements WebDriver()
    {
    public void get(String sURL)
    {
    System.out.println("URL="+sURL);
    }
    }
    

    In above example a class is FireFoxDriver and Interface is WebDriver.

    Interface instance

    A reference variable can be created of an interface but any interface can’t be instantiate since it is just a contract to be implemented in a Class. For example

    WebDriver driver = New WebDriver ...Not allowed,

    The correct implementation is

    WebDriver driver = New FireFoxDriver(); driver.get(testUrl);
    Important points
    • Class that implements an interface must implement all the methods declared in the interface. For example, FirefoxDriver class should implement all the methods declared inside a WebDriver interface, same is the case with ChromeDriver or IEDriver classes.
    • While implementing methods, you must follow the exact same signature (name + parameters) as declared in the interface
    • Object of an Interface can’t be instantiate/create.
    • All the variable inside an interface are by default Final
    • Class cannot Extend Interface only Implements it
    • Interface an Extend another Interface but then the class which implements the interface need to implemented the methods of both interface
    • Class can implement multiple Interface(Remember class cannot extend multiple classes, multiple inheritance in class is not possible)
    • Interface cannot hold constructor.
    • Interface cannot hold instance fields/variables.
    • By default all the methods of Interface are public so no need to provide access modifiers.
    • An interface can have another interface i.e. known as nested interface.
    Abstract Class v/s Interface

    Abstract class is similar to interface and contain a mix of methods declared with or without an implementation. But in abstract class, we can declare fields that are not static and final, and define public, protected, and private concrete methods. With interfaces, all fields are automatically public, static, and final, and all methods that are declared or defined (as default methods) are public. Any number of interfaces can be implement unlike abstract class which can extend only one class.

    How to Add Interface In the Project

    To add Interface In the project, Right click on package and go to -> New -> Interface. It will open New java Interface popup. Give any name to Interface (example: College) and click on save button. It will add College.java Interface file under the package.

    Now let us see at simple example of Interface.

    1. Create One Interface file with name = College.java as shown in below example.
    2. Create 3 class file with name = Computer.java, Mechanical.java and TestCollege.java as shown in below example.
    College.java
    public interface College {//Interface file
    //Initialized static variable.
    //No need to write static because It Is by default static. String Collegename = "XYZ";
    
    //Created non static methods without body. 
    void StudentDetails();
    void StudentResult();
    }
    
    Computer.java
    //Class file Implemented with Interface file using implements keyword.
    public class Computer implements College {
    
    
    //@Override annotation describes that methods are overridden on
    //interface method.
    //Methods name, return type are same as methods of an Interface. @Override
    public void StudentDetails() { 
    System.out.println("Computer Dept. Student Detail Part");
    }
    
    
    @Override
    public void StudentResult()  { 
    System.out.println("Computer Dept. Student Result Part");
    }
    }
    
    Mechanical.java
    //Class file Implemented with Interface file using implements
    //keyword.
    public class Mechanical implements College{
    
    
    @Override
    public void StudentDetails() { 
    System.out.println("Mechanical Dept. Student Detail Part");
    }
    
    
    @Override
    public void StudentResult()  { 
    System.out.println("Mechanical Dept. Student Result Part");
    }
    }
    
    TestCollege.java
    public class TestCollege {//Class file. No need to implement Interface.
    
    
    public static void main(String[] args) {
               //Can access Interface variable directly using
    Interface name.
    System.out.println(College.Collegename+" Collage student details.");
    
    
    //Created Computer class object with reference of interface
    //College.
     College compdept = new Computer();
    //Methods will be called from Computer class.             
     compdept.StudentDetails();
     compdept.StudentResult();
    
    //Created Mechanical class object with reference of interface
    //College.
    College mecdept = new Mechanical();
    
    //Methods will be called from Mechanical class.
     mecdept.StudentDetails();
     mecdept.StudentResult();
    }
    }
    

    3. Now if you will run TestCollege.java file, Output will looks like below.

    XYZ Collage student details.
     Computer Dept. Student Detail Part
     Computer Dept. Student Result Part
     Mechanical Dept. Student Detail Part
     Mechanical Dept. Student Result Part
    
    WebElement Interface

    WebElement represents an HTML element. HTML documents are made up by HTML elements. HTML elements are written with a start tag, with an end tag, with the content in between. For example

    <tagname> content </tagname>

    The HTML element is everything from the start tag to the end tag. For example:

    <p> My first HTML paragraph. </p>

    HTML elements can be nested (elements can contain elements). All HTML documents consist of nested HTML elements.

    <html>
    <body>
    <h1> My First Heading </h1>
    <p>My first paragraph. </p>
    </body>
    </html>
    
    List of WebElement Commands

    The methods followed by Object keyword are the generic methods gets from Object Class in Java. The findElement command of WebDriver always returns WebElement. To get the WebElement write object below statement

    WebElement element = driver.findElement(By.id(“UserName“));

    And now ontype element dot, Eclipse’s intellisence will populate the complete list of actions just like the above image. WebElement can be of any type, such as Text, Link, Radio Button, Drop Down, WebTable or any HTML element. But all the actions will always populate against the any element irrespective of whether the action is valid on the WebElement or not. For example clear() command, even for a link element still there is the option to choose clear() command on it, which on choose may result in some error or may not does anything.

    Launching Firefox Browser

    The Selenium WebDriver calls the native methods of the different browsers to automate them. In Selenium there are different WebDrivers for different browsers such as FirefoxDriver for Firefox browser, ChromeDriver for Google Chrome, InternetExplorerDriver for Internet Explorer.

    Firefox is one of the most traditional widely used browsers in automation. Thus Selenium WebDriver do not require any additional utility to be set before launching the browser. The Selenium package automatically references towards the default location of the firefox.exe, thus the user need not to set any other property. FirefoxDriver comes as a part of Selenium package and is present in xpi format. Now in order to launch Firefox with Selenium 3, GeckoDriver will be needed.

    GeckoDriver

    Gecko is the proprietary web browser engine developed by Mozilla. GeckoDriver is a proxy which is used to run Selenium 3 tests in Firefox. GeckoDriver is compatible with all versions of Mozilla Firefox, unlike Selenium 2. This essentially means that Mozilla will have to introduce a new version (or update) of GeckoDriver with every new release of Firefox.

    The command to launch Firefox browser is:

    WebDriver driver = newFirefoxDriver();

    This is the java implementation of launching a browser in Selenium. Here, ‘WebDriver’ is an interface and are creating a reference variable ‘driver’ of type WebDriver, instantiated using ‘FireFoxDriver’ class.

    For those who are not very proficient in Java, an interface is like a contract that classes implementing it must follow. An interface contains a set of variables and methods without any body (no implementation, only method name and signature). As objects cannot be instantiate from interfaces. Hence, the below line of code is incorrect and throws compile time error saying “Cannot instantiate the type WebDriver”.

    WebDriver driver = new WebDriver();

    For instantiation of driver object, the classes like FirefoxDriver or ChromeDriver which have implemented the WebDriver interface are use. In other words, these driver classes have followed the contract of WebDriver by implementing all the methods of the WebDriver interface.

    A reference variable of type FirefoxDriver can be create like this-

    FirefoxDriver driver = newFirefoxDriver();

    But having a WebDriver reference object helps in multi-browser testing as the same driver object can be used to assign to any of the desired browser specific driver. To launch Firefox with Selenium Geckodriver, first download Geckodriver and then set its path. This can be done in two ways as depicted in the below diagram.

    Install Geckodriver

    The steps to install and configure Geckodriver are:

    1. Check if Firefox is 32-bit or 64-bit

      There are two versions of Geckodriver for Windows: 32-bit and 64-bit. Based on whether the Firefox is 32-bit or 64-bit, download the corresponding Geckodriver exe

    2. Open Firefox on the machine. Click on Hamburger icon from the right corner to open the menu as shown below
    3. From this menu, click on Help icon (Help icon is marked in red box in the above image)
    4. Once click on Help icon, the Help Menu would be displayed
    5. Click on About Firefox from the Help menu. About Mozilla Firefox popup would be displayed
    6. Note down whether Firefox is 32 or 64 bit. Here, Firefox is 64-bit as shown in the above image. Now close this popup and close Firefox as well.
    7. Download the latest version of Selenium Geckodriver
    8. Follow the steps given below to download Geckodriver
    9. Open this Github page – https://github.com/mozilla/geckodriver/releases
    10. Download the latest release (windows version) based on whether your Firefox is 32-bit or 64-bit. We are downloading geckodriver-v0.20.1-win64.zip, as we have 64-bit Firefox
    11. Once the zip file is downloaded, unzip it to retrieve the driver – geckodriver.exe

    There are 2 methods using which this driver can be configure in the project.

    • Launch Firefox Method 1 : webdriver.gecko.driver system property Follow the steps given below to use this method
      1. Copy the entire path of unzipped geckodriver.exe. Let us assume that the location is –
        D:\Firefox\geckodriver.exe. System.
        setProperty will be need to add with the driver location to the code.

        The code to launch Firefox browser would look like this. In the folder paths in the below code, double backslash (\\) is used. This is because Java treats single back slash (\) as an escape character. So use double back slash, where it add some folder path.

        public class FirefoxTest {
        public static void main(String[] args) {
        
        System.setProperty("webdriver.gecko.driver","D:\\Firefox\
        \geckodriver.exe");
                  WebDriver driver = new FirefoxDriver();
                  driver.get("http://www.google.com");
        }
        }
        
      2. Run the code to verify that everything is working fine. The google.com gets opened in new Firefox window
    • Launch Firefox Method 2 : Set property in Environment Variables
      1. Copy the entire folder location where geckodriver.exe is saved. If the entire path is

        D:\Firefox\geckodriver.exe, then the folder location would be D:\Firefox\
      2. Open advanced tab in System Properties window as shown in below image.
      3. Open Environment Variables window.
      4. In System variables section, select the Path variable (highlighted in the above image) and click on Edit button. Then add the location of Geckodriver (D:\Firefox\), to path variable (below image shows UI for Windows 10)
      5. If Windows 7 is using, then move to the end of the Variable value field, then add a semi-colon (;) and then add the folder location as shown below (Semicolon acts as a separator between multiple values in the field)
      6. Click on Ok button to close the windows. Once the path is set, the System property is not need to set every time in the test script. The test script would simply look like this
        public class FirefoxTest {
                public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("http://www.google.com")
                 }
                 } 
      7. Run the code to check that it works fine.
    Summary
    • Selenium WebDriver’s architecture is designed in a way that it talks to browser in its native language
    • WebElement represents an HTML element. HTML documents are made up by HTML elements
    • The Methods followed by Object keyword are the generic methods gets from Object Class in Java
    • The Selenium WebDriver calls the native methods of the different browsers to automate them

    Copyright 1999- Ducat Creative, All rights reserved.