Search code examples
javajunitcucumbercucumber-jvmcucumber-junit

Cucumber-JUnit, is there a way to control the order in which @before and @after tags run


I have two @after tags, @close-browser and @screenshot.So, right now when I use both tags for feature file it is first executing @close-browser and It fails in executing after method for @screenshot. Is there a way that I can tell cucumber to run @screenshot after method first?


Solution

  • The screenshot should be taken in one of the @After annotated methods. Like this:

    @After
    public void finish(Scenario scenario) {
        try {
            byte[] screenshot =
            helper.getWebDriver().getScreenshotAs(OutputType.BYTES);
            scenario.embed(screenshot, "image/png");
        } catch (WebDriverException somePlatformsDontSupportScreenshots) {
    
        System.err.println(somePlatformsDontSupportScreenshots.getMessage());
        }
        finally {
            helper.getWebDriver().close();
        }
    }