如何在黄瓜jvm场景之间传递变量和值

How to pass variable & values between cucumber jvm scenarios

我有两个场景 A 和 B。我将 'A' 场景的字段输出值存储在一个变量中。现在我必须在场景 'B' 中使用该变量。我如何在 Cucumber Java

中将变量及其值从一个场景传递到另一个场景

尚不完全清楚这些场景的步骤定义是否分开 类,但我假设它们是分开的,并且场景 A 中的步骤在场景 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;
}

那么场景B.

public class ScenarioB {

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

仅作记录,可以使用 cucumber-jvm 的依赖注入功能而不是依赖静态。

如@Mykola 所述,最好的方法是使用依赖注入。要使用手动依赖注入提供一个简单的解决方案,您可以执行

public class OneStepDefinition{

private String user;

// and some setter which could be your step definition methods

public String getUser() {
  return user;
}

}

public class AnotherStepDefinition {

private final OneStepDefinition oneStepDefinition;

 public AnotherStepDefinition(OneStepDefinition oneStepDefinition) {
        this.oneStepDefinition = oneStepDefinition;
    }

// Some step defs
@Given("^I do something on the user created in OneStepDefinition class$")
    public void doSomething() {
  String user = oneStepDefinition.getUser();
// do something with the user
    }
}

有静态变量

public class CcumberCintext {
   public static String value;
}