Useful Groovy Snippets for Katalon Studio

In the article you can find some useful snippets for Katalon Studio.

Katalon Studio allows expert users to programmatically write automation tests in the Groovy Script view of test cases. To learn how to create a Katalion Studio test project, follow the link.

Check If the String Contains the Specified Symbols

The snippet demonstrates how to check if a specified string contains symbols from an array.

In the snippet the text is extracted from a web page and checked if it contains any symbols of the array letters.

def AboutTextContent = WebUI.getText(findTestObject('AboutUsPage/PageTextContent')

def letters = ['a', 'e', 'u', 'o', 'i', 'y', 'A', 'B', 'U', 'O', 'I', 'Y']

def valid = AboutTextContent.findAll{ a ->
    letters.any { a.contains(it) }
}

assert valid.size() > 0

Write Information to Verification Log

The secion explains how to write an information in the verification log.

Add to <import> block:

import com.kms.katalon.core.logging.KeywordLogger as KeywordLogger

Initialize the log variable:

KeywordLogger log = new KeywordLogger()

Write info in the logger:

log.println("sdsdOptimizationId -> "+GlobalVariable.someVariable)

Check Response Property

This section demonstrates how to validate a web request response based on the status code and value of a property.

Add to <import> block the code below:

import groovy.json.JsonSlurper

Initialize variable:

def jsonSlurper = new JsonSlurper()

Verify if the value of the response property is equal to the specified value:

WS.verifyResponseStatusCode(response, 200)

WS.verifyElementPropertyValue(response, 'results[0][1]', '301 MARKET SHELL')

Examine the whole verification script:

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

response = WS.sendRequest(findTestObject('AddressBookContact/Address Book Contacts Filter'))

def jsonSlurper = new JsonSlurper()

WS.verifyResponseStatusCode(response, 200)

WS.verifyElementPropertyValue(response, 'results[0][1]', '301 MARKET SHELL')

Read Response Variable

The section demonstrates how to get a variable from a web request’s response.

Add to the <import> block the code below:

import groovy.json.JsonSlurper

Initialize the object jsonSlurper:

def jsonSlurper = new JsonSlurper()

Extract the value from the response property book_id:

GlobalVariable.BookId = jsonSlurper.parseText(response.responseText).get("book_id")

Read the whole verification script:

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
import com.kms.katalon.core.logging.KeywordLogger as KeywordLogger

KeywordLogger log = new KeywordLogger()

RequestObject request = WSResponseManager.getInstance().getCurrentRequest()

ResponseObject response = WSResponseManager.getInstance().getCurrentResponse()

def jsonSlurper = new JsonSlurper()

GlobalVariable.sdsdOptimizationId = jsonSlurper.parseText(response.responseText).get("optimization_problem_id")

log.println("sdsdOptimizationId -> "+GlobalVariable.sdsdOptimizationId)

Cast an Object into a Class

This section describes how to cast an object into a custom predefined class.

To create a custom class, select the menu item File >New > Keyword. The dialog window invites you to create a keyword.

Name the keyword as PostClass and write into it:

// The import statements are omitted here.

class PostClass {

	Integer userId
	Integer id
	String title
	String body
}

Now you can use this class to validate a value for this class type. For example, You can retrieve from a web request a response object.

Include in the import statements block the command:

import PostClass

and cast to it a response object:

def postObject = jsonSlurper.parseText(postResponse.responseText)
def post = postObject as PostClass

How to Validate a Web Page

The section describes how to validate a specified web page in Katalon Studio using a Groovy script.

Create the test object in Object Repository with Selector Locator = "//*[@id = 'hero']".

Open default browser and navigate to the specified URL:

openBrowser()
WebUI.navigateToUrl('https://jsonplaceholder.typicode.com/')
def heroText = WebUI.getText(findTestObject('hero'), Username)
println(heroText)

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *