Data Driven Testing with YAML in Java Script frameworks

Let’s keep it simple & easy to handle!

What is Data Driven Testing?

DATA DRIVEN TESTING is a test automation framework that stores test data in a table or spreadsheet format. This allows automation engineers to have a single test script that can execute tests for all the test data in the table.

In this framework, input values are read from data files and are stored into a variable in test scripts. Ddt (Data Driven testing) enables building both positive and negative test cases into a single test.

In Data-driven test automation framework, input data can be stored in single or multiple data sources like xls, XML, csv, and databases.

As it says exactly we can store data in different sources as mentioned. Nowadays, test engineers commonly use JSON files to store data. That’s because we can build up a user friendly syntax in our test frameworks. Json have lots of serializers/deserializers for all the programming languages. In Java Script once you import a json file you can simply access the data through its hierarchy. Check the below json:

You can access the data of this file like “User.Email” & “User.Password”, there is no need to write a separate readers and all to read data when using json in Java Script. Pretty easy right?

But..

YAML — Yaml Ain’t Markup Language

What is YAML?

YAML is a human friendly data serialization
standard for all programming languages. — yaml.org

If you go through it, it is true to it’s definition! It’s a super user friendly way of storing and handling data. Check the following snippet of a YAML file:

Pretty simple right? It’s the same set of data shown in JSON file before. But the issue is, YAML files can not be read like we do in JSON files. Still there are not deserializers for YAML to read data, the way it does in JSON.

So how to use it?

Okay.. We can convert the YAML to JSON and read data from JSON.. Simple as that. So what we do is, we write/store data on yaml files since it’s pretty user friendly to do so AND convert the YAML to JSON and read data from the JSON.

You might think it’s a complex way and you have to always do something to convert YAML to JSON and you might think it’s like maintaining two files. It’s not like that. We can do something like this..

In your test project you can write a simple js script to convert the YAML to JSON which will create the JSON file at the same YAML location. In my words I call this script a “custom tool”. Note that in order to write this you need to install the npm package js-yaml to you project first! Now setup a file watcher to your IDE to watch the changes in YAML files, and let it trigger this custom tool written to convert the YAML to JSON. So you just don’t have to bother about it anymore!

Here is sample of the js script to convert YAML to JSON:

var inputfile = 'cypress/support/keys.yml',outputfile = 'cypress/support/keys.json',yaml = require('js-yaml'),fs = require('fs'),obj = yaml.load(fs.readFileSync(inputfile, {encoding: 'utf-8'}));fs.writeFileSync(outputfile, JSON.stringify(obj, null, 2));

There are lot of file watcher for IDEs pick wisely and configure it! So now when you write you test scripts import the JSON file and read the data. When you want to change a test data or add/delete test data, open the YAML file and do it, save it. No worries! File watcher will do the rest!

Something worth mentioning..

LoginPage: 
ContentElementOne:
EmailField: .emailtext
SubscribeButton: .subscribe
ContentTitle: .title
ContentSubtitle: .subtitle
HomePage:
ContentElementOne:
EmailField: .emailtext
SubscribeButton: .subscribe
ContentTitle: .title
ContentSubtitle: .subtitle
AboutPage:
ContentElementOne:
EmailField: .emailtext
SubscribeButton: .subscribe
ContentTitle: .title
ContentSubtitle: .subtitle

Now if you can see, the nodes under “ContentElementOne” are repeating and you have to put a separate effort in writing all those right.

In YAML there is this feature called Alias & Reference to simplify this. What you have to do is create an Alias with the nodes under ContentElementOne. Then refer it on every place you have to apply it.

ContentElementOne: &ContentElementControls
EmailField: .emailtext
SubscribeButton: .subscribe
ContentTitle: .title
ContentSubtitle: .subtitle
LoginPage:
ContentElementOne: *ContentElementControls
HomePage:
ContentElementOne: *ContentElementControls
AboutPage:
ContentElementOne: *ContentElementControls

Check the above YAML! With the “&” and a name I have create an Alias for the node ContentElementOne and referred that node using “*AliasName”. If you convert this to JSON, it will have those nodes under ContentElementOne duplicated at every place where it referred!

Cool! That’s simple right?

So that’s about it.. Try it and let me know..

Associate Lead QE @ Nagarro