<commit_before>package com.github.yev;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Date;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

/**
 * TestNg listeners which is responsible to make call the Selenium Screenshot API and store the images in
 * target directory of current maven project.
 * Next, those images can be used after by jenkins job to send then as attachments in email notification.
 * User: yev
 * Date: 6/7/15
 * Time: 21:33
 */
public class FailTestScreenshotListener<T> extends TestListenerAdapter {
  private static String currentDir = null;

  static {
    try {
      currentDir = new File(".").getCanonicalPath();
    } catch (IOException e) {
      System.err.println("Error while detecting current working dir. Reason:"+e.getMessage());
    }
  }

  @Override
  public void onTestFailure(ITestResult tr) {
    super.onTestFailure(tr);

    WebDriver webDriver = findWebDriverByReflection(tr);
    if ( webDriver == null){
      System.err.println( String.format("The test class '%s' does not have any field of type 'org.openqa.selenium.WebDriver'. " +
              "ScreenshotTestListener can not continue.", tr.getClass().getName()));
      return;
    }


    File f = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
    try {

      final long timestamp = new Date().getTime();
      Path screenshotPath = Paths.get(currentDir, "target", "screenshot_" + tr.getMethod().getMethodName() + "_"+ timestamp+".png");
      System.out.println("copying "+screenshotPath);
      Files.copy(f.toPath(),screenshotPath, StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
      System.err.println("error during the screenshot copy file operation:" + e.getMessage());
    }

  }

  private WebDriver findWebDriverByReflection(ITestResult tr){
    Class<?> c = tr.getInstance().getClass();
    Field[] fields = c.getDeclaredFields();
    WebDriver webDriver = null;
    for (Field eachField: fields){
      if (eachField.getType() == WebDriver.class){
        try {
          webDriver = (WebDriver) eachField.get(tr.getInstance());
        } catch (IllegalAccessException e) {<commit_msg>code cleaning<commit_after>package com.github.yev;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Date;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

/**
 * TestNg listeners which is responsible to make call the Selenium Screenshot API and store the images in
 * target directory of current maven project.
 * Next, those images can be used after by jenkins job to send then as attachments in email notification.
 * TODO: we print error messages to system.err, for don't importing any log dependency. Find out more elegant way
 * User: yev
 * Date: 6/7/15
 * Time: 21:33
 */
public class FailTestScreenshotListener<T> extends TestListenerAdapter {
  private static String currentDir = null;

  static {
    try {
      currentDir = new File(".").getCanonicalPath();
    } catch (IOException e) {
      System.err.println("Error while detecting current working dir. Reason:"+e.getMessage());
    }
  }

  @Override
  public void onTestFailure(ITestResult tr) {
    super.onTestFailure(tr);

    WebDriver webDriver = findWebDriverByReflection(tr);
    if ( webDriver == null){
      System.err.println( String.format("The test class '%s' does not have any field of type 'org.openqa.selenium.WebDriver'. " +
              "ScreenshotTestListener can not continue.", tr.getClass().getName()));
      return;
    }

    File f = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
    try {
      final long timestamp = new Date().getTime();
      Path screenshotPath = Paths.get(currentDir, "target", "screenshot_" + tr.getMethod().getMethodName() + "_"+ timestamp+".png");
      System.out.println("copying "+screenshotPath);
      Files.copy(f.toPath(),screenshotPath, StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
      System.err.println("error during the screenshot copy file operation:" + e.getMessage());
    }

  }

  private WebDriver findWebDriverByReflection(ITestResult tr){
    Class<?> c = tr.getInstance().getClass();
    Field[] fields = c.getDeclaredFields();
    WebDriver webDriver = null;
    for (Field eachField: fields){
      if (eachField.getType() == WebDriver.class){
        try {
          webDriver = (WebDriver) eachField.get(tr.getInstance());
        } catch (IllegalAccessException e) {
{'fraction_non_alphanumeric': 0.11568322981366459, 'fraction_numerical': 0.003105590062111801, 'mean_word_length': 3.9122974261201144, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3520726', 'n_tokens_mistral': 1555, 'n_tokens_neox': 1508, 'n_words': 403}