Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cucumber-JVM: Cucumber's @After hook is executed twice

I'm implementing Cucumber Testng for learning purposes. I've realized that the @After hook method is executed twice. I can confirm it with debugging set and a test report output. I only run 1 feature file and my test report shows two entries with identical class names.

Does anyone know why?

enter image description here

Code:

@After
    public void tearDown(Scenario scen) throws IOException {
        ExtentTest logger = reportMgr.getLogger();
        String feature = getClass().getName() + " Feature";    //+ "." + Thread.currentThread().getStackTrace()[1].getMethodName().toString();

        logger = reportMgr.getExtent().createTest(feature);

        String screenShot = CaptureScreenshot.captureScreen(WebDriverManager.driver, CaptureScreenshot.generateFileName(feature));
        if (!scen.isFailed()) {
            logger.pass("Pass");
            logger.addScreenCaptureFromPath(screenShot);
        } else {
            logger.fail("Fail");
            logger.addScreenCaptureFromPath(screenShot);
        }
    }

Feature File:

@Login_Valid Feature: Login to Volare Collector Description:  As a user, I want login to Volare Collector 

  Scenario: Valid Login
    Given Volare Collector Home Page opens in browser
    When I login to Volare Collector with Username and Password
    Then Page navigate to Volare Collector Home Page

There is warning in the feature file called Multiple Definitions "Volare Collector Home Page opens in browser".

Please download my source code from this link.

like image 974
nicholas Avatar asked Apr 12 '18 08:04

nicholas


People also ask

Can we have multiple Hooks in Cucumber?

Unlike TestNG Annotations, cucumber supports only two hooks (Before & After) which works at the start and the end of the test scenario.

How does Hooks work in Cucumber?

Why Cucumber Hooks? Hooks are blocks of code that run before or after each scenario in the Cucumber execution cycle. This allows us to manage the code workflow better and helps to reduce code redundancy. Hooks can be defined anywhere in the project or step definition layers using the methods @Before and @After.

Which of the following symbol is used for tagging Hooks in Cucumber JVM?

Define tagged hooks in Hooks class file. Hooks can be used like @Before("@TagName"). Create before and after hooks for every scenario.

How are Cucumber Hooks setup globally?

To ensure we get the global hooks feature, we implement the Base Class with ConcurrentEventListener interface and define setEventPublisher method. After this is done, we add this class as a plugin. This has been tested with cucumber-4. This works even if the tests are running are in parallel.


1 Answers

testng.xml calls the TestRunner class. In the cucumberoptions there is no tags option, therefore no filtering based on tags. Thus it will pick up all the feature files in the folder given in features option - "src/Features" ie. Login.feature and Logout.feature.

Each of the feature files has one scenario each. Therefore there are 2 scenarios in total and thus 2 tests will be run. This explains why you see 2 tests in the report.

When each scenario runs, they will run any present Before and After hooks. So for 2 scenarios, hooks will be run twice.

Also hooks are global in nature ie, as long they are present in any classes in the option defined in glue(glue = {"Step_Definitions"}) they will be picked up. In your case you have two Before hooks in Login and Logout classes, you may want to look at fixing that. Use a Before hook and pass it a tag filter in the value option.

In the single After hook defined in the Login class this piece of code

String feature = getClass().getName();          
logger = reportMgr.getExtent().createTest(feature);

This means that name of the test will always be the full class name ie. Step_Definitions.Login. Therefore it shows up as 2 tests with same names although they are different.

like image 122
Grasshopper Avatar answered Nov 10 '22 00:11

Grasshopper