Cucumber 类扩展步骤定义和钩子

2022-01-22 00:00:00 hook extend cucumber java

我想从 java 中的AbstractBase_step"类扩展.所以我想要一个像这样的钩子:

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

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:不允许扩展定义步骤定义或钩子的类.类 Hello_Steps 扩展类 AbstractBase_Steps

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

有人对此有诀窍吗?

为了重用相同的步骤定义,我创建了一个新的类 Common_Steps 并将其包含在粘合路径中.因此它的定义可用于测试中的所有功能文件.

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.

推荐答案

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

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

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

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

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

2) Define the steps config to use the base package

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

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

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

会导致在所有包中搜索步骤,会找到常用步骤和具体步骤.

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

相关文章