Struggling to parameterize an assertion value in groovy script
Hi,
I have a script assertion (courtesy of @nmrao) which verifies an .json response's specific attribute is the same value for repeating groups within the response. It is as follows:
assert context.response, 'Request parameter is correct' def json = new groovy.json.JsonSlurper().parseText(context.response) //assert json.data.Name.every{1== it}
//OR the next line assert json.data.Name.every{context.expand('${REST Request#Name}') == it} //this is the same assertion as the previous line except I'm parameterizing the assertion's expected result value
I have a script (yet again, courtesy of nmrao) that dynamically builds a GET REST request using template/URI and query parameter values from properties set on the test case and a Properties step
//Script developed by Rao
//import decs import groovy.json.* import wslite.rest.* def serviceHost = context.expand ('${#Project#TestEndpoint}') //'https://whatevs.azurewebsites.net' //Below to handle template parameter, additional $ required before def getNamespaceDatasetPath = '''/api/1/${namespace}/${dataset}''' //define all template param values as shown below def binding = [namespace : context.expand( '${#TestCase#namespace}') , dataset : context.expand('${#TestCase#dataset}')] //def binding = [namespace : 'certification-nomenclature', dataset : 'certification-nomenclature'] def template = new groovy.text.SimpleTemplateEngine().createTemplate(getNamespaceDatasetPath) def queryParams = [:] //Get the properties of Property Test step context.testCase.testSteps["Properties"].properties.each { queryParams[it.key] = it.value.value } def client = new RESTClient(serviceHost) def response = client.get(path: template.make(binding) as String, accept: ContentType.JSON, query : queryParams ) assert response.statusCode == 200 log.info groovy.json.JsonOutput.prettyPrint(response.text) def object = new JsonSlurper().parseText(response.text) assert object.header.success == true
I wanted to add in the assertion (from the script assertion) into my groovy script test step - now if I copy in the script assertion making a couple of changes - the script assertion works in the groovyscript but ONLY if I use the hardcoded assertion line, rather than the parameterized line.
assert json.data.Name.every{1== it} //hardcoded line assert json.data.Name.every{context.expand('${REST Request#Name}') == it} //parameterized line
So my attempt at the complete groovy script is as follows:
//Script developed by Rao //import decs import groovy.json.* import wslite.rest.* def serviceHost = context.expand ('${#Project#TestEndpoint}') //'https://whatevs.azurewebsites.net' //Below to handle template parameter, additional $ required before def getNamespaceDatasetPath = '''/api/1/${namespace}/${dataset}''' //define all template param values as shown below def binding = [namespace : context.expand( '${#TestCase#namespace}') , dataset : context.expand('${#TestCase#dataset}')] //def binding = [namespace : 'certification-nomenclature', dataset : 'certification-nomenclature'] def template = new groovy.text.SimpleTemplateEngine().createTemplate(getNamespaceDatasetPath) def queryParams = [:] //Get the properties of Property Test step context.testCase.testSteps["Properties"].properties.each { queryParams[it.key] = it.value.value } def client = new RESTClient(serviceHost) def response = client.get(path: template.make(binding) as String, accept: ContentType.JSON, query : queryParams ) assert response.statusCode == 200 log.info groovy.json.JsonOutput.prettyPrint(response.text) def object = new JsonSlurper().parseText(response.text) assert object.header.success == true
assert response.text, 'Request parameter is correct' //This line was context.response rather than response.text in the script assertion //def json = new groovy.json.JsonSlurper().parseText(response.text) //this line isn't needed as I already pass the response to the object variable above assert object.data.CertificateType_Name.every{'Other certificates'== it} //groovy executes and passes
//OR assert object.data.CertificateType_Name.every{context.expand('${REST Request#CertificateType_Name}') == it} //groovy executes but this assertion fails
Essentially when I comment out the hardcoded line and uncomment the parameterized line - the assertion fails.
I have spent the last 2 days playing with this trying to get the parameterized line working. I have 2000 tests to create in ReadyAPI! and this is one of the final problems to my being able to parameterize a large percentage of my tests so I could just have 1 looping test case and just datasource all the requests.
Can anyone see what I'm missing? - I keep thinking I've spotted the problem, but I just don't know enough about groovy to spot it
Many thanks to all!
richie