import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.remote.Augmenter;

public class Screenshot extends TestListenerAdapter
{
   public void onTestFailure (ITestResult testResult)
   {
      //Get a driver instance from the thread specific session store
      WebDriver driver = (WebDriver) sessionStore.get().get(SESSION_WEBDRIVER);

      //Create the folders/filename for the screenshots
      String screenShotsFolder = null;
      File file = new File("S:/screenshots");
      screenShotsFolder = file.getAbsolutePath();

      Calendar calendar = Calendar.getInstance();
      String testMethodAndTestClass = testResult.getMethod().getMethodName() + "(" + testResult.getTestClass().getName() + ")";
      String sessionBrowser= (String) sessionStore.get().get(SESSION_BROWSER);
      String filename = screenShotsFolder + "/"
            + calendar.get(Calendar.YEAR) + "-"
            + calendar.get(Calendar.MONTH) + "-"
            + calendar.get(Calendar.DAY_OF_MONTH) + "/"
            + sessionBrowser + "/"
            + testMethodAndTestClass + "/"
            + calendar.get(Calendar.HOUR_OF_DAY) + "-"
            + calendar.get(Calendar.MINUTE) + "-"
            + calendar.get(Calendar.SECOND) + "-"
            + calendar.get(Calendar.MILLISECOND)
            + ".png";

      WebDriver augmentedDriver = new Augmenter().augment(driver);
      File scrFile = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);

      try
      {
         FileUtils.copyFile(scrFile, new File(filename));
      } catch(IOException e){
         e.printStackTrace();
      }
   }
}
share me!:))Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someonePrint this page