@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()));
   }
 }
    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;
    }