Insert POST twice and verify second response error status and body

the second part of test case failed at verification to verify if status code 400 and body returned correctly, how do I correct them?

test case
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

//send POST message to insert Product
WS.sendRequest(findTestObject(‘add’))

result = CustomKeywords.‘testlog.AddPro’()

print(result)

//insert same Product ID should not be allowed
WS.delay(5)
def response = WS.sendRequest(findTestObject(‘AddPro’))
WS.sendRequest(findTestObject(‘AddPro’))

WS.verifyResponseStatusCode(response, 400)

WS.verifyElementText(response, “duplicate ID”)

print response
return response

console log
2019-07-29 17:16:48.754 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/AddPro FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords.verifyElementText() is applicable for argument types: (com.kms.katalon.core.testobject.ResponseObject, java.lang.String) values: [400 273 bytes, duplicate ID]
Possible solutions: verifyElementText(com.kms.katalon.core.testobject.ResponseObject, java.lang.String, java.lang.String), verifyElementText(com.kms.katalon.core.testobject.ResponseObject, java.lang.String, java.lang.String, com.kms.katalon.core.model.FailureHandling)
at AddPro.run(AddPro:18)
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 TempTestCase1564391798581.run(TempTestCase1564391798581.groovy:21)

You miss a parameter

See also

what should be the locator? i did not put it as i only get response body “duplicate ID” when i inspect the API log.

See examples from the doc

did not get any good ideas from the brief intro/people’s comments.

Katalon Studio give you the locator when you mouse over the response. Here “5”.

in my case the body isn’t in json/xml format.
it is returned from codes.
Content-Type: text/plain; charset=utf-8

Share an example

which example?
it is just plain text response
duplicate ID

So try with Response body: Contains string
assertThat(response.getResponseText()).contains('duplicate ID')

you mean in test case? i tried earlier, got error. don’t think assert is good to use inside test case. if this to be added in object repo when sending the POST message, then i need advise how ot make it passed only on second POST, not first POST, as first POST is not response the duplicate ID, only second POST.

Reason:
groovy.lang.MissingMethodException: No signature of method: Script1564388610308.assertThat() is applicable for argument types: (java.lang.String) values: [duplicate ID]

“assertThat” works inside a TestCase, example :

import static org.assertj.core.api.Assertions.*

String a = "123"
assertThat(a).contains("1")

You can try println response.getResponseText().contains('duplicate ID') to see if you have the good value (true/false) then you can create a function to use this result

1 Like

ok the root cause was import not there , it is running without error now. is that katalon defect? i was trying to do shift ctrl + o but import not auto added.
import static org.assertj.core.api.Assertions.*

Because it’s static. We should write Assertions.assertThat('something') then press Ctrl + Shift + O to add imports