/**
   * @param step the step to run
   * @param xStream used to convert a string to declared stepdef arguments
   * @return an Array matching the types or {@code parameterTypes}, or an array of String if {@code
   *     parameterTypes} is null
   */
  private Object[] transformedArgs(Step step, LocalizedXStreams.LocalizedXStream xStream) {
    int argumentCount = getArguments().size();

    if (step.getRows() != null) {
      argumentCount++;
    } else if (step.getDocString() != null) {
      argumentCount++;
    }
    Integer parameterCount = stepDefinition.getParameterCount();
    if (parameterCount != null && argumentCount != parameterCount) {
      throw arityMismatch(parameterCount);
    }

    List<Object> result = new ArrayList<Object>();

    int n = 0;
    for (Argument a : getArguments()) {
      ParameterInfo parameterInfo = getParameterType(n, String.class);
      Object arg = parameterInfo.convert(a.getVal(), xStream);
      result.add(arg);
      n++;
    }

    if (step.getRows() != null) {
      result.add(tableArgument(step, n, xStream));
    } else if (step.getDocString() != null) {
      result.add(step.getDocString().getValue());
    }
    return result.toArray(new Object[result.size()]);
  }
 private CucumberException arityMismatch(int parameterCount) {
   List<Argument> arguments = createArgumentsForErrorMessage(step);
   return new CucumberException(
       String.format(
           "Arity mismatch: Step Definition '%s' with pattern [%s] is declared with %s parameters. However, the gherkin step has %s arguments %s. \nStep: %s%s",
           stepDefinition.getLocation(true),
           stepDefinition.getPattern(),
           parameterCount,
           arguments.size(),
           arguments,
           step.getKeyword(),
           step.getName()));
 }
 private Object tableArgument(
     Step step, int argIndex, LocalizedXStreams.LocalizedXStream xStream) {
   ParameterInfo parameterInfo = getParameterType(argIndex, DataTable.class);
   TableConverter tableConverter = new TableConverter(xStream, parameterInfo);
   DataTable table = new DataTable(step.getRows(), tableConverter);
   Type type = parameterInfo.getType();
   return tableConverter.convert(table, type, parameterInfo.isTransposed());
 }
Beispiel #4
0
 @Override
 public void step(Step step) {
   if (!featureElementEvents.isEmpty()) {
     featureElementEvents.add(step);
   } else {
     backgroundEvents.add(step);
   }
   featureElementRange = new Range(featureElementRange.getFirst(), step.getLineRange().getLast());
 }
 private List<Argument> createArgumentsForErrorMessage(Step step) {
   List<Argument> arguments = new ArrayList<Argument>(getArguments());
   if (step.getDocString() != null) {
     arguments.add(new Argument(-1, "DocString:" + step.getDocString().getValue()));
   }
   if (step.getRows() != null) {
     List<List<String>> rows =
         map(
             step.getRows(),
             new Mapper<DataTableRow, List<String>>() {
               @Override
               public List<String> map(DataTableRow row) {
                 return row.getCells();
               }
             });
     arguments.add(new Argument(-1, "Table:" + rows.toString()));
   }
   return arguments;
 }
  @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()));
   }
 }
 public List<Argument> matchedArguments(Step step) {
   try {
     Object args = backend.invoke(iokeStepDefObject, "arguments_from", step.getName());
     if (args.equals(ioke.nil)) {
       return null;
     } else {
       return (List<Argument>) args;
     }
   } catch (ControlFlow controlFlow) {
     throw new RuntimeException("Failed to get step args", controlFlow);
   }
 }
  @Test
  public void shouldAssignUnequalDescriptionsToDifferentOccurrencesOfSameStepInAScenario()
      throws Exception {
    List<CucumberFeature> features =
        CucumberFeature.load(
            new ClasspathResourceLoader(this.getClass().getClassLoader()),
            asList("cucumber/junit/feature_with_same_steps_in_scenario.feature"),
            Collections.emptyList());

    ExecutionUnitRunner runner =
        new ExecutionUnitRunner(
            null, (CucumberScenario) features.get(0).getFeatureElements().get(0), null);

    // fish out the two occurrences of the same step and check whether we really got them
    Step stepOccurrence1 = runner.getChildren().get(0);
    Step stepOccurrence2 = runner.getChildren().get(2);
    assertEquals(stepOccurrence1.getName(), stepOccurrence2.getName());

    assertFalse(
        "Descriptions must not be equal.",
        runner.describeChild(stepOccurrence1).equals(runner.describeChild(stepOccurrence2)));
  }
 public List<Argument> matchedArguments(Step step) {
   return argumentMatcher.argumentsFrom(step.getName());
 }
 public String getStepName() {
   return step.getName();
 }
 public StackTraceElement getStepLocation() {
   return step.getStackTraceElement(featurePath);
 }