Selenium WebDriver Basics – What is Selenium?

Selenium WebDriver Basics

Selenium

Selenium is an API using which we can perform automated tests on the Application Under Test(AUT). Selenium is used to automate websites, but it cannot automate windows or android applications etc.

However, we can handle windows pop up which appears while interacting a website, for instance, print dialog, Save As prompt etc. We can use other Open Source application(AutoIt, Sikuli etc.), in conjunction with Selenium to handle these kinds of prompts.

Selenium WebDriver

A major change in Selenium API after version 2 is the introduction of WebDriver API. This WebDriver API is used to drive the browser and perform our desired actions on WebElements. WebDriver supports all the major browsers like chrome, Firefox, IE etc. WebElement is the term which refers to all the objects that could be displayed on a website(links, textbox, radio button etc.).

In Java, Selenium API is a collection of jar files, which comprise of Java Classes and Interfaces. In fact, WebDriver class in Selenium is itself an interface which is implemented by other classes. These other classes are used to drive the desired browser.

For eg., ChromeDriver java class in Selenium API implements WebDriver interface and is used to work with Chrome driver. Similarly, we have FireFox and IEDriver java classes for Firefox and IE respectively.

Selenium WebDriver as an Interface – Case Study

We have discussed in the previous section that WebDriver interface is the main change that happened to Selenium after ver. 2.0. Now using this single interface we can initialize our favorite browser instance and work on it.
Now let’s see how this implementation works. This involves java interface property, so it is assumed you are already aware of an Interface in Java.
In selenium, WebDriver has 2 methods amongst many other methods- getTitle() and findElement(). The WebDriver interface will look like:

WebDriver Interface

Now let’s get our hands dirty. Follow the steps to configure Selenium

in your eclipse:

  • Navigate to https://www.seleniumhq.org/download/ and download Selenium Client & WebDriver Language Bindings for Java.
  • Download ChromeDriver.exe(for windows).

  • Open eclipse, select any workspace and create a new Java Project.

  • Create a package inside java project and name it as ch01_WebDriverInterface.

  • Right click on the project and navigate to Build Path > Configure Build Path.

  • In the pop-up windows navigate to Library tab and select Add External jars. Browse where you have downloaded and extracted selenium jar files.

  • Select all the jar files, click on Apply and Close.

  • Right click on the package and create a new Interface. Name it WebDriver.

WebDriver Interface

Create two abstract methods inside this class, get() and findElement().

WebDriver.java (Interface)
package ch01_WebDriverInterface;

public interface WebDriver {
 
 public void get();
 public void findElement();

}

Create two separate Classes inside this same package, ChromeDriver and FireFoxDriver.

ChromeDriver.java
package ch01_WebDriverInterface;

public class ChromeDriver implements WebDriver {
    
    public void get() {
        System.out.println("In ChromeDriver");
    }
    
    public void findElement() {
        System.out.println("In ChromeDriver");
    }

}
FirefoxDriver.java
package ch01_WebDriverInterface;

public class FireFoxDriver implements WebDriver {
    public void get() {
        System.out.println("In FireFoxDriver");
    }

    public void findElement() {
        System.out.println("In FireFoxDriver");
    }App.java

}

Once implementation of WebDriver is done, we will create a main

class to test this functionality.

App.java
package ch01_WebDriverInterface;

public class App {

    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        WebDriver driverFF = new FireFoxDriver();
        
        driver.get();    //In ChromeDriver
        driverFF.get();    //In FireFoxDriver
        driver.findElement();    //In ChromeDriver
        driverFF.findElement();    //In FireFoxDriver
    }

}

As it is evident from the above example, single WebDriver interface is used to drive all the browsers, using their respective client libraries.

In the above example, we learned how to add Selenium jars and set up a Java project for browser automation, though we didn’t use any of its features. We will see how to use the WebDriver to automate our AUT in upcoming tutorials.

Author: Dhawal Joshi

A post-graduate in MCA, ISTQB & ITIL certified QA with more than 8 years of experience in QA working with a CMMI Level 5 organization as System Analyst. I started my automation journey with HP UFT(formerly known as QTP) and for the past few years, I am using Selenium for automation. I also have experience in Android Application Development, Java, HTML, and VBScript. When I am not working, I like to spend time with my family, cooking and learning new developments in IT.

4 thoughts on “Selenium WebDriver Basics – What is Selenium?

Leave a Reply

Your email address will not be published. Required fields are marked *