Is There a Cucumber Hook to Run Before and After Each Feature

Execute Cucumber step before/after a specific feature

Do you use cucumber-jvm? I found an article that fits your requirement.

http://zsoltfabok.com/blog/2012/09/cucumber-jvm-hooks/

Basically, do not use JUnit @BeforeClass and @AfterClass for this, as they are unaware of Cucumber Hook Tags. You would want Init and Teardown methods to run for certain scenarios only right?

Is there a cucumber hook to run before and after each feature

Few days ago I've spoke with Matt Wynne (one of the core team member of cucumber gem) and he told me that there is no such feature in cucumber (at the time of writing this).

As a workaround he suggested to tag whole feature and use before each hook with a flag like so:

Before('@feature_with_expensive_setup') do
unless @setup_is_done
# perform expensive setup code here ..
@setup_is_done = true
end
end

Multiple @Before/@After with Cucumber and Spring?

All step definitions and hooks on the glue path are global. So all scenarios can access all step definitions on the glue path and all hooks on the glue path are executed before/after each scenario.

If you have hooks that should only be executed for a particular scenario you could use conditional hooks. To run a particular hook only for certain scenarios, you can associate a Before or After hook with a tag expression.

Feature: Example

@browser
Scenario: Open a browser window
...

@headless
Scenario: Make a http call
...
@After("@browser and not @headless")
public void doSomethingAfter(Scenario scenario){
// only executed after "Open a browser window"
}

https://cucumber.io/docs/cucumber/api/#conditional-hooks


Alternatively you can change the organization of your code.

 |- src/test/java/com/example/one/RunCucumberTest1.java
|- src/test/resources/com/example/one/example-1.feature

|- src/test/java/com/example/two/RunCucumberTest2.java
|- src/test/resources/com/example/two/example-2.feature

|- src/test/java/com/example/common/AbstractCucumberTest.java
|- src/test/java/com/example/common/SeleniumApplicationTests.java
@RunWith(Cucumber.class)
@CucumberOptions(extraGlue = "com.example.common")
public abstract class AbstractCucumberTest {}
public class RunCucumberTest1 extends AbstractCucumberTest {}
public class RunCucumberTest2 extends AbstractCucumberTest {}

Cucumber will scan the package of the runner for glue and features so the glue and feature path can be ommited. By setting the extraGlue property the common configuration doesn't have to be duplicated.

Do we have any annotation in cucumber where it will run before any of the tests in the feature file?

You can use gerkin with qaf where you can use different TestNG listeners and annotations. In addition to that if you are using webdriver you can get additional driver and element listeners support. For example

package my.test.pkg
public class MyClass{
@BeforeSuite
public void beforeSuite() {
//get executed before suite
}

@BeforeTest
public void beforeTest() {
//get executed before each xml test

}
@BeforeMethod
public void beforeMethod() {
//get executed before each test case/scenario

}
@BeforeGroups(["p1","p2"])
public void beforeMethod() {
//get executed before group

}
//same for after methods @afterXXXXX

}

You need to add class in config file:

<test name="Gherkin-QAF-Test">
<classes>
<class name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
<class name="my.test.pkg.Myclass" />
</classes>
</test>

This example is not using listeners. You also can use different listeners.



Related Topics



Leave a reply



Submit