Forum Discussion

RobertDAltman's avatar
RobertDAltman
Occasional Contributor
7 years ago
Solved

Parsing REST API result in keyword test

Ok, so now that I've successfully called my REST API from my keyword test, how do I parse the result (presumably JSON, although I can just as easily return XML)?  Do I need to roll my own script code or is there something in the world of keyword tests that will help me?  If I need to write JavaScript to parse the JSON, a quick Google search turns up plenty of examples of how to do that.  I'm really asking about the "big picture" and whether or not I can get from here to there entirely in the "keyword test" world.

 

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!

  • 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

2 Replies

  • RobertDAltman's avatar
    RobertDAltman
    Occasional Contributor

    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

    • AlexKaras's avatar
      AlexKaras
      Champion Level 3

      Hi,

       

      > // 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?

      Guessing it is either JScript or JavaScript... If my guess is correct, then you are a lucky one :) as you can use built-in in JScript/JavaScript JSON parsing functionality.

      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.