Retrieve JavaScript object from AUT and use it in Groovy/Katalon (was Converting string into an object?)

Hello guys,

I am using javascript to get mine “universal variables” from site into Katalon.

So im using this functions:

Object universal_variable = WebUI.executeJavaScript("return window.universal_variable;", null)

print (universal_variable)

and in katalon console after i run the test im getting my universal variable it looks like this : [site_subscription={active=1, corp_offer=0, created_by=1, email_suppress=0, etc.

so now what i want to do is to take each of this value and somehow convert it into an variable object in katalon so later on in other test cases i can compare if variables that im taking from the site are the same as in katalon. I’ve found some simillar topic but im not quite sure if they solve my problem

https://docs.katalon.com/katalon-studio/docs/webui-execute-javascript.html

What EXACTLY does universal.variable contain in JavaScript ?

The reason I ask is, Groovy (and therefore Katalon) has an annoying “feature” that it prints object notation as a series of assignments using "=" where JSON/JavaScript would use ":". I suspect that’s why you get active=1, corp_offer=0, etc.

Essentially, I think you’re looking at a Groovy Map. To pass it around various Test Cases you could store it in a GlobalVariable (perhaps intialized to an empty map - [:])

Earlier, you said:

Did you remove anything? What happened to the first embedded object, {page_type: "home", ...} ?

It looks like this in katalon, those universal variables are rly long, it all depends on which site am i and what type of user i am log-in with on my sites

whatever is a string, it can be parsed.
once parased, can be casted as an object.
once casted as object, it can be manipulated.
just saying …

all im getting in this error, looks like katalon is not definingpage_type

I dont understand, how is it working for you? I’ve used exact same function as you

Sorry. I mistakenly thought you had a list/array of objects.

def universal_variable = WebUI.executeJavaScript("return window.universal_variable;", null)

println universal_variable.page_type
// or
println universal_variable["page_type"]

The principle is, the last one will allow you dig into the object by “named strings” - this will prevent the Katalon IDE from underlining the member as “unknown”.

1 Like

Great job! Its working now;) Thanks for help again Russ

btw, can i have arrays in katalon?

Great. Glad it’s working for you.

In Groovy, they’re called lists:

List mylist = ["Oskar", "Russ", "Batman", "etc."]

println mylist[0] // "Oskar"

println mylist.size() // 4

Okay, and can i for example make a list of universal variables that i know my function now is taking from the site and compare if they match?

Sure…

if(universal_variable["page_type"] != "home") {
  KeywordUtil.markFailed("Bad page_type: " + universal_variable["page_type"])
}
1 Like

Nice, thanks but how am i using my global variables from profiles here?

Okay, it was more simpler than i though. And looks like its working. Screenshots in case someone need help with that

Screenshot_197

Ok I can’t help myself.

Just to clarify our terms here, the title of this topic is: “Converting string into an object?”

An instance of a String is an Object…

According to the rest of the OP, what they really want is to assign a String to a variable. I know this is a nitpick, but clearly-defined terms can help immensely in the long run :wink:

1 Like

@wolczoskar i think is time to read a bit:

http://docs.groovy-lang.org/next/html/documentation/working-with-collections.html

1 Like

Not at all. I fully meant to change the title once the problem was nailed - then I took the day off :blush:

@wolczoskar I strongly recommend you start a new topic with new/different questions since this one, as it originally stood, has been solved. The reason Brandon is calling us to task is sound: future users want to search for one question and find one clear answer. This topic is starting to wander…

I’ll close this now.

2 Likes