private void write2ErrorTxtFile(ITestResult iTestResult, File testFolder) {
    if (testFolder == null) {
      LogUtils.log("Can not write error.txt - test folder is null");
      return;
    }
    //noinspection ThrowableResultOfMethodCallIgnored
    if (iTestResult.getThrowable() == null) {
      LogUtils.log("nothing to write to error.txt - throwable is null");
      return;
    }
    // trim if too long
    //noinspection ThrowableResultOfMethodCallIgnored
    String errorMsg = iTestResult.getThrowable().toString();
    if (errorMsg.length() > 120) {
      errorMsg = errorMsg.substring(0, 120 - 3) + "...";
    }

    File errorTxtFile = new File(testFolder.getAbsolutePath(), "error.txt");
    PrintWriter out = null;
    try {
      out = new PrintWriter(new BufferedWriter(new FileWriter(errorTxtFile, true)));
      out.println(errorMsg);
    } catch (IOException ioe) {
      LogUtils.log("Failed to write contents into error.txt file", ioe);
    } finally {
      if (out != null) {
        out.close();
      }
    }
  }
 @Override
 public void onTestFailure(ITestResult iTestResult) {
   if (suiteName.toLowerCase().contains("webui")) {
     if (testInvocationCounter < maxCount) {
       testInvocationCounter++;
       iTestResult.setAttribute("retry", true);
     } else {
       LogUtils.log("Number of retries expired.");
       iTestResult.setStatus(ITestResult.FAILURE);
       // reset count
       testInvocationCounter = 1;
       testMethodName = TestNGUtils.constructTestMethodName(iTestResult);
       LogUtils.log("Test Failed: " + testMethodName, iTestResult.getThrowable());
       File testFolder = DumpUtils.createTestFolder(testMethodName, suiteName);
       write2LogFile(iTestResult, testFolder);
       write2ErrorTxtFile(iTestResult, testFolder);
     }
   } else {
     testMethodName = TestNGUtils.constructTestMethodName(iTestResult);
     LogUtils.log("Test Failed: " + testMethodName, iTestResult.getThrowable());
     File testFolder = DumpUtils.createTestFolder(testMethodName, suiteName);
     write2LogFile(iTestResult, testFolder);
     write2ErrorTxtFile(iTestResult, testFolder);
   }
   super.onTestFailure(iTestResult);
 }
 @Override
 public void onTestSkipped(ITestResult iTestResult) {
   super.onTestSkipped(iTestResult);
   testMethodName = TestNGUtils.constructTestMethodName(iTestResult);
   LogUtils.log("Test Skipped: " + testMethodName, iTestResult.getThrowable());
   write2LogFile(iTestResult, DumpUtils.createTestFolder(testMethodName, suiteName));
 }
  protected void addTest(Class<?> clazz, ITestResult result) {
    try {
      TestCase test_case =
          new TestCase(
              result.getStatus(),
              clazz.getName(),
              getMethodName(result),
              result.getStartMillis(),
              result.getEndMillis());
      switch (result.getStatus()) {
        case ITestResult.FAILURE:
        case ITestResult.SKIP:
          Throwable ex = result.getThrowable();
          if (ex != null) {
            String failure_type = ex.getClass().getName();
            String failure_msg = ex.getMessage();
            String stack_trace = printException(ex);
            test_case.setFailure(failure_type, failure_msg, stack_trace);
          } else test_case.setFailure("exception", "SKIPPED", null);
          break;
      }

      synchronized (
          this) { // handle concurrent access by different threads, if test methods are run in
        // parallel
        DataOutputStream output = tests.get(clazz);
        test_case.writeTo(output);
      }
    } catch (Exception e) {
      error(e.toString());
    }
  }
  @Override
  public void onConfigurationFailure(ITestResult iTestResult) {
    super.onConfigurationFailure(iTestResult);
    String testName = iTestResult.getTestClass().getName();
    String configurationName = iTestResult.getMethod().toString().split("\\(|\\)")[0];
    if (!enableLogstash && isAfter(iTestResult)) {
      DumpUtils.copyBeforeConfigurationsLogToTestDir(testName, suiteName);
      testName = testMethodName;
    }
    if (suiteName
        == null) { // this is in case the suite has a @BeforeSuite method. which is invoked before
                   // the onStart is.
      suiteName = System.getProperty("iTests.suiteName", "sgtest");
    }
    LogUtils.log("Configuration Failed: " + configurationName, iTestResult.getThrowable());

    if (enableLogstash && iTestResult.getMethod().isBeforeClassConfiguration()) {
      initLogstash2(iTestResult);
    }

    String newmanTestFolder = System.getProperty("newman.test.path");
    if (newmanTestFolder != null) {
      File testFolder = new File(newmanTestFolder);
      ZipUtils.unzipArchive(testFolder);
      try {
        copyAllFilesToLogDir(testFolder, testFolder);
      } catch (IOException e) {
        LogUtils.log("Failed to copy all log files");
      }
    } else {
      ZipUtils.unzipArchive(testMethodName, suiteName);
    }

    if (enableLogstash
        && isAfter(iTestResult)
        && !iTestResult.getMethod().isAfterClassConfiguration()
        && !iTestResult.getMethod().isAfterSuiteConfiguration()) {
      testName = testMethodName;
    }
    File testFolder = DumpUtils.createTestFolder(testName, suiteName);
    write2LogFile(iTestResult, testFolder);
    write2ErrorTxtFile(iTestResult, testFolder);

    if (isAfter(iTestResult)) {
      if (enableLogstash) {
        if (process != null) {
          killLogstashAgent(1, logstashLogPath);
        }
        if (process2 != null && iTestResult.getMethod().isAfterClassConfiguration()) {
          killLogstashAgent(2, logstashLogPath2);
        }
      }
    }
  }
 public void onConfigurationFailure(ITestResult tr) {
   error("failed config: " + tr.getThrowable());
   onTestCompleted(tr, "FAIL: ", old_stderr);
 }