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