Forum Discussion

llarocca's avatar
llarocca
Occasional Contributor
9 years ago

I need to be able to put a Range into my Assertions

In order to be able to change Environments easily, I need to be able to put a Range into my Assertions for a TotalResults field.  I have found references on the board to the ability to do this but I can't get the syntax quite right and there doesn't seem to be an example for exactly what I need.

 

Here is the tag for the aggregate I am targeting:

 

<ItemPage>
<FirstResult>1</FirstResult>
<ResultsReturned>19</ResultsReturned>
<TotalResults>190</TotalResults>
<Items>

 

Here is the Assertion I currently have without a Range:

 

XPath Expression -

declare namespace ns1='http://ws.ussco.com/eCatalog/catalog/1';
//ns1:getSearchResponse[1]/ns1:searchResponse[1]/ns1:ItemPage[1]/ns1:TotalResults[1]/text()

 

Expected Result -

${DataSource RedPen SmallBS#totalResults}

 

DataSource RedPen SmallBS#totalResults

190

 

What I would like to see is a + or - tolerance of 5%.  I have tried things like two Assertions ">= 181" and "<=200", or "greater than or equal to 181" and "less than or equal to 200".  I have tried one Assertion "Between 181 and 200", and so on.

 

Can someone help me with the exact syntax?

 

Thanks,

 

Laura 

 

Endpoint:

https://ws.ussco.com/eCatalog/catalog/001/sync

  • That got me close enough.  Here is what I did.  Two assertions:

     

    Lower End of the Range:

    XPath Expression:

    declare namespace ns1='http://ws.ussco.com/eCatalog/catalog/1';
    ${DataSource RedPen SmallBS#totalResultsLowerRange} <= //ns1:getSearchResponse[1]/ns1:searchResponse[1]/ns1:ItemPage[1]/ns1:TotalResults[1]

     

    Expected Result:

    true

     

    Upper End of the Range:

    declare namespace ns1='http://ws.ussco.com/eCatalog/catalog/1';
    ${DataSource RedPen SmallBS#totalResultsUpperRange} >= //ns1:getSearchResponse[1]/ns1:searchResponse[1]/ns1:ItemPage[1]/ns1:TotalResults[1]

     

    Expected Result:

    true

     

    And then all the Requests in my data set don't have to match exactly.

     

    Thanks for giving me both methods!

     

    Laura

     

     

    • llarocca's avatar
      llarocca
      Occasional Contributor

      Thanks for the quick reply.  I have tried the script and it does work with my Requests.

       

      A follow up question.  In my automation project, I am using a Data Driven Approach whenever possible.  A Script Assertion doesn't seem to be able to call back to the Data Source for the script (use the string ${DataSource RedPen SmallBS#totalResults} to go to a Grid to pick up the Groovy Script you created, although it works if I put that script directly into the Groovy Script Assertion.)

       

      So does this mean I can either use a Range Groovy Script Assertion for Request that will come back within a similar Range or use a Data Source for Exact TotalResults to store for Assertions, but I would not be able to use a Data Source to store a Groovy Script for a Range of TotalResults?  (Let me know if that question doesn't make sense.)

       

      Laura

    • llarocca's avatar
      llarocca
      Occasional Contributor

      That got me close enough.  Here is what I did.  Two assertions:

       

      Lower End of the Range:

      XPath Expression:

      declare namespace ns1='http://ws.ussco.com/eCatalog/catalog/1';
      ${DataSource RedPen SmallBS#totalResultsLowerRange} <= //ns1:getSearchResponse[1]/ns1:searchResponse[1]/ns1:ItemPage[1]/ns1:TotalResults[1]

       

      Expected Result:

      true

       

      Upper End of the Range:

      declare namespace ns1='http://ws.ussco.com/eCatalog/catalog/1';
      ${DataSource RedPen SmallBS#totalResultsUpperRange} >= //ns1:getSearchResponse[1]/ns1:searchResponse[1]/ns1:ItemPage[1]/ns1:TotalResults[1]

       

      Expected Result:

      true

       

      And then all the Requests in my data set don't have to match exactly.

       

      Thanks for giving me both methods!

       

      Laura

       

       

      • llarocca's avatar
        llarocca
        Occasional Contributor

        This solution worked for the Web Services version of the call but now moving to the JSON version, the same method isn't quite the same.

         

        JSONPath Expression

        $.searchResult.products

         

        Expected Result

        ${SBS DataSource#count}

         

        If I try -

        ${SBS DataSource#countLowerRange} <= $.searchResult.products is true

         

        it throws the following error -

        Assertion failed. Path [${SBS DataSource#count} = $.searchResult.products]:AssertionException:Assertion failed. Path [${SBS DataSource#count} = S.searchResult.products]: ClassCastException:java.lang.String cannot be cast to net.sf.json.JSON.

         

        Is there a way to do it in the JSON as well?

         

        Thanks,

         

        Laura