Working with JSON Files


Introduction

JSON is a text file that can convert to any JavaScript object and can be sent to a server. You can also convert any JSON received from the server into JavaScript objects. This way you work with the data as JavaScript objects, with no complicated parsing and translations. In Natural Screen Tester, you could generate the test case with initial test file Data.json file containing the default inputs and assertions given by the test case procedure. For example, the following input and output parameters:

{
"inputs":
   {
   "Code":"cu"
   "Password":"demoy2"
   "UserId":"demo02"
   "structure1":
      {
      "Birthday":
      "Customer_ID":
      "First_Name":
      "Last_Name":
      "Sex":
      "TotalPoliciesAmount":
      "Type":
      }
   }

"Assertions":
    {
    "Firstname":"Charles</Firstname>
    "House_Number":"12</House_Number>
    "Lastname":"Chappell</Lastname>
    "Mobile":
    "Municipal_Code":
    "Nationality_Long":"British"
    "Nationality_Short":"UK"
    "Occupation_Long":"Actor"
    "Occupation_Short":"81177"
    "PO_Box":
    "Person_Sex>M</Person_Sex>
    }
} 

Working with Arrays

You can also provide input using an array. The runTest method is provided for sending inputs from a given JSON file to the Base Object and verifing the assertions from the JSON file, using the response data.

Each input entry in the JSON file is converted to GXElement so that the test case can run. Each output attribute given by the Base Object response is converted to a JSON object and compared with the assertion loaded from the resources folder automatically.

JSON Configuration File

The JSON configuration file enables you to change the following values for excuting the tests:

{
   "serverPort": "2323",
   "serverAddress": "localhost",
   "nsrUser": "administrator",
   "nsrPassword": "",
   "hostApplication": "SAG_MainFrame"
}

Parameter hostApplication is mandatory; all others are optional. If a parameter is not defined, the test will try to run with the default value.

If parameter hostApplication is not defined, the test will run on actual application host.

Working with Multiple JSON Files

The following mechanisms are provided to enable you to work with multiple JSON files:

* (asterisk) any files
f1.json, f2.json... list of files

The following rules apply:

  • The file extention must be ".json".

  • The list must not be recursive.

  • The file name must be valid for the operating system.

Example:

public static Collection <Object[]> getTestData() {
    String[] fileNames = {"file1.json", "file2.jsOn"};
     return TestCase.getTestData(fileNames);
}

The default filter for test data is "*".

Comparing the Results of a Unit Test

If a failure occurs in a unit test, you can use a compare function to easily locate the source of failure.

Start of instruction setTo compare the actual results of unit test with the expected results

  • Double click the line org.junit.ComparisonFailure in the JSON file. See (1) in screenshot below.

    Or:
    Select the line in the marked rectangle area (2) in the screenshot below, and from the context menu choose Compare Actual With Expected Test Result.

    Or:
    Select the line and click the Compare icon graphics/json_compare-icon.png. See (3) in the screenshot below.

    graphics/json_compare-submit.png

This produces the following output, where the discrepancies are highlighted.

graphics/json_compare-result.png