java Cucumber 类扩展步骤定义和钩子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34771928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:19:43  来源:igfitidea点击:

Cucumber class extending step definitions and hooks

javacucumberhookextend

提问by user24502

I want to extend from a "AbstractBase_step" class in java. So I want to have a hook like:

我想从 Java 中的“AbstractBase_step”类扩展。所以我想要一个像这样的钩子:

public abstract class AbstractBase_Steps {
    protected Scenario scenario;
    @Before
    public void background(Scenario scenario) {
        this.scenario = scenario;
    }
}

which is called for every step file:

每个步骤文件都会调用它:

public abstract class Hello_Steps extends AbstractBase_Steps {
}

When I do this I get

当我这样做时,我得到

cucumber.runtime.CucumberException: You're not allowed to extend classes that define Step Definitions or hooks. class Hello_Steps extends class AbstractBase_Steps

黄瓜.runtime.CucumberException:你不能扩展定义步骤定义或钩子的类。类 Hello_Steps 扩展类 AbstractBase_Steps

Does somebody has a trick for that?

有人对此有诀窍吗?

EDIT: For reusing same step definitions I've created a new Class Common_Steps and included it in the glue path. So its definitions are available for all feature files in the test.

编辑:为了重用相同的步骤定义,我创建了一个新的 Common_Steps 类并将其包含在胶水路径中。因此它的定义可用于测试中的所有特征文件。

采纳答案by jmj

Check this discussion.

检查此讨论

Hope this Helps.

希望这可以帮助。

回答by Servio Andrés Pantoja Rosero

As I understand your problem, you want to reduce the logic for steps. Here is the solution.

据我了解您的问题,您想减少步骤的逻辑。这是解决方案。

1) Define a common class in this case Awith steps in a general package like co.com.test

1)在这种情况下定义一个通用类,A其中包含通用包中的步骤,例如co.com.test

2) Define the steps config to use the base package

2)定义步骤配置以使用基础包

@CucumberOptions(format = {"pretty", "html:target/html/"},
features = {"src/test/resources/acceptance/general/general.feature"},
glue = {"co.com.test"})

3) Doesn't inheritance from class B with specific steps to A

3) 不从类 B 继承到 A 的特定步骤

It will cause that steps will be searched in all packages and will find the common steps and the specific steps.

它会导致在所有包中搜索步骤,并会找到通用步骤和具体步骤。

回答by Nitish Kumar

You can define your @Before steps in stepdefinition and then add method in base class ,

您可以在 stepdefinition 中定义 @Before 步骤,然后在基类中添加方法,

My scenario was to print message in the cucumber jvm report after every steps and to reduce complication here's the solution,

我的方案是在每个步骤后在黄瓜 jvm 报告中打印消息并减少复杂性,这是解决方案,

stepdefinition:

步骤定义:

   @Before
public void after(Scenario scenario) {
    this.scenario = scenario;
}

baseclass method:

基类方法:

public static Scenario scenario;

   public Scenario printResponse_Report(Scenario scenario, String responseStr{
      scenario.write("Response body: \n" +responseStr);
      return scenario;
}