Katalon Verifying Elements in Array by Property Value

Katalon Verifying Elements in Array by Property Value

Posted by

Katalon’s WS.verifyElementPropertyValue function

As I have previously mentioned, Katalon is a good tool for performing automated API testing. However, I ran into some difficulty with verifying elements in a JSON array with a certain property value. When using the WS.verifyElementPropertyValue keyword, you can use the “element locator” to find the specific values you’re trying to test.

Element locator is the concept utilized by Katalon Studio to explore into hierarchical data structure such as JSON or XML to look for the expected data.


The documentation on how to handle response messages explains how to get an object within an array using its index. This is fine if you always know what index you need to check, but if you don’t there’s a problem. Say I have the following response from an API:

   "data": [
       "name": "lastname",
       "value": "Valenta"
       "name": "firstname",
       "value": "Mitch"

If I want to verify that the value of the firstname attribute is “Mitch” with the index approach, I specify data[1].value as the element locator. But, if the elements are out of order, data[1].value is “Valenta”, which fails verification.

Groovy’s find function

Katalon uses Groovy behind the scenes for it’s scripting, so it stands to reason that there is a Groovy function to manually verify the data we want. Sure enough, a find function is available on collections to return a single object. When using find, you specify keyword “it” as the object you’re looking for. An example of how to use this function manually is below:

def json = new JsonSlurper().parseText(readResponse.getResponseText())

def attributes = json.data

def name2EqValue2 = attributes.find({
		it.name == 'name2'
	}).value == 'value2' 

WS.verifyEqual(name2EqValue2, true)

Here’s where it gets interesting. While not called out in the Katalon documentation, you can use that find function in the element locator. In the below example, I’m getting the value of the element that has a name of “name2” in the data array.

WS.verifyElementPropertyValue(readResponse, 'data.find{it.name == "name2"}.value', 'value2')

Final thoughts

My initial thoughts on how to get an element by property value were to use a sort of CSS selector. This logic came from the fact that that Katalon uses CSS selectors to find elements with UI testing. When that didn’t work, the developer in me thought to figure out how to do it manually in the “script” section. On a whim, I noticed that find function works in the locator and I was perplexed. There’s no mention of this functionality in the Katalon documentation as far as I could find. Plus, I thought the locator was just a text string, so I’m at a loss for how exactly it gets evaluated behind the scenes.

Regardless, I have updated my GitHub repositories for my mock API and demo project with Find examples.

Katalon mock API: https://github.com/mvalenta/katalon-mock-api

Katalon demo project: https://github.com/mvalenta/katalon-api-demo

One comment

  1. Hey Mitch, This is exactly where I am stuck at. Can you teach me more on this in detailed ? We can talk over more. Pls see my contact listed below.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.