Verify json response execution error


#1

i am following the guideline posted at

but test case failed during execution
Reason:
groovy.lang.MissingPropertyException: No such property: parsedJson for class: Script1567646760099
at json.run(json:31)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1567647263665.run(TempTestCase1567647263665.groovy:21)

2019-09-05 09:34:27.003 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/json

================
i have groovy as follow, i paste the expected response in the script. basically the response is identical to request.

import groovy.json.JsonSlurper

String jsonString = ‘’’{
“itemId”: “test123”,
“codes”: [
{
“code”: “C1”,
}
],
“name”: “item one”,
“status”: “active”,
“created”: “2019-09-04T05:47:30.944Z”,
“itemType”: “A1”
}’’’
JsonSlurper slurper = new JsonSlurper()
Map parsedJson = slurper.parseText(jsonString)

=======
then the test case part 2 to verify response, after part 1 sent the request successfully. part 1 had been sent successfully in this case, the problematic area is the part 2 verify json response.

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable

import com.kms.katalon.core.util.KeywordUtil

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import static org.assertj.core.api.Assertions.*

/part 1. /send the POST message
WS.sendRequest(findTestObject(‘x/DS-post-x’))

result = CustomKeywords.‘json.API.Addx’()
print(result)

//part 2. to check the json response
String getSelectedKey = parsedJson.itemId.name

if(getSelectedKey == null) {
KeywordUtil.markFailed(“Key is not present”)
}

if (!isKeyPresent) {
KeywordUtil.markFailed(“Key is not present”)
}


#2

Let me assume that the following is a entire test case script you wrote (from the very 1st line to the very last line).

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable

import com.kms.katalon.core.util.KeywordUtil

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import static org.assertj.core.api.Assertions.*

//part 1. send the POST message
WS.sendRequest(findTestObject('Drug Service/DS-post-drugformulary'))

result = CustomKeywords.'json.API.AddDrug'()
print(result)

//part 2. to check the json response
String getSelectedKey = parsedJson.itemId.name

if(getSelectedKey == null) {
    KeywordUtil.markFailed("Key is not present")
}

if (!isKeyPresent) {
    KeywordUtil.markFailed("Key is not present")
}

In this framgment, the variable named parsedJson IS NOT defined at all. Therefore surely you will encounter a error with message “groovy.lang.MissingPropertyException: No such property: parsedJson for class: …”


#4

i thought it is in the groovy file, not?
import groovy.json.JsonSlurper

String jsonString = ‘’’{
“itemId”: “test123”,
“codes”: [
{
“code”: “C1”,
}
],
“name”: “item one”,
“status”: “active”,
“created”: “2019-09-04T05:47:30.944Z”,
“itemType”: “A1”
}’’’
JsonSlurper slurper = new JsonSlurper()
Map parsedJson = slurper.parseText(jsonString)


#6

yes 2 groovy, as i separated 1 groovy for test case part 1, another groovy for test case part 2. the groovy content i posted is for part 2 only.


#8

And please use “Block Code Formatting” feature described as follows:


#9

what is not clear?
let me know which one you needed.
i have posted the groovy and test case. part 1 has no issue, please do not look at it, thus groovy for part 1 i would not posting here.