Thursday, 15 October 2015

Program to capture screenshot

* For JAVA Project add jars for selenium.
* For Maven Project, add Dependency for selenium

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class captureScreenshot {
 
 public WebDriver drv;
 public void screenshot(WebDriver drv, String fileName) throws Exception
 {
  File screenshot = ((TakesScreenshot)drv).getScreenshotAs(OutputType.FILE);
       FileUtils.copyFile(screenshot, new File(fileName));
 }
 
 public void executeScript() throws Exception{
  drv=new FirefoxDriver();
  drv.manage().window().maximize();
  drv.navigate().to("https://www.google.com/");
  screenshot(drv, "D:\\google.png");
  drv.close();
 }

 public static void main(String[] args) {
  captureScreenshot c1=new captureScreenshot();
  try {
   c1.executeScript();
  } catch (Exception e) {
   e.printStackTrace();
  }
  
 }

}

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:

We have created two logs.
*Simple Log
*Error Log

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();
  }
 }
}


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:

We have created two logs.
*Simple Log

*Error Log

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();
  }
 }
}


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();
  }
 }
}