0

I use Cucumber-JVM and I'm trying to take screenshot after every step (if there is a failure). The problem is that I do not know how to set methods that will be executed before and after every step.

I've used https://github.com/cucumber/cucumber-java-skeleton as a reference. I added to runner https://github.com/cucumber/cucumber-java-skeleton/blob/master/src/test/java/skeleton/RunCukesTest.java BeforeClass and AfterClass (JUnit adnotations) and methods that provide me start and stop of webrdiver (before and after every scenario). I know I can use Before/After annotations (Cucumber ones), but methods with those annotation must be placed in the same class that contains step definitions. The problem is that I have plenty of classes with steps, so I do not want to put those annotated methods in every step class, I need to define them once.

Please, tell me how to do it.

[edit]

Here is my Runner

package example;



import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;



@RunWith(Cucumber.class)
    @CucumberOptions(
        features = "src/test/resources/"
    )
public class TodoTest {


    @BeforeClass
    public static void beforeClass() {

        System.setProperty("browser", "chrome");


        System.setProperty("remote", "http://localhost:4444/wd/hub");

    }

    @AfterClass
    public static void afterClass() {

    }

}

I need to define methods that will be executed before and after every step in whole project similarly like those methods for scenarios.

1
  • Please include the relevant code snippets in the question itself. Maybe you could use a JUnit Rule, as recommended here Mar 16, 2015 at 6:34

1 Answer 1

1

It is not necessary that Before/After annotated method should reside in the same class. EDIT: Create sample project as below: keep your StepDef classes in package named cukes like src/test/java/cuckes

import cucumber.api.java.en.Given;

public class StepDef
{
    @Given("^order$")
    public void order()
    {
        System.out.println("Test hook");
    }
}

keep your Hook class in the same package as above

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

public class Hook {

    @Before
    public void before(Scenario scenario)
    {
        System.out.println("running scenario: "+scenario.getName());
    }

    @After
    public void after(Scenario scenario)
    {
        scenario.write("Test string to output in reports.");
        System.out.println("Finished running scenario: "+scenario.getName());
    }
}

Keep your runner (in this case JUnitRunner) in the same package.

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@CucumberOptions(features={"src/test/resources/test.feature"},glue={"cukes"},plugin={"pretty","html:target/report"})
@RunWith(Cucumber.class)
public class JUnitRunner
{

}

Under your src/test/resources folder create a cukes named folder and keep test.feature file

Feature: Test feature
Scenario: To test scenario
Given order

Now run your JUnitRunner and you should see below output on console:

Feature: Test feature
passed
Test hook
To test scenario

Take a look at this github repo. it has got good examples in cucumber: CucumberJVMExamples. Excellent work done by Mr. Pradeep. This should answer all your questions

6
  • Thanks for comment! Can you show me some working example, cause I still do not know how to set it.
    – master.py
    Mar 16, 2015 at 10:46
  • Mrunal, thanks again, but apparently I'm completely Java newbie... What does it exactly mean: "Paste below code where your stepdefinitions reside"? Should I create class containig those methods? And what else? Every class containing StepDefinitions should inherit that content? How exactly should I do it? Thanks again and look forward for your help again:)
    – master.py
    Mar 16, 2015 at 20:11
  • Please check the URL which i've added. That should answer all your question. Let me know if you still need any help. Mar 17, 2015 at 3:30
  • Thanks a lot! Correct me if I'm wrong, "driver = Hooks.driver;" is the key for my question? How can I avoid this? Perhaps all classes containing Step Definitions should extend Hook class? Or there is different way to use those hooks? I'm pretty new to Java and Cucumber-JVM, so please be understanding:) Thanks again!
    – master.py
    Mar 17, 2015 at 7:28
  • i've added a complete example with cucumber. this working example should give you clear idea now. also with regards your query your stepdefs will either have to extend the Hook class or you will have to make driver instance static and then use same instance everywhere, if you do not wish to keep extending Hook class Mar 17, 2015 at 8:58

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.