Thursday, 15 October 2015

Adding csv Logger to Selenium Project

* For JAVA Project add jars for csv.
* For Maven Project, add Dependency for csv:

<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>2.3</version>
</dependency>


Program for csv logger:

package ProjectInputOutput.Files;

import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import au.com.bytecode.opencsv.CSVWriter;


public class csvLogsProgram {
 public CSVWriter writeLog;
 public CSVWriter writeErrLog;
 public static List<String[]> logs = new ArrayList<String[]>();
 public static List<String[]> errLogs = new ArrayList<String[]>();

 public void writeLog(List<String[]> logResult)throws Exception{
  try{
   writeLog = new CSVWriter(new FileWriter("D:\\log.csv"));
   writeLog.writeAll(logResult);
   writeLog.close();   
  }
  catch(Exception e){
   e.printStackTrace();
   throw e;
  }
 } 

 public void writeErrLog(List<String[]> logResult)throws Exception{
  try{
    writeErrLog = new CSVWriter(new FileWriter("D:\\errorLog.csv"));
    writeErrLog.writeAll(logResult);
    writeErrLog.close();    
   }
   catch(Exception e){
    e.printStackTrace();
    throw e;
   }
 }

 public static void main(String [] args) {
  logs.add(new String[]{"Starting "+" at "+new Date(),"","Pass"});
  errLogs.add(new String[]{"Starting "+" at "+new Date(),"","Fail"});
  csvLogsProgram io=new csvLogsProgram();
  try {
   io.writeLog(logs);
   io.writeErrLog(errLogs);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}


No comments:

Post a Comment