Forum Discussion

madhu_112's avatar
madhu_112
Occasional Contributor
4 years ago

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 ...
  • nmrao's avatar
    nmrao
    4 years ago

    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