How to get name of scenario in cucumber java?

后端 未结 4 1558
予麋鹿
予麋鹿 2020-12-16 10:31

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 getSourceTagName

相关标签:
4条回答
  • 2020-12-16 11:20

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

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

    0 讨论(0)
  • 2020-12-16 11:23

    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());
        }
    
    0 讨论(0)
  • 2020-12-16 11:28
    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.

    0 讨论(0)
  • 2020-12-16 11:30

    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.

    0 讨论(0)
提交回复
热议问题