Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get name of scenario in cucumber java?

I would like to get name of scenario to have meaningful logs and to generate custom report at run-time in java. Scenario class have only has getStatus() and getSourceTagNames() methods. I don't find a way to get scenario name.

Can someone help me to get this resolved ?

like image 969
user2365105 Avatar asked May 12 '14 07:05

user2365105


People also ask

How do you get the scenario name in Cucumber step?

Inside the step definition, you can use CucumberHelper. scenario. getName() . Based on this API you can use getID , getSourceTagNames , getStatus and getClass methods.

How do I get a tag name in Cucumber?

For this, Cucumber has already provided a way to organize your scenario execution by using tags in feature file. We can define each scenario with a useful tag. Later, in the runner file, we can decide which specific tag (and so as the scenario(s)) we want Cucumber to execute. Tag starts with “@”.

Can we parameterize scenario name in Cucumber?

Parametrization in CucumberCucumber supports Data Driven Testing using Scenario Outline and Examples keywords. Creating a feature file with Scenario Outline and Example keywords will help to reduce the code and testing multiple scenarios with different values.

What is Scenario class in Cucumber?

It allows writing text and embedding media into reports, as well as inspecting results (in an After block). Note: This class is not intended to be used to create reports.


2 Answers

From version 1.6, in addition to, getStatus() and getSourceTagNames(), there is another method, getName() that returns the scenario's description. For example, for a scenario as follows:

Scenario: verify number of topics shown in the UI 

scenario.getName() returns "verify number of topics shown in the UI"

I initialize scenario in @Before as follows:

@Before public void before(Scenario scenario) {     this.scenario = scenario; } 

Hope this helps.

like image 112
Sridevi Yedidha Avatar answered Sep 26 '22 00:09

Sridevi Yedidha


String scenarioName = scenario.getName(); String[] arrayScenarioName = scenarioName.split("--"); String scenarioName1 = arrayScenarioName[0];  String scenarioName2 = arrayScenarioName[1];  System.out.println("Scenario Name 1 for this test is -> " + scenarioName1); System.out.println("Scenario Name 2 for this test is -> " + scenarioName2);  String scenarioId = scenario.getId(); //Takes the Scenario ID and removes the ; and splits it into 2 strings String scenarioId4 = scenarioId; String[] parts = scenarioId4.split(";"); String part1 = parts[0];  String part2 = parts[1];  String part11 = part1.replace('-', ' '); String part22 = part2.replace('-', ' '); System.out.println("Scenario ID for this test is -> part11 " + part11); System.out.println("Scenario ID for this test is -> part22 " + part22); 

Once you have the @Before set up them try this to retrieve your Cucumber Feature and Scenario items.

like image 22
Jrf Avatar answered Sep 27 '22 00:09

Jrf