Reputation: 395
We have a couple of feature files, that share common steps. Login for example is the simplest step that comes to mind, if I want to get the user object that the login step created and was set in the LoginStepDefinitions.java file. Is it in my context, is there someway to access this class' variables, can I Autowire another step definition or something?
Upvotes: 0
Views: 1303
Reputation: 96
nilesh's answer is close, but to be more modular, you can keep your step definitions separated from each other and have context-aware objects injected into each of them. Cucumber can guarantee the state cleanliness before each scenario.
Upvotes: 1
Reputation: 14279
Yes you can. Use Context Injection
. Cucumber supports many dependency injection frameworks like Spring, Guice and Picocontainer. So you can use any of these framework to Autowire your objects.
Let's call gherkin text as steps and their implementation as step defs to avoid confusion. Let's say you have two feature files login.feature and product.feature. Their corresponding implementations are in LoginStepDef.java
and ProductStepDef.java
. Now lets say you have reused steps from login.feature
in product.feature
. What you should do in this case is inject
the context
in required stepdef, i.e. inject LoginStepDef
object in ProductStepDef
. Cucumber will internally use this context object(injected LoginStepDef object) to call the step you want to reuse from login.feature. Just have a getter
method like getUser()
in LoginStepDef
. Use the injected LoginStepDef
object in ProductStepDef
now to get the User object or whatever created/exposed in LoginStepDef
.
Upvotes: 2