How to Pass Variables Between Cucumber-Jvm Steps

How to pass variables between cucumber-jvm steps

In order to share commonalities between steps you need to use a World. In Java it is not as clear as in Ruby.

Quoting the creator of Cucumber.

The purpose of a "World" is twofold:

  1. Isolate state between scenarios.

  2. Share data between step definitions and hooks within a scenario.

How this is implemented is language specific. For example, in ruby,
the implicit self variable inside a step definition points to the
current scenario's World object. This is by default an instance of
Object, but it can be anything you want if you use the World hook.

In Java, you have many (possibly connected) World objects.

The equivalent of the World in Cucumber-Java is all of the objects
with hook or stepdef annotations
. In other words, any class with
methods annotated with @Before, @After, @Given and so on will be
instantiated exactly once for each scenario.

This achieves the first goal. To achieve the second goal you have two
approaches:

a) Use a single class for all of your step definitions and hooks

b) Use several classes divided by responsibility [1] and use dependency
injection [2] to connect them to each other.

Option a) quickly breaks down because your step definition code
becomes a mess. That's why people tend to use b).

[1] https://cucumber.io/docs/gherkin/step-organization/

[2] PicoContainer, Spring, Guice, Weld, OpenEJB, Needle

The available Dependency Injection modules are:

  • cucumber-picocontainer
  • cucumber-guice
  • cucumber-openejb
  • cucumber-spring
  • cucumber-weld
  • cucumber-needle

Original post here https://groups.google.com/forum/#!topic/cukes/8ugcVreXP0Y.

Hope this helps.

Cucumber /Gherkin: Passing variables between different step definition

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:

  • PicoContainer
  • Guice
  • Spring

They are all Java based. I assume that it will be possible to use them with Groovy, but I haven't tried.

How to pass variable & values between cucumber jvm scenarios

It's not entirely clear if your step definitions for these scenarios are in separate classes, but I assume they are and that the step in ScenarioA is executed before the one in B.

public class ScenarioA {

public static String getVariableYouWantToUse() {
return variableYouWantToUse;
}

private static String variableYouWantToUse;

Given("your step that expects one parameter")
public void some_step(String myVariable)
variableYouWantToUse = myVariable;
}

Then in scenario B.

public class ScenarioB {

Given("other step")
public void some_other_step()
ScenarioA.getVariableYouWantToUse();
}


Related Topics



Leave a reply



Submit