Hi Community members,
Have you ever needed to verify a status code that is different than the default 200 status code that is typically returned for API Calls, or have you needed to have more than one status code to be checked in a single call?
In today’s KShare article, we will show you how you can change the default status code verification as well as handling verification for multiple status codes.
1. Changing the Default Status Code
The default verification method can be found by opening one of the API objects from your Object Repository. From this page you will see several tabs at the bottom of the Query Parameters section:
Select the Verification tab, and you will see on line 12 where the default status code check is, from this line you can change it to any other status code (such as 202, 204, 206, etc.):
import static org.assertj.core.api.Assertions.*
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webservice.verification.WSResponseManager
import internal.GlobalVariable as GlobalVariable
RequestObject request = WSResponseManager.getInstance().getCurrentRequest()
ResponseObject response = WSResponseManager.getInstance().getCurrentResponse()
assert response.getStatusCode() == 200
WS.verifyElementPropertyValue(response, "age", 25)
WS.verifyElementPropertyValue(response, "username", "mimi")
WS.verifyElementPropertyValue(response, "password", "123456789")
WS.verifyElementPropertyValue(response, "gender", "MALE")
Adapting for Multiple Status Codes
When handling multiple status codes, an if / else statement can be added to check each status code individually, in this instance status codes 202, 204, and 206 are checked for a match before we check the default status code of 200:
import static org.assertj.core.api.Assertions.*
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webservice.verification.WSResponseManager
import internal.GlobalVariable as GlobalVariable
RequestObject request = WSResponseManager.getInstance().getCurrentRequest()
ResponseObject response = WSResponseManager.getInstance().getCurrentResponse()
int responseCode = response.getStatusCode()
if (responseCode == 202) {
assert response.getStatusCode() == 202
}
else if (responseCode == 204) {
assert response.getStatusCode() == 204
}
else if (responseCode == 206) {
assert response.getStatusCode() == 206
}
else assert response.getStatusCode() == 200
WS.verifyElementPropertyValue(response, "age", 25)
WS.verifyElementPropertyValue(response, "username", "mimi")
WS.verifyElementPropertyValue(response, "password", "123456789")
WS.verifyElementPropertyValue(response, "gender", "MALE")
If you find this article helpful, then don’t forget to give us a like or a heart
and share it with your colleagues or teammates to show your appreciation!
To see more KShare articles, simply navigate to our support tag!