Forum Discussion

thatsthat's avatar
thatsthat
Contributor
2 years ago

Check previous test step for the existence of a value

Hopefully this is something that is covered off in the existing functionality of Ready API.

 

I have a value in the results of Test Step B which is the Amount 742.50

 

<ns2:Rates>
<ns2:RateTotals>
<ns2:BuyTotal>
<ns2:Currency>AUD</ns2:Currency>
<ns2:Amount>742.50</ns2:Amount>
</ns2:BuyTotal>

 

I simply want to check - or 'Assert' - if the value 742.50 appears in a previous Test Step (i.e. it 'exists' in the previous test step results). I know how to do Existence check (true or false) out of the box but the previous step contains a list of all kinds of amounts like 800.00 and 200.00 and 595.00 etc. 

8 Replies

  • The issue is KarelHusa the value can appear in a variety of positions .. there is not just 1 amount returned .. there are many .. so i am looking to match the current result 'anywhere' in the previous step's result.

    • KarelHusa's avatar
      KarelHusa
      Champion Level 3

      thatsthat,

      XPath assertions work exactly for such purpose.

       

      An XPath expression like //ns2:BuyTotal/ns2:Amount = 742.50 should work for you.

       

      ReadyAPI will help you declare namespaces and set up the XPath expression; please look at XPath Match docs for more detail.

       

  • Thanks for your suggestion KarelHusa but here is an example of just one of the elements I want to inspect. You can see the [1] in the response .. but there are (in this example I am looking at) 7 entries I need to check to see if a match exists. Xpath (without writing a script) can only point to 1 value .. unless I am mistaken?

     

    ${AddService#Response#declare namespace ns2='http://xyz.com/schema'; //ns2:AddServiceResponse[1]/ns2:Response[1]/ns2:ServiceDetail[1]/ns2:TotalCost[1]}

    • KarelHusa's avatar
      KarelHusa
      Champion Level 3

      thatsthat,

      you can use XPath predicates in your assertion, such as:

       

      //ns2:BuyTotal/ns2:Amount[text() = 742.50]

       

      will select the XML element amount with a value of 742.50.

       

      You can also check the count of nodes:

       

      count(//ns2:BuyTotal/ns2:Amount[text() = 742.50]) > 0

      .

      XPath gives you multiple options to select the node, see e.g. https://www.w3schools.com/xml/xpath_syntax.asp .

       

      • thatsthat's avatar
        thatsthat
        Contributor

        Thanks for your patience KarelHusa .. I will need to do some more reading it appears (thanks for the URL you sent) .. I attempted to implement your graceful solution with the count .. but no luck:

         

        count(//ns2:BuyTotal/ns2:Amount[text() = 742.50]) > 0

        But to no avail .. the below is what I am using to match a 'single' amount record that is being returned .. but I can't get it to 'count' up all the records and find if > 0 of them = 742.50

         

        ${FastSearchPackage#Response#declare namespace ns2='http://www.xyz.com/gateway/fire/schema'; //ns2:SearchResponse[1]/ns2:Response[1]/ns2:Suppliers[1]/ns2:Supplier[1]/ns2:Products[1]/ns2:Product[1]/ns2:Rates[1]/ns2:RateTotals[1]/ns2:BuyTotal[1]/ns2:Amount[1]}

         

        I get a 1 for 1 match .. i tried adding count blah blah but could not land the correct answer!!

         

         

        I did also try with an 'Count' xpatj expression

         

        declare namespace ns2='http://www.xyz.com/gateway/fire/schema';
        ${FastSearchPackage#Response#declare namespace ns2='http://www.xyz.com/gateway/fire/schema'; //ns2:SearchResponse[1]/ns2:Response[1]/ns2:Suppliers[1]/ns2:Supplier[1]/ns2:Products[1]/ns2:Product[1]/ns2:Rates[1]/ns2:RateTotals[1]/count(ns2:BuyTotal[1]/ns2:Amount[text()=742.50])>0}

         

        But no joy.