Ionut
Ionut

Reputation: 23

serenity-bdd with cucumber feature hooks

I am using Serenity-BDD with cucumber and I would like to run certain things only once per feature file. It looks like cucumber doesn't support this at the moment. I was wondering if serenity has some workaround for this.

I've also tried to use the JUnit @BeforeClass, @AfterClass hooks in the test suite class but the 2 annotations require static methods and I cannot access the serenity page objects methods at that time (there is no instance injected at that point in time).

Upvotes: 2

Views: 2762

Answers (2)

Bogdan Pisarenko
Bogdan Pisarenko

Reputation: 386

You could try to implement net.thucydides.core.steps.StepListener interface and connect it via SPI. I described this in answer in this post

Upvotes: 0

Grasshopper
Grasshopper

Reputation: 9058

You could try setting up a static global flag which will make sure that the before method will runs only once.

Setup the feature file with a tag.

@RunOnce
Feature: Run Once

Use the following hook in your stepdefinition.

    private static boolean onceFlag = true;

    @Before(value="@RunOnce")
    public void beforeOnce(){

        if(onceFlag) {
            onceFlag = false;

            //Your code to write once per feature file

        }
    }

Upvotes: 3

Related Questions