CMM
CMM

Reputation: 573

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?

Upvotes: 1

Views: 1705

Answers (3)

CloudM
CloudM

Reputation: 38

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.

Upvotes: 1

Balakrishnan
Balakrishnan

Reputation: 299

Create the cucumber runner class with testng. In run_cukes method will get the current feature file & scenario name by using the method cucumberFeature.getCucumberFeature().getGherkinFeature().getName()

enter image description here

Upvotes: 0

Cucumber implementation shall not allow you accessing scenarios name without using @Before hook

Upvotes: 0

Related Questions