mmar mmar - 6 months ago 97
Java Question

How to pass variable & values between cucumber jvm scenarios

I have two scenarios A and B. I am storing the value of a field output of 'A' scenario in a variable. Now i have to use that variable in the Scenario 'B'. How can i pass a variable and its value from one scenario to other in Cucumber Java

Answer

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();
}