Search code examples
jsonjscripttestcomplete

Can I use JSON for desktop application testing in TestComplete?


I'm creating some automated testing scripts in SmartBear TestComplete 10.60, and I'm wondering if I can read in data from a .json file. Places I've looked speak almost exclusively to web app testing, but I'm running automated testing on a Windows desktop application. I could use XML, but for the team I am working with, JSON currently looks like a better option going forward.

I haven't set up anything in TestComplete since I don't know how complex it would be. Here are contents of a simple JSON file, named "testFile.json"

I am using JScript and will be initially testing by using Log.Message("data here");

What would be a way for TestComplete to read this data in?

{"Person":[
    {"Name": "Joe"},
    {"Sex": "Male"},
    {"Job": "Software Engineer"},
    {"Married": false}
]}

Solution

  • You can use the eval function to parse a JSON string using JScript in TestComplete 10. For example:

    function test()
    {
      var fileName = "d:\\testFile.json";
      var txt = aqFile.ReadWholeTextFile(fileName, aqFile.ctANSI);
      var obj = eval("(" + txt + ")");
      Log.Message(obj.Person[0].Name);
    }
    

    BTW, I've heard that the upcoming TestComplete 11 will have an updated version of the JScript language. This new version has native support for the JSON scripting object.