2

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

Ionut
  • 23
  • 6

2 Answers2

3

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

        }
    }
Grasshopper
  • 8,908
  • 2
  • 17
  • 32
  • Is this possible to implement once per each test run as global before hook. Means rune once before all feature file – Shabar May 15 '20 at 23:10
  • @Grasshoper Sorry what I meant was, Not only once it should run before all other feature files. I tried above, it didn't run at the beginning before all other feature files though. – Shabar May 16 '20 at 09:40
  • U should look at plugin sysytem. TestSourceRead event may be what u r looking for – Grasshopper May 16 '20 at 17:21
  • I tried that but looks like with my `serenity bdd` it has some sort of dependency issue. I followed this. https://github.com/cucumber/cucumber-jvm/issues/515 and https://medium.com/@hemanthsridhar/global-hooks-in-cucumber-jvm-afc1be13e487, After that I cannot run tests due to dependency conflicts. This is my current status https://stackoverflow.com/questions/61815030/global-hooks-in-serenity-bdd – Shabar May 17 '20 at 01:54
0

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

Bogdan Pisarenko
  • 357
  • 1
  • 5
  • 17