About - Selenium

Definition

Development and Versions

Pre-requisites

Languages supported

Manual vs Automation

Choosing Selenium

Selenium - Components

Selenium IDE

Selenium RC

Selenium WebDriver

Selenium Grid

QTP

Selenium vs QTP

Selenium - IDE

Features

Install and configure

Selenium - Webdriver

About WebDriver

install and configure

First test case

Web element

Element Locators

Example of locators

Chropath

Web driver commands

Eclipse and TestNG

About Eclipse

Choose and download

About TestNG

Testng installation on eclipse

Testng Annotations

Pass Fail and Skip test cases

Run test cases in batch

testng.xml - Explanation

testng grouping

Running tests parallely

dependsOnMethods

dependsOnGroups

Prioritization

Passing parameters

Data providers

Read data from excel

Ant - XSLT reports

Testng listeners

Cross browser testing

Page Object Model



Selenium - Read data from excel
 
Data driven testing in Selenium through TestNG Data providers.

a) Let's see first how to create data in Excel file for the same login functionality




c) Enter the data as above in Sheet1 of above file named "selenium test data 1"
d) Write the DataProvider method that returns the data as follows:
Eg:
@DataProvider(name = "data1")
public Object [] [] readExcelFile() throws BiffException, IOException {
File file1= new File("C:\Users\Naresh\Desktop\selenium test data 1.xls");
Workbook workbook1 = Workbook.getWorkbook(file1);
Sheet s1 = workbook1.getSheet("Sheet1");
int rows = s1.getRows();
int columns = s1.getColumns();

String excelData [] [] = new String [rows] [columns];

for (int i=0; i < rows; i++){
for (int j=0; j < columns; j++){
Cell c1 = s.getCell(j, i);
excelData [i][j] = c1.getContents();
System.out.println(excelData [i][j]);
}
}
return excelData;
}
}


e) Explanation of the above code:

f) First of all through this code, we will get the data from excel (where test data is present in the form of rows and columns) into a two dimensional array of String data.

g) This data will be provided to the TestNG (@Test method - one test case) to validate login functionality in Selenium WebDriver.The following code shows how to integrate Selenium with Java and TestNG for automating test cases.

h) Before that we need to see how the above code works.

i) File file1= new File("C:\Users\Naresh\Desktop\selenium test data 1.xls");


This above line links the file1 to selenium test data 1.xls which is an excel file. This code is generally ok. But, if any compile errors occur, then import java io jar files from google and configure them in project build path.

Right click on the project , See Build Path -> Configure build path and select Libraries -> Add external jars and the navigate to the jar files and select the jar files and after that click on apply button.

j) Workbook workbook1 = Workbook.getWorkbook(file1);
Sheet s1 = workbook1.getSheet("Sheet1");
int rows = s1.getRows();
int columns = s1.getColumns();


In the above few lines of code, Workbook means excel file, which is takes "file1" input and instantiates Excel file.

After instantiates Excel file, you need to get the Sheet where the data is present. After getting the sheet, get the number of rows and number of columns.

code is generally is not OK. You will get compile errors, so import java excel jar files from google and configure them in project build path.

Right click on the project , See Build Path -> Configure build path and select Libraries -> Add external jars and the navigate to the jar files and select the jar files and after that click on apply button.

String excelData [] [] = new String [rows] [columns];
for (int i=0; i < rows; i++){
for (int j=0; j < columns; j++){
Cell c1 = s.getCell(j, i);
excelData [i][j] = c1.getContents();
System.out.println(excelData [i][j]);
}
}
return excelData;


The above code just fills the data from excel file into excelData object iterating through each cell of excel sheet.

And finally, we return two dimensional data in excelData object. But here data is not hard-coded in java files. We took the data from Excel file.

g) Now lets look at the @Test method for the above operation.

public class AutomationTest {
public WebDriver driver;
@Test(dataProvider =" data1")
public void login(String email, String password){
driver = new FirefoxDriver();
driver.get("http://flipkart.com");
driver.findElement(By.name("email")).sendKeys(email);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.id("loginButton")).click();
driver.close();
}
}


h) Explanation of above code.

1) We annotated login method with @Test and dataProvider attribute set to "data1".It means data is taken from dataProvider attribute named "data1"
2) This concept is already explained which belongs to TestNG.
3) Then, we initialized Firefox Browser through WebDriver interface.
4) And through driver object we got email, password and loginButton element locators and corresponding operations are performed.

5)For email and password we use sendKeys operaional method.
6) For loginButton, we used click operation on the loginButton.
7) After click operation, the user will be redirected to corresponding flipkart account, if there is correct input data.Else throws an error .
8)Finally the browser closes

I conduct online tutions on Manual Testing, Selenium and the following. For more details contact anandaibmindia@gmail.com
Manual Testing - 1600/-           Selenium - 1600/-
           Jsp and Servlet -2000/-           Java training - 2000/-