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   I retrieve a list of Document Numbers I retrieve a list of Document Numbers for AutoComplete (parameter value...
  • groovyguy's avatar
    groovyguy
    8 years ago

    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"));
    }