Cucumber Before Suite

Profile picture for user devraj

Like TestNG, we don't have any BeforeSuite hook in JUNIT 4. Consider a case when you have to clean extent report screenshot directory or allure report result directory before running your suite. To perform such BeforeSuite or BeforeAll operations you can use below code.

private static boolean bsuite = false;

@Before()
public void bf0(Scenario scenario) 
{
    if(!bsuite) 
    {
        FileUtils.cleanDirectory(new File(screenshotdir));
        FileUtils.cleanDirectory(new File(allureResults));
        bsuite = true;
    }
}

Here we have declared a static boolean variable bsuite and set its default value to false. Then inside our @Before hook we are checking if bsuite is true then clean screenshot and allure result directory. During next run of @Before hook bsuite value will be true; and if condition will be false therefore clean directory operations will not be executed.