Monday 26 May 2014

How to write data to Excel Sheet in Selenium WebDriver

Before start writting data to excel sheet make sure Apache POI library is downloaded and add into your IDE you are going to use. The same library is being used for reading data from excel file. So this is Microsoft API which used for reading and writting data from/to Microsoft stuff.

Below code is performed using TestNg. Now writting data to excel sheet in a sequential order or maintain insertion order of keys, Order in which keys are inserted in to LinkedHashMap.


Following is the code to write to excel workbook:


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Assert;
import org.junit.Before;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Set;

/**
 * Created by Gaurav on 5/26/2014.
 */
public class demoWriteToExcel {

    LinkedHashMap<String,Object[]> table;
    HSSFWorkbook hssfWorkbook;
    HSSFSheet hssfSheet;
    FirefoxDriver firefoxDriver;


    @BeforeClass
    public void setUp(){

        hssfWorkbook = new HSSFWorkbook();
        hssfSheet = hssfWorkbook.createSheet("TestData");

        

        //Used linkedHashMap to maintain insertion order of keys


        table = new LinkedHashMap<String, Object[]>();
        table.put("1",new Object[]{"SrNo", "TestCase", "Result"});

        firefoxDriver = new FirefoxDriver();
        firefoxDriver.get("http://newtours.demoaut.com/");

    }


    @Test
    public void login() {


       // It will insert data into table when it can reach to the login page.


        Assert.assertEquals(firefoxDriver.getTitle(), "Welcome: Mercury Tours");
        table.put("2", new Object[]{"1", "I can reach login page", "Pass"});

    }


    // Initialization of rows and cells to get data into the excel sheet.


    @AfterClass
    public void filldata(){

        Set<String> strings = table.keySet();

        //rows loop

        int rowCount = 0;
        for(String key: strings){

            HSSFRow row = hssfSheet.createRow(rowCount);
            Object[] objects = table.get(key);

            //columns loop

            int cellcount =0;
            for(Object obj: objects) {

                HSSFCell cell = row.createCell(cellcount);
                cell.setCellValue(obj.toString());
                cellcount++;
            }
            rowCount++;
            }

        try {


           // Following path is given where the workbook will get created


            FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\\Users\\Gaurav\\Desktop\\Selenium_TC\\Selenium_practicals\\gaurav.xls"));
            hssfWorkbook.write(fileOutputStream);
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}

No comments:

Post a Comment

Select Language