How to write function in JAVA to take screenshot and save into the folder?

Writing JAVA function for taking screenshot and saving into the folder is simple.  While writing the selenium test scripts, we need to transform the web driver into Augmenter constructor which is an desired capabilities features.

Link for selenium syntax reference : Click here

Below code is written for JAVA function to take the screenshot and place it into the folder.

Code:
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;

public class captureScreenshot {
 
//Webdriver intiation for firefox
WebDriver driver = new FirefoxDriver();
 
//function to capture the screen
public String captureScreenAndSave() 
 
{
String path;

try 
{
//Assigning driver into augmentedDriver    
WebDriver augmentedDriver = new Augmenter().augment(driver);
File source = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
path = "c:\\Selenium_Screenshots\\" + source.getName();
FileUtils.copyFile(source, new File(path)); 
}
catch(IOException e) {
path = "Failed to capture screenshot: " + e.getMessage();
}
return path;
}
 
public static void main(String args[]) throws IOException
{
//constructing the class
captureScreenshot funcScreenshot = new captureScreenshot();

//call the screenshot function
funcScreenshot.captureScreenAndSave();
} 
}
Socialize It and Share the post with your friends
SOCIALIZE IT →
FOLLOW US →
SHARE IT →