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();
      }
    }
  }
 public synchronized void onTestFailure(ITestResult arg0) {
   System.out.println(getThreadId() + " Test " + getTestDesc(arg0) + " failed.");
   if (arg0.getThrowable() != null)
     log.error("Test failed " + getTestDesc(arg0), arg0.getThrowable());
   failed.incrementAndGet();
   printStatus();
 }
 @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 afterInvocation(IInvokedMethod method, ITestResult testResult) {

    if (!method.isTestMethod()) return;
    if (testResult.getThrowable() != null)
      log.errorf(
          testResult.getThrowable(), "Method %s threw an exception", getTestDesc(testResult));
  }
 public synchronized void onTestSkipped(ITestResult arg0) {
   System.out.println(getThreadId() + " Test " + getTestDesc(arg0) + " skipped.");
   log.info(" Test " + getTestDesc(arg0) + " skipped.");
   if (arg0.getThrowable() != null)
     log.error("Test skipped : " + arg0.getThrowable(), arg0.getThrowable());
   skipped.incrementAndGet();
   printStatus();
 }
  public void afterInvocation(IInvokedMethod method, ITestResult result) {
    resultT = result;
    String textMsg = "Completed executing " + returnMethodName(method.getTestMethod());
    Reporter.log(textMsg, true);

    Reporter.setCurrentTestResult(result);

    if (method.isTestMethod()) {

      List<Throwable> verificationFailures = TestBase.getVerificationFailures();

      // if there are verification failures...
      if (verificationFailures.size() > 0) {

        // set the test to failed
        result.setStatus(ITestResult.FAILURE);

        // if there is an assertion failure add it to verificationFailures
        if (result.getThrowable() != null) {
          verificationFailures.add(result.getThrowable());
        }

        int size = verificationFailures.size();
        // if there's only one failure just set that
        if (size == 1) {
          result.setThrowable(verificationFailures.get(0));
        } else {
          // create a failure message with all failures and stack traces (except last failure)
          StringBuffer failureMessage =
              new StringBuffer("Multiple failures (").append(size).append("):\n\n");
          for (int i = 0; i < size - 1; i++) {
            failureMessage
                .append("Failure ")
                .append(i + 1)
                .append(" of ")
                .append(size)
                .append(":\n");
            Throwable t = verificationFailures.get(i);
            String fullStackTrace = Utils.stackTrace(t, false)[1];
            failureMessage.append(fullStackTrace).append("\n\n");
          }

          // final failure
          Throwable last = verificationFailures.get(size - 1);
          failureMessage.append("Failure ").append(size).append(" of ").append(size).append(":\n");
          failureMessage.append(last.toString());

          // set merged throwable
          Throwable merged = new Throwable(failureMessage.toString());
          merged.setStackTrace(last.getStackTrace());

          result.setThrowable(merged);
        }
      }
    }
  }
  public void onFailure(ITestResult result) {
    if (getWebDriver() == null) {
      System.out.println(
          "Can't take a screenshot and save HTML, because there is no driver available.");
      return;
    }

    Throwable throwable = result.getThrowable();
    String stacktrace = null;

    if (throwable != null) {
      stacktrace = ExceptionUtils.getStackTrace(throwable);
    }

    String filenameIdentification = getFilenameIdentification(result);

    // TODO traffic can be captured using BrowserMob Proxy
    // String traffic;
    // try {
    // traffic = selenium.captureNetworkTraffic(NetworkTrafficType.PLAIN).getTraffic();
    // } catch (SeleniumException e) {
    // traffic = ExceptionUtils.getFullStackTrace(e);
    // }

    try {
      File screenshot = null;
      if (((GrapheneProxyInstance) getWebDriver()).unwrap() instanceof TakesScreenshot) {
        screenshot = ((TakesScreenshot) getWebDriver()).getScreenshotAs(OutputType.FILE);
      }

      String htmlSource = getWebDriver().getPageSource();

      File stacktraceOutputFile =
          new File(failuresOutputDir, filenameIdentification + "/stacktrace.txt");
      File imageOutputFile =
          new File(failuresOutputDir, filenameIdentification + "/screenshot.png");
      // File trafficOutputFile = new File(failuresOutputDir, filenameIdentification +
      // "/network-traffic.txt");
      // File logOutputFile = new File(failuresOutputDir, filenameIdentification +
      // "/selenium-log.txt");
      File htmlSourceOutputFile =
          new File(failuresOutputDir, filenameIdentification + "/html-source.html");

      File directory = imageOutputFile.getParentFile();
      FileUtils.forceMkdir(directory);

      FileUtils.writeStringToFile(stacktraceOutputFile, stacktrace);
      if (!HtmlUnitDriver.class.isInstance(getWebDriver())) {
        FileUtils.copyFile(screenshot, imageOutputFile);
      }

      // FileUtils.writeStringToFile(trafficOutputFile, traffic);
      // FileUtils.writeLines(logOutputFile, methodLog);
      FileUtils.writeStringToFile(htmlSourceOutputFile, htmlSource);
    } catch (Exception e) {
      System.err.println("Can't take a screenshot/save HTML source: " + e.getMessage());
      e.printStackTrace(System.err);
      // LOGGER.log(Level.WARNING, "Can't take a screenshot/save HTML source.", e);
    }
  }
Exemplo n.º 8
0
  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 onTestSkipped(ITestResult iTestResult) {
   super.onTestSkipped(iTestResult);
   testMethodName = TestNGUtils.constructTestMethodName(iTestResult);
   LogUtils.log("Test Skipped: " + testMethodName, iTestResult.getThrowable());
   write2LogFile(iTestResult, DumpUtils.createTestFolder(testMethodName, suiteName));
 }
 private void onTestFinished(ITestResult iTestResult, TestResult.ResultType resultType) {
   Object testId;
   TestStartEvent startEvent = null;
   synchronized (lock) {
     testId = tests.remove(iTestResult);
     if (testId == null) {
       // This can happen when a method fails which this method depends on
       testId = idGenerator.generateId();
       Object parentId = testMethodToSuiteMapping.get(iTestResult.getMethod());
       startEvent = new TestStartEvent(iTestResult.getStartMillis(), parentId);
     }
   }
   if (startEvent != null) {
     // Synthesize a start event
     resultProcessor.started(
         new DefaultTestMethodDescriptor(
             testId, iTestResult.getTestClass().getName(), iTestResult.getName()),
         startEvent);
   }
   if (resultType == TestResult.ResultType.FAILURE) {
     resultProcessor.failure(testId, iTestResult.getThrowable());
   }
   resultProcessor.completed(
       testId, new TestCompleteEvent(iTestResult.getEndMillis(), resultType));
 }
  /**
   * Delegates to the {@link IHookCallBack#runTestMethod(ITestResult) test method} in the supplied
   * {@code callback} to execute the actual test and then tracks the exception thrown during test
   * execution, if any.
   *
   * @see org.testng.IHookable#run(org.testng.IHookCallBack, org.testng.ITestResult)
   */
  @Override
  public void run(IHookCallBack callBack, ITestResult testResult) {
    callBack.runTestMethod(testResult);

    Throwable testResultException = testResult.getThrowable();
    if (testResultException instanceof InvocationTargetException) {
      testResultException = ((InvocationTargetException) testResultException).getCause();
    }
    this.testException = testResultException;
  }
 private void generateForResult(ITestResult ans, ITestNGMethod method, int resultSetSize) {
   Object[] parameters = ans.getParameters();
   boolean hasParameters = parameters != null && parameters.length > 0;
   if (hasParameters) {
     tableStart("result", null);
     m_out.print("<tr class=\"param\">");
     for (int x = 1; x <= parameters.length; x++) {
       m_out.print("<th>Parameter #" + x + "</th>");
     }
     m_out.println("</tr>");
     m_out.print("<tr class=\"param stripe\">");
     for (Object p : parameters) {
       m_out.println("<td>" + Utils.escapeHtml(p.toString()) + "</td>");
     }
     m_out.println("</tr>");
   }
   List<String> msgs = Reporter.getOutput(ans);
   boolean hasReporterOutput = msgs.size() > 0;
   Throwable exception = ans.getThrowable();
   boolean hasThrowable = exception != null;
   if (hasReporterOutput || hasThrowable) {
     if (hasParameters) {
       m_out.print("<tr><td");
       if (parameters.length > 1) {
         m_out.print(" colspan=\"" + parameters.length + "\"");
       }
       m_out.println(">");
     } else {
       m_out.println("<div>");
     }
     if (hasReporterOutput) {
       if (hasThrowable) {
         m_out.println("<h3>Test Messages</h3>");
       }
       for (String line : msgs) {
         m_out.println(line + "<br/>");
       }
     }
     if (hasThrowable) {
       boolean wantsMinimalOutput = ans.getStatus() == ITestResult.SUCCESS;
       if (hasReporterOutput) {
         m_out.println("<h3>" + (wantsMinimalOutput ? "Expected Exception" : "Failure") + "</h3>");
       }
       generateExceptionReport(exception, method);
     }
     if (hasParameters) {
       m_out.println("</td></tr>");
     } else {
       m_out.println("</div>");
     }
   }
   if (hasParameters) {
     m_out.println("</table>");
   }
 }
  @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);
        }
      }
    }
  }
Exemplo n.º 14
0
 @AfterMethod(alwaysRun = true)
 public void addFailureToHttpLog(ITestResult result) {
   if (httpLog != null && result.getStatus() == ITestResult.FAILURE) {
     // Add the failure details after the HTTP trace so it's clear what test it belongs to.
     httpLog.println(">>> [FAILURE] Test: " + result.getName());
     Throwable thrown = result.getThrowable();
     if (thrown != null) {
       httpLog.append(thrown.getLocalizedMessage());
       httpLog.println();
     }
     httpLog.println();
   }
 }
Exemplo n.º 15
0
  @Override
  public void onTestFailure(ITestResult result) {
    driver = NewDriverProvider.getWebDriver();
    if (driver == null) {
      driver = NewDriverProvider.getWebDriver();
    }

    imageCounter += 1;
    if ("true".equals(Configuration.getLogEnabled())) {
      try {
        new Shooter().savePageScreenshot(screenPath + imageCounter, driver);
        CommonUtils.appendTextToFile(screenPath + imageCounter + ".html", getPageSource(driver));
      } catch (Exception e) {
        log(
            "onException",
            "driver has no ability to catch screenshot or html source - driver may died",
            false);
      }

      String exception =
          escapeHtml(
              result.getThrowable().toString()
                  + "\n"
                  + ExceptionUtils.getStackTrace(result.getThrowable()));

      StringBuilder builder = new StringBuilder();
      builder.append(
          "<tr class=\"error\"><td>error</td><td><pre>"
              + exception
              + "</pre></td><td> <br/><a href='screenshots/screenshot"
              + imageCounter
              + ".png'>Screenshot</a><br/><a href='screenshots/screenshot"
              + imageCounter
              + ".html'>HTML Source</a></td></tr>");
      CommonUtils.appendTextToFile(logPath, builder.toString());
      logJSError(driver);
      onTestSuccess(result);
    }
  }
 public void onConfigurationFailure(ITestResult testResult) {
   // Synthesise a test for the broken configuration method
   TestDescriptorInternal test =
       new DefaultTestMethodDescriptor(
           idGenerator.generateId(),
           testResult.getMethod().getTestClass().getName(),
           testResult.getMethod().getMethodName());
   resultProcessor.started(test, new TestStartEvent(testResult.getStartMillis()));
   resultProcessor.failure(test.getId(), testResult.getThrowable());
   resultProcessor.completed(
       test.getId(),
       new TestCompleteEvent(testResult.getEndMillis(), TestResult.ResultType.FAILURE));
 }
Exemplo n.º 17
0
    @Override
    public void onTestFailure(ITestResult tr) {
      failed++;
      count++;
      String location =
          tr.getTestClass().getRealClass().getSimpleName() + '#' + tr.getMethod().getMethodName();
      try {
        Method realTestMethod = tr.getMethod().getMethod();
        Test testAnnot = realTestMethod.getAnnotation(Test.class);
        if (testAnnot != null
            && testAnnot.description() != null
            && !testAnnot.description().isEmpty()) {
          if (tr.getThrowable() != null) {
            StringWriter sw = new StringWriter();
            PrintWriter w = new PrintWriter(sw);
            tr.getThrowable().printStackTrace(w);
            w.flush();
            log("[FAILED]  " + testAnnot.description() + "(" + location + "):\n" + sw.toString());
          } else {
            log("[FAILED]  " + testAnnot.description() + "(" + location + ")");
          }
        } else {

          if (tr.getThrowable() != null) {
            StringWriter sw = new StringWriter();
            PrintWriter w = new PrintWriter(sw);
            tr.getThrowable().printStackTrace(w);
            w.flush();
            log("[FAILED]  " + location + ":\n" + sw.toString());
          } else {
            log("[FAILED]  " + location);
          }
        }
      } catch (IOException e) {
        throw new IllegalStateException("IO Error", e);
      }
    }
 @Override
 public void afterInvocation(IInvokedMethod method, ITestResult result) {
   Throwable throwable = result.getThrowable();
   if (throwable != null) {
     TestBase.setFailed(throwable.getMessage());
   }
   super.afterInvocation(method, result);
   if (method.isTestMethod()) {
     if (!TestBase.getPassed()) {
       logBusinessScreenshot("Error Occurred!");
     } else {
       if (TestBaseWebDriver.takePassedScreenshot) {
         logBusinessScreenshot("Test Passed");
       }
     }
   }
 }
 public void onConfigurationFailure(ITestResult testResult) {
   if (failedConfigurations.put(testResult, true) != null) {
     // workaround for bug in TestNG 6.2 (apparently fixed in some 6.3.x): listener is notified
     // twice per event
     return;
   }
   // Synthesise a test for the broken configuration method
   TestDescriptorInternal test =
       new DefaultTestMethodDescriptor(
           idGenerator.generateId(),
           testResult.getMethod().getTestClass().getName(),
           testResult.getMethod().getMethodName());
   resultProcessor.started(test, new TestStartEvent(testResult.getStartMillis()));
   resultProcessor.failure(test.getId(), testResult.getThrowable());
   resultProcessor.completed(
       test.getId(),
       new TestCompleteEvent(testResult.getEndMillis(), TestResult.ResultType.FAILURE));
 }
Exemplo n.º 20
0
 @Override
 public void onTestFailure(ITestResult tr) {
   super.onTestFailure(tr);
   Logger.log("=======================================================");
   Logger.log("测试方法 【" + tr.getName() + "】 执行失败");
   if (DriverBase.driver != null) {
     // 截图
     String name = tr.getName() + "_" + String.valueOf(System.currentTimeMillis());
     DriverBase.driver.screenShot(name);
     Logger.log(
         "请参考:<a href=\"../screenshot/"
             + name
             + ".jpg"
             + "\" style=\"color:red;\">"
             + name
             + ".jpg"
             + "</font></a>");
   }
   Logger.log("失败原因为:" + tr.getThrowable().getMessage());
   Logger.log("=======================================================");
 }
Exemplo n.º 21
0
 @Override
 public void onTestFailure(ITestResult testResult) {
   @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
   Throwable throwable = testResult.getThrowable();
   if (throwable instanceof AssertionError) {
     Matcher m = ASSERT_EQ_PATTERN.matcher(throwable.getMessage());
     if (m.matches()) {
       String test = m.group(1);
       String expected = m.group(2).replaceAll("\n", "\\\\n");
       String actual = m.group(3).replaceAll("\n", "\\\\n");
       System.out.println(test);
       System.out.println("- " + expected);
       System.out.println("+ " + actual);
     } else {
       System.out.println("failed: " + throwable.getMessage());
     }
   } else {
     System.out.println(
         throwable.getClass().getSimpleName() + ": " + testResult.getParameters()[0]);
   }
 }
  @Override
  public void onTestFailure(ITestResult result) {
    if (driver == null) {
      return;
    }

    Throwable throwable = result.getThrowable();
    String stacktrace = null;

    if (throwable != null) {
      stacktrace = ExceptionUtils.getStackTrace(throwable);
    }

    String filenameIdentification = getFilenameIdentification(result);

    String htmlSource = driver.getPageSource();

    File stacktraceOutputFile =
        new File(failuresOutputDir, filenameIdentification + "/stacktrace.txt");
    File imageOutputFile = new File(failuresOutputDir, filenameIdentification + "/screenshot.png");
    File htmlSourceOutputFile =
        new File(failuresOutputDir, filenameIdentification + "/html-source.html");

    try {
      File directory = imageOutputFile.getParentFile();
      FileUtils.forceMkdir(directory);

      FileUtils.writeStringToFile(stacktraceOutputFile, stacktrace);
      FileUtils.writeStringToFile(htmlSourceOutputFile, htmlSource);

      File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
      FileUtils.copyFile(scrFile, imageOutputFile);

    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  @Override
  public void onTestFailure(ITestResult result) {
    logEndOfTest(result, "FAILED");
    if (BaseUITestClass.getDRIVER() != null) {
      byte[] scrFile =
          ((TakesScreenshot) BaseUITestClass.getDRIVER()).getScreenshotAs(OutputType.BYTES);
      try {
        String filename =
            OSUtil.getPath(
                "target",
                "surefire-reports",
                "screenshots",
                String.format(
                    "%s.%s.png",
                    result.getTestClass().getRealClass().getSimpleName(), result.getName()));
        FileUtils.writeByteArrayToFile(new File(filename), scrFile);
      } catch (IOException e) {
        logger.info("Saving screenshot FAILED: " + e.getCause());
      }
    }

    logger.info(ExceptionUtils.getStackTrace(result.getThrowable()));
    logLine();
  }
Exemplo n.º 24
0
 /**
  * Invoked each time a test fails.
  *
  * @param result <code>ITestResult</code> containing information about the run test
  * @see org.testng.ITestResult#FAILURE
  */
 public void onTestFailure(ITestResult result) {
   log.error(result.getThrowable());
 }
Exemplo n.º 25
0
 @Override
 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
   recordResult(result, ResultType.FAILURE, result.getThrowable());
 }
Exemplo n.º 26
0
 @Override
 public void onTestFailure(ITestResult result) {
   recordResult(result, ResultType.FAILURE, result.getThrowable());
 }
Exemplo n.º 27
0
 @Override
 public void onTestSuccess(ITestResult result) {
   recordResult(result, ResultType.SUCCESS, result.getThrowable());
 }
Exemplo n.º 28
0
 @Override
 public void onConfigurationFailure(ITestResult itr) {
   super.onConfigurationFailure(itr);
   throwable = itr.getThrowable();
   stopStreamCapture();
 }
Exemplo n.º 29
0
 public void onConfigurationFailure(ITestResult tr) {
   error("failed config: " + tr.getThrowable());
   onTestCompleted(tr, "FAIL: ", old_stderr);
 }