A customer asked us:
I’ve just bought the PRO version, and imported my selenium IDE test suite into UI.Vision but I’m having issues. I had steps in my SIDE Selenium IDE to parse JSON data on a page, but these commands don’t work in UI.vision
So if there is JSON data on a page, how can I read/scrape a Json value into a variable in my macro?
The JSON data is returned by a API that I am loading in the browser.
There is no HTML in the page source - just the JSON array.
The SIDE Selenium IDE had “store Json” command for this, but I see no storeJson in Ui.Vision.
I used:-
- store text | css=pre | page_data
- store json | ${page_data} | json
Then, I was able to grab json values like this:-
- execute script | return ${json}.Count | count
The purpose of the store json command is to retrieve and store json content into a variable in Selenium IDE.
UI.Vision has no specialized store Json command, but that is not needed. The solution (found by the user himself ) is to use Javascript and executeScript_Sandbox to recreate the store Json command. The demo macro below shows this. You need only 2 lines to scrape JSON. The macro first scrapes the JSON output from the browser window with storeText and the css=pre
locator and then uses JSON.parse to get the required value.
{
"Name": "scrape_json",
"CreationDate": "2023-1-25",
"Commands": [
{
"Command": "open",
"Target": "https://api.ocr.space/parse/imageurl?apikey=helloworld&url=http://i.imgur.com/s1JZUnd.gif&language=chs&isOverlayRequired=true",
"Value": "",
"Description": "Free OCR API call"
},
{
"Command": "storeText",
"Target": "css=pre",
"Value": "page_data",
"Description": ""
},
{
"Command": "echo",
"Target": "JSON=${page_data}",
"Value": "green",
"Description": ""
},
{
"Command": "executeScript_Sandbox",
"Target": "var json_object = JSON.parse(${page_data}); return json_object.ProcessingTimeInMilliseconds",
"Value": "ptime",
"Description": ""
},
{
"Command": "echo",
"Target": "OCR API Processing Time In Milliseconds = ${ptime}",
"Value": "blue",
"Description": ""
}
]
}
1 Like