Skip to content

Category: Selenium



SELENIUM by Ravi H Basawa



This will be a Full Tutorial on Selenium Automation Tool
I will be updating this thread as I get time. SO NO ETA’s.


If anybody wants to use / copy this tutorial to their Website or Blog please feel free to contact me at my personal email id:
or at our official Website:


1. What is Selenium?

2. What all we need before we get it started?

3. How to setup Selenium for Eclipse?

4. Simple Selenium Test Script for

5. Execution using Junit

5a. Execution using TestNG

6. DataDriven Testing using POI jar files

7. Issues or challenges with Selenium


1. What is Selenium?

Selenium is an open source ( It’s free !!!!) automation testing tool used for automating websites under any browser(not all).

Selenium supports the following browsers: IE, FireFox(Default), Chrome, Safari, Opera.

We write Selenium WebDriver Scripts in Eclipse. These scripts are written in Java language.

Things that you should be good at Java are: Constructors, Overriding, Overloading, Constructor Chaining, Interface, Inheritance, Abstract Class and UpCasting – DownCasting concepts are enough to write Selenium WebDriver Script.

2. What all we need before we get it started?

1. Windows 7 / 8

2. Java

3. Eclipse ((Juno or ADT) what I have worked on !! )

4. selenium-server-standalone-2.38.0

5. TestNG plugin for Eclipse

6. POI jar files


POI jar files:

Selenium Jar file

3. How to setup Selenium for Eclipse?

1. Open Eclipse goto Help -> Install new software.. -> give work with edittext field value as ” TestNG – ” -> select all -> Next -> Finish

2. when you create a new test project inside its build path add selenium webdriver jar files. I will show it in next chapter.

4. Simple Selenium Test Script for Google website

1. Create new java project with name Selenium Test as shown below:

2. Now we have to add the selenium Jar file to the build path ( Goto project -> Properties -> Java Build path ) as shown in below screen shot and add the downloaded selenium-server-standalone-2.38.0.jar file. by clicking on the add external Jar’s button. After this goto “Order and Export” tab click on “Select All” button and click “ok” button.

3.After completion of the above step now create a new java class ( Goto Project -> New -> Class) as shown in below ss and give name as TestWebDriver

4. After this now we will write a code to open Mozilla FireFox Browser and also open Google website inside it. The code for this is as below:


package com.rhb.selenium; // package name what I have given. It can differ with yours

import org.openqa.selenium.WebDriver; // Automatically imported by eclipse [This is for invoking the WebDriver class from selenium API]
import org.openqa.selenium.firefox.FirefoxDriver; // Automatically imported by eclipse [This is for invoking the FireFoxDriver class from selenium API]

public class TestWebDriver {

static WebDriver driver = null; // initialization

//the below method is made static to call this method inside our package without creating any instance for it.
public static void setup(){

driver = new FirefoxDriver(); // inbuilt method called from selenium class
driver.get(“”); // opens the website which is written inside the braces.


5. Now Lets create another java class called FunLib and inside this what we do is we will try to enter some text into the edit text box of the Google website and also we will click on the search button. the code for this is as below:


import org.openqa.selenium.By;

public class FunLib extends TestWebDriver {

public static void Search() throws InterruptedException {

driver.findElement(By.xpath(“//div[@id=’hplogo’]”)).click(); // clicks on the Google Logo
Thread.sleep(3000); // sleeps for 3000 milliseconds
driver.findElement(By.xpath(“//input[@name=’q’]”)).sendKeys(“RHBROMS”); // type as RHBROMS in the edit text field
driver.findElement(By.xpath(“//button[@id=’gbqfb’]”)).click(); // Click on the Search button
Thread.sleep(3000); // again sleeps for 3000 milliseconds
driver.findElement(By.xpath(“//li[1]/div/div/h3/a”)).click(); // Click on the first link that is

System.out.println(“button got clicked”); // displays the msg on console.


6. Okay now you might be thinking as how to identify the edit text field as well as other elements.!! Its easy for that we need some Extensions for FireFox they are as below:

7. After installing these Extensions we now see how to use them to Identify the objects. Thing is we don’t use all these but some, It again depends on which tool u want to use!!. which one is best for you.

8. First I will show you how to Identify the Google Logo by using Xpath. To do this open FireFox and open Google website and right click on Google Logo and select View XPath. After this you will see something like as shown below:

9. As you can see from the image as XPath is given as : id(‘hplogo’) but thing is how to use this in our code.. Simple just add the tag type in this case it is <div> (The <div> tag defines a division or a section in an HTML document.) the changes what u have to do is as shown in below Screen Shot.

10. Now the same goes for other objects too..!! If you find any doubts on this feel free to ask me by either commenting here or by emailing me.

11. Now we will see how to execute our first Automation Script in the next chapter.

5. Execution using Junit

1. First we will see how to execute our script via JUnit Suit.

2. Let us create a new JUnit test case (project -> New -> Junit test case) as shown in below screen shot:

3. Now let us give the Junit test case name as “Junit” as shown below in the screen shot. NOTE:Uncheck setup and teardown options.

4. Okay as we are done with creating new Junit test case just do the below editing:


package com.rhb.selenium; // package name

import junit.framework.TestCase; // auto import

import org.testng.annotations.Test; // auto import to give annotations that are necessary

public class Junit extends TestCase {

public void test1() throws Exception{

Main m = new Main(); // creating instance for Main class as “m”

m.setup(); // as setup is a method which is declared under Main class we can access it using access specifier “m”
m.Search(); // as Search is a method which is declared under Main class we can access it using access specifier “m”



5. To run the above JUnit Test right click on file -> RunAs -> Junit Test

6. When you run the Junit test FireFox runs automatically in WebDriver mode as shown below and all the operations that we have specified in the Main class will be performed. Screen Shot of this is as shown below:

7. The below snap shows the test case is pass and the color is changed to Green. And also we got a msg at console as “button got clicked” what we have written in !!

8. The Result at FireFox WebDriver should be as shown below:

9. Okay now we will see how to execute using TestNG ( Easy one šŸ˜› )

5a. Execution using TestNG

1. Don’t worry it is very easy compared to JUnit.

2. I hope you have installed the plugin of TestNG to your Eclipse.

3. Lets Create a new java class with name TestNGSuite1. Copy paste the below code after creation:


package com.rhb.selenium;


import org.apache.poi.openxml4j.exceptions.InvalidFormatE xception;
import org.testng.annotations.Test;

public class TestNGSuite1 {

public void testngtest() throws FileNotFoundException, InvalidFormatException, IOException, Exception {

Main m = new Main(); // instance of Main class
m.setup(); // call for setup method
m.Search(); // call for Search method


3. Right click on file -> TestNG -> Convert to TestNG as shown in below screen shot:

4. Now you will see as below Screen Shot, here TestNG is converting our java file to executable xml format.

5. Now click on Finish button and you will see a Testing.xml file as shown below:

6. Now Just right click on the xml file -> RunAs -> TestNG Suite as shown below:

7. Finally you will see the final result as below:

6. DataDriven Testing using POI jar files

1.We go for data driven testing when we have some modules that need to be tested for multiple values. For example in a application which has a login page with username and password field we have to test these edit text boxes for multiple inputs, It can be a number or it also can be a string or both together.

2. Here I will take as example and show you how to extract data from a excel file and push the same to Google website.

3. First we will create an excel file with some data present in it to test our application. (An Excel file for testing our app is attached here)

4. To extract data from Excel we use ā€œFileInputStreamā€ Class by which we can create/delete and modify a file.

5. Now add POI.jar files to Java Build Path same as how we added Selenium Jar file.

6. After adding of POI jar files to Java Build Path you will see them added as shown below:

7. After this is done we will create a new java class and will give the name as “DataDriveTest” and extend it from “TestWebDriver” Class.

8. Now what we will do is we will open enter “search” value as “RHBROMS” ( 1st value from the excel sheet ) click on the first link and then we will click on back button of the browser and then we will clear the search box and enter “xda developers” ( 2nd value from the excel sheet ) and click on first link that will be xda developers website.

9. The code for this is as written below with explanation.


package com.rhb.selenium;

// below package imports are for File I/O operations


// below packages are for excel sheet operations

import org.apache.poi.openxml4j.exceptions.InvalidFormatE xception;
import org.openqa.selenium.By;

public class DataDriveTest extends TestWebDriver {

public static void test() throws Exception, IOException,
FileNotFoundException,InvalidFormatException {

String cellval1 = null; // set the current value of cellval1 as NULL
String cellval2 = null; // set the current value of cellval2 as NULL

FileInputStream fis= new FileInputStream(“E:\\Test.xls”);

Workbook wb = WorkbookFactory.create(fis); // creates object for workbook Test.xls

Sheet ws = wb.getSheet(“Sheet1”); // opens Sheet1 from workbook Test.xls

int rc = ws.getLastRowNum(); // counts the number of rows which are used

for(int i=1; i<=rc; i++)

Row r = ws.getRow(i); // ponts to the i’th ROW

for (int j=0;j==0;j++){

Cell c = r.getCell(j); // points to the j’th Column
cellval1=c.getStringCellValue(); // gets the data from J’th cell

Cell c1 = r.getCell(j+1); // ponts to the J+1’th column
cellval2=c1.getStringCellValue(); // gets the data from J+1’th cell

driver.findElement(By.xpath(“//div[@id=’hplogo’]”)).click(); // Clicks on the google logo
driver.findElement(By.xpath(“//input[@name=’q’]”)).sendKeys(cellval1); // take the first value from first column and paste it at the search box

driver.findElement(By.xpath(“//button[@id=’gbqfb’]”)).click(); // clicks on the search button

Thread.sleep(3000); // sleeps for 3000milisecs
driver.findElement(By.xpath(“//li[1]/div/div/h3/a”)).click(); // clicks on first link

driver.navigate().back(); // clicks on the back button of the browser

driver.findElement(By.xpath(“//input[@name=’q’]”)).clear(); // clears the search box
driver.findElement(By.xpath(“//input[@name=’q’]”)).sendKeys(cellval2); // enters the second value from the excel sheet

driver.findElement(By.xpath(“//button[@id=’gbqfb’]”)).click(); // clicks on the search button again

driver.findElement(By.xpath(“//li[1]/div/div/h3/a”)).click(); // clicks on the first link





10. Now to execute this we have to do some editing at file as:

public class Main extends DataDriveTest {
public static void main(String args[]) throws FileNotFoundException, InvalidFormatException, IOException, Exception{

11. And also in file as:

public void testngtest() throws FileNotFoundException, InvalidFormatException, IOException, Exception {
	Main m = new Main();

12. Now as usual convert the TestNGSuit1 file to TestNG and execute.

13. To see your TestNG results go to your eclipse workspace and open selenium project inside that you will find test-output folder. As shown in below Screen Shot:

14. For Generation of Graphical and more clean Results we use TestNG xslt with ANT. lets see how to do it in Next Chapter

6. TestNG xslt with ANT

1. Download Ant from here:

2. Unzip it and rename the folder as ant.

3. Set ANT_HOME to environmental variables.( In windows 7 Right click on Computer -> properties -> ā€œAdvance system settingā€
-> Choose Advanced Tab
-> Press Environment Variables Button
-> In the System Variables, click New Button

Give the Variable Name:ANT_HOME
Give the Value: E:\ant
Click OK )

as shown in Below Screen Shot.

4. Set ANT_HOME path,
go to path
Give the Value C:\ANT\bin
Click OK

5. To check ANT works properly or not
In the command prompt, type:

ant -version

you will see as below Screen Shot:

6. Now download testng-xslt from HERE

7. After this extract the downloaded zip file and go to testNG-xslt -> src -> main -> resources and copy the file testng-results.xsl and also copy this file from testNG-xslt -> lib that is saxon-8.7.jar and keep them at any folder for time being.

8. Now go to your project workspace and goto SeleniumTest -> test-output and paste testing-results.xsl that you copied.

9. and now goto eclipse and add saxon-8.7.jar to buildpath.

NOTE: the thing is you have to keep all your jar files in a same folder as I have kept at jar folder as shown below in my setup:

10. Now after doing all this create new xml file and call it as Build.xml

11. After creating this just copy paste the below code:

<?xml version="1.0" encoding="UTF-8"?>
<project name="SeleniumTest" default="compile" basedir=".">

<path id="cp">
    <pathelement path="bin" />
        <fileset dir="jars"/>

<!-- for compiling  -->
 <target name="compile">
        <javac classpathref="cp" srcdir="src" destdir="bin"/>

<!-- for running  -->
<target name="run" depends="compile">
    <java classpathref="cp" classname="org.testng.TestNG" args="testng.xml"/>

<!-- for report generation -->
  <target name="report" depends="run">
    <xslt in="./test-output/testng-results.xml" style="./test-output/testng-results.xsl" out="./test-output/testng-xslt.html">
     <param expression="${basedir}/test-output/" name="testNgXslt.outputDir" />
     <classpath refid="cp"/>

12. After this save the file.

13. Now right click on the project and do as below and select TestNG:

14. Now Run the Build.xml file. and your results will be stored at Index.html at test-output folder.

To be continued .. NEXT will be Maven with TestNG!!

Leave a Comment