Search code examples
javacucumbercucumber-java

How to give priority order for cucumber.io 5.6.0 after hooks and a specific tag


I trying to update my knowledge in Cucumber, but some features are very hard to find or change. Right know I'm trying and failing to find the way. I tried and can use in step definitions various hooks @After and define the order or conditional hooks like a featured tag but can't do both. Any idea if it's possible to do both in the newest version?! If yes, how to do it? Please!

https://cucumber.io/docs/cucumber/api/?sbsearch=CucumberOptions#hooks

exemple (for the tag use):

@After("not @unit")
public void screenshot(Scenario scenario) {
    if (scenario.isFailed()) {
        byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
        scenario.embed(screenshot, "image/png");
    }
}
@After("not @unit")
    public void closeBrowser() {
        driver.quit();
}

exemple (for the order):

@After(order = 10)
public void screenshot(Scenario scenario) {
    if (scenario.isFailed()) {
        byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
        scenario.embed(screenshot, "image/png");
    }
}
@After(order = 0)
    public void closeBrowser() {
        driver.quit();
}

In old cukes version it was possible to use:

@After(order = 1, value={"~@unit"})

Solution

  • Hooks no longer take multiple tags. Instead you use a single tag expression. So the correct way to use hooks would be:

    @After(order = 1, value="(@firefox or @chrome) and not @unit")
    public void screenshot(Scenario scenario) {
    
    }