SCREENSHOT

 


Screenshot in Selenium Webdriver is used for bug analysis. Selenium webdriver can automatically take screenshots during the execution. But if users need to capture a screenshot on their own, they need to use the TakeScreenshot method which notifies the WebDrive to take the screenshot and store it in Selenium.



package mypackage;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

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 Screenshot {

public static void main(String[] args) throws IOException{
System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.google.com");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

TakesScreenshot scrShot =((TakesScreenshot)driver);

File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);

File DestFile=new File("C:\\Users\\ramak\\OneDrive\\Pictures/screenshots/img1.png");
 
FileUtils.copyFile(SrcFile, DestFile);

System.out.println("screenshot");

}

}