GetData based on another value
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Was trying to be clever and get the the id by doing this.
${LISProperties#Response#$['data'][?(@.translationkey=ActionCode)].data.id}
LISProperties is the request that pulls the data back. This does not seem to work but I think the above is slightly wrong? The Json structure is in the screenshot. Trying to avoid the numeric value under data as theis wont always be the same. Can you skip a level in the query like I have done above?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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())
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Change from (there is space in your response')
def expectedMatchingProperty = 'ActionCode'
to
def expectedMatchingProperty = 'Action Code'
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you. That worked.
Now.......I have another 20 I need to do for that API. Whats the best way to add them all?
Do i just repat the code you have provided (but updating the expected match property)? How can I name them differently so I can then call them individually like you put above ${#TestCase#ID}
You have been very helpful 🙂 Learnt somethign very useful today!
