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

Pre-requisites:

  1. You have completed the section “Set up Env” and have your environment ready with JDK, maven and IntelliJ
  2. You have read and understood HTML DOM
  3. You were able to comprehend “Test Data” post
  4. You downloaded the project code base for class10 under – https://github.com/machzqcq/CucumberJVMExamples and imported into IntelliJ and the structure should look as below
class10_intellij_project_structure
If imported into eclipse, it should look as below
class10_eclipse_project_structure

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) 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 calleddeserialization.

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

3) Java Options for JSON

There are many libraries in Java that help dealing with JSON. Few of them are

  1. Jackson Library
  2. Google gson (this is what we will be using here)
  3. JSON-lib
  4. XStream
  5. JsonMarsheller
  6. More……

4) Gson library

Serialization

DeSerialization

Parse Json 

Parse Json and input to WebForm

Entire Cucumber Feature:

Step Definitions

Execution Output

Execution from eclipse and intelliJ is pretty much the same.

IntelliJ Output (How to execute is explained in previous post)

class10_intellij_output

Eclipse output looks as below

class10_eclipse_output

Command line Output

class10_cmd_output

CI Server Output

class9_ci_output

class10_ci_output1