黄瓜表达式是否以某种方式与正则表达式交互?
Are cucumber expressions interact somehow with regular expression?
从 Cucumber-JVM 2.X 迁移到 Cucumber-JVM 5.5.0 后,我遇到了使用正则表达式和可选捕获组的现有步骤定义的问题。
对于版本 2.X,当小黄瓜步骤中没有可选文本时,步骤定义方法得到空值。
对于 5.5.0 版,如果我没有在小黄瓜步骤中提供可选文本,那么我最终会出现异常。
场景
Scenario: cucumber expression vs regular expression
And text aaaa text2
And text text2
GlueCode
@Given("^text(?: (.+))? text2$")
public void optionalCaptureGroup(String optionalGroup) {
System.out.println(optionalGroup);
}
@ParameterType(".+")
public String paramType1(String s) {
return s;
}
异常
io.cucumber.core.exception.CucumberException: Could not convert arguments for step [^text(?: (.+))? text2$] defined at 'migrate.ParameterTypeStepDefinition.optionalCaptureGroup(java.lang.String)'.
The details are in the stacktrace below.
at io.cucumber.core.runner.PickleStepDefinitionMatch.couldNotConvertArguments(PickleStepDefinitionMatch.java:119)
at io.cucumber.core.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:56)
at io.cucumber.core.runner.TestStep.executeStep(TestStep.java:64)
at io.cucumber.core.runner.TestStep.run(TestStep.java:49)
at io.cucumber.core.runner.PickleStepTestStep.run(PickleStepTestStep.java:46)
at io.cucumber.core.runner.TestCase.run(TestCase.java:51)
at io.cucumber.core.runner.Runner.runPickle(Runner.java:66)
at io.cucumber.core.runtime.Runtime.lambda$run(Runtime.java:100)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at io.cucumber.core.runtime.Runtime$SameThreadExecutorService.execute(Runtime.java:243)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
at io.cucumber.core.runtime.Runtime.lambda$run(Runtime.java:100)
at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:193)
at java.util.stream.SliceOps.accept(SliceOps.java:204)
at java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1359)
at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:499)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:486)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at io.cucumber.core.runtime.Runtime.run(Runtime.java:101)
at io.cucumber.core.cli.Main.run(Main.java:75)
at io.cucumber.core.cli.Main.main(Main.java:31)
Caused by: io.cucumber.cucumberexpressions.CucumberExpressionException: ParameterType {paramType1} failed to transform [] to class java.lang.String
at io.cucumber.cucumberexpressions.ParameterType.transform(ParameterType.java:237)
at io.cucumber.cucumberexpressions.Argument.getValue(Argument.java:67)
at io.cucumber.core.stepexpression.ExpressionArgument.getValue(ExpressionArgument.java:15)
at io.cucumber.core.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:47)
... 25 more
Caused by: io.cucumber.core.backend.CucumberBackendException: Failed to invoke public java.lang.String migrate.ParameterTypeStepDefinition.paramType1(java.lang.String)
at io.cucumber.java.Invoker.invoke(Invoker.java:29)
at io.cucumber.java.JavaParameterTypeDefinition.execute(JavaParameterTypeDefinition.java:81)
at io.cucumber.cucumberexpressions.ParameterType.transform(ParameterType.java:233)
... 28 more
Caused by: java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at io.cucumber.java.Invoker.invoke(Invoker.java:27)
... 30 more
我的问题是:
1. 为什么我会收到关于错误数量参数的异常 - 自版本 2.X 以来,可选捕获组的处理是否有任何变化?
2. 为什么cucumber在步骤定义使用纯正则表达式的情况下要与自定义类型参数进行交互?
所以我删除了我之前的答案,因为它是错误的。看起来 Cucumber 处理可选参数类型的方式存在错误。
您可以通过定义一个以数组作为参数的参数类型来解决这个问题。
@ParameterType(value = ".+")
public String paramType1(String... s) {
if (s.length > 0) {
return s[0];
}
return null;
}
通常这将用于具有多个捕获组的参数类型,但它也适用于这些情况。
@ParameterType(value = "([a-z]+)([0-9]+)")
public String paramType1(String... s) {
}
从 Cucumber-JVM 2.X 迁移到 Cucumber-JVM 5.5.0 后,我遇到了使用正则表达式和可选捕获组的现有步骤定义的问题。
对于版本 2.X,当小黄瓜步骤中没有可选文本时,步骤定义方法得到空值。
对于 5.5.0 版,如果我没有在小黄瓜步骤中提供可选文本,那么我最终会出现异常。
场景
Scenario: cucumber expression vs regular expression
And text aaaa text2
And text text2
GlueCode
@Given("^text(?: (.+))? text2$")
public void optionalCaptureGroup(String optionalGroup) {
System.out.println(optionalGroup);
}
@ParameterType(".+")
public String paramType1(String s) {
return s;
}
异常
io.cucumber.core.exception.CucumberException: Could not convert arguments for step [^text(?: (.+))? text2$] defined at 'migrate.ParameterTypeStepDefinition.optionalCaptureGroup(java.lang.String)'.
The details are in the stacktrace below.
at io.cucumber.core.runner.PickleStepDefinitionMatch.couldNotConvertArguments(PickleStepDefinitionMatch.java:119)
at io.cucumber.core.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:56)
at io.cucumber.core.runner.TestStep.executeStep(TestStep.java:64)
at io.cucumber.core.runner.TestStep.run(TestStep.java:49)
at io.cucumber.core.runner.PickleStepTestStep.run(PickleStepTestStep.java:46)
at io.cucumber.core.runner.TestCase.run(TestCase.java:51)
at io.cucumber.core.runner.Runner.runPickle(Runner.java:66)
at io.cucumber.core.runtime.Runtime.lambda$run(Runtime.java:100)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at io.cucumber.core.runtime.Runtime$SameThreadExecutorService.execute(Runtime.java:243)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
at io.cucumber.core.runtime.Runtime.lambda$run(Runtime.java:100)
at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:193)
at java.util.stream.SliceOps.accept(SliceOps.java:204)
at java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1359)
at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:499)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:486)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at io.cucumber.core.runtime.Runtime.run(Runtime.java:101)
at io.cucumber.core.cli.Main.run(Main.java:75)
at io.cucumber.core.cli.Main.main(Main.java:31)
Caused by: io.cucumber.cucumberexpressions.CucumberExpressionException: ParameterType {paramType1} failed to transform [] to class java.lang.String
at io.cucumber.cucumberexpressions.ParameterType.transform(ParameterType.java:237)
at io.cucumber.cucumberexpressions.Argument.getValue(Argument.java:67)
at io.cucumber.core.stepexpression.ExpressionArgument.getValue(ExpressionArgument.java:15)
at io.cucumber.core.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:47)
... 25 more
Caused by: io.cucumber.core.backend.CucumberBackendException: Failed to invoke public java.lang.String migrate.ParameterTypeStepDefinition.paramType1(java.lang.String)
at io.cucumber.java.Invoker.invoke(Invoker.java:29)
at io.cucumber.java.JavaParameterTypeDefinition.execute(JavaParameterTypeDefinition.java:81)
at io.cucumber.cucumberexpressions.ParameterType.transform(ParameterType.java:233)
... 28 more
Caused by: java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at io.cucumber.java.Invoker.invoke(Invoker.java:27)
... 30 more
我的问题是:
1. 为什么我会收到关于错误数量参数的异常 - 自版本 2.X 以来,可选捕获组的处理是否有任何变化?
2. 为什么cucumber在步骤定义使用纯正则表达式的情况下要与自定义类型参数进行交互?
所以我删除了我之前的答案,因为它是错误的。看起来 Cucumber 处理可选参数类型的方式存在错误。
您可以通过定义一个以数组作为参数的参数类型来解决这个问题。
@ParameterType(value = ".+")
public String paramType1(String... s) {
if (s.length > 0) {
return s[0];
}
return null;
}
通常这将用于具有多个捕获组的参数类型,但它也适用于这些情况。
@ParameterType(value = "([a-z]+)([0-9]+)")
public String paramType1(String... s) {
}