Friday, 13 January 2012

Data Driving With Selenium RC Using .csv file

Steps


1. Create your project in Eclipse
2. Add a jave class with name DataDrivingInSelenium to project
3. create a csv file and save it in  data folder of C drive


4. Run the class with junit


csv File("C:\\data\\MyData.csv")


First Name,Last Name
Shiv,Kumar Jha
Selenium,Test
Selenium,Shiv




DataDrivingInSelenium .java file




//write your package name
//package tests;

//import The Below Selenium class for starting server and application
import com.thoughtworks.selenium.*;
import org.openqa.selenium.server.SeleniumServer;

//The beow mentioned classes needs to be imported for reading the file
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.StringTokenizer;

//Defined the Class to parameterize the test
public class DataDrivingInSelenium extends SeleneseTestCase {

//Define the Setup mentod to invoke the base URL
public void setUp() throws Exception {
            SeleniumServer sServer = new SeleniumServer();
            sServer.start();
            selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.google.co.in/");
            selenium.start();
}
public void testDataDriveGoogle() throws Exception {

//Definition of .csv file to read
String sFileName = "C:\\data\\MyData.csv";
String sFirstName;
String sLastName;
String sFullName;
//Define CSV Data File variable
String [][] sTestData;

selenium.setSpeed("2000");
sTestData = this.readCSVFile(sFileName);

// This logic works for 3 rows 2 columns of your test data file
for (int iRow=1; iRow<4; iRow++) {

//Mapping the column numbers with column names
sFirstName = sTestData[iRow][0]; //refers to firstName in CSV
sLastName = sTestData[iRow][1]; //refers to lastName in CSV

// Concatinating the firstName and lastName
sFullName = sFirstName + " " + sLastName;
selenium.open("/");

//Entering the full name in the Google Text Field
selenium.type("q", sFullName);
selenium.click("btnG");
}

}
public String [][] readCSVFile(String sFileName) throws IOException, InterruptedException {

String [][] sCSVData = new String [15][15];
int iRow = 0;
int iCol = 0;
//String variable which takes each line at a time as a record
String sCurLine;

//A FileInputStream obtains input bytes from a file in a file system
FileInputStream fFileStream = new FileInputStream(sFileName);

//Data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way
DataInputStream dDataStream = new DataInputStream(fFileStream);

//Beginning of outer while loop to read each row in the data file
while ((sCurLine = dDataStream.readLine()) != null) {

//System.out.println(sCurLine);
StringTokenizer sTookens = new StringTokenizer(sCurLine,",");
//Beginning of inner while loop to read each column of the row in the data file
while (sTookens.hasMoreTokens())
{

//get next token as column and store it in the array
sCSVData[iRow][iCol] = sTookens.nextToken();
//System.out.println(sCSVData[iRow][iCol]);
iCol++;
Thread.sleep(1000);
} //End of inner while loop
iCol = 0;
iRow++;
} //Ending of outer while loop

return sCSVData;
} //End of CSV Read method

} // End of class file



1 comment:

  1. Thanks for sharing great information in your blog.

    Selenium training institute which provides Real Time Hands on Training…THE CREATING EXPERTS

    http://thecreatingexperts.com/selenium-training-in-chennai/


    contact-8122241286

    ReplyDelete