Search code examples
selenium-webdrivercucumber-jvmserenity-bddcucumber-serenity

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


Solution

  • 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
    
            }
        }