Forum Discussion

smadji's avatar
smadji
Contributor
9 years ago
Solved

generalizing an expression - beginner user needs help :)

I have a groovy script that I use as an assertion

The script is working fine with a direct expression generated by the 'get data' tool

I am trying to cut off the part that makes the expression look in a specific requst so I can clone this assertion to other request without changing anything.

 

The script is a defining a variable that stores a property I create earlier on the test and making it available for the script

Followed by commented-out definition of a variable as it was generated by the get data tool

Followed by a definition of the same second variable in a way that should work (in my opinion) but doesnt, what am I doing wrong?

After the defining of the variables I convert them to int and compare them in the rest of the script...

 

The script I wrote:

 

def solr_total_agents = context.expand( '${#TestSuite#solr_total_agents}' )

//def request_total = context.expand( '${GET - getMethod - get agent details#ResponseAsXml; //ns1:Response[1]/ns1:totalCount[1]}' )

def request_total = context.expand('$.totalCount')

int solr_total_agentsInt = Integer.parseInt(solr_total_agents)
//log.info item1Int
int request_totalInt = Integer.parseInt(request_total)
//log.info item2Int

//for asc use >= and for desc use <=
if( solr_total_agentsInt > request_totalInt){
testRunner.fail("request returns all items in the db")
}

 

please help, I have been looking at this for 2 days already and need to implement this on 100s of test steps...

  • OK, got it. Well, getting test step names i pretty simple, however you will need to supply the 0-based index of where the desired step is meaning that if you want to generalize you have to put that particular step at the same index everywhere (btw, test step name is the same as the request name). Here goes:

    def testStepName = context.testCase.getTestStepAt(n).getName()

     

    where n is the 0-based index. Now you can simply use testStepName here:

    def request_total = context.expand('${' + testStepName + '#response#$[n].totalCount}')

     

    Notice that since testStepName is a variable you have to concat the strings, hence the ' and the + surrounding it.

5 Replies

  • If I understand you correctly, this is the failing line:

    def request_total = context.expand('$.totalCount')

     

    Yes?

     

    Well, the first error is missing {}, like this:

     

    def request_total = context.expand('${totalCount}')

     

    Second, is totalCount the property of a response? In other words, do you send a request and get a response that contains totalCount and a value for it? In that case the proper syntax would be

     

    def request_total = context.expand('${"request name"#response#$[n].totalCount}')

     

    where "request name" would be the name of the request and [n] is the node number (in brackets) in the response where the totalCount value is

    • smadji's avatar
      smadji
      Contributor

      What I am trying to do is avoid using the request name.

      The assertion is not a groovy test step, it is a groovy assertion (script assertion) so it is happening from within the request.

      I want to find a syntax that will say:

      Look at the totalCount within the current request (and compare it to a value held by property).

       

      The reason why I want to do it that way is becaue I would like to use this assertion on all my requests. This is to assure that non of my requests are returning the entire scope of results (which would tell me that something went wrong somewhere).

       

      It is easy to clone an assertion and push it to all the requests, but if I have to then manually open each one and point it to the specific request it defeats the purpose of the assertion cloning. And is what I am trying to avoid.

      • Armageddonsoft's avatar
        Armageddonsoft
        Contributor

        OK, got it. Well, getting test step names i pretty simple, however you will need to supply the 0-based index of where the desired step is meaning that if you want to generalize you have to put that particular step at the same index everywhere (btw, test step name is the same as the request name). Here goes:

        def testStepName = context.testCase.getTestStepAt(n).getName()

         

        where n is the 0-based index. Now you can simply use testStepName here:

        def request_total = context.expand('${' + testStepName + '#response#$[n].totalCount}')

         

        Notice that since testStepName is a variable you have to concat the strings, hence the ' and the + surrounding it.