0

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?

Mohamed Sobhy
  • 385
  • 1
  • 5
  • 17

2 Answers2

2

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.

nilesh
  • 14,131
  • 7
  • 65
  • 79
1

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.

Maher Gamal
  • 96
  • 2
  • 3