java cucumber,如何在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.

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值