Reputation: 133
I am new to Cucumber and gherkin world. I wanted to know is there a good way of sharing variables across the different step definition files? For my example: I have 2 feature files(GET and POST) and this has 2 step definition files(GET and POST). Both the feature file has a common step for test data set up . So I thought I will move this data set up to a CommonSteps file, which I was able to do. But in this data set up method, I am assigning a value to a variable, which needs to be consumed in the 2 step defintion files(GET and POST).My question is how can I accomplish this through Gherkin?
Upvotes: 0
Views: 5758
Reputation: 4343
Sharing state between steps defined in different step classes is as @Marit says done using dependency injection.
Other flavors of Cucumber uses a shared World object. You can use a public static
variable to share state, but it is troublesome as state may leak between steps. You may end up in a situation where steps start to depend on each other with a shared variable that isn't reset before every execution. Steps that depends on other steps are a well known problem withing the Cucumber community and something you want to avoid at all cost.
Cucumber supports a few different dependency injection frameworks. If your project already uses a dependency injection framework, see if you can use the same for your Cucumber scenarios. If you don't use any dependency injection framework, I would suggest using PicoContainer.
I have written a few blog posts about some of the different options:
They are all Java based. I assume that it will be possible to use them with Groovy, but I haven't tried.
Upvotes: 2