Data Driven with YAML


In the previous section , Data Drive with excel-2, we have explained the entire data driven capability and concepts, so this section would contain only information specific to YAML. The cucumber scenarios and step definitions are mostly same as that we have seen with excel, just the reading of the file is a little different probably few lines of code difference. Anyways, we posted an entire video and the project code base is available too, that contains both excel and yaml scenarios inside same project. We will fill a contact us form on website with different data sets. The contact form looks as below with many input fields.




  1. Describe the cucumber scenario
  2. Populate object repository
  3. Data YAML
  4. Combine cucumber scenario outline + data table to specify multiple data sets
  5. Consume Data Helper and parse data
  6. Change default data input yaml
  7. Execute the scenario
  8. Observe the results
  9. Closing thoughts and Project Code

1) Describe Cucumber scenario

We will:

  1. open website,
  2. Then click “Contact us” button at the right top corner, which will take us to contact form.
  3. On the contact form, select heading, set email, set order reference number, set message
  4. We will input all the necessary values and eventually click submit.

The simple cucumber scenario when written might look as below. I want to use the data from “DataSet1” of the excel workbook and I would go with the default yaml file “default.yml”.

2) Populate object repository

We will inspect the html elements and use the “id” attributes for each of the fields viz. heading, email, order reference, message and submit button. The file “lib/config/object_repository.yml” looks as below.  We are also printing the object repository values in the cucumber scenario, so on the output window or results , you can see what values were used.

3) Data yaml

We will use the data yaml ‘lib/config/default.yml‘ file and the file looks as below [We will post the entire project codebase at the end of this section in case you want to download the excel file]. We are also printing the current data set used by the scenario inside step_definitions, so that we know which data set was used.

4) Cucumber Scenario Outline

The second cucumber scenario outline uses multiple data sets and repeats the operations for each data set. This is just to show how data “drives” the test case execution.

5) Consume Data helper and parse data

We first mixin the Data Helper module with Cucumber by specifying this line in env.rb file. This will ensure that we can use the methods specified on the Data helper module within the step_definitions [which you will see in a while]

We then access the data helper method inside the step_defintion as follows:

Now we have a data_set which is a hash that contains the first data set from above yaml file. The step_definition code is posted at the bottom of this page, so you will know the exact values of the arguments inside the code.

5) Change default input yaml

If you intend to change and use data from another yaml, then put the yaml inside ‘/lib/config/data’ folder and set it in cucumber.yml file as below. Let’s say your new yaml is “beta_data.yml”. This file will override the “default.yml” and the code will use beta_data.yml file.

Entire Cucumber Scenario:

Step Definitions:

Execution and Results:

Right click on any of the scenarios and execute. The first scenario should use the first row values and second scenarios should run with both the row values. The results file should look as below:



Closing Thoughts and Project Code:

As we see, we have externalized data with multiple data formats. That said, we have just read data from yaml into hash, after that we can do pretty much anything with the data structure. In the previous section, we have used excel

Feel free to check out the entire project code base from my github repo: data_driven_framework

The code base contains YAML and excel data driven examples too, however you will be able to figure out which one to use for excel/yaml

Data Driven Framework