Search code examples
guicebddcucumber-jvmcucumber-java

How can I get cucumber scenario name without using hooks?


I want to get a scenario name for each scenario to set it as Test name while running it on saucelabs. This test name can be set using MutableCapabilities just before creating webdriver. I'm creating the driver using google Guice @ScenarioScoped, hence driver will be created even before @Before hook method is invoked. So, is there any way I can access Scenario name without using @Before hook?


Solution

  • As per cucumber implementation, it is not possible to get the scenario names without using @Before or @After hooks. However, below should work for your problem: You can utilize the advantage of ordering hooks. These hooks will be executed based on the order specified. For reference: https://cucumber.io/docs/cucumber/api/ Example:

    @Before(order = 10)
    public void doSomething(){
        // Do something before each scenario
    }
    

    So, you can add another hook with lower order @Before hook in the class where you are creating webdriver, and this method needs to be placed before the webdriver is created.