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