tosha tosha - 4 months ago 18
Java Question

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?

Answer

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();
    }
}
Comments