Skip to content
Advertisement

Is there a way to get the list of scenarios that are to be run in the @BeforeClass annotation in cucumber JVM

I have got a requirement to get the list of all the scenarios that are to be executed based on the tag I provided in cucumber Test runner. However I have to get this list before tests start execution.

I know there is a tag called “@BeforeClass” but I am not sure if I can use to get the list of all the scenarios that are going to be run. For example something like this

 @BeforeClass
public void intialize(Scenario[] scenario) throws Exception { }

Below is the code for me test runner class

    package com.automation.cucumber;

import com.automation.Utils;
import io.cucumber.java.Scenario;
import io.cucumber.testng.*;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;

import java.io.File;

@CucumberOptions(features = "features/amazon"
        ,glue="com.automation.cucumber"
        ,tags = "@tt"
        ,dryRun = true
        , plugin = {"json:target/cucumber-reports/cucumber.json"})

public class CucumberTestRunner extends AbstractTestNGCucumberTests {

    static String resultFolder;
    @DataProvider(parallel = true)
    public Object[][] scenarios() {
        return super.scenarios();
    }

    @BeforeClass
    public void intialize() throws Exception {
        resultFolder =  Utils.createTestReportFolder();
        if(resultFolder==null)
        {
            throw new Exception("Unable to create a result folder");
        }

        System.out.println();

    }

}

Advertisement

Answer

You may have to implement EventListener class to get that information and do dryRun = true in your Runner class in @CucumberOptions

Quoting from a question that can help you achieve what you need

public class DryRunPlugin implements EventListener {

    @Override
    public void setEventPublisher(EventPublisher publisher) {
        publisher.registerHandlerFor(TestCaseStarted.class, this::handleCaseStarted);
    }

    private void handleCaseStarted(TestCaseStarted event) {
        System.out.println(event.getTestCase().getUri());
        System.out.println(event.getTestCase().getName());
        System.out.println(event.getTestCase().getScenarioDesignation());
        event.getTestCase().getTags().stream().forEach(t -> 
        System.out.println(t.getName()));
    }

}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement