Forum Discussion

AlekseiKlimkin's avatar
AlekseiKlimkin
Occasional Contributor
8 years ago

Transferring properties from JSON response to Script TestStep

Hi Everyone,

I am pretty new to writing automated tests for APIs using scripts, so my question might have a completely obvious answer (and I assume it does), but I haven't been able to find it on the web.

 

Here is a test case I am writing and what I want to achieve:

 

- I am working with RestAPI and obtain a result from one test step in a form of a JSON response;

- From that JSON response I need to transfer some property called "ids" into my testSuite property in order to use it as a parameter in the Endpoint of the following TestStep; (transferring from one JSON response to another JSON request is, of course, straight forward, but in my case that would not work since the property is to be transferred to the Endpoint)

- So I decided to make a Script testStep where I want to append that "ids" property to the TestSuite Property I have. Here is the script:

def endpoint = testRunner.testCase.testSuite.getPropertyValue(property name);
def ids; //this is what I tried to use as a destination to store my "ids" property from a Property Transfer testStep
def updated = endpoint.concat("?posts=").concat(ids);
testRunner.testCase.testSuite.setPropertyValue(property name, updated);

 

- Between the test step where I am getting my JSON response, and this Script test step I put a Property Transfer test step, where I transfer "ids" from the JSON response by using JSONPath, then I define a destination as my Script test step, but I don't know how to define a location for "ids" to be transferred. So far, I tried ids and def ids but these do not seem to work.

 

And more importantly, I don't know which Path language to use for this, since JSONPath is obviously not going to work, because my script isn't in JSON format, but it isn't in XML either, so xPath and xQuery don't seem to be a solution, do they?

 

I am using open source version of SoapUI, so these three Path languages are the only choices.

But I am pretty sure that there should be a way to transfer a property from JSON to a script since one can define a Script test step as a Target in the Property Transfer test step.

 

Apologise for the poem I've written, but any help would be highly appreciated.

Thanks in advance.

2 Replies

  • New2API's avatar
    New2API
    Frequent Contributor

    Hello,

               I am assuming that you would to like perform these steps to achieve your test:

    1. Execute a REST step
    2. Extract IDs from JSON response (possibly a groovy script) and transfer to a TestSuite property
    3. Transfer or append above identified property to another Rest Step URL

    If above is correct then use variable or parameterize your second rest ENDPOINT (URL)

     

    for Ex: http://TestHost/v2/{IDs}  (Resource parameter - Style TEMPLATE)

                http://TestHost/v2/?{IDs}  (query parameter - Style QUERY)

     

    In second rest step point this parameter to your TestSuite Property like ${#TestSuite#updated}

     

    Hope this helps!

     

     

    • AlekseiKlimkin's avatar
      AlekseiKlimkin
      Occasional Contributor

      Hello!

      Yep, this helps, I made my tests work by changing the TestSuite property in a Script test step, everything works fine, thank you!

       

      But the main question remains: if I can define my Script test case to be a Target for a PropertyTransfer, how do I transfer some value from the response of previous test step to a variable in the script?

       

      Of course I understand that I can do it using script anytime, but this is just for the sake of my curiosity.

      Here is a screen shot to make things more concrete: