how can read the account number in json response and store it in datasource/properties?
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
how can read the account number in json response and store it in datasource/properties?
- I have an account number
- i have a JSON response that has 100 records with different account number and other details
- how do i find this account number exists in 100 records and store it somewhere like datasource or property?
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
this is my account number 265954 which i got from one response
now in below response this (265954) account is there in third record ("value":"265954F3365220210621") first 6 digits is my account number
[
{
"part": 1,
"set": 265,
"ifsc":"88977",
"value":"569954F3365220210621"
},
{
"part": 2,
"set": 266,
"ifsc":"88978",
"value":"965954F3365220210621"
},
{
"part": 3,
"set": 267,
"ifsc":"88979",
"value":"265954F3365220210621"
}
]
so since i have the account number in "value" field i want to copy the whole field value (265954F3365220210621) and store it in datasource or property..
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Add script assertion for the REST test step.
/**
Save the 265954 at test case level custom property say ACCOUNT_PREFIX and it will read from there; next time if the value changes, you can replace there without modifying assertion
*/
assert context.response, 'The response is empty or null'
def accountPrefix = context.testCase.getPropertyValue('ACCOUNT_PREFIX')
def json = new groovy.json.JsonSlurper().parseText(context.response)
//Retrive respective account number from the response
def accountNumber = json.find {it.value.startsWith(accountPrefix)}.value
log.info "Account number fetched: ${accountNumber}"
//Save account number at test case level
//Use can use ${#TestCase#ACCOUNT_NUMBER} In next test steps if you need it.
context.testCase.setPropertyValue('ACCOUNT_NUMBER', accountNumber)
Please follow comments in above code snippet
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'm getting error like this, from the logs i can see that from the response I'm getting only the account number but i want the entire string which has that account number
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Have you followed the comments provided?
Solution provided as per information provided. And it fetches entire account number.
You can try it online here
What is the value for ACCOUNT_PREFIX ?
What is the value for ACCOUNT_NUMBER after running the assertion ?
Do you have multiple values with same ACCOUNT_PREFIX in your response ?
Please show the screen shot for test case custom properties, script assertion and response.
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sorry. i think I've posted the wrong response,
[
{
"part": 1,
"set": 265,
"ifsc":"88977",
"value": "{\"event\":\"569954F3365220210621"}"
},
{
"part": 2,
"set": 266,
"ifsc":"88978",
"value":"{\"event\":\"965954F3365220210621"}"
},
{
"part": 3,
"set": 267,
"ifsc":"88979",
"value":"{\"event\":\"265954F3365220210621"}"
}
]
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
There is one more thing the accountPrefix doesn't always starts at starting it will be in the middle or end as well
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Got the solution, I used contains instead of startsWith.. 🙂
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
But you what i'm getting only one record with the given account number there might be some other transactions with the same account number .. how can i get all the string that has this account number?
