Search code examples
javagroovycucumbercucumber-jvmcucumber-java

How to pass variables from features to cucumber glue


How do you pass a variable from the feature to the glue in Cucumber? I am trying to pass the "dev1" from the given background part of the feature to the glue so it can be printed out. I have tried following the instructions from cucumber, but it doesn't seem to be working. The glue is written in groovy. The j variables are just for me to stick breakpoints on.


Feature
Feature: Tas Tcid Ruid Mappings

  Background: Logging into database
    Given User is logging into "dev1"
    When User submits name and password
    Then User is logged in

  @Weekly
  Scenario: Default No TCID RUID Mapping
    Given TCID RUID mapping is empty
    And a deal added with valid TCID to RUID mapping
    When the deal is submitted via an add_deal message as a standard publisher
    And receive a receiver notification
    Then recover receiver

  @Daily
  Scenario: Default No TCID RUID Mapping2
    Given TCID RUID mapping is empty
    And a deal added with valid TCID to RUID mapping
    When the deal is submitted via an add_deal message as a standard publisher
    And receive a receiver notification
    Then recover receiver

Glue
package cucumber.stepDefinitions.addDeal

import com.reuters.adt.dev.regression.tests.BaseTest
import com.reuters.adt.dev.regression.utils.TestRunException
import io.cucumber.java.en.*
import org.junit.Assert

class StepDefinitionsNoRuidMapping extends BaseTest{

    @Given("User is logging into \"{string}\"")
    GivenUserIsLoggingIn(String devEnv) {
        println(devEnv)
        def j=1+1
    }

    @When("User submits name and password")
    WhenUserSubmitsNameAndPassword() {
        def j=1+1
    }

    @Then("User is logged in")
    ThenUserIsLoggedIn() {
        Assert.assertTrue(true)
        def j=1+1
    }

    @Given("TCID RUID mapping is empty")
    GivenDealNotifyAnLBNViaMoreThanOneParty() {
        def j=1+1
    }



    @And("a deal added with valid TCID to RUID mapping")
    DealAddedWithValidTCIDToRUIDMappiing() {
        def j=1+1
    }

    @When("the deal is submitted via an add_deal message as a standard publisher")
    DealSubmittedByAddDealAsAStandardPublisher() {
        def j=1+1
    }

    @And("receive a receiver notification")
    ReceivedReceiverNotification() {
        def j=1+1
    }

    @Then("recover receiver")
    ExpectNAck() {
        Assert.assertTrue(true)
        def j=1+1
    }

    @Override
    protected void runTest() throws TestRunException {

    }
}

Thank you for any help in advance :)


Solution

  • You do it with a variable and pattern matching, like this:

    Gherkin:

    Given User is logging into "dev1"
    

    Groovy:

    Given(~/^User is logging into "([^"]*)"$/) { String env ->
        println env
    }
    

    or Java 8 and later:

    Given("User is logging into {String}", (String env) -> {
        System.out.println(env);
    });