Forum Discussion

endorium's avatar
endorium
Frequent Contributor
6 years ago

GetData based on another value

I have a test I am trying to make more robust since DB ID's change often.....

 

I call one API to retrieve the informaiton I need. I need to now use a value that i returned based on another value.

 

Example will help explain.....

Screen 1 is an example of what I get returned.

The id can change but the propertyName will not.

 

The API post call I then make requires the id be entered. So I need to pass the id from the 'Get' based off its propertyName.

 

How do I do this? I though doing getdata or using it as a data source might work but cant seem to do it. Any help is appreciated.

  • nmrao's avatar
    nmrao
    Champion Level 3

    endorium ,

     

    Use below script assertion for the REST Request test step which will extract the id value and saves it at test case level custom property, say ID, which can be used used in further test steps of the same test case.

     

    /**
    * Script Assertion for REST Request test step
    **/
    //Change the value for below statement as needed
    def expectedMatchingProperty = 'Action Code'
    
    assert context.response, 'Response is empty or null
    def json = new groovy.json.JsonSlurper().parseText(context.response)
    
    context.testCase.setPropertyValue('ID', json.data.find {it.propertyName == expectedMatchingProperty}?.id?.toString())

    Use ${#TestCase#ID} where it is required later in the test steps.

    • endorium's avatar
      endorium
      Frequent Contributor

      Hi

      Thanks for the code but it does not seem to work. See the screenshot

       

       

       

      • nmrao's avatar
        nmrao
        Champion Level 3
        Sorry about it, there was typo.

        Change below statement from:
        context.testCase.setPropertyValue(json.data.find {it.propertyName == expectedMatchingProperty}?.id?.toString())

        To

        context.testCase.setPropertyValue('ID', json.data.find {it.propertyName == expectedMatchingProperty}?.id?.toString())
  • richie's avatar
    richie
    Community Hero

    Hi,

     

    I just want to make sure I understand the situation before I try and suggest an option thats totallly wrong.

     

    You have a request and in the response is a value.

    You want to grab this value and save it to a property so that you can use this value in a subsequent test step.

     

    Is that right?

     

    Cheers,

     

    rich

    • endorium's avatar
      endorium
      Frequent Contributor

      Sorry if its not clear :) Was truggling to explain it.

      I make a request that returns an id with another matchiung attribute

      In the screen shot for example id 66 is returned and it matching attribute to 'ActionCode'.

       

      I want to use the id value above but want to reference it by its matching attribute as the attribut is fixed and wont change.

      So I want to say get the value of the id given the sttribute 'ActionCode'. Then use the value in a subsequent 'Post' api call

      • richie's avatar
        richie
        Community Hero

        Hi,

         

        If you just need a property transfer to pass on value to subsequent step, you can use the following instructions - other people may have niftier ways of doing this though using groovyscript.

         

        1.  Highlight the attribute value (id = 66) in the response you want to transfer in the 'Outline' tab

        2.  Click on the 'Transfer to' button and select 'Add properties step' - a form dialogue generates entitled 'Create Properties step'. 

        3.  Click 'ok' to accept the creation and name of the properties step (probably 'Properties 1')

        4.  A 'Create target property' form dialogue is generated.  Replace the name with 'Action Code' and Click 'OK' to accept - this launches a 'Transfer to Property' form.  Click on 'OK' to accept and close - this inserts a 'Property Transfer' step in the test case object hierarchy.

        5.  Right click, select 'insert step' and select to add in the POST request associated with your POST API 

        6.  Ensure the POST is AFTER the properties and property transfer step in the test step hierarchy

        7.  Its at this point that depends on how you've setup the POST and which parameters you are populating - one option is to highlight the POST request in your test case, click on the Outline tab and click on Get Data and grab the value that is written to the Properties step.

         

        REMEMBER - at this point though, the 'Action Code' property in the Properties step doesn't yet have a value to be transferred - you need to run the transfer to populate the Properties step with the '66' value, before the script will work - if you just execute the test - this will also run the transfer.

         

        Hope this helps - this does link the ID value to the ActionCode property - although I dont know if this satisfies or not.

         

        Cheers,

         

        richie

         

         

        Cheers,

         

        rich