Yes, using a web driver in Selenium, we can capture the screenshot. Following is the code to do the same:
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
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 TakeScreenshot {
WebDriver drv;
@ Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
drv.get("
https://google.com");
}
@ After
public void tearDown() throws Exception {
drv.quit();
}
@ Test
public void test() throws IOException {
// Capture the screenshot
File scrFile = ((TakeScreenshot)drv).getScreenshotAs(OutputType.FILE);
// Code for pasting screenshot to a user-specified location
FileUtils.copyFile(scrFile, new File("C:\\Screenshot\\Scr.jpg"))
}
}