Selenium WebDriver – Installation & Configuration

By Sandhya

Selenium-WebDriver  is  an elegant programming interface and a compact Object Oriented API. It is an efficient tool for testing web applications  in different browsers irrespective of the programming language of the application. WebDriver makes direct calls to browser using each browser’s native support.

I will discuss here the installation and configuration of WebDriver, illustrating the process with a simple example.

Step 1:

Check  whether java is installed in your machine   [enter  command –   java – version in command prompt] . If your machine is not having java ; download and install java jdk [download JDK].

 

fig1

Now set java path.  [How to set path]

Step 2:

Download and install Eclipse IDE [ Download Eclipse IDE] . Extract the downloaded Zip file, Installation  is not required to use Eclipse.

fig2

Step 3:

Download Selenium JavaClientDriver  [JavaClientDriver]; extract the JavaClientDriver.

fig3

Sample WebDriver program below:

i) Open Firefox browser, go to Google website.

ii) Click on Gmail link, get the image dimensions and print , get the Title and print .

iii) Enter username and  password then check the stay signed in check box and click on sign in .

iv) Navigate to browser back and then close the browser .

Step 1: Open Eclipse ,select default location for work space.

fig4

Step 2:

Go to File>New>Java Project . Name the Project as Google and click on Next.

Go to Libraries and Add External Jar files which we downloaded in step 3 of installation process. [selenium-java-2.44.0, selenium-java-2.44.0-srcs and all jars  from lib folder].

Click on Finish after adding the jar files.

fig5

fig6

Step 3:

Right click on the project [Google] > New >Class to create class under the project.

Give class name as GoogleClass and select method “public static void main” while creating class.

fig7

You should see the screen with GoogleClass as  shown  below.

fig8

 

Step 4 : Now it’s time to write some code

  1. i) Open Firefox browser, go to Google website.

Create Class Object and a Method to Open browser

> Create ClassObject under main method

> Create openBrowser method under class

Write code to 1. launch the Firefox browser and

  1. Open the Google application and
  2. maximize the browser window.

Create WebDriver Object for Firefox browser.

import below packages for above statement

Open the Google application.

Maximize the window

call the openGoogle() from main method using class Object.

 ii) Click on Gmail link, get the image dimensions and print , get the Title and print .

> Create another method for gmailSignIn.

> Click on Gmail link.

find the element and then click on it .

Open the Firefox browser go to Google application and then press  f12 from keyboard

, it will open a fire bug select .

Select inspect element button located at left most  of the fire bug panel. click on the element [Gmail] for which you want the attribute type and value. It will be shown in the firebug html.  See the Screen shot here.

If you don’t have firebug please install [click here to download ],it is an add on to  Firefox browser.

fig9

Then click on it.

> get the image dimensions

Find the image [web element] attribute and value using firebug and save the image           as WebElement.

import below package for above statement

import org.openqa.selenium.WebElement;

Get the dimensions from the saved web element and print height and width

import below package for dimension statement

import org.openqa.selenium.Dimension;

>get the title and print [we have driver object method to get title]

iii) Enter Input values in username and in password fields and then check the stay signed in           check box status is selected or not, if not selected select it and click on sign in .

> Enter input values in user name and password fields . As I mentioned earlier we  need to know the attribute type and value for web elements on which we going to perform action.

find Attribute type [id/name/classname/link/xpath/css selector etc] and its value  using fire bug.

write method for action you wanted to do, on the element . In my example we need to input the username and password, web element has sendkeys() method to enter values.

As I use eclipse for WebDriver programming the methods are automatically shown; we need to choose appropriate method.[am sharing the screen here]

fig10

> check the stay signed in check box status is selected or not, if not selected select it      and click on sign in .

 iv) Navigate to browser back and then close the browser .

 Driver object methods can be find just type driver. ,the list will be shown to you select appropriate method to browser navigate back . Am sharing my screen here.

fig11To close browser write driver.quit();

I have written another method to close the browser. You can write it in the earlier method also.

Shared sample code screen shot below :

fig12

The output you get to see in the console is

96,96

Google+

already selected/not selected.