How to Convert TestObject to String

@Russ_Thomas

In have used the below method to create Test Object in Memory at Runtime.

import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject as TestObject


println makeTO("input").getClass()  // Output - class com.kms.katalon.core.testobject.TestObject
println makeTO("input").toString()  // Output  -TestObject - ''

static TestObject makeTO(String css) {
	TestObject to = new TestObject()
	to.addProperty("css", ConditionType.EQUALS, css)
	return to
}

My question is, how do I convert the TestObject to String.

The toString() method is returning TestObject - ''

https://docs.katalon.com/katalon-studio/docs/manage-web-test-object.html#creation-of-test-object-in-memory-at-runtime

Why not try my solution:

You can download the jar from

and locate the jar in you projects Drivers folder.

You can write you test case for example

import com.kazurayam.ks.testobject.TestObjectExtension
TestObjectExtension.apply()

WebUI.comment("#prettyPrint()\n" + tObj.prettyPrint())

You will see output like:

{
    "cachedWebElement": null,
    "objectId": "Object Repository/Page_CURA Healthcare Service/a_Make Appointment_BASIC",
    "parentObjectShadowRoot": false,
    "properties": [
        {
            "active": true,
            "value": "a",
            "condition": "EQUALS",
            "name": "tag"
        },
        {
            "active": true,
            "value": "btn-make-appointment",
            "condition": "EQUALS",
            "name": "id"
        },
        {
            "active": false,
            "value": "./profile.php#login",
            "condition": "EQUALS",
            "name": "href"
        },
        {
            "active": false,
            "value": "btn btn-dark btn-lg",
            "condition": "EQUALS",
            "name": "class"
        },
        {
            "active": false,
            "value": "Make Appointment",
            "condition": "EQUALS",
            "name": "text"
        },
        {
            "active": false,
            "value": "id(\"btn-make-appointment\")",
            "condition": "EQUALS",
            "name": "xpath"
        }
    ],
    "imagePath": null,
    "selectorMethod": "BASIC",
    "selectorCollection": {
        "BASIC": "//a[@id = 'btn-make-appointment']"
    },
    "useRelativeImagePath": false,
    "parentObject": null,
    "xpaths": [

    ],
    "activeProperties": [
        {
            "active": true,
            "value": "a",
            "condition": "EQUALS",
            "name": "tag"
        },
        {
            "active": true,
            "value": "btn-make-appointment",
            "condition": "EQUALS",
            "name": "id"
        }
    ],
    "activeXpaths": [

    ]
}
2 Likes

Too long?

Then you can parse the JSON string by groovy.json.JsonSlurper. You can do any query as you like on the json object.

1 Like

Thanks a lot @kazurayam, appreciate your prompt response.

This really helped me :pray:

I realized that you do not need my codes.
All you need is groovy.json.JsonOutput.
See the following snippet.

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.testobject.TestObject

import groovy.json.JsonOutput

TestObject tObj = findTestObject('Object Repository/Page_CURA Healthcare Service/a_Make Appointment_BASIC')

String json = JsonOutput.toJson(tObj)
println json

println JsonOutput.prettyPrint(json)
1 Like