Reputation: 1269
Is there a way to share custom parameter types across multiple step definitions? I have a few I'd like to not duplicate them several times over. I tried extending a CommonStepDefintions
, but apparently step definition classes can't be extended. Any help is appreciated.
Example:
public class PersonStepDefinitons {
@Given("the person has {myenum}")
public void personHasMyEnum(MyEnum myEnum){
...
}
}
public class AnimelStepDefinitons {
@Given("the animel has {myenum}")
public void animalHasMyEnum(MyEnum myEnum){
...
}
}
How to not duplicate this?
public MyEnum(String raw) {
return MyEnum.valueOf(raw);
}
Upvotes: 1
Views: 1183
Reputation: 2244
I assume that you want to use a custom parameter type in many different projects. Otherwise follow the steps that @M.P. Korstanje gave in his answer.
I used to solve that problem with some trick.
The example is in written Kotlin but it will not be diffucult to translate it to Java.
I have an interface in which there are lots of custom parameter types. I put this interface in a cucumber helper library source code.
// com.example.cucumber.helper.library
interface CucumberCustomParameterTypes {
@ParameterType("yes|no|ok|ko")
fun flag(text: String) : Boolean { // ... }
}
In a cucumber test project, create a class in your glue path and it should implement
your CucumberCustomParameterTypes
. Do not forget to import your cucumber helper library to your project.
// com.example.yourproject
class CucumberTypes : CucumberCustomParameterTypes {
// ...
// additional parameter types can be defined here
}
Hope, it helps
Upvotes: 1
Reputation: 12039
You don't have to duplicate it. When executing a scenario Cucumber uses all step definitions, hooks and parameters that it finds.
Cucumber searches for these on the glue path. So suppose your glue path is com.example
then Cucumber will use everything it finds in com.example.PersonStepDefinitons
, com.example.AnimelStepDefinitons
and com.example.ParameterTypes
.
If you are used to JUnit where only one class is created at once this can be a little confusing.
If things don't work, make sure everything is in the same package and double check what your IDE actually put in the run configuration.
Upvotes: 1