Search code examples
seleniumcucumbertestngcucumber-java

how to execute Cucumber Step defination with TestNG annotation


I am supposed to migrate on Cucumber. I do have project framework with Selenium, TestNG with Data Driven Framework, Maven. I am exploring Cucumber feasibility with TestNG annotation.

My question is, How we can create connection between @Test method and Step definition of cucumber. Let's example our code is written in @BeforeClass, @Test, @AfterClass method. So how we can migrate with Step definition.

Feature File :

Feature: Is it Friday yet?
  Everybody wants to know when it's Friday

  Scenario: Sunday isn't Friday
    Given today is Sunday
    When I ask whether it's Friday yet

Step Definition:

@Given("^today is Sunday$")
public void today_is_Sunday() {
    // Write code here that turns the phrase above into concrete actions
    System.out.println("this is demo1");
}

@When("^I ask whether it's Friday yet$")
public void i_ask_whether_is_s_Friday_yet() {
    // Write code here that turns the phrase above into concrete actions
    System.out.println("this is demo2");
}

Class Exection:

@CucumberOptions(features = "cfeature/firstDemo.feature", glue = { "mytest/Stepd" })
public class demo01  extends AbstractTestNGCucumberTests {

    private TestNGCucumberRunner tcr;

    @BeforeClass(alwaysRun = true)
    public void beforeClass() throws Exception {
        tcr = new TestNGCucumberRunner(this.getClass());
    }

    @Test(groups="cucumber", description="Runs CucumberFeature")    
    public void testdemo() {
        System.out.println("Hello");        
    }

    @AfterClass(alwaysRun = true)
    public void afterClass() {
        tcr.finish();
    }   
}

Console:

Hello

[33mUndefined scenarios:[0m
[33mcfeature/firstDemo.feature:4 [0m# Sunday isn't Friday

1 Scenarios ([33m1 undefined[0m)
5 Steps ([33m5 undefined[0m)
0m0.073s


You can implement missing steps with the snippets below:

As of now, @Test annotation is calling. But, How to replace it with Step Definition. Please assist.


Solution

  • Not sure what the confusion here. Here's how you can relate TestNG and cucumber terminologies.

    • <test> tag in TestNG can be visualized as a feature file in cucumber.
    • @Test method in TestNG can be visualized as a scenario in cucumber.
    • A Step definition in cucumber has nothing directly equivalent to in TestNG because, its part of a scenario. But for the sake of understanding you can visualize it as one line of code doing a logical operation in TestNG.

    The default implementation of AbstractTestNGCucumberTests is as below:

    • It contains a data provider internally which provides one feature file at a time.
    • It contains a @Test method which is bound to the above mentioned data provider, which retrieves all the scenarios in the feature file and then runs them one after the other.

    You can build your own variant of AbstractTestNGCucumberTests to do various different things (such as support concurrent scenario execution which is currently not available in Cucumber JVM bindings).

    As an example you can take a look at Cucumber-roadrunner library that I built which uses the above concept to support parallel scenario execution and also provides thread safe reports.

    With respect to the error you are facing viz., You can implement missing steps with the snippets below: is basically because cucumber jvm bindings perhaps isn't able to bind your feature file with a glue code (which is what you are providing via the @CucumberOptions annotation). You should perhaps take a closer look at the cucumber jvm bindings documentation to understand how to provide the correct values.