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?
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.
Unlike TestNG Annotations, cucumber supports only two hooks (Before & After) which works at the start and the end of the test scenario.
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.
Define tagged hooks in Hooks class file. Hooks can be used like @Before("@TagName"). Create before and after hooks for every scenario.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With