Ask a Question

Groovy Script assertion always passes even though the values don't match the response

raygoel
Occasional Contributor

Groovy Script assertion always passes even though the values don't match the response

Hi,

 

I am writing assertions for an API which has the following JSON response - 

 

 {
      "id" : "dd72080a-be22-4e60-a0e4-58f35d4144e5",
      "name" : "I_Plate3",
      "containerType" : null,
      "isInventory" : true,
      "isProcess" : false,
      "inventoryTemplateName" : "I_Plate3",
      "process" : null,
      "lid" : null
   },
   {
      "id" : "1f2e0738-358e-478b-abf9-d4b79383d587",
      "name" : "Plate1",
      "containerType" : "SLAS/SBS Standard",
      "isInventory" : false,
      "isProcess" : true,
      "inventoryTemplateName" : null,
      "process" : {
         "id" : "a3b5e431-dc18-43af-b204-ae877c7c72b9",
         "name" : "UsingProcessPlates"
      },
      "lid" : null
   },

 

 

In the response I iterate over isProcess and if it is true, I put an assertion on the process name and lid. Here is the script I am using for this - 

 

 

import groovy.json.JsonSlurper

def response = messageExchange.response.responseContent

def slurpedResponse = new JsonSlurper().parseText(response)
if ( slurpedResponse*.isProcess) {
	switch (slurpedResponse.name) {
		case "Plate1" :
			assert slurpedResponse.process.name == "UsingProce"
			assert slurpedResponse.lid == null
		}
		case "Plate2" : 
			assert slurpedResponse.process.name == "UsingProcessPlates"
			assert slurpedResponse.lid == "Plate2Lid"
	}
}

 

The issue is that the assertion below passes when it should be failing since the name of the process does not match with what it is in the JSON response. 

 

assert slurpedResponse.process.name == "UsingProce"

 

I have tried it with assert slurpedResponse.process.name.toString() as well but the assertion still passes when it should fail. 

Can someone please help me with the error in my script? 

4 REPLIES 4
MConneely
Staff

A couple of things to mention.

ReadyAPI offers out of the box assertions, so that you don't necessarily have to script assertions. There may be one available that might assist you

https://support.smartbear.com/readyapi/docs/testing/assertions/reference/index.html

 

On the script itself, there doesn't seem to be any brackets ( [ ] ) at the start/end of your JSON. When added in, and I tried some of your script, I logged the following (debug purposes). 

slurpedResponse*.isProcess = [false, true]
slurpedResponse[0].isProcess = false
slurpedResponse[1].isProcess = true

 

Have you thought about iterating through your array of responses to check your values as a test? its useful to use the log.info() to write script logs to console for checking values 

nmrao
Champion Level 3

@raygoel 

Couple of things

1. Response has array. So it should be looped thru and and assert each.

2. There should be default in the switch.



Regards,
Rao.
raygoel
Occasional Contributor

Thanks for the response! 

So, I believe the right way to approach this would be using a for loop? 

 

I used the * operator for JasonSlurper thinking it will iterate over all the elements in the array but guess it works differently from a for loop. 

raygoel
Occasional Contributor

I fixed the assertions using a for loop 

 

import groovy.json.JsonSlurper

def response = messageExchange.response.responseContent

def slurpedResponse = new JsonSlurper().parseText(response)

def count = slurpedResponse.Size


for(i=0; i< count ; i++) {

	if(slurpedResponse[i].isProcess){

		if(slurpedResponse[i].name == "Plate1") {
			assert slurpedResponse[i].process.name == "UsingProcessPlates"
			assert slurpedResponse[i].lid == null
		}

		if(slurpedResponse[i].name == "Plate2") {
			assert slurpedResponse[i].process.name == "UsingProcessPlates"
			assert slurpedResponse[i].lid == "Plate2Lid"
		}

		if(slurpedResponse[i].name == "Plate2Lid") {
			assert slurpedResponse[i].process.name == "UsingProcessPlates"
			assert slurpedResponse[i].lid == null
		}

		if(slurpedResponse[i].name == "PlateSet") {
			assert slurpedResponse[i].process.name == "UsingProcessPlates"
			assert slurpedResponse[i].lid == "PlateSetLid"
		}

		if(slurpedResponse[i].name == "PlateSetLid") {
			assert slurpedResponse[i].process.name == "UsingProcessPlates"
			assert slurpedResponse[i].lid == null
		}

		if(slurpedResponse[i].process.name == "Plate_StallRun") {
			assert slurpedResponse[i].process.name == "StallRun"
			assert slurpedResponse[i].lid == null
		}
	}
	else {
		log.info("The container is not used in any process")

	}
}

 

Thanks for the idea! 

 

I would like to know if there is documentation about different kind of assertions that ReadyAPI offers through scripting. I am okay writing code and would like to explore the assert library more in depth. 


Thanks,

Ray

cancel
Showing results for 
Search instead for 
Did you mean: