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

4 comments:

  1. Try this,

    Fillo fillo=new Fillo();
    Connection connection=fillo.getConnection("C:\\Test.xlsx");
    String strQuery="select * from [Sheet1] where [ID]='100'";
    Recordset recordset=connection.executeQuery(strQuery);

    while(recordset.hasNext()){
    System.out.println(recordset.getField("Details"));
    recordset.moveNext();
    }

    recordset.close();

    http://www.codoid.com/products/view/2/29

    ReplyDelete
  2. Thanks for sharing Fillo, it is great a API to query xls.

    ReplyDelete
  3. nice :p but how to do it in c#

    ReplyDelete
  4. how to append data in existing sheet?
    i tried but its deleteing existing data and adding new data.

    I used POI

    ReplyDelete