3

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.

Ishita Shah
  • 3,955
  • 2
  • 27
  • 51
  • U just need to extend the class. Rest all remains the same as in junit. https://github.com/cucumber/cucumber-jvm/tree/master/testng. – Grasshopper Jul 13 '18 at 13:05
  • @Grasshopper I have replaced Junit by TestNG. I am having especially doubt with Step Definition. – Ishita Shah Jul 13 '18 at 13:30
  • There is no direct migration between test and stepdefinition. you will need to create appropriate scenarios and split up the existing testing logic into step definitions. – Grasshopper Jul 13 '18 at 13:45
  • @Grasshopper Yes, I think so I need to do so. But, from current execution Step defination are not calling. If would be great if you can assist with sample example(In Answer). – Ishita Shah Jul 14 '18 at 04:46
  • You should use java package format for the glue option value. maybe something like 'mytest.Stepd' – Grasshopper Jul 14 '18 at 08:51

2 Answers2

4

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.

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
  • Information is useful,Thanks. But It is not what I am looking for, Sorry. – Ishita Shah Jul 14 '18 at 05:46
  • @IshitaShah - What exactly are you looking for? Care to elaborate? – Krishnan Mahadevan Jul 14 '18 at 06:07
  • I think you will be find clarity in my question itself. Yet, Usage of Step Definition if using with TestNG annotation. What to manage in Step definition and what to manage in TestNG annotation. – Ishita Shah Jul 14 '18 at 06:19
  • 1
    @IshitaShah - I believe I already answered that question. Step definition is a lingo used in the cucumber world.. TestNG does not know about it and it doesn't make any sense to try to merge these two things together. A step is a granular attribute of a test case (scenario) and in TestNG the maximum granularity that you can attain is only a `@Test` method which literally represents a scenario. – Krishnan Mahadevan Jul 14 '18 at 06:22
1

You can also take a look to gherkin with QAF which is pure TestNG implementation for gherkin. It is using TestNG (NOT cucumber runner) and provides you all the features of testNG including parallel execution, listeners, grouping, priority etc...

Each scenario converted as TestNG test and you can run scenarios as parallel. Furthermore you can also use inbuilt or custom data-providers while authoring BDD. No need additional runner just configure as usual using appropriate factory class for the BDD syntax you are using.

user861594
  • 5,733
  • 3
  • 29
  • 45