Test Data with JSON

What is json?:

JSON is a lightweight data-interchange format. JSON is completely language agnostic, making it the ideal interchange format.It is also a subset of JavaScript’s Object Notation (the way objects are built in JavaScript). JSON is extensively used in web services ever and in many cases is replacing XML as data-interchange format.

How is it relevant to test data ?

We can maintain our test data in json format too (instead of excel or other formats) and that makes the performance relatively faster [if we are talking about extremely large # of files and data]. Whether we generate data or not, at least as a consumer of data from web services, applications and other integrating systems, we might need to understand how json works and be ready to have the capabilities to parse json with all CRUD operations as needed.

Agenda:

  1. JSON overview
  2. Options available in Ruby Stack
  3. JSON serialization and deserialization
  4. Cucumber Feature and Code

1) JSON overview:

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence

Json_pic

 

The visual depictions here pretty much sum up what a JSON is. Thanks to JSON.org.  Another quick round of summary that is relevant to Automation is:

  • An object is JSON is (key,value) pair [or multiple key,value pairs]
  • Key is a String. value can be String, json, object, array and so on

2) Ruby Options:

Ruby has an in-built Module for JSON and it is exactly called that – JSON. There is a json gem as well, however for many test data operations, the in-built module is sufficient. The below examples also use the in-built module.

If you intend to use the “json” gem, please add “gem json” to Gemfile , run “bundle install/update” and then “require json” in your env.rb. All of these instructions on how to add a new gem to your project is posted with screenshots in previous post

3) Serialization and de-serialization:

Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

An example of serialization is to retrieve a web service response body as json and save it to a file as json string.

An example of deserialization would be to read a json string from a file and pass it as request body when making a web service call

Entire Cucumber Feature:

  • The first scenario parses a json string and depicts how to access different (key, value) pairs at different levels in the object hierarchy
  • The second scenario parses another json string which has arrays
  • The third scenario creates a json and saves it to a file (Serialization)
  • The fourth scenario accesses the json from file and uses it in program (deserialization)
  • The fith scenario is an application of json to a web UI Automation scenario. This example does the same actions of filling a form with data sets, the same example what we explained for Test Data with Excel 
  • Download the files and put in /features/support/datafiles directory: practiceform.json , sample.json

Step Definitions: