30

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 ?

Sonali Das
  • 943
  • 1
  • 7
  • 24
user2365105
  • 325
  • 2
  • 5
  • 8

4 Answers4

38

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.

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
Sridevi Yedidha
  • 1,183
  • 1
  • 12
  • 13
  • Thanks a lot. I actually forgot to mention the version of cucumber which I'm using. I was trying with version 1.5. Tried with 1.6 and it worked. – user2365105 May 13 '14 at 05:16
  • Does Cucumber have something like step.getName() ? To get the name of the current Step being executed. – Alichino Aug 29 '18 at 10:27
  • when using Scenario Oulines, Name will not be unique. – Purus Feb 01 '19 at 10:19
  • Can this work for features as well, or only scenarios? – Kyle Mar 29 '19 at 17:29
  • You forgot to mention that \@Before Hooks go in the Step Definition file, not the Runner that specifies the \@RunWith(Cucumber.class) and \@CucumberOptions annotations. – Tihamer Feb 25 '20 at 19:36
1
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.

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
Jrf
  • 31
  • 1
0

Inside the step definition, you can use CucumberHelper.scenario.getName().

Based on this API you can use getID, getSourceTagNames, getStatus and getClass methods.

Philipos D.
  • 2,036
  • 1
  • 26
  • 33
  • 2
    Sounds like a great idea. However, where is CucumberHelper defined? – Tihamer Apr 27 '20 at 16:15
  • You can create a helper class and import `cucumber.api.Scenario;` and declare the Scenario as static inside the class to have it available. `public static Scenario scenario;` – Philipos D. Apr 28 '20 at 06:41
0

Below is the entire code for the problem asked, to save your time:

import io.cucumber.java.Before;
import io.cucumber.java.Scenario;
Scenario scenario;

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

    @Given("I test scenario name")
    public void test() {
        System.out.println(scenario.getName());
    }
Arun
  • 9
  • 1