Forum Discussion

bmcim123's avatar
bmcim123
Occasional Contributor
8 years ago

Autocomplete API :-Check a string parameter in the request matches the string value in the response?

I am using a REST API to check for autocomplete

 

Example Test Case:-

 

Request

 

  1. I retrieve a list of Document Numbers
  2. I retrieve a list of Document Numbers for AutoComplete (parameter value I pass in is '123')

 

Response

 

Document Numbers containing 123 are returned.

 

{
"results": [
{"documentNumber": "112369353213670593"},
{"documentNumber": "120212364882313025"},
{"documentNumber": "123055742821186593"},
{"documentNumber": "123134986738657857"},
{"documentNumber": "123207962820539585"},
{"documentNumber": "1234567"},
{"documentNumber": "123456789"},
{"documentNumber": "123597020827608033"},
{"documentNumber": "123639294264534913"}

 

 

Question, How do I assert the response FOR EACH NODE RETURNED AS ABOVE contains '123' OR better still if I pass in dynamic autocomplete search in the request each time and returning a dynamic number of document numbers/nodes in the RESPONSE?

 

Can I please ask to add comments with your proposed solution as this will aid my own learning.

 

Thanks for your time.

 

 

  • Okay, as long as your REST test request contains a response, I've made some adjustments based on the new sample response you provided.

     

    As indicated in the comment of the script, please ensure "Retrieve a list of Document Numbers for autocomplete example 1" completely matches the name of the test step whose response we want, and that it's in the same test case as this groovy script. Otherwise, use the Get-Data right-click option to redefine response to the appropriate test step.

     

    Here's an updated groovy script:

     

     

    package json
    import com.eviware.soapui.support.XmlHolder
    import groovy.json.*
    
    // get the response of the JSON test step / request. 
    // "Retrieve a list of Document Numbers for autocomplete example 1" should be the name of your test step, 
    // and it should be in the same test case as the groovy script.
    
    def response = context.expand( '${Retrieve a list of Document Numbers for autocomplete example 1#Response}' );
    log.info response;
    
    
    // set up a json slurper to parse the response string
    def autocompletenodesreturned = new JsonSlurper().parseText(response).results
    log.info autocompletenodesreturned;
    
    // set N to the number of elements in the parsed response
    def n = autocompletenodesreturned.size()
    log.info(n);
    for (int i=0; i < n; i++)
    {
    	log.info(autocompletenodesreturned[i]);
    	assert(autocompletenodesreturned[i].documentNumber.contains("123"));
    }

     

  • groovyguy's avatar
    groovyguy
    Community Hero

    Here's a script that takes your response and will output the document number and then assert that it contains "123". It'll need to be adjusted accordingly to the name of your REST test request step name.

     

    package json
    import com.eviware.soapui.support.XmlHolder
    import groovy.json.*
    
    // get the response of the JSON test step / request. 
    def response = context.expand( '${REST Request#Response}' ).toString();
    
    // set up a json slurper to parse the response string
    def r = new JsonSlurper().parseText(response)
    
    // set N to the number of elements in the parsed response
    def n = r.size()
    for (int i=0; i < n; i++)
    {
    	log.info(r[i].documentNumber);
    	assert(r[i].documentNumber.contains("123"));
    }

     

    Let me know how that works. 

    • bmcim123's avatar
      bmcim123
      Occasional Contributor

      Hi

       

      I will try the script this evening and get back to you with an update.

       

      Your time and effort in helping me is much appreciated.

      • bmcim123's avatar
        bmcim123
        Occasional Contributor

        Here is my solution as provided by you:-

         

        package json
        import com.eviware.soapui.support.XmlHolder
        import groovy.json.*

        // get the response of the JSON test step / request.
        def response = context.expand( '${#[AutoComplete#API: Retrieve a list of document numbers for autocomplete_valid (24554)#Retrieve a list of Document Numbers for autocomplete example 1]#Response}' ).toString();


        // set up a json slurper to parse the response string
        def r = new JsonSlurper().parseText(response)

        // set N to the number of elements in the parsed response
        def n = r.size()
        for (int i=0; i < n; i++)
        {
        log.info(r[i].documentNumber);
        assert(r[i].documentNumber.contains("123"));
        }

         

         

        I am receiving the following error:-

        • java.lang.String cannot be cast to java.lang.Integer

         

        Can you help?

         

        Thanks

         

         

  • nmrao's avatar
    nmrao
    Champion Level 3

    bmcim123,

     

    Below is what I have learned over time.

     

    Script used to have multiple asserts.

    When the first assertion fails, the execution stops there i.e., the remaining asserts aren't even checked.

    Used to file the defect for the first failure and wait the fix to come.

    Once the fix is available, then re-run the test and happen to found next assertion failure i.e., there could be potential defects in the remaining / rest of the script . And those are not run yet as execution stop on first assert failure.

     

    This lead to increase test / development time.

     

    What has to be done differently?

      List all the failures once

     

    For example, to apply this to your case, there are two  incorrect document numbers in result.

    Below Script Assertion, to the same REST request test step (not additional groovy script test step which is obsolete), does find all unwanted document numbers once in the failure message.

     

    Here we are finding opposite i.e, document numbers does not contain expected string 123 and that their count should be zero.

     

     

    def parsedJson = new groovy.json.JsonSlurper().parseText(context.response)
    def unwantedDocuments = parsedJson.results.documentNumber.findAll{ !it.contains('123') }
    //Unwanted document numbers should be zero, fail otherwise and list numbers assert 0 == unwantedDocuments.size(), "There are invalid document numbers found : ${unwantedDocuments.join(', ')}"

     

    • nmrao's avatar
      nmrao
      Champion Level 3

      Here is more better version with above mentioned multiple failure (changed response, marked in red coloured text, as if you get incorrect response data and list failures)

       

      def json = '''{
      "results": [
      {"documentNumber": "112069353213670593"},
      {"documentNumber": "120212364882313025"},
      {"documentNumber": "120055742821186593"},
      {"documentNumber": "123134986738657857"},
      {"documentNumber": "123207962820539585"},
      {"documentNumber": "1234567"},
      {"documentNumber": "123456789"},
      {"documentNumber": "123597020827608033"},
      {"documentNumber": "123639294264534913"}
      ]
      }'''

       

      Added getFilteredDocuments closure(can be used as a method/function), so that it can be re-used with different condition say only matching documents, not matching documents, and all documents with a simple condition as below:

       

       

       

      You can find full script here 

      Also you can run the same online from here