Samantha
Samantha

Reputation: 367

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?

Upvotes: 1

Views: 2835

Answers (2)

Seb Rose
Seb Rose

Reputation: 3666

There is an 'order' argument that you can pass to @Before and @After to control order of execution:

@Before( order = 5 )
public void foo() {}

@After( order = 500 )
public void bar() {}

Before hooks are run in ascending order (lowest order number first), while After hooks are run in descending order (hightest order number first).

Upvotes: 14

Eugene S
Eugene S

Reputation: 6910

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

Upvotes: 3

Related Questions