Parsing REST API result in keyword test
A complete functional example (call a REST API with a GET verb, pull an answer out of the result, and use the answer later in the keyword test as the value to enter into a text box) would be really, really helpful!
Solved! Go to Solution.
EDIT: Thanks Alex for the guidance re: getting the LastResult. I've updated my own answer to my question accordingly.
After a bit of fiddling with the product (bear in mind, I'm learning as I go along... I just started using TC a few days ago), here's a sequence of KeywordTest steps that calls the petstore API and parses out a value from the result.
// Call the petstore API. This returns a JSON string
SendRequest | "GET", "http://petstore.swagger.io/v2/store/inventory"
// To get the JSON string from the previous call, I need to use a Set Variable action to fetch
// the magic LastResult (i.e. the JSON string returned by the previous API call)
Set Variable Value | apiResult [Local] | LastResult
// Now that I have the JSON string, parse it and store the result in a variable I can use later
// in the Keyword Test. In this example, I'm parsing the "sold" member.
Set Variable Value | value [Local] | JSON.parse(KeywordTests.MyTest.Variables.apiResult).sold
> // I'm sure there's a way to skip this step [...]
No, this step is mandatory. You must store returned value to some variable and work with this variable than.
What script language is your test project based on?
Example can be found here: https://mindcemetery.wordpress.com/2009/10/19/json-in-testcomplete/
First, you must convert returned string to JSON object. This is a two-step process: a) ensure that the string is enclosed in curly braces, then b) execute eval(<StringInCurlyBraces>); statement to convert a string into object and assign the result to some variable.
After obtaining JSON object, you can get/set the values from it as usual in JScript.
Note: All the above steps will require scripting as there are no corresponding actions for this in Keyword operations.
/Alex [Community Hero]
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.