Forum Discussion

madhu_112's avatar
madhu_112
Occasional Contributor
4 years ago

Update the previously available balance only when there is a change in available balnce

Hai, can someone help me with this problem..

 

I've availbalance = 123 and prevavailbalance = 121(this should only update when there is a change in availbalance, in this case 121 was previously available balnce) - both are coming from json response

now my question is i'm writing a script to check whether the prevavailbalance is correct or not? i'm stuck here how to set this thing up.

 

  • madhu_112 It should be easy to deal with it with groovy script.

    Since it retrieve available balance from json response, you may need to save it in properties (i.e test suite properties) and next time get new balance and compare them.

    For example:

    test suite properties: availbalance = 123

    in REST step:

     

    // add script in response
    import groovy.json.JsonSlurper
    
    def response = messageExchange.response.contentAsString
    def json = new JsonSlurper().parseText(response)
    // assume that you save availbalance in test suite level
    def availbalance = context.expand ('${#TestSuite#availbalance}').toInteger()
    // please replace with your json path
    def newbalance= json.availableBalance
    if (availbalance != newbalance) {
        messageExchange.modelItem.testStep.testCase.testSuite.setPropertyValue("availbalance ", newbalance.toString())
    }

     

     

    Thanks,

    /Aaron

  • richie's avatar
    richie
    Community Hero
    Hey madhu_112,

    I think we're gonna need some more info to help you.

    You mention you need to assert that the prevavailbalance is correct but you dont indicate what makes it correct (i.e. what is the underlying requirement youre trying to verify?)

    Looking at your post's subject header it appears you want to assert that preavailbalance is updated when the curravailbalance is updated, is that right? You could do this with a simple if/then bit of groovy.
    People will need either the full jsonpath to the relevant attributes (i.e. the curravailbalance and prevavailbalance) or the actual payload so they can determine the jsonpath to build the groovy you need.

    Ta

    Rich
  • aaronpliu's avatar
    aaronpliu
    Frequent Contributor

    madhu_112 It should be easy to deal with it with groovy script.

    Since it retrieve available balance from json response, you may need to save it in properties (i.e test suite properties) and next time get new balance and compare them.

    For example:

    test suite properties: availbalance = 123

    in REST step:

     

    // add script in response
    import groovy.json.JsonSlurper
    
    def response = messageExchange.response.contentAsString
    def json = new JsonSlurper().parseText(response)
    // assume that you save availbalance in test suite level
    def availbalance = context.expand ('${#TestSuite#availbalance}').toInteger()
    // please replace with your json path
    def newbalance= json.availableBalance
    if (availbalance != newbalance) {
        messageExchange.modelItem.testStep.testCase.testSuite.setPropertyValue("availbalance ", newbalance.toString())
    }

     

     

    Thanks,

    /Aaron