0

I have a fairly simple Cucumber test framework with a feature file, a step definitions file, and a test runner class that looks like this:

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/java/com/tests/cucumber/features/ui/ExampleTest.feature", 
                 glue = { "com.tests.cucumber.stepdefinitions" },
                 )
public class ExampleTestRunner {

}

This runs a scenario in the feature file just fine. Now I want to add a Before and After hook to do some setup and teardown, but I can't for the like of me get the hooks to run. I've tried adding the hooks to the ExampleTestRunner and to the StepDefinition class, but they never run. Where should I put these hooks? At the moment, the hooks just look like this, but I'll add content to them once I've worked this out!

package com.tests.cucumber.stepdefinitions;

import cucumber.api.java.After;
import cucumber.api.java.Before;

public class StepDefinitions {

    @Before
    public void before() {
        System.out.println("starting before()");
    }
}

Thanks for any help.

2
  • 1
    It should work when the hooks are placed in the step definition or any class inside the packages mentioned in the glue value. Are u sure u are importing the cucumber before annotation and not the junit one? Oct 20, 2019 at 15:54
  • I have added the StepDefinitions class to the question. As far as I know, it's importing the correct annotation, but I'm still learning!
    – user304582
    Oct 20, 2019 at 16:09

2 Answers 2

0

I am a little hesitant to answer this question even though I managed to get this to work. As far as I can tell, the problem was that I had added the Before and After methods in classes that were extended by other classes. In this situation, the tests would not run. I had to add the Before and After methods to a class that was not extended.

It feels like this is similar to the situation in which if you specify a step definition in a class that is extended by another class, then the step definition is considered to have a duplicate definition. Do I have the correct diagnosis here?

0

I use like this;

Runner Class:

@RunWith(Cucumber.class)
@CucumberOptions(

    features = {"src\\test\\features\\ui_features"},
    glue = {"com\\base\\tm\\auto_reg\\tests\\ui_tests\\price_features"},
    plugin = {"com.cucumber.listener.ExtentCucumberFormatter:"}
)

public class PriceFeatureRunner {
@BeforeClass
public static void setup() {
    RunnerUtil.setup(PriceFeatureRunner.class);
}

@AfterClass
public static void teardown() {
    RunnerUtil.teardown();
}

}

RunnerUtil.java:

public class RunnerUtil {
public static void setup(Class<?> clazz) {
    String reportPath = "target/cucumber-reports/" + clazz.getSimpleName().split("_")[0] + "_report.html";

    ExtentProperties extentProperties = ExtentProperties.INSTANCE;
    extentProperties.setReportPath(reportPath);

}


public static void teardown() {
    UiHooks uiHooks = new UiHooks();
    uiHooks.afterScenario();
    ExtentReportConfiguration.configureExtentReportTeardown();
}
}

UiHooks.java

public class UiHooks implements HookHelper {


public static final String BASE_URL = "https://www.stackoverfow.com/";

private Scenario scenario;

@Override
@Before
public void beforeScenario(Scenario scenario) {
    this.scenario = scenario;

    Reporter.assignAuthor(System.getProperty("user.name"));
}

@Override
@After
public void afterScenario() {

   if (HookUtil.driver != null) {
        HookUtil.driver.quit();
    }
    if (HookUtil.seleniumBase != null) {
        HookUtil.seleniumBase.stopService();
    }

}

@Override
@After
public void afterTest() {
   if (HookUtil.driver != null) {
        HookUtil.driver.quit();
    }
    if (HookUtil.seleniumBase != null) {
        HookUtil.seleniumBase.stopService();
    }
}

}

HookHelper.Java

public interface HookHelper {

@Before
void beforeScenario(Scenario scenario);

@After
void afterScenario();

void afterTest();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.