Parameter Type: Step Argument Transformation for Cucumber Java

Viewed 52

I am working on Cucumber Java. And trying to transform a Parameter.

Here are the steps I followed.

Below Step I am using, so that to transform the parameter before consuming,

@When("^I format \"([^\"]*)\" and save by key \"([^\"]*)\"$")
    public void i_format_something_and_save_by_key_something(StringValue value, String key) throws Throwable {
        String createdValue = value.value;
    }

Here StringValue is a class for holding a string,

public class StringValue {
    public String value;

    public StringValue(String value) {
        this.value = value;
    }
}

I have a code written to transform the parameter. Which is used from Cucumber Type Registry,

public class StepTrasform {
    @ParameterType(".*")
    public StringValue TransfromParam(String functionExpression) {
        return new StringValue(TransformFunction.Transform(functionExpression));
    }
}

Here is the error I am getting when I run the scenario,

io.cucumber.core.exception.CucumberException: Could not convert arguments for step [^I format "([^"]*)" and save by key "([^"]*)"$] defined at 'stepdefins.CommonPageSteps.i_format_something_and_save_by_key_something(utils.StringValue,java.lang.String)'.
    at io.cucumber.core.runner.PickleStepDefinitionMatch.couldNotConvertArguments(PickleStepDefinitionMatch.java:112)
    at io.cucumber.core.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:56)
    at io.cucumber.core.runner.ExecutionMode$1.execute(ExecutionMode.java:10)
    at io.cucumber.core.runner.TestStep.executeStep(TestStep.java:86)
    at io.cucumber.core.runner.TestStep.run(TestStep.java:57)
    at io.cucumber.core.runner.PickleStepTestStep.run(PickleStepTestStep.java:51)
    at io.cucumber.core.runner.TestCase.run(TestCase.java:95)
    at io.cucumber.core.runner.Runner.runPickle(Runner.java:75)
    at io.cucumber.testng.TestNGCucumberRunner.lambda$runScenario$1(TestNGCucumberRunner.java:132)
    at io.cucumber.core.runtime.CucumberExecutionContext.lambda$runTestCase$3(CucumberExecutionContext.java:151)
    at io.cucumber.core.runtime.RethrowingThrowableCollector.executeAndThrow(RethrowingThrowableCollector.java:23)
    at io.cucumber.core.runtime.CucumberExecutionContext.runTestCase(CucumberExecutionContext.java:151)
    at io.cucumber.testng.TestNGCucumberRunner.runScenario(TestNGCucumberRunner.java:129)
    at io.cucumber.testng.AbstractTestNGCucumberTests.runScenario(AbstractTestNGCucumberTests.java:35)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:577)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1129)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:746)
    at org.testng.TestRunner.run(TestRunner.java:600)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1264)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1189)
    at org.testng.TestNG.runSuites(TestNG.java:1104)
    at org.testng.TestNG.run(TestNG.java:1076)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57)
Caused by: io.cucumber.cucumberexpressions.CucumberExpressionException: ParameterType {anonymous} failed to transform [@GenerateRandomString(10)] to class utils.StringValue
    at io.cucumber.cucumberexpressions.ParameterType.transform(ParameterType.java:264)
    at io.cucumber.cucumberexpressions.Argument.getValue(Argument.java:42)
    at io.cucumber.core.stepexpression.ExpressionArgument.getValue(ExpressionArgument.java:17)
    at io.cucumber.core.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:47)
    ... 35 more
Caused by: java.lang.IllegalArgumentException: Can't transform '@GenerateRandomString(10)' to class utils.StringValue
BuiltInParameterTransformer only supports a limited number of class types
Consider using a different object mapper or register a parameter type for class utils.StringValue
    at io.cucumber.cucumberexpressions.BuiltInParameterTransformer.createIllegalArgumentException(BuiltInParameterTransformer.java:120)
    at io.cucumber.cucumberexpressions.BuiltInParameterTransformer.doTransform(BuiltInParameterTransformer.java:99)
    at io.cucumber.cucumberexpressions.BuiltInParameterTransformer.transform(BuiltInParameterTransformer.java:22)
    at io.cucumber.cucumberexpressions.RegularExpression.lambda$match$0(RegularExpression.java:66)
    at io.cucumber.cucumberexpressions.ParameterType$TransformerAdaptor.transform(ParameterType.java:299)
    at io.cucumber.cucumberexpressions.ParameterType.transform(ParameterType.java:259)
    ... 38 more
1 Answers

I suppose that you have a scenario like this.

Scenario: cucumber parameter
  Given: ...
  When I format "@GenerateRandom(10)" and save by key "someKey"

Latest versions of cucumber allow you use Cucumber Expressions like {string}, {int}, {customExpression}.

@When("I format {stringValue} and save by key {string}")
@ParameterType(".*") // method name is stringValue
public StringValue stringValue(String functionExpression) { /* ... */ }

// or with a different method name
@ParameterType(value = ".*", name = "stringValue")
public StringValue transfromParam(String functionExpression) { /* ... */ }

Otherwise, you can change ParameterType regex to "[^\"]*".

// @ParameterType(".*")
        
@ParameterType("[^\"]*")
public StringValue TransfromParam(String functionExpression) {
  return new StringValue(TransformFunction.Transform(functionExpression));
}
@When("^I format \"([^\"]*)\" and save by key \"([^\"]*)\"$") 
public void i_format_something_and_save_by_key_something(StringValue value, String key) throws Throwable {
    String createdValue = value.value;
}
Related