Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass variable values between steps in Cucumber Java?

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.

like image 432
Sayom Avatar asked Dec 24 '15 08:12

Sayom


People also ask

How do you make one scenario dependent on another scenario in Cucumber?

You can set test dependency with qaf bdd. You can use dependsOnMethods or dependsOnGroups in scenario meta-data to set dependency same as in TestNG, because qaf-BDD is TestNG BDD implementation. if you are stating new project you don't need any cucumber dependency, refer qaf-step-by-step tutorial.

How do you link step definitions in cucumbers?

An annotation followed by the pattern is used to link the Step Definition to all the matching Steps, and the code is what Cucumber will execute when it sees a Gherkin Step. Cucumber finds the Step Definition file with the help of the Glue code in Cucumber Options.

How do cucumbers handle multiple step definitions?

You can have all of your step definitions in one file, or in multiple files. When you start with your project, all your step definitions will probably be in one file. As your project grows, you should split your step definitions into meaningful groups in different files.

How do I set environment variables in Cucumber?

Click on the Environment Variables field. Enter the environment variable and its value into the dialog.


2 Answers

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.

like image 52
Seb Rose Avatar answered Oct 01 '22 17:10

Seb Rose


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));
}
like image 20
N.. Avatar answered Oct 01 '22 19:10

N..