Sree
Sree

Reputation: 351

Facing challenges while using relative path and mapping test data from a json file to a request

I am facing few issues while using relative path and mapping test data from a JSON file. I am having JSON POST request and a test data file in JSON format.

This is the test data I am using.

    {
  "name": "Test Data",
  "description": "Information's mainly related with Users",
  "testData": [
    {
      "Scenario1": {
        "givenName": "Joseph",
        "familyName": "George",
        "addressType": "Current",
        "lineOne": "BNRA-222, Kowdiar lane",
        "cityName": "Trivandrum",
        "countryID": "India",
        "postcode": "695006"
      }
    },
    {
      "Scenario2": {
        "givenName": "Sreenath",
        "familyName": "Bhasi",
        "addressType": "Current",
        "lineOne": "HSE-123, Karyavatom",
        "cityName": "Trivandrum",
        "countryID": "India",
        "postcode": "695552"
      }
    }
  ]
}

This is the feature file

    Feature: Test using the Data from a JSON file

    Background:
    * def baseJsonRequest = read('../requests/jsonrequest.json')
    * def baseData = read('../data/sampledata.json')
    * def endPointURL = endPointURI + path

   Scenario: A sample scenario to test the data parametrization

    Given url endPointURL
    And request baseJsonRequest
    * set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.givenName = baseData.testData[*].Scenario1.givenName
    * set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.familyName = baseData.testData[*].Scenario1.familyName
    * set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.residenceAddress.addressType = baseData.testData[*].Scenario1.addressType
    * set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.residenceAddress.lineOne = baseData.testData[*].Scenario1.lineOne
    * set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.residenceAddress.cityName = baseData.testData[*].Scenario1.cityName
    * set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.residenceAddress.countryID = baseData.testData[*].Scenario1.countryID
    * set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.residenceAddress.postcode = baseData.testData[*].Scenario1.postcode

My Questions are:

  1. I am not able to give relative path on both sides. The relative path is returning me a json array. For eg I cannot use $..Scenario1.givenName, which makes me write longer paths.
  2. To include this mapping on every scenario will be practically difficult. How can we implement a parameterized solution for that. What will better way? Can I invoke the data reading using a feature file and pass the informations to another feature. If that's possible then I need to parameterize . How to do that?
  3. Or do I need to use a java class to read the JSON file?

Upvotes: 0

Views: 272

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

Yes, the moment you have a wildcard in JsonPath, it returns an array. Anyway, 2 points that should help here straight away:

  • you can move repeating nested paths into a table-set
  • you can refer to a nested chunk of JSON by assigning to a variable

So this should be the way to go:

* def first = get[0] baseData.testData[*].Scenario1
* set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson
  | path                         | value             | 
  | familyName                   | first.familyName  |
  | residenceAddress.addressType | first.addressType |

I would try to not use wildcards as far as possible, for e.g.

* def first = $baseData.testData[0].Scenario1

Hope this helps !

Upvotes: 1

Related Questions