How to link feature and step definition in cucumber
Asked Answered
R

4

5

I'm new to Cucumber java and had this problem in initial stages: I'm not using MAVEN project for some reason. I just created a simple java project in eclipse.

I have my features under "src/dummy/pkg/features", and my implementation "StepDef.java" is under "src/dummy/pkg/features/implementation"

I have written step definitions for Given, When, and Then, but when I run my features file, it is unable to recognize the implementation. How do I link the features with step definitions?

Ronn answered 8/10, 2014 at 10:25 Comment(0)
S
7

create a class YourClass and it would look something like the below and run it as JUnit test.

@RunWith(Cucumber.class)

@CucumberOptions(  monochrome = true,
                     features = "src/dummy/pkg/features/",
                       format = { "pretty","html: cucumber-html-reports",
                                  "json: cucumber-html-reports/cucumber.json" },
                         glue = "your_step_definition_location_package" )

public class YourClass {
  //Run this from Maven or as JUnit
}
Skipjack answered 8/10, 2014 at 11:19 Comment(1)
that's correct we have to use glue = "pkg location" in the @CucumberOptionsRonn
D
4

When you run your Runner class then it will scan all the feature file mentioned within features options and load the them afterward all step definition within package started by text mentioned within glue options will get loaded. For e.g.

@RunWith(Cucumber.class)
@CucumberOptions(
        plugin = { "pretty","json:target/cucumberreports.json" }, 
        glue = "stepDefinition", 
        features = "src/test/resources/TestCases/", 
        tags={"@onlythis"},
        dryRun=false
    )
public class RunTest {

}

Here all the feature file present within

src/test/resources/TestCases/

will get loaded

then all the stepdef within or it's subdirectory will get loaded

stepDefinition

and whenever your step from feature get run then cucumber will look for function corresponding to step's regex and function will run.

for e.g.

whenever step When User enters email id in src/test/resources/TestCases/Login.feature will run then cucumber will find its corresponding function in all stepdef classes

Login.feature
@LoginValidation 
Feature: To smoke test functionalities of app 

@Browser @ValidLogin 
Scenario: Verify scenario in case of successful login 
    When User enters email id 
    And User enters password 
    Then User clicks on sign in button and able to sign in 

And moment it will reach class in subdirectory of stepDefinition i.e. in stepDefinition.ui.home.LoginPageStepDef.java cucumber will find function with @When("^User enters email id$") and will execute this function.

LoginPageStepDef.java
public class LoginPageStepDef {

    LoginPage loginPage = new LoginPage(AttachHooks.driver);
    private static Logger LOGGER = Logger.getLogger(LoginPageStepDef.class);

    @When("^User enters email id$")
    public void user_enters_email_id() throws Throwable {
        //LoginPage.obj = loginPage;
        loginPage.enterEmailId(ConfigManager.getProperty("UserName"));
    }
}
Dori answered 29/5, 2018 at 18:0 Comment(0)
Q
2

You have to convert your project to Cucumber project. Right-click on your project from the Project Explorer > Configure > Convert as Cucumber Project.

Quincuncial answered 10/10, 2020 at 22:54 Comment(0)
A
1

Create a runner class something like this and you should be able to execute. There is also no need to write step definitions manually, just create a feature file and run it, it will create a snippet of the step definition which can be used to create a step definition class:

A class file called Runnerclass is required to run the cucumber:

@RunWith(Cucumber.class)
@CucumberOptions(plugin={"pretty","html:format"},

features = "Features/name.feature",glue={"path where step definitions exist"})
public class RunnerClass {

}
Anodize answered 11/4, 2017 at 10:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.