How to pass variable values between steps in Cucumber Java?
Asked Answered
S

2

8

I have a variable and I want to pass this variable across all the steps. Anyone can suggest with an code snippet example please on how to pass a variable value between the steps please. Any help will be highly appreciated.

Salchunas answered 24/12, 2015 at 8:56 Comment(1)
Use cucumber-guice or cucumber-jutils which implements scenario variables on both levels: Gherkin and Java.Parenthood
A
14

In Cucumber for Java (cucumber-jvm) the intended way of sharing data between steps is to use a dependency integration (DI) container - several of which have been integrated with Cucumber.

The method in which you use DI varies slightly from container to container, but here's an example using PicoContainer:

// MySharedData.java
public class MySharedData {
    public String stringData;
}

// SomeStepDefs.java
public class SomeStepDefs {
    private MySharedData sharedData;

    public SomeStepDefs(MySharedData sharedData) {
        this.sharedData = sharedData;
    }

    // StepDefs omitted
}

// MoreStepDefs.java
public class MoreStepDefs {
    private MySharedData sharedData;

    public MoreStepDefs(MySharedData sharedData) {
        this.sharedData = sharedData;
    }

    // StepDefs omitted
}

The DI container will ensure that a single instance of MySharedData is created for each scenario and is passed to every step definition class that requires it. The benefit of this approach is that Cucumber ensures that no shared state leaks between scenarios, because the injected dependency is created afresh for each scenario.

The example above uses constructor injection (so the injected dependency is specified by a constructor parameter) but other DI containers also support other injection mechanisms, such as Spring's @Autowired.

To get Cucumber to use DI you'll need to choose one (and only one) of the DI integrations and include it on your classpath (or in your POM). The choice is between:

  • PicoContainer (cucumber-picocontainer.jar)
  • Guice (cucumber-guice.jar)
  • Weld (cucumber-weld.jar)
  • Spring (cucumber-spring.jar)
  • OpenEJB (cucumber-openejb.jar)

You'll also need to install the selected DI container itself, because the Cucumber jars only provide the integration between Cucumber and the DI container.

Autobiography answered 30/12, 2015 at 14:27 Comment(2)
Could you just use a Hashmap for this? Or even an object with one hashmap in it?Frias
As long as the DI container you choose can manage an instance of the class that you choose, then yes.Autobiography
F
4
private static String myName = null;

@Given("I have a cucumber step")
public void i_have_a_cucumber_step() throws Throwable {
    myName = "Stackoverflow"

}

@Given("^I have (\\d+) (.*) in my basket$")
public void i_have_in_my_basket(int number, String veg) throws Throwable {
    System.out.println(myName));
}
Faraway answered 24/12, 2015 at 14:33 Comment(2)
This technique is useful, but relies on the scope and visibility of the variable. And it potentially allows state to leak between scenarios, which would be a bad thing.Autobiography
As @SebRose says, the example above looks good until you find out that another feature file uses the Given from one Java class, and then uses the Then in another Java class and now the information you want needs to go from the Given to the Then. Cucumber is a different BDD beast than others. It really influences you to put Given in a separate class, the When in a separate and the Then in a separate, the use some strategy to share information between them (World pattern, Dependency Injection, ...)Sweatt

© 2022 - 2024 — McMap. All rights reserved.