Working With Doc Strings and Data Tables

Learn how to use step definitions to extract data from Doc strings and Data tables

Alan Parkinson avatar
Written by Alan Parkinson
Updated over a week ago

During the capture phase, teams are encouraged to capture concrete examples that contain mentions of real life data that we expect users to submit. Whilst concrete examples are useful for explaining behaviour they are also allow us to use real life data to drive our automated acceptance tests

So let's look at how we can extract that data from steps and use it within our step definitions.

Doc Strings

The most straight forward of the three options is Doc strings which when added to an example look like this:

Scenario: Demonstrating Doc Strings
  Given I have filled in the address details form
  When I submit the form
  Then the address is processed and I receive the message
    Thank you for your submission

    We will be in touch

The Doc string, at the bottom of the Then step, not only allow us to demonstrate complex strings in examples but it can be stored into a variable to use within our step definitions, like so:

@Then("the address is processed and I receive the message")
public void docStringDemo(String docString) {

Notice how the function has a parameter of String docString . The message stored between the triple quotes, including line breaks, in the scenario are added to the docString variable for us to use.

Code examples
Java | Ruby | JavaScript

Data Tables

Data tables are used in concrete example when we want to describe a range of data or the order of a data set, like in this example:

Scenario: Demonstrating Data Tables
  Given I have filled in the following address
    | house_number | street_name     | town   |
    | 17           | Galexie Gardens | London |
  When I select address type
  Then I will be presented with three address types
    | residential |
    | business    |
    | public      |

We have two types of data tables in the example. The first demonstrates a table with headers that allow us to create a list from the table where we can use the first row as headers to extract a specific value:

@Given("I have filled in the following address")
public void fillInAddress(List<Map<String, String>> tableWithKeys) {
    Map<String, String> firstRow = tableWithKeys.get(0);

With the second example we can convert that table in a simple list of strings, which we can then extract specific items from in a list:

@Then("I will be presented with three address types")
public void addressTypes(List<String> listOfRows) {

How you want to parse a table is based on the data type you set.  Notice how the parameter of tableWithKeys and listOfRows differ. There are lots of different ways in which you format Data tables and depending on how you have laid out your table will determine how you parse into a data type that is most useful for you.

Code examples
Java | Ruby | JavaScript

Did this answer your question?