Forum Discussion

avidCoder's avatar
avidCoder
Super Contributor
7 years ago
Solved

How to make the REST request JSON Key's Value dynamic?

Let say, I have following request as JSON:-

 

{
"code":"${DataSource#code}",
"ciDate":"${Properties#StartDate}",
"coDate":"${Properties#EndDate}"

}

 

I want to make ciDate and coDate dynamic in the sense that the count of StartDate and EndDate could be increase or decrease.

So, I am storing it into properties as StartDate1, StartDate2, StartDate3.......So..on. Similarly EndDate1, EndDate2, EndDate2... So..on.. Now How should I pass these values automatically for ciDate and coDate respectively and get the response for each StartDate and EndDate stored at some where.

  • The Open Source way to do it would be to run a Groovy Script in the same Test Case as your Test Request. As an example:

     

    def step = testRunner.testCase.getTestStepByName("My REST Test Step")
    def data = [
        [startDate: "01/01/2001", endDate: "02/02/2002"],
        [startDate: "03/03/2003", endDate: "04/04/2004"],
        [startDate: "05/05/2005", endDate: "06/06/2006"]
    ]

    data.each {
        testRunner.testCase.setPropertyValue("startDate", it.startDate)
        testRunner.testCase.setPropertyValue("endDate", it.endDate)
        testRunner.runTestStep( step )
        log.info step.testRequest.responseContentAsString // just logging it here but you could write to file etc
    }

    And request body as:

    {
    	"code":"${DataSource#code}",
    	"ciDate":"${#TestCase#startDate}",
    	"coDate":"${#TestCase#endDate}"
    }

    There may be easier ways to do it in Pro, but I wouldn't know about that. I mention it because you have mentioned a DataSource, which I believe is only for Pro.

6 Replies

  • JHunt's avatar
    JHunt
    Community Hero

    The Open Source way to do it would be to run a Groovy Script in the same Test Case as your Test Request. As an example:

     

    def step = testRunner.testCase.getTestStepByName("My REST Test Step")
    def data = [
        [startDate: "01/01/2001", endDate: "02/02/2002"],
        [startDate: "03/03/2003", endDate: "04/04/2004"],
        [startDate: "05/05/2005", endDate: "06/06/2006"]
    ]

    data.each {
        testRunner.testCase.setPropertyValue("startDate", it.startDate)
        testRunner.testCase.setPropertyValue("endDate", it.endDate)
        testRunner.runTestStep( step )
        log.info step.testRequest.responseContentAsString // just logging it here but you could write to file etc
    }

    And request body as:

    {
    	"code":"${DataSource#code}",
    	"ciDate":"${#TestCase#startDate}",
    	"coDate":"${#TestCase#endDate}"
    }

    There may be easier ways to do it in Pro, but I wouldn't know about that. I mention it because you have mentioned a DataSource, which I believe is only for Pro.

    • avidCoder's avatar
      avidCoder
      Super Contributor

      Hey JHunt, Somehow it is working for the list of data, But I am not aware of the number of startDate and endDate I will have. Since, you have put three(3) startDate and endDate a/c to your wish. It is working as expected. But the main thing is the startDate and endDate I am taking from excel sheet like this way - 2018-03-02#2018-03-04#2018-0706 and then splitting it and after that I will have to store it to data variable. Can you please give some insight on this?

       

  • PaulMS's avatar
    PaulMS
    Super Contributor

    Usually it would be easier to reset the value of a property by script as many times as necessary than to create many properties.

     

    If you have already created the properties with an index number at the end of the name then it is possible to use a variable or property to store that index number. To get date values in a request using a property named "Index"

     

    {
    "code":"${DataSource#code}",
    "ciDate":"${= context.testCase.getTestStepByName('Properties').getPropertyValue('StartDate${Properties#Index}' )}",
    "coDate":"${= context.testCase.getTestStepByName('Properties').getPropertyValue('EndDate${Properties#Index}' )}",
    }

    • avidCoder's avatar
      avidCoder
      Super Contributor

      But the Index you have mentioned here is all about 1 property of startDate. How to do it for dynamic properties.. I will not be knowing the total number of startDate and endDate. How will I set the index based on that?

  • JHunt's avatar
    JHunt
    Community Hero

    Hi, I didn't understand exactly what you mean. But something like below for getting data might help you together with the other code above for re-running the test steps. Let me know if I've misunderstood what you need.

     

    def strings = [ 
    	"2017-03-02#2017-03-04#2017-07-06", 
      	"2018-03-02#2018-03-04#2018-07-06",
      	"2019-03-02#2019-03-04#2019-07-06",
    ]
    def data = []
    strings.each {
    	def m = it =~ /(.*)\#(.*)\#(.*)/
    	data += [ code: m[0][1], ciDate: m[0][2], coDate: m[0][3] ]
    }

    log.info data

     

    INFO:[{code=2017-03-02, ciDate=2017-03-04, coDate=2017-07-06}, {code=2018-03-02, ciDate=2018-03-04, coDate=2018-07-06}, {code=2019-03-02, ciDate=2019-03-04, coDate=2019-07-06}]

     

    • avidCoder's avatar
      avidCoder
      Super Contributor

      Thanks for your support, It is resolved now.. I wrote for loop to get it one by one dynamically and stored the result.,