Showing posts with label JXL. Show all posts
Showing posts with label JXL. Show all posts

Friday, 8 March 2013

How read the data from excel and write the pass/fail status to another excel sheet

Hi,

This post would help you all to understand in better manner how to read and write to an excel file using jxl. Please go through the following code,


package excel;

import com.thoughtworks.selenium.*;
import org.testng.annotations.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import jxl.*;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;


public class write2excel{
private Selenium selenium;

@Before
public void startSelenium() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in");
selenium.start();
selenium.windowMaximize();
}


@Test
public void testGoogle1() throws Exception{

String s;
selenium.open("/");
Thread.sleep(3000);
FileInputStream file = new FileInputStream("src\\Resources\\Data\\data1.xls");
Workbook wb = Workbook.getWorkbook(file);
Sheet st = wb.getSheet("DataPool");
String a[][] = new String[st.getRows()][st.getColumns()];
System.out.println("s.getRows() = " + st.getRows());
FileOutputStream op = new FileOutputStream("src\\Resources\\Data\\data2.xls");
WritableWorkbook wb1 = Workbook.createWorkbook(op);
WritableSheet st1 = wb1.createSheet("DataPool",0);
for(int i=0;i<st.getRows();i++)
{
for(int t=0;t<st.getColumns();t++)
{
a[i][t] = st.getCell(t, i).getContents();
Label l = new Label(t, i, a[i][t]);
Label l1 = new Label(2, 0, "Status");
st1.addCell(l);
st1.addCell(l1);
}
}
for(int i=1;i<st.getRows();i++)
{
System.out.println("Opened");
String k = st.getCell(0,i).getContents();
selenium.type("id=gbqfq", k);
Thread.sleep(3000);
System.out.println("Typed");
selenium.click("id=gbqfb");
Thread.sleep(3000);
System.out.println("Clicked");
String j = st.getCell(1,i).getContents();
if(selenium.isTextPresent(j))
{
s= "Pass";
}
else
{
s="Fail";
}

Label l2=new Label(2,i,s);
st1.addCell(l2);

}
wb1.write();
wb1.close();
}

@After
public void tearDown() throws Exception {
//selenium.stop();
}


}

Code explanation:

Don't try to get/create an excel before "selenium.open("/");" function, if you do the browser does not open the url which need to be opened by the browser.

Below code helps to set the source file and sheet from which it gonna get the input data,

FileInputStream file = new FileInputStream("src\\Resources\\Data\\data1.xls");
Workbook wb = Workbook.getWorkbook(file); //Setting the workbook which will be used to get/read the data
Sheet st = wb.getSheet("DataPool"); //Setting the worksheet where the data is stored

Below code helps to create new file and sheet,

FileOutputStream op = new FileOutputStream("src\\Resources\\Data\\data2.xls");
WritableWorkbook wb1 = Workbook.createWorkbook(op); //Creating workbook
WritableSheet st1 = wb1.createSheet("DataPool",0);//Creating worksheet

Below code helps to copy the data from one excel to another excel file,


for(int i=0;i<st.getRows();i++)//st.getRows()-->count the number of rows present in sheet
{
for(int t=0;t<st.getColumns();t++)//st.getColumns()-->count the number of columns present in sheet
{
a[i][t] = st.getCell(t, i).getContents();//Read the data from sheet and Storing it into the array variable
Label l = new Label(t, i, a[i][t]);//Helps to set to which cell the data need to be inserted and specify which data to be inserted
Label l1 = new Label(2, 0, "Status");
st1.addCell(l);//Insert the data to the specified cell
st1.addCell(l1);
}
}

Below code helps to check the pass/fail status of the scenario,


if(selenium.isTextPresent(j))//selenium.isTextPresent("Text")-->helps to check whether specified text present in the loaded page
{
s= "Pass";
}
else
{
s="Fail";
}



Below code helps to write the test case status to the excel


Label l2=new Label(2,i,s);
st1.addCell(l2);

}
wb1.write();

Happy Testing

Tuesday, 26 February 2013

Data driven testing using TestNG

Data driven testing is executing the same script with set of multiple data from the external source file. Selenium does not have any inbuilt source for data driven testing to fetch the data like QTP has. To overcome this selenium uses external sources like excel, etc. To achieve data driven testing in selenium through many frameworks. In that TestNG also one of the frameworks which could help to pass the multiple data to a single script.

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:



Steps:

  1. Open eclipse IDE
  2. Create new JAVA project
  3. Create new package under the project
  4. Create new class under the package
  5. Add the jxl,TextNG, selenium server, junit jar files in java build path
  6. Add new folder inside src folder
  7. Add another folder inside newly added folder
  8. Create excel file as defined below and save it in finally created folder.
  9. Type the following code inside the class editor
  10. Save the project
  11. Right click on project name and click on Build Project option
  12. 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.