Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction👨‍✈‍
2.
What is Katalon❓
3.
Handle Response Messages in Katalon✅
3.1.
ResponseObject💡
3.2.
Element locator🔎
4.
Frequently Asked Questions
4.1.
What is the Katalon platform?
4.2.
What programming languages are used in the Katalon platform?
4.3.
What is XHR?
4.4.
What is JSON?
4.5.
What is XML?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Handle Response Messages in Katalon

Author Sanchit Kumar
0 upvote

Introduction👨‍✈‍

Katalon is an automation testing software tool developed by Katalon, Inc. It helps to create and reuse automated test scripts without coding. We often use Katalon for API testing, perform different requests, and get response messages.

handle response messages in katalon

But one might question how to handle these response messages in Katalon. The answer to this question is using ResponseObject and the element locator concept. 

Alright! Before we learn about handling response messages in Katalon, let us first see what Katalon is.💫

What is Katalon❓

katalon

Katalon is an automation testing software tool developed by Katalon, Inc. The software is built on top of the open-source automation frameworks Appium and Selenium with a specialised IDE interface to test desktop, mobile, web and APIs.

Handle Response Messages in Katalon✅

When we handle response messages or web service results in Katalon, most keywords ask for a ResponseObject or an element locator.

Okay! Let us learn about ResponseObject and the element locator.

ResponseObject💡

ResponseObject - is a convenient class to wrap the HTTP content returned from a web service call. It is the return from the keyword WSBuiltInKeywords.sendRequest.

SendRequest  It is the method that sends an HTTP Request to the web server.

📌Syntax - sendRequest(RequestObject request).

📌Return type - static ResponseObject.

WSBuiltInKeywords ➼ Is the class in com.kms.katalon.core.webservice.keyword package.


Following are some useful functions to handle the result.

1️⃣getContentType() - To get the content-type header of a response/request.

📌Return type - public String.

📌Data types - text/xml, application/json, application/soap+xml, application/xml, etc.

2️⃣getContentLength() - To get the content-length header of a response/request.

📌Return type - public long.


Following is the XHR(XMLHttpRequest) with highlighted "content-length" and "content-type" under "Response Headers".
So when we use getContentType() and getContentLength() functions, we will get these "content-type" and "content-length" data respectively.

response header

Element locator🔎

Element locator is the concept utilised by the Katalon Studio to explore hierarchical data structures (for example, JSON or XML) to look for the expected data. Following are examples of using an element locator to handle response messages in Katalon on JSON/XML data.
 

⚙️JSON data typically have a root node (a parent element to wrap all content) consisting of many child nodes. Element locator syntax begins with the element name, the child element and its index (if it has children nodes, the index is zero-based numbering), name of the property we want to get its value. A dot character separates those parts of the element locator.


Example🔗: For the following JSON response, we will use the element locator concept. This example shows how to locate the first_name from the 0th and 1st index of data(element name) and verify it.

json


•Web service test script[Verification].

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 groovy.json.JsonSlurper
import internal.GlobalVariable as GlobalVariable

RequestObject request = WSResponseManager.getInstance().getCurrentRequest()
ResponseObject ResObj = WSResponseManager.getInstance().getCurrentResponse()
WS.verifyElementPropertyValue(ResObj, 'data[0].first_name', "Michael")
WS.verifyElementPropertyValue(ResObj, 'data[1].first_name', "Lindsay")


Element locator - To locate the first_name from the 0th and 1st index of data.

element locator


Verification log after we test the request and verify.

log


Note ⚠️- If, in the case, returned data doesn't have a root node, prefix the element locator with "[the index number]", where the index is zero-based numbering. Following is the example of JSON data without a root node.

without root

So, for the above JSON data. We will write the following to locate the element "name" at the 0th index.

[0].name


⚙️XML data are similar in the way and structure we define element locator. Our expected info may come from the attribute of the XML tag. You can use keywords for property(for example, verifyElementPropertyValue) and handling text (for example, verifyElementText).


Example🔗: For the following XML response, we will use the element locator concept to locate the highlighted text(54) and verify it.

xml


•Web service test script[Verification].

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 groovy.json.JsonSlurper
import internal.GlobalVariable as GlobalVariable

RequestObject request = WSResponseManager.getInstance().getCurrentRequest()
ResponseObject ResObj = WSResponseManager.getInstance().getCurrentResponse()

WS.verifyElementText(ResObj, 'MultiplyResponse.MultiplyResult', '54')


Element locator - To locate 54(which is text).

element locator


Verification log after we test the request and verify.

log


Alright! Now we hope you understand how to handle response messages in Katalon.

Frequently Asked Questions

What is the Katalon platform?

The Katalon platform is the automation testing software tool developed by Katalon, Inc.

What programming languages are used in the Katalon platform?

Java and groovy are the main programming languages used in the Katalon platform.

What is XHR?

XHR(XMLHttpRequest) is a javascript API to create asynchronous javascript and XML(AJAX) requests. Its methods help in sending network requests between a server and the browser.

What is JSON?

It stands for JavaScript Object Notation(JSON). It is a lightweight format for transporting and storing data. JSON is often used when a server sends data to a web page.

What is XML?

It stands for eXtensible Markup Language(XML). It is also a markup language similar to HTML. XML was designed to transport and store data.

Conclusion

In this article, we discussed handling response messages in Katalon. We learnt about ResponseObject and the element locator to handle response messages in Katalon.

We hope this blog on the handle response messages in Katalon was helpful. You can also refer to other similar articles. 


Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Learning Ninja! 🥷

Live masterclass