private Element writeTo(Document doc) {
      Element tc = doc.createElement("testcase");
      tc.setAttribute("classname", feature.getName());
      tc.setAttribute(
          "name", examples > 0 ? scenario.getName() + "_" + examples-- : scenario.getName());
      long totalDurationNanos = 0;
      for (Result r : results) {
        totalDurationNanos += r.getDuration() == null ? 0 : r.getDuration();
      }

      double totalDurationSeconds = ((double) totalDurationNanos) / 1000000000;
      String time = NUMBER_FORMAT.format(totalDurationSeconds);
      tc.setAttribute("time", time);

      StringBuilder sb = new StringBuilder();
      Result skipped = null, failed = null;
      for (int i = 0; i < steps.size(); i++) {
        int length = sb.length();
        Result result = results.get(i);
        if ("failed".equals(result.getStatus())) failed = result;
        if ("undefined".equals(result.getStatus()) || "pending".equals(result.getStatus()))
          skipped = result;
        sb.append(steps.get(i).getKeyword());
        sb.append(steps.get(i).getName());
        for (int j = 0; sb.length() - length + j < 140; j++) sb.append(".");
        sb.append(result.getStatus());
        sb.append("\n");
      }
      Element child;
      if (failed != null) {
        sb.append("\nStackTrace:\n");
        StringWriter sw = new StringWriter();
        failed.getError().printStackTrace(new PrintWriter(sw));
        sb.append(sw.toString());
        child = doc.createElement("failure");
        child.setAttribute("message", failed.getErrorMessage());
        child.appendChild(doc.createCDATASection(sb.toString()));
      } else if (skipped != null) {
        child = doc.createElement("skipped");
        child.appendChild(doc.createCDATASection(sb.toString()));
      } else {
        child = doc.createElement("system-out");
        child.appendChild(doc.createCDATASection(sb.toString()));
      }
      tc.appendChild(child);
      return tc;
    }
 private void handleHook(Result result) {
   if (result.getStatus().equals(Result.FAILED)) {
     if (testCase == null) {
       testCase = new TestCase();
     }
     testCase.results.add(result);
   }
 }
  @Override
  public void result(Result result) {
    // addTestNgAttribute(RESULTS, result);
    String timing =
        result.getDuration() != null
            ? " : " + (Math.round(result.getDuration() / 1000000000f * 100f) / 100f) + "s"
            : "";
    Step step;
    if (steps.isEmpty()) {
      step = new Step(null, "MISMATCH BETWEEN STEPS AND RESULTS", "", 0, null, null);
    } else {
      step = steps.pop();
    }
    org.testng.Reporter.log(
        "<div class=\"result\">"
            + step.getKeyword()
            + " "
            + step.getName()
            + " ("
            + result.getStatus()
            + timing
            + ")</div>");

    if (Result.FAILED.equals(result)) {
      ITestResult tr = org.testng.Reporter.getCurrentTestResult();
      tr.setThrowable(result.getError());
      tr.setStatus(ITestResult.FAILURE);
      failureCount.incrementAndGet();
    } else if (Result.SKIPPED.equals(result)) {
      ITestResult tr = org.testng.Reporter.getCurrentTestResult();
      tr.setThrowable(result.getError());
      tr.setStatus(ITestResult.SKIP);
      skipCount.incrementAndGet();
    } else if (Result.UNDEFINED.equals(result)) {
      ITestResult tr = org.testng.Reporter.getCurrentTestResult();
      tr.setThrowable(result.getError());
      tr.setStatus(ITestResult.FAILURE);
      failureCount.incrementAndGet();
    } else {
      passCount.incrementAndGet();
    }
  }
 @Override
 public void result(Result result) {
   if (result.getError() != null) {
     // If the result contains an error, report a failure.
     testResult.putString(REPORT_KEY_STACK, result.getErrorMessage());
     resultCode = REPORT_VALUE_RESULT_FAILURE;
     testResult.putString(Instrumentation.REPORT_KEY_STREAMRESULT, result.getErrorMessage());
   } else if (result.getStatus().equals("undefined")) {
     // There was a missing step definition, report an error.
     List<String> snippets = runtime.getSnippets();
     String report =
         String.format(
             "Missing step-definition\n\n%s\nfor step '%s'",
             snippets.get(snippets.size() - 1), currentStep.getName());
     testResult.putString(REPORT_KEY_STACK, report);
     resultCode = REPORT_VALUE_RESULT_ERROR;
     testResult.putString(
         Instrumentation.REPORT_KEY_STREAMRESULT,
         String.format("Missing step-definition: %s", currentStep.getName()));
   }
 }