What is Selenium WebDriver? How to setup Selenium?

What is Selenium?

Selenium is an umbrella project for a range of tools and libraries that enable and support the automation of web browsers.

It provides extensions to emulate user interaction with browsers, a distribution server for scaling browser allocation, and the infrastructure for implementations of the W3C WebDriver specification that lets you write interchangeable code for all major web browsers.

Selenium is divided into 3 different frameworks –

1. Selenium WebDriver

If you want to create robust, browser-based regression automation suites and tests, scale and distribute scripts across many environments, then you want to use Selenium WebDriver, a collection of language specific bindings to drive a browser – the way it is meant to be driven.

2. Selenium IDE

If you want to create quick bug reproduction scripts, create scripts to aid in automation-aided exploratory testing, then you want to use Selenium IDE; a Chrome and Firefox add-on that will do simple record-and-playback of interactions with the browser.

3. Selenium Grid

If you want to scale by distributing and running tests on several machines and manage multiple environments from a central point, making it easy to run the tests against a vast combination of browsers/OS, then you want to use Selenium Grid.

What is Selenium WebDriver?

WebDriver drives a browser natively, as a user would, either locally or on a remote machine using the Selenium server, marks a leap forward in terms of browser automation.

Selenium WebDriver refers to both the language bindings and the implementations of the individual browser controlling code. This is commonly referred to as just WebDriver.

Selenium WebDriver is a W3C Recommendation

  • WebDriver is designed as a simple and more concise programming interface.
  • WebDriver is a compact object-oriented API.
  • It drives the browser effectively.

How Selenium WebDriver works?

At its minimum, WebDriver talks to a browser through a driver. Communication is two way: WebDriver passes commands to the browser through the driver, and receives information back via the same route.
The driver is specific to the browser, such as ChromeDriver for Google’s Chrome/Chromium, GeckoDriver for Mozilla’s Firefox, etc. The driver runs on the same system as the browser. This may, or may not be, the same system where the tests themselves are executing.

selenium webdriver internal working onlyfullstack

How to setup Selenium?

1. Create new maven project
Lets create a maven project for our automation testing.

selenium new project onlyfullstack
2. Add Selenium dependancy

Add below selenium-java maven dependancy to your pom.xml as below –

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>selenium-tutorial</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.1.0</version>
        </dependency>
    </dependencies>
</project>

3. Download the WebDriver
Through WebDriver, Selenium supports all major browsers on the market such as Chrom(ium), Firefox, Internet Explorer, Opera, and Safari. Where possible, WebDriver drives the browser using the browser’s built-in support for automation, although not all browsers have official support for remote control.

WebDriver is an interface and all the browsers implement this interface to provide automation support. We need to download the WebDriver of the browser on which we what to do the testing. Selenium uses this WebDriver to do the automation with our code. Below is the list of supported browsers by Selenium and it also have the WebDriver links which we will have to download before doing the automation. Here I will be using chrome browser to do the automation for this tutorial. So first of all you need to see the version of your chrome browser.

selenium tutorial webdriver setup version
My Chrome version is 80.0.3987.132 so I will go to the below link –
This link will list all the versions of Chrome browser. Now I will go to the version which is matching to my browser version.
selenium tutorial webdriver setup chrome version onlyfullstack

If you go inside your version it will show you 3 links for different Operating Systems.

selenium tutorial webdriver setup version chrome

Below is the list of other browser and their WebDriver link –

Browser Supported OS Maintained by Download
Chromium/Chrome Windows/macOS/Linux Google Downloads
Firefox Windows/macOS/Linux Mozilla Downloads
Edge Windows 10 Microsoft Downloads
Internet Explorer Windows Selenium Project Downloads
Safari macOS El Capitan and newer Apple Built in
Opera Windows/macOS/Linux Opera Downloads

4. Write simple test case
Don’t worry about the code, we will discuss it in our next blog. Below code is just opening the https://onlyfullstack.blogspot.com/2020/03/best-demo-website-to-practice-selenium.html and will print the title of the page.

package com.onlyfullstack;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;

public class FirstTest {

    public static void main(String[] args) throws InterruptedException {

        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://onlyfullstack.blogspot.com/2020/03/best-demo-website-to-practice-selenium.html");

        Thread.sleep(3000);

        System.out.println("Page title - " + webDriver.getTitle());
        webDriver.close();
    }
}

Lets run this program with Run As main() method. We will get the IllegalStateException as we haven’t specified WebDriver for our Chrome browser.

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html

5. Specify the WebDriver path
We need to specify the path of the WebDriver which we have downloaded. Selenium uses this WebDriver to communicate with our local Browser. Now lets specify the WebDriver through code.
We will use System.setProperty method to set the environment variable and provide the path of our downloaded WebDriver.

package com.onlyfullstack;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;

public class FirstTest {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, "chrome-web-driver/chromedriver");
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://onlyfullstack.blogspot.com/2020/03/best-demo-website-to-practice-selenium.html");

        Thread.sleep(3000);

        System.out.println("Page title - " + webDriver.getTitle());
        webDriver.close();
    }
}

This program will open the https://onlyfullstack.blogspot.com/2020/03/best-demo-website-to-practice-selenium.html and will print the title of the page.

So now you are all set to run the Selenium on your system.

Lets understand the Selenium Program

Here we are setting the “webdriver.chrome.driver” property with the chrome web driver path which we have downloaded. We need to specify the path of the WebDriver which we have downloaded. Selenium uses this WebDriver to communicate with our local Browser. Now lets specify the WebDriver through code.We are using the System.setProperty method to set the environment variable and provide the path of our downloaded WebDriver.

System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, "chrome-web-driver/chromedriver");

WebDriver is an interface which define the specification. All the browsers implement this specifications. Here we are creating an object of the ChromeDriver().

WebDriver webDriver = new ChromeDriver();

get method will open the browser with the url passed as an argument.

webDriver.get("https://onlyfullstack.blogspot.com/2020/03/best-demo-website-to-practice-selenium.html");

We are calling the Thread.sleep method so the browser will be open for 3 seconds and then it will print the title of the page.

Thread.sleep(3000);
System.out.println("Page title - " + webDriver.getTitle());

close method is used to close the browser opened by the web driver.

webDriver.close();

Lets go to our next tutorial where we will discuss below points :

Part 2 – Locators in Selenium WebDriver – Id, Name, Class Name, Tag Name, CSS, XPath

– Locators in Selenium WebDriver
– What is HTML element?
– Locators in Selenium WebDriver
– Finding the html element information using Chrome / Firefox

          – How to find the elements in Selenium?
      – 1. Id as Locator in Selenium WebDriver
– How to use Developer Tool to view HTML element
      – 2. Class Name as Locator in Selenium WebDriver
– 3. Name as Locator in Selenium WebDriver
      – 4. XPath as Locator in Selenium WebDriver
– 5. Tag Name as Locator in Selenium WebDriver
      – 6. CSS Selectors in Selenium WebDriver

– 7.2. Partial Link text selector

Source Code
You can find the complete source code on below GitHub repository – 
https://github.com/onlyfullstack/selenium-tutorial

Selenium Tutorial
https://www.onlyfullstack.com/selenium-webdriver-tutorial/