Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cucumber class extending step definitions and hooks

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: You're not allowed to extend classes that define Step Definitions or hooks. class Hello_Steps extends class 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.

like image 829
user24502 Avatar asked Jan 13 '16 16:01

user24502


People also ask

Can we extend step definition files in Cucumber?

runtime. CucumberException: You're not allowed to extend classes that define Step Definitions or hooks? You could stop your search for the solution and follow the steps provided in this article to solve the problem.

What are the hooks in Cucumber?

Hooks. Hooks are blocks of code that can run at various points in the Cucumber execution cycle. They are typically used for setup and teardown of the environment before and after each scenario. Where a hook is defined has no impact on what scenarios or steps it is run for.


2 Answers

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 A with steps in a general package like co.com.test

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) 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.

like image 191
Servio Andrés Pantoja Rosero Avatar answered Sep 28 '22 05:09

Servio Andrés Pantoja Rosero


The only problem is with the cucumber annotations which use AOP and create a proxy class so just move the @Before and @After in your concrete implementations

public class BaseStepDefinition {
    protected void init(){
        PageFactory.initElements(..);
    }
}

public class MyStepDefinition extends BaseStepDefinition {
    @Before
    @Override
    public void init() {
       super.init();
    }
}
like image 20
amstegraf Avatar answered Sep 28 '22 07:09

amstegraf