How to get the line number of scenario in cucumber using the event publisher

Viewed 542

I have written an event publisher on "TestCaseStarted". However it return the wrong line number when I am trying to access its location. I am using below code and feature file.

Expected Line number should be 4 but I am getting value as 13

Feature File

 Feature: to test a sample test case

  @tc1
  Scenario Outline: sc1
    Given    user has waited for "1" seconds and clicked checkboxes
    |button_id|
    |a        |
    |b        |
    When  user has to enter"<Value>"
    Then validates that the scenarios completes successfully
    Examples:
    |Value    |
    |3        |
    |5        |

Code Snippet

private void EHTestCaseStarted(TestCaseStarted event) {
    String ffPath = event.getTestCase().getUri().getPath();
    String scnType = event.getTestCase().getKeyword();
    Location loc = event.getTestCase().getLocation();
    int scnLine = event.getTestCase().getLocation().getLine();
    int scnCol = event.getTestCase().getLocation().getColumn();

    if (scnType.equalsIgnoreCase("Scenario Outline")) {
        parseGherkin(ffPath, scnLine, scnCol);
    } else {

    }
}
2 Answers

According to this it should be

event.getTestCase().getLine()

Have a look at this . It may provide you (kind of out of the box) what you are trying to do

Because you are using a Scenario Outline the actual scenario is defined by the line of the concrete Example used in the outline. So the line number is correct.

If you need the parent of a scenario, you can use the TestSourceParsed evnet to access the hierarchy the test cases are in.

Related