automaticien
automaticien

Reputation: 153

Tagged Cucumber Scenarios Functioning

I have been experiencing something really weird. Maybe someone can explain me where I am making mistake. I have a following scenario in a feature file

@DeleteUserAfterTest
Scenario: Testing a functionality
Given admin exists
When a user is created
Then the user is verified

My @After method in Hooks class looks like following

@After
public void tearDown(Scenario scenario) {
    if (scenario.isFailed()) {
        final byte[] screenshot = ((TakesScreenshot) driver)
                .getScreenshotAs(OutputType.BYTES);
        scenario.embed(screenshot, "image/png"); //stick it in the report
    }
    driver.quit();
}

I am using the following method in my step definition to delete the created user based on tag passed in the Test Scenario as follows:

@After("@DeleteUserAfterTest")
public void deleteUser(){
//Do fucntionalities to delete user
}

My test runner looks something like this:

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;

@CucumberOptions(
    plugin = {"pretty","com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:", "json:target/cucumber-report/TestResult.json"},
    monochrome = false,
    features = "src/test/resources/features/IntegrationScenarios.feature",
    tags="@DeleteUserAfterTest",
    glue="Steps")
public class IntegrationTest extends AbstractTestNGCucumberTests {
}

However, when I launch the test case, sometimes my user is deleted in the After("@DeleteUserAfterTest") but sometimes my test does not recognises the tagged After at all. It directly goes to After method in my Hooks class and quits the driver. Maybe someone has encountered this problem or knows a workaround!

Upvotes: 1

Views: 98

Answers (1)

M.P. Korstanje
M.P. Korstanje

Reputation: 12039

Method order is not defined in Java. So you have to tell Cucumber in which order you hooks should be executed. Higher numbers are run first (before hooks are the other way around).

@After(order = 500)
public void tearDown(Scenario scenario) {

}

@After(value = "@DeleteUserAfterTest", order = 1000)
public void deleteUser(){

}

Upvotes: 1

Related Questions