We have some page-internal javascript that runs behind the scenes and collects various page metrics values, etc that we return to a database for collection and analysis.
I’m trying to capture this data “live” as my Katalon browser script runs so that I can perform the same calculations that the database is supposed to be doing and validate that the results are the same from both places.
I know how to execute console javascript commands from within Katalon, but this particular example has me stumped, as a basic return won’t work; nor am I immediately certain of the variable format this return data would get parsed into within Katalon for me to retrieve selected values, any of you forum gurus have any ideas on the how to retrieve and what result type will come back for the below console query snippet?
However I’m not quite sure how to parse this information from within the variable.
I recall seeing an example of this type of two dimensional array in Katalon, and how to reference the value by the key, but can’t recall exactly what Katalon calls it, any thoughts?
Trying the above, but getting the below error message, could the formatting be getting interpreted as something other than JSON?
I did observe that in your example above the “JSON” is delineated with curly brackets, whereas the data I’m retrieving is returning with square brackets, could this be causing Katalon to interpret the return as a different data type (perhaps a map?)
groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (java.util.HashMap) values: [[firstPaint:1178, pageValue:0, tul:0, CLS:, ssl:63, redirectStart:0, ...]]
The message reveals that the WebUI.executeJavaScript() returned an instance of type java.util.HashMap in Groovy with value [firstPaint:1178, pageValue:0, tul:0, CLS:, ssl:63, redirectStart:0, ...].
On the other hand, the javascript in the the browser returned an instance of javascript object with value:
We can find a slight but significant difference between the 2 strings. In JavaScript, an object is stringified as { key: value, ... } guarded by a pair of curly braces { ... }, but in Groovy a Map instance is stringified as [ key: value, ... ] guarded by a pair of square brackets [ ... ].
This phenomenon revealed an automatic data-type conversion between JavaScript object and Groovy Map:
My sample code above does an explicit conversion of data types: JavaScript object --(JSON.strigify)–> JSON text --(JsonSlurper.parseText)–> Groovy Map.
@kevin.jackey 's experiment revealed that my explict data type conversion was not necessary. The same serialization and deserialization will be done automatically by Selenium3 JSON WIRE PROTOCOL.
Interesting.
The data type conversion between browser and clients (of WebDriver API) has changed historically and will change in future.
Selenium3 does a data type conversion by JSON WIRE PROTOCL.
Selenium4 does the same by the W3C WebDriver API.
WebDriver BiDirectional API in future will carry out in a different API.
The automatic data type conversion will continue evolving, will present technical challenges on and on.
I guess, the explicit (foolish) data-type conversion via JSON-text as I did above is robust. It will stay operational in future on top of whichever underlying protocols.