HOWTO: Read script configuration data from XML in TestComplete using JScript

So, you've written a script and have some variables that change from computer to computer. For example, your application under test is located at c:\program files\MyApp\MyApp.exe while on your neighbor's computer it is located at C:\MyApp\MyApp. Since MyApp is your application under test, you need to be able to run it on any computer without any errors.
2 Approaches come to mind:

  • Create a config file that users will have to edit before running your script
  • Parse Windows Registry to figure out install path of application in question
  • This blog entry will show you how to use both approaches.

    XML Config File approach

    First, let's take a look at the config file approach. You might want to create a simple XML file that will contain some of the global parameters. Those parameters could be application path, path to a log file, email addresses of people you want to send your test results to, etc. Your XML file should not be complex and could be as easy as this:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
       <AppUnderTest>C:\Program Files\MyApp\MyApp.exe</AppUnderTest>
       <LogPath>C:\test_output_log.txt</LogPath>
    </configuration>

    If you have configured your XML file in a similar manner, you can use the following JScript code to read your file and get needed info out like so:

    function ReadConfigFile (xmlLoc, xmlTagArray) {
       // create xml object
       var objXML = new ActiveXObject("Microsoft.XMLDOM");
     
       // Do not load XML file asynchronously
       objXML.async = false;
     
       // Open XML File
       objXML.load(xmlLoc);
     
       // make sure that we were able to read the file
       if (objXML.parseError.errorCode != 0) {
          Log.Error("There were errors while reading xml file. " + objXML.parseError.reason);
          return null;
       }
     
       // initialize return value
       var retVal = new Array(xmlTagArray.length);
     
       // get to the 'configuration' element
       var config = objXML.selectSingleNode("//configuration");
     
       // loop through config tags
       for (var i in xmlTagArray) {
          var node = config.getElementsByTagName(xmlTagArray[i]);
          // get the first item out
          var item = node.item(0);
          if (item == null) {
             Log.Error("Could not find item '" + itemsToGet[i] + "'");
             continue;
          }
          retVal[i] = item.text;
       }
     
       return retVal;
    }

    Now that you have your code in place, this is how you use it:

    function TestXMLConfigFile() {
       var xmlConfigTags = {
         appLoc:"AppUnderTest",
         logPath:"LogPath"
       }
       var loc = "C:\\config_file.xml";
       var res = ReadConfigFile(loc, xmlConfigTags);
       if (res == null) {
          Log.Error("OOPS! Something have gone terribly wrong!");
       }
       // Update path of your Application Under Test
       TestedApps.MyApp.path = res["appLoc"];
       var logPath = res["logPath"];
    }

    Windows Registry approach

    Here's the link to the script to that finds install path by using Windows Registry.

    Comments