About SELENIUM WEBDRIVER-3

Read it in 10 Mins

Last updated on
08th Jun, 2022
Published
26th Jun, 2017
Views
1,137
About SELENIUM WEBDRIVER-3

Why WebDriver-3?

In present market we have a tool called IDE. It is a tool which works only on Mozilla firefox. It has many drawbacks and doesn’t generate proper reports. IDE is not a preferred tool because sometimes when the scripts run, it finds the locator and sometimes it doesn’t find it and so the script fails. So, to overcome all these challenges we have a tool called WebDriver.

WebDriver works on Multiple browsers like Firefox, IE, Chrome, Safari, Opera etc. It supports multiple languages like Java, C#, Ruby, Python and JavaScript. With WebDriver, we can make dynamic scripts like scripts on Webpages which changes everyday and we can make our scripts run on multiple browsers.

Downloading WebDriver-3 Jars and configuring in eclipse

To start with WebDriver, the first thing we need to do is to download WebDriver Jars and configure it in Eclipse IDE. We need to download Jar files of WebDriver from the official website of Selenium, which is Selenuimhq.org where we need to go to download section and under the download section, we will see the latest version which is 3.4.0. Click on the link and file will be downloaded in the downloads folder.

Also, click on Change log at Selenium website to describe what has changed in the new version. The last 2.x version was 2.53 which is a stable build, meaning it has no major issues. After 2.53, the next build which came up was 3.XX. The changes made in 3.0 is that, minimum java version requires 1.8, where we need to make sure that jdk8 is installed.

Installing Java

  1. To install java, Go to Google and type download Jdk.
  2. Go to first link as shown below

Depending upon the system, we need to install either Windows x32 or Windows x64 bit and accept the License Agreement. For downloading Java, we need to click on Download link, where Zip file will be downloaded on our PC. Go to downloaded folder and extract it and double click on exe. Java will be installed in the system and now we need to configure the class path.

If we are using v2.53.0 version, then we will be needing Firefox 40 or below. In case, we are using 3.0 then we need to go for latest version of Firefox.

Configuring Jar Files in Eclipse

  • Step1: Open Eclipse and make a New Project (ex:Sample)
  • Step2: Right click on the project and select the option Properties as shown below

  • Step3: Go to Java Build Path > Libraries Tab and click on Add External Jars as shown below:

Now, go to the location where we saved our Jar Files and add all the jar files. It will appear like below image after adding all Jar files:

Once we are done with configuring Jar files in eclipse, we can see a folder called Referenced Libraries under Project folder where we have all our Jar Files.

Architecture of selenium webdriver 3

First we need to know a few concepts if we want to understand the working of Selenium WebDriver Architecture.

  1. Language Binding or Selenium Client Library
  2. Selenium API
  3. Remote WebDriver
  4. Json Wired Protocol
  5. WebDrivers
  1. Language Binding or Selenium Client Library : It is nothing but a collection of Jar files, where the language in which we write our selenium framework is present. It means that the language which we used to write the script may be Java, C#, Ruby, Python, perl.
  2. Selenium API : API Stands for Application Programming Interface. API is a particular set of rules and specifications that software programs can follow to communicate with each other. API serves as an interface between software program and facilitate their interaction. API is a software to software interaction which means API works between software to software. With the help of APIs, applications talk to each other without any user knowledge.
  3. Remote WebDriver : It is an implementation class of the webdriver interface that a test script developer can use to execute their test script through webdriver server on remote machine.
  4. JSON:  It stands for JavaScript Object Notation. It is a lightweight data interchange format which helps us to interchange the data. JSON is used to transfer data between client and server on the web. A simple JSON file has .json extension. JSON wired protocol send data in JSON format. Thereafter, the server will firstly parse the data and execute it and after that, response given by the server will be sent back to the client in JSON format.
  5. Webdriver: Webdriver is a tool for automating web applications and verifying that     they work as expected.

 Architecture:

Tester writes script in any language like java, C# etc and selenium client library will communicate with selenium API. Selenium API will send the command taken from language level binding to browser driver with the help of JSON wired protocol and sends the request to Browser Driver, it may be Firefox driver, IE driver, Chrome driver. When we run all the commands we write in the script, it is converted with JSON wired protocol over HTTP and it will convert into URL and the request made by the HTTP and send to the browser. These browser drivers will use the HTTP server for getting HTTP request and these HTTP Server filter out all commands which we need to execute.

Drivers for Firefox, IE, Chrome

  • FirefoxDriver: It is an inbuilt class in Selenium. Lets create a New Class in Eclipse

Class Name: Drivers_Selenium

Inside main function, create an object of FirefoxDriver class like shown below:

FirefoxDriver is an inbuilt class inside Referenced Libraries jars, where we can move mouse over the error and import FirefoxDriver class. After creating the object and running the program, we will get an exception like “The path to the driver executable must be set by the webdriver.gecko.driver system property” like shown in below image.

import org.openqa.selenium.firefox.FirefoxDriver;

public class Drivers_Selenium {

public static void main(String[] args) {

// TODO Auto-generated method stub

new FirefoxDriver();

}

}

Steps to Download Geckodriver exe file:

Step1: Go to seleniumhq.org

Step2: Click on Download Tab

Step3: Click on Mozilla Geckodriver 0.16.1 version in browsers session

Step4: Click on geckodriver-v0.17.0-win64.zip or geckodriver-v0.17.0-win32.zip based upon our operating system.

Open the downloaded folder and extract it, after extracting we will get geckodriver.exe

file like shown below image.

Now there is no need to double click on the geckodriver file, all we need is to set the System property known as “webdriver.gecko.driver” and we have to give the path of  geckodriver exe file. As java is a case sensitive language make sure that “webdriver.gecko.driver” is in lower case.

Run the program where we can see a blank firefox getting opened.

Now we need to give object reference as “FirefoxDriver fd” so that we can use the features of FirefoxDriver object. Inside the documentation of selenium, we find WebDriver interface. Inside this interface, we see a function get( ) which loads the new webpage in the current browser window. It open up a new URL.We can use the function “fd.get ” and get the URL.

Run the above program where we can see the browser launching and it opens yahoo.com

  • ChromeDriver : As the name suggests, it is for chrome. After creating the object and running the program, we will get an exception like “The path to the driver executable must be set by the webdriver.chrome.driver system property”.Now we also need to download an exe file.Go to download session in seleniumhq.org and scroll down where we find Google Chrome Driver link and click on it and it will redirect to the next page. From here we can download based upon our operating system as shown in below image.Whatever be the operating system, whether it is 32xbit or 64xbit, we can download the same file.

Open the downloaded folder and extract it, after extracting we will get “chromedriver” file like shown in the below image.

Now, there is no need to double click on chromedriver file, all we need to do is set the System property known as “webdriver.chrome.driver” and we have to give the path of chromedriver.exe file. As java is a case sensitive language, make sure that “webdriver.chrome.driver” is in lower case.

Run the above program where we can see a blank chrome browser will launch.

Now we need to give object reference as “ChromeDriver ” so that we can use the features of ChromeDriver object. Inside the documentation of selenium, we find WebDriver interface.Inside this interface we see a function get( ) which loads the new webpage in the current browser window. It opens up a new URL.We can use the function “cd.get ” and get the URL.

Run the above program where we can see the Chrome browser launching and open it on yahoo.com

  • IEDriver: As the name suggests it is for InternetExplorer. After creating the object and running the program, we will get an exception like “The path to the driver executable must be set by the webdriver.ie.driver system property”.Now we also need to download an exe file.Go to download session in seleniumhq.org and scroll down where we find IE Driver link and click on it and it will reside to next page from here we can download based upon our operating system, as shown in the below image.What ever be the operating system, whether it is 32xbit or 64xbit, we can download the same file.

Open the downloaded folder and extract it, after extracting we will get “ieDriver” file.

Now no need of double click on iedriver file, all we need is to set the System property known as “webdriver.ie.driver” and we have to give the path of  ie exe file. As java is a case sensitive language, make sure that “webdriver.ie.driver” is in lowercase.

Now we need to give object reference as “IEDriver ” so that we can use the features of IEDriver object.Inside the documentation of selenium, we find WebDriver interface. Inside this interface, we see a function get( ) which loads the new webpage in the current browser window. It opens up a new URL.We can use the function “ie.get ” and get the URL.

Run the above program where we can see the IE browser launching and open it in yahoo.com

  • Launching Browsers Dynamically

Let us Create New Class “Launch Browsers”

Let us build a Testcase. Now our requirement is that we have a parameter called browser. If we write parameter as “Mozilla”, then the script we write in testcase should run in Mozilla browser. If we write a parameter as “Chrome”, it should run on chrome. Later on, we read this parameter from an excel file (xls) or any other file like property file, xml file.

If our browser is Mozilla then below script should execute on Mozilla. If the browser is Mozilla then we want to launch the FirefoxDriver and take this FirefoxDriver to launch our desired website.

In the above example, we keep on repeating the line to launch any website like fd.get(“http://gmail.com”). Later on if we want to click on a link in firefox then we need to write the code for each browsers separately. To overcome this, we use a concept called Interface reference points towards the class implementing the interface.

WebDriver   d = new FirefoxDriver( );

WebDriver   d1= new ChromeDriver( );

Webdriver is an Interface and FirefoxDriver, ChromeDriver, IEDriver are implementing that Interface. Here d1 is  pointing towards chrome, so we can make d1 point towards FirefoxDriver as well , because the reference of the webdriver interface can point towards any implementing class or we can make d1 point towards InternetExplorerDriver as well, like shown below.

d1 = new FirefoxDriver( );

d1 = new InternetExplorerDriver( );

So in the beginning of the program we can write

WebDriver driver  =  null;

Here Null means that driver is not pointing anywhere. So based on our choice, we can make driver point towards FirefoxDriver (or) ChromeDriver (or) InternetExplorerDriver as shown in the below program. Initially driver is “null”. Based on the options, driver will be initialized and we can give the command driver.get(“http://gmail.com”);

All the driver classes will have same set of functions coming from WebDriver Interface,

so “get” function will be there in all the driver classes.

From the above program, we can launch the browser dynamically and we have to write a single script after launching the browser to interact with the browser. We do not need to write browser specific script. The reasons are : firstly all the drivers have same set of functions and secondly, the driver can point towards any of the driver classes like ChromeDriver, IEDriver.

  • Selenium RC and WebDriver

  • What is Firefox profile

Let’s take an example where we have a PC at our place, being shared by two people. If the first person opens the PC and goes to Firefox, then firefox should get opened and it should have 1st person set of settings, his own set of bookmarks, his own set of plugins installed inside it. If a 2nd person opens PC and goes to Firefox, he should have his own set of settings, his own set of bookmarks, his own set of plugins. This concept is know as Firefox Profile.

  • Importance of Firefox profile

First where selenium launches Firefox, we write-

driver  = new FirefoxDriver( );

Selenium will create its own profile and launch the browser. That profile is created programmatically by selenium and it gets destroyed when the script stops.

  • Creating Profile manually on our PC

Go to Firefox browser -> Go to File option and select Exit to close the firefox from the Exit menu, we should not close the firefox from the (x) option on the top as shown in below image.

Go to windows session and type “firefox.exe -p profilemanager”. Copy the command and it will open the window as shown below.

Click on Create Profile and give name as Selenium_Mod11. Click on the Finish button and click Start Firefox button where a new firefox browser will open up and this firefox browser will be like freshly installed firefox on our machine. It will have no bookmarks, no plugins, no history etc.

Go to Firefox browser and bookmark the page as shown below

Create a New Class -> Selenium_Profiles

Run the program where it will open Firefox browser with Google Bookmark as shown in below image

  • Close and Quit Difference

driver.quit( ): It will end selenium session and close all browsers in the current session.

Suppose we go to a website and click on a link, that link will open in new pop-up and if we fire driver.quit( ), then selenium will close all the browsers and end the selenuim session.

driver.close( ): It won’t end selenuim session, but it closes the browser on which current focus is there.

  • Importing Webdriver documentation in eclipse.

Go to seleniumhq.org –> Go to download session and click Javadoc link and copy the URL.

https://selenium.dev/selenium/docs/api/java/index.html” and go to the project -> Right click and go to Properties->Go to Java Build Path and expand the link and click on the Javadoc location: (None).

Click on Edit button and paste the URL and remove index.html and click on validate button and click OK button.

  • Webdriver Desired Capabilities Class

It helps us understand Selenium, and under which circumstances the script should run. Below example shows how we should run script on Mozilla with Javascript enabled on a windows platform.

 

Profile

Raghav Globalservices

Blog Author

Raghav has 8+ years of experience with 7+ years of experience in Automation Testing using Selenium, Appium, Ranorex tools etc. in multiple project domains. He is a Consultant &amp; Trainer has trained more than 6000+ US, UK, Russian &amp; Indian students &amp; is a corporate Trainer for several MNCs. <a href="https://www.linkedin.com/in/aroraraghav/" rel="no follow">linkedin.com</a>