Sunday, August 11, 2019

Taking screenshot



In our automation scripts or framework, we need take screenshot whenver we want. for example, when scripts fail or any exception occurs we can take screenshot to know what was issue.

 Below method we can add in our scripts or in framework so that we can call that method whenver we need.



public static void takeScreenShot(WebDriver driver) throws IOException{
  
  //directory
  destDir = System.getProperty("user.dir")+"\\test-output\\html\\screenshots";
  
  //capturing screenshot
  File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  
  //Set date
  dateFormat =  new SimpleDateFormat("dd-MMM-yyyy__hh_mm_ssaa");
  
  
  //create folder
  new File(destDir).mkdir();
  
  destFile = dateFormat.format(new Date())+".png";
  
  
  FileUtils.copyFile(scrFile, new File(destDir+"/"+destFile));
  
  
 }

we can call this method whenever we need.


No comments:

Post a Comment

If any suggestions or issue, please provide