Skip to main content
Extracting Data From Gherkin Steps

Learn how to take specific words and numbers from a step to use in your code

Alan Parkinson avatar
Written by Alan Parkinson
Updated over 3 years ago

When examples are captured, they can sometimes reference real life data to create concrete examples. Concrete examples are useful for describing in detail how we expect users and our systems to behave. But they are also useful as test data for our automated acceptance tests.

So let's take a look at how we extract test data from steps to use within our step definitions.

Extracting data from steps

Typically when test data is described in an example it is put within double quotes like so:

Scenario: Demonstrating data in steps
  Given I have "2" addresses
  When I add my "business" address
  Then I will have "3" addresses

The reason for adding the double quotes in is because when these steps are processed that data in the double quotes are converted in parameters in an step definition:

@Given("I have {string} addresses")
public void setAddresses(String addresses) {
    System.out.println(addresses);
}

@When("I add my {string} address")
public void addAddress(String address) {
    System.out.println(address);
}

@Then("I will have {string} addresses")
public void countAddresses(String addresses) {
    System.out.println(addresses);
}

The plain text we put in double quotes in the step is converted into an 'Cucumber Expression' in the step definition. Meaning anything inside the double quotes from the step are then passed to a string parameter for further use in our code.

Code Examples
Java | Ruby | JavaScript 

Matching Strings

You can also use regexes to match specific parts of a step and add them to a parameter. For example say we had this step:

Scenario: Demonstrating complex string regexes
  Given I have a pineapple
  When I take an apple
  Then I have an apple and a pineapple

I could use a regex matcher to extract the string without needing quotes:

@Given("I have a ([a-z]+)")
public void checkFruit(String fruit) {
    System.out.println(fruit);
}

I could even have multiple regexes to match multiple pieces of data:

@Then("I have an ([a-z]+) and a ([a-z]+)")
public void checkFruits(String fruit1, String fruit2) {
    System.out.println(fruit1);
    System.out.println(fruit2);
}

Code Examples
Java | Ruby | JavaScript 

Matching numbers

The previous examples shows how to extract strings but we can also extract numbers in the forms of integers and floats:

Scenario: Demonstrating number data types
  Given I have divided the number 1 in half
  Then I will have a result of 0.5

Again, for this we can use Cucumber expressions in our step definitions to extract the data:

@Given("I have divided the number {int} in half")
public void setSum(Integer integerNumber) {
    System.out.println(integerNumber);
}

@Then("I will have a result of {float}")
public void evalOutput(float floatNumber) {
    System.out.println(floatNumber);
}

Code Examples
Java | Ruby | JavaScript 

Did this answer your question?