Mar 20, 2018

How to take Screenshots in Selenium using Webdriver?

While automating UI tests, it's very necessary to take screenshots during tests in order to capture and analyse failures. Here's the piece of code that you need:

* Documentation:
* To take Screenshot anywhere in framework just use:
* utility.Screenshot.captureScreenshot(driver);
* Screenshot will be saved in directory: C:/selenium
package utility;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
public class Screenshot {
public static void captureScreenshot(WebDriver driver)
TakesScreenshot ts = (TakesScreenshot)driver;
File source = ts.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(source, new File("C:/selenium/"+System.currentTimeMillis()+".png"));
System.out.println("Screenshot Taken");"Screenshot Taken");
catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Error while taking Screenshot");