TestNG is a testing framework which helps for data driven testing with additional some more features that make it more powerful and easier to use. DataProvider is one of the feature which passes the set of data stored in the variables.
Jxl is a java API that enables to read, write, and modify excel spreadsheets dynamically.
Prerequesties:
- TestNG plug in need to be installed in eclipse IDE. In my previous post i explained about how to configure TestNG with Eclipse IDE.
Steps:
- Open eclipse IDE
- Create new JAVA project
- Create new package under the project
- Create new class under the package
- Add the jxl,TextNG, selenium server, junit jar files in java build path
- Add new folder inside src folder
- Add another folder inside newly added folder
- Create excel file as defined below and save it in finally created folder.
- Type the following code inside the class editor
- Save the project
- Right click on project name and click on Build Project option
- Right click on project name and choose Run as-->click on TestNG test option
Please look at the following example for data driven testing using jxl and TestNG,
Example: (Searching data in Google)
package google4;
import com.thoughtworks.selenium.*;
import static org.testng.AssertJUnit.*;
import org.testng.annotations.*;
import java.io.File;
import jxl.*;
public class google1{
private Selenium selenium;
@BeforeClass
public void startSelenium() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in");
selenium.start();
selenium.windowMaximize();
}
@DataProvider(name = "DP1")
public Object[][] createData1() throws Exception{
Object[][] retObjArr=getTableArray("src\\Resources\\Data\\data1.xls",
"DataPool", "imdbTestData1"); // /test1/src/Resources/Data/data1.xls
return(retObjArr);
}
@Test (dataProvider = "DP1")
public void testGoogle1(String search) throws Exception{
selenium.open("http://www.google.co.in/");
Thread.sleep(3000);
System.out.println("Opened");
selenium.type("id=gbqfq", search);
Thread.sleep(3000);
System.out.println("Typed");
selenium.click("id=gbqfb");
Thread.sleep(3000);
System.out.println("Clicked");
}
@AfterClass
public void tearDown() throws Exception {
//selenium.stop();
}
public String[][] getTableArray(String xlFilePath, String sheetName, String tableName) throws Exception{
String[][] tabArray=null;
Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
Sheet sheet = workbook.getSheet(sheetName);
int startRow,startCol, endRow, endCol,ci,cj;
Cell tableStart=sheet.findCell(tableName);
startRow=tableStart.getRow();
startCol=tableStart.getColumn();
Cell tableEnd= sheet.findCell(tableName, startCol+1,startRow+1, 100, 64000, false);
endRow=tableEnd.getRow();
endCol=tableEnd.getColumn();
System.out.println("startRow="+startRow+", endRow="+endRow+", " +
"startCol="+startCol+", endCol="+endCol);
tabArray=new String[endRow-startRow-1][endCol-startCol-1];
ci=0;
for (int i=startRow+1;i<endRow;i++,ci++){
cj=0;
for (int j=startCol+1;j<endCol;j++,cj++){
tabArray[ci][cj]=sheet.getCell(j,i).getContents();
}
}
return(tabArray);
}
}
Excel data:
In the above example following code helped to read the data from the excel sheet and store it in the variable.
@DataProvider(name = "DP1")
public Object[][] createData1() throws Exception{
Object[][] retObjArr=getTableArray("src\\Resources\\Data\\data1.xls",
"DataPool", "imdbTestData1"); // /test1/src/Resources/Data/data1.xls
return(retObjArr);
}
public String[][] getTableArray(String xlFilePath, String sheetName, String tableName) throws Exception{
String[][] tabArray=null;
Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
Sheet sheet = workbook.getSheet(sheetName);
int startRow,startCol, endRow, endCol,ci,cj;
Cell tableStart=sheet.findCell(tableName);
startRow=tableStart.getRow();
startCol=tableStart.getColumn();
Cell tableEnd= sheet.findCell(tableName, startCol+1,startRow+1, 100, 64000, false);
endRow=tableEnd.getRow();
endCol=tableEnd.getColumn();
System.out.println("startRow="+startRow+", endRow="+endRow+", " +
"startCol="+startCol+", endCol="+endCol);
tabArray=new String[endRow-startRow-1][endCol-startCol-1];
ci=0;
for (int i=startRow+1;i<endRow;i++,ci++){
cj=0;
for (int j=startCol+1;j<endCol;j++,cj++){
tabArray[ci][cj]=sheet.getCell(j,i).getContents();
}
}
return(tabArray);
}
Happy Testing.
Your blog is very helpful. I am automating quikr and I have a doubt.
ReplyDeleteMy doubt is that while editing any add that you posted in quikr.com, then there is a subcategory field in the add.
I think that is not a driopdown. After selecting the main category, corresponding subcategories are displayed there on the same dropdown (here i am saying dropdown but please do not get confused as that is not dropdown i think)...
Now I am unable to select any subcategory from that field.
Can you please help me out here.
Please help how to select the value here...
How to use this code in our Automation scripts. I mean to say this is to read the data from Excel file but how to use only few data from the file
ReplyDeleteI want to pass my base url from excel sheet using eclipse ide for selenium RC can you please help me out from here
ReplyDelete