How to take Screenshots in Selenium using Webdriver? - Software Testing

Empowering Technology

Post Top Ad

Responsive Ads Here

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");

1 comment:

  1. Hats off to your presence of mind..I really enjoyed reading your blog. I really appreciate your information which you shared with us.

    software testing training in chennai


Thanks a lot for your valuable Comment!

Post Bottom Ad

Responsive Ads Here