Introduction
Hi there!
Do you know what makes Katalon Studio one of the most popular tools for automated testing? Katalon offers dual scripting interfaces for users with different programming skills - a simple user interface and a Script view. Creating a web service object using scripts is most preferred by users proficient in programming and Katalon but is easy even for beginners.
The article Introduction to Web Service Objects in Katalon shows how to create a Web Service Object using the user interface.
Creating a Web Service Object
Katalon Studio allows the creation of web service objects for REST and SOAP services. They can be manipulated even within test cases in the Script View. Here are some examples.
REST GET Object
Step 1: Create a new Test case and switch to the Script View.
Step 2: Add the following code in the Editor.
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.TestObjectProperty
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import java.util.*
// Setting HTTP header properties
List<TestObjectProperty> httpHeaderProperties = new ArrayList<>()
httpHeaderProperties.add(new TestObjectProperty("Content-Type", ConditionType.EQUALS, 'application/json'))
httpHeaderProperties.add(new TestObjectProperty("Authorization", ConditionType.EQUALS, 'None'))
RequestObject request = new RequestObject()
request.setServiceType('REST')
request.setHttpHeaderProperties(httpHeaderProperties)
request.setRestUrl('https://reqres.in/api/users?page=2')
request.setRestRequestMethod('GET')
// Sending request
response = WS.sendRequest(request)
// Verifying response
WS.verifyResponseStatusCode(response, 200)
The above code creates a RESTful web service object. All HTTP header properties are stored in a list. A request is created using the RequestObject class with the URL and request method values. The request is sent, and the response is verified.
Step 3: Click on the Run icon to execute the script.
REST POST Object
For the POST method in REST, we require a request body. The request body is stored in a HashMap object. It is n converted into a JSON Object and then to a String. The resultant String is stored as a HttpTextBodyContent object for the POST request.
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.HttpBodyContent
import com.kms.katalon.core.testobject.impl.HttpTextBodyContent
import com.kms.katalon.core.testobject.TestObjectProperty
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import java.util.*
HashMap<String, Object> requestBody = new HashMap<>();
requestBody.put('username', 'Marcus')
requestBody.put('job', 'Team Lead')
// Map to JSON
def jsonrequestBody = new groovy.json.JsonBuilder()
jsonrequestBody fields: requestBody
// JSON to String
String content = jsonrequestBody.toString()
// Create body content property
HttpTextBodyContent httpBody = new HttpTextBodyContent(content);
// HTTP Header
List<TestObjectProperty> httpHeader = new ArrayList<>()
httpHeader.add(new TestObjectProperty("Content-Type", ConditionType.EQUALS, 'application/json'))
// Request
RequestObject reqOBJ = new RequestObject()
reqOBJ.setBodyContent(httpBody)
reqOBJ.setServiceType('REST')
reqOBJ.setRestRequestMethod('POST')
reqOBJ.setHttpHeaderProperties(httpHeader)
reqOBJ.setRestUrl('https://reqres.in/api/users')
resp = WS.sendRequest(reqOBJ)
// Verify Response Status
WS.verifyResponseStatusCode(resp, 201)