Calculate sum of several columns in an XML databank

StaticAnalysisStaticAnalysis Posts: 8

I'm extracting and storing values (let us say List Price of several products). I can see them saved as shown in console:

Scenario: GetProductPriceOfCartItems
Test 1: GetProductPricing(ProductPricingRequest) - success
set Test 2: ListPrice1=45.00
set Test 2: ListPrice2=65.00
set Test 2: ListPrice3=55.00
set Test 2: ListPrice4=55.00
set Test 2: ListPrice5=45.00
set Test 2: ListPrice6=50.00
set Test 2: ListPrice7=50.00
set Test 2: ListPrice8=7.98
set Test 2: ListPrice9=78.28
set Test 2: ListPrice10=102.64
set Test 2: ListPrice11=null
set Test 2: ListPrice12=null
set Test 2: ListPrice13=null

Now I need to calculate the total price of items in the cart. what is the best way to achieve this via a Jython script, especially when the cart may have 1 or 2 or 3 etc. and upto 13 items?

Thanks in advance.


  • jakubiakjakubiak Posts: 519 admin

    The simplest way is not to use a Jython script but instead to construct an XPath that uses the XPath sum() function. First you need to construct an XPath that selects all nodes that have the values you want. Next, you pass that XPath to the sum() function. Consider the following example:


    For this example you could create the XPath "sum(/xml/foo/text()))" which will return the value "5.15". You could then use this XPath in an XML Data Bank or XML Assertor tool.

  • StaticAnalysisStaticAnalysis Posts: 8

    @jakubiak Thank you. this perfect, but the problem I'm running into is I cannot add or extract any other attribute/element to DataBank that is not in the response object. Is there a way to add one or more elements to Databank that are not part of the Test's response?

    What I mean to say in this case, if I had 10 rows of price in the response and I need all 10 + 1 more for storing the SUM, how do I go about it? Please advise.

  • jakubiakjakubiak Posts: 519 admin

    In this case you would then need a script to calculate the sum of the data from the response plus the additional data from outside the response. You can use the context.getValue("Generated Data Source", "") API call to get the value stored from the data bank.

  • StaticAnalysisStaticAnalysis Posts: 8

    @jakubiak Thanks again. It looks like I was not too far off with the need for a script in this situation. While I have your attention, do you happen to have any code snippets examples I could use?

  • jakubiakjakubiak Posts: 519 admin

    Here is a sample groovy script. I didn't run it so it may have minor errors but it should have the main idea:

    import com.parasoft.api.*;
    public void calculate(Object input, ScriptingContext context) {
       String extractedValue = context.getValue("Generated Data Source", "<data bank column name>");
       double extraValue = 1.00;
       double sum = Double.parseDouble(extractedValue) + extraValue;
Sign In or Register to comment.