Showing posts with label writing data. Show all posts
Showing posts with label writing data. Show all posts

Friday, 12 August 2016

Basic Excel Reader useful for selenium WebDriver


In this post I am going to explain how can we read and write data on excel file with the help of Java using Apache POI jar libraries.

 Download Apache POI jar libraries in following link  and configure to your project
 Download Apache POI jar

Sample Code:

       package DriverMethods;

       import java.io.FileInputStream;
       import java.io.FileNotFoundException;
       import java.io.FileOutputStream;
       import java.io.IOException;
       import org.apache.poi.ss.usermodel.Cell;
       import org.apache.poi.xssf.usermodel.XSSFCell;
       import org.apache.poi.xssf.usermodel.XSSFRow;
       import org.apache.poi.xssf.usermodel.XSSFSheet;
       import org.apache.poi.xssf.usermodel.XSSFWorkbook;

        public class ExcelReader {
public String path;
public FileInputStream file;
public XSSFWorkbook workbook;
public XSSFSheet sheet;
public XSSFRow row;
public XSSFCell cell;
       public FileOutputStream fos;

       //constructor for path setting
public ExcelReader(String path) throws IOException{

this.path =path;
file = new FileInputStream(path);
workbook= new XSSFWorkbook(file);
}
//Reusable method for reading data from excel
public  String getCellData(String sheetName,int rowNum,int colNum){
int index = workbook.getSheetIndex(sheetName);
sheet =workbook.getSheetAt(index);
row = sheet.getRow(rowNum);
cell = row.getCell(colNum);
String data=cell.getStringCellValue();
return data;
}

//Reusable method for writing data to the excel
    public void setCellData(String sheetName,String data,int rowNum,int colNum) throws                IOException{

   int index = workbook.getSheetIndex(sheetName);
sheet =workbook.getSheetAt(index);
row = sheet.getRow(rowNum);

if(row==null){

row =sheet.createRow(rowNum);
}
cell = row.getCell(colNum);
if(cell==null){
cell= row.createCell(colNum);
}
  cell.setCellValue(data);

fos = new FileOutputStream(path);
workbook.write(fos);
fos.close();
   }
 
     //Usage
public static void main(String[] args) throws IOException{

ExcelReader ereader = new                                          ExcelReader("C:/Users/Sudharsan/Desktop/TestData.xlsx");
System.out.println(ereader.getCellData("Sheet1", 0, 0));
ereader.setCellData("Sheet2", "ShreyaGhosal", 11, 11);
}

}





Thursday, 11 August 2016

Data read and write from a properties file useful for selenium

Generally in selenium, we used to use properties file for maintaining configuration related data of project like object locators and URL's.
In this post, I will show you how to read and write data from a properties file.

Please find below sample code for the same.

Sample code:
package DriverMethods;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertyFileReader {
Properties prop = new Properties();
FileInputStream file;
FileOutputStream fileOut;

//Reusable method for reading data
public String readingData(String key) throws IOException{

//path of properties file
file = new FileInputStream("path of property file");

// load a properties file
prop.load(file);

//get the value of key
String out=prop.getProperty(key);
return out;

}

//Reusable method for writing data
public void writeData(String key,String data) throws IOException{

//path of properties file
  fileOut = new FileOutputStream("path of property file");

  // set the value to key
  prop.setProperty(key, data);

  // save properties
prop.store(fileOut, null);


}
        //Usage
public static void main(String[] args) throws IOException{
PropertyFileReader Pf = new PropertyFileReader();
System.out.println(Pf.readingData("username"));
Pf.writeData("password", "ShreyaGhosal");
}

}