Search in sources :

Example 1 with Argument

use of cucumber.api.Argument in project page-factory-2 by sbtqa.

the class DataReplacer method replaceStepArguments.

private void replaceStepArguments(PickleStepTestStep testStep, boolean isStash) throws IllegalAccessException {
    PickleStepTag step = (PickleStepTag) testStep.getPickleStep();
    String stepText = step.getText();
    String pattern = isStash ? STASH_PARSE_REGEX : PATH_PARSE_REGEX;
    Pattern stepDataPattern = Pattern.compile(pattern);
    Matcher stepDataMatcher = stepDataPattern.matcher(stepText);
    int offset = 0;
    for (Argument argument : testStep.getDefinitionArgument()) {
        String argVal = argument.getValue();
        if (argVal != null) {
            String data = replaceData(step, argVal, isStash);
            boolean isReplaceNeededParameter = stepDataPattern.matcher(argVal).find() && stepDataMatcher.find();
            Object group = FieldUtils.readField(argument, "group", true);
            if (isReplaceNeededParameter && offset == 0) {
                // this is first replace-needed parameter
                offset = data.length() - argVal.length();
                FieldUtils.writeField(group, "value", data, true);
                FieldUtils.writeField(group, "end", argument.getEnd() + offset, true);
            } else if (isReplaceNeededParameter) {
                // this is not first replace-needed parameter
                FieldUtils.writeField(group, "value", data, true);
                FieldUtils.writeField(group, "start", argument.getStart() + offset, true);
                int thisOffset = data.length() - argVal.length();
                FieldUtils.writeField(group, "end", argument.getEnd() + offset + thisOffset, true);
                offset += thisOffset;
            } else if (offset != 0) {
                // this is ordinary parameter
                FieldUtils.writeField(group, "start", argument.getStart() + offset, true);
                FieldUtils.writeField(group, "end", argument.getEnd() + offset, true);
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Argument(cucumber.api.Argument) DataTableArgument(io.cucumber.stepexpression.DataTableArgument) DocStringArgument(io.cucumber.stepexpression.DocStringArgument) Matcher(java.util.regex.Matcher) PickleStepTag(ru.sbtqa.tag.pagefactory.optional.PickleStepTag) PickleString(gherkin.pickles.PickleString)

Aggregations

Argument (cucumber.api.Argument)1 PickleString (gherkin.pickles.PickleString)1 DataTableArgument (io.cucumber.stepexpression.DataTableArgument)1 DocStringArgument (io.cucumber.stepexpression.DocStringArgument)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 PickleStepTag (ru.sbtqa.tag.pagefactory.optional.PickleStepTag)1