Forum Discussion

mkhan031486's avatar
mkhan031486
Contributor
7 years ago
Solved

How can I add two values in a request that is taken from another response

I have two web services that I'm testing right now. 

 

First webservice basically calculates taxes for different products. The second one actually adds the total taxes and makes a payment. For example Below is the response I get where I have get credit taxes. 

 

<CreditDetailsListResponse>
<CreditDetails>
<SubOfferId>OFF108</SubOfferId>
<SubOfferPrice>0.0</SubOfferPrice>
<CreditAmount>10.0</CreditAmount>
<AvailableAmount>0.0</AvailableAmount>
<CreditTax>0.7</CreditTax>
<AvailableTax>0.0</AvailableTax>
</CreditDetails>
<CreditDetails>
<SubOfferId>OFF062</SubOfferId>
<SubOfferPrice>0.0</SubOfferPrice>
<CreditAmount>2.33</CreditAmount>
<AvailableAmount>0.0</AvailableAmount>
<CreditTax>0.16</CreditTax>
<AvailableTax>0.0</AvailableTax>
</CreditDetails> <CreditDetailsListResponse>

Now I want to use the two credit tax (0.7 & 0.16) above and add them and then use them in another request in the TotalTaxAmt attribute. for example;

 

<PaymentRequest>
  <RefundPaymentAmt currency="USD">30</RefundPaymentAmt>
  <ConvenienceFeeAmt currency="USD">0.0</ConvenienceFeeAmt>
  <TotalTaxAmt currency="USD">0.86</TotalTaxAmt>
</PaymentRequest>

Is there a way to do that in SoapUI?? 

 

Any help will be appreciated!!

 

  • mkhan031486,

     

    Here is the quick demo to extract and the sum the taxes. Note that this demo uses fixed xml. But, you may want it to be dynamic for each test request.

     

    Here you what you need to do:

     

    For the first soap request, add script assertion as shown below:

     

    assert context.response, 'Response is empty or null'
    
    //extract the tax and sum all of them
    
    def cTax = new XmlSlurper().parseText(conext.response)
    .'**'
    .findAll{it.name() == 'CreditTax'}
    .collect{it.text() as double}
    .sum() log.info "Total tax : $cTax" //Store the above cTax value at test case level custom property TOTAL_TAX context.testCase.setPropertyValue('TOTAL_TAX', cTax.toString())

     

     

    In the next request, use Property Expansion where the total tax is needed.

    For eg:

    <TotalTaxAmt currency="USD">${#TestCase#TOTAL_TAX}</TotalTaxAmt>

9 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    mkhan031486,

     

    Here is the quick demo to extract and the sum the taxes. Note that this demo uses fixed xml. But, you may want it to be dynamic for each test request.

     

    Here you what you need to do:

     

    For the first soap request, add script assertion as shown below:

     

    assert context.response, 'Response is empty or null'
    
    //extract the tax and sum all of them
    
    def cTax = new XmlSlurper().parseText(conext.response)
    .'**'
    .findAll{it.name() == 'CreditTax'}
    .collect{it.text() as double}
    .sum() log.info "Total tax : $cTax" //Store the above cTax value at test case level custom property TOTAL_TAX context.testCase.setPropertyValue('TOTAL_TAX', cTax.toString())

     

     

    In the next request, use Property Expansion where the total tax is needed.

    For eg:

    <TotalTaxAmt currency="USD">${#TestCase#TOTAL_TAX}</TotalTaxAmt>
    • mkhan031486's avatar
      mkhan031486
      Contributor

      nmrao Thanks alot for the reply. Also if I need to save the property on test suite or project level. Below is what I should use..?

      context.testRunner.testCase.testSuite.setPropertyValue("TOTAL_TAX",cTax.toString());
      
      context.testRunner.testCase.testSuite.project.setPropertyValue("TOTAL_TAX",cTax.toString());

       

      • nmrao's avatar
        nmrao
        Champion Level 3
        that should have worked, I believe.
    • mkhan031486's avatar
      mkhan031486
      Contributor

      nmrao so whats happening is that with the script you gave me.. if the taxes are $0.. then its saving the property as null instead of 0.0 .. what would I change in the script then in order get 0.0 instead of null??

      • nmrao's avatar
        nmrao
        Champion Level 3
        mkhan031486,
        It shouldn't be the case, I guess. Can you show the groovy script along with the respective response and screen shot of property value at test case level.