Forum Discussion

JMassa1976's avatar
JMassa1976
New Contributor
7 years ago
Solved

TestComplete javascript implementation of an Dictionary object bugged.

The code example they give for the new "getActiveXObject("Scripting.Dictionary")" implementation of a dictionary is as follows:

function DictionaryDemo()
{
  let d = getActiveXObject("Scripting.Dictionary");

  // Add keys and items.
  d.Add("a", "Alphabet");
  d.Add("b", "Book");
  d.Add("c", "Coffee");

  // Get the item by key.
  var s = d.Item("c");
  Log.Message(s);

  // Assign a new item to the same key.
  d.Item("c") = "Cookie";
  s = d.Item("c"); 
  Log.Message(s);

  // Assign a new key to the same item.
  d.Key("c") = "Co";

  // Remove second pair.
  d.Remove("b");
  if (d.Exists("b")) 
      Log.Message("The pair exists.") 
    else
      Log.Message("The pair does not exist.")
}

This code breaks in a couple of places. 

 

The line 'd.Item("c") = "Cookie";' gives the following error:

 

JavaScript runtime error.
ReferenceError: Invalid left-hand side in assignment.

 

The line 'd.Key("c") = "Co";' gives the following error:

 

JavaScript runtime error.

Error: Member not found.

  • That's because that's the JScript version of the code.   If you go to https://support.smartbear.com/testcomplete/docs/scripting/dictionary-object.html?q=SCripting%20Dictionary and look at the code examples, there are JavaScript, JScript, Python, etc... if you go to the actual JavaScript tab, this is the code that they give.  Notice the bolded lines that are different.

    function DictionaryDemo()
    {
      let d = getActiveXObject("Scripting.Dictionary");
    
      // Add keys and items.
      d.Add("a", "Alphabet");
      d.Add("b", "Book");
      d.Add("c", "Coffee");
    
      // Get the item by key.
      let s = d.Item("c");
      Log.Message(s);
    
      // Assign a new item to the same key.
      d.$set("Item", "c", "Cookie");
      s = d.Item("c"); 
      Log.Message(s);
    
      // Assign a new key to the same item.
      d.$set("Key", "c", "Co");
    
      // Remove second pair.
      d.Remove("b");
      if (d.Exists("b")) 
          Log.Message("The pair exists.") 
        else
          Log.Message("The pair does not exist.")
    }

4 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    That's because that's the JScript version of the code.   If you go to https://support.smartbear.com/testcomplete/docs/scripting/dictionary-object.html?q=SCripting%20Dictionary and look at the code examples, there are JavaScript, JScript, Python, etc... if you go to the actual JavaScript tab, this is the code that they give.  Notice the bolded lines that are different.

    function DictionaryDemo()
    {
      let d = getActiveXObject("Scripting.Dictionary");
    
      // Add keys and items.
      d.Add("a", "Alphabet");
      d.Add("b", "Book");
      d.Add("c", "Coffee");
    
      // Get the item by key.
      let s = d.Item("c");
      Log.Message(s);
    
      // Assign a new item to the same key.
      d.$set("Item", "c", "Cookie");
      s = d.Item("c"); 
      Log.Message(s);
    
      // Assign a new key to the same item.
      d.$set("Key", "c", "Co");
    
      // Remove second pair.
      d.Remove("b");
      if (d.Exists("b")) 
          Log.Message("The pair exists.") 
        else
          Log.Message("The pair does not exist.")
    }
    • JMassa1976's avatar
      JMassa1976
      New Contributor

      That was a direct paste from the website for the javascript (JScript doesn't use 'let') ... seems the website had been updated since I had been experimenting with it. It's an odd mix, however, you don't need $call for Exists, Remove, ect and you don't need to use $get either ... seems only to be the $set that is required in this implementation.

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        That's because Exists, Remove, etc., don't need the $call or $get.  Check out

        https://support.smartbear.com/testcomplete/docs/scripting/specifics/javascript.html#indexed-properties

         

        The reason for the $set is that you're assigning values to an indexed property.  $get and $call CAN be used, but they aren't required... $set is for indexed properties in JavaScript.

         

        I checked my installation of TC 12.31... and the code that you pasted earlier was not in the JavaScript tab... so you must be referencing a VERY old web page for that.  The current web pages are for TC 12.42, updated on 12/11/2017 and 12.40 was released about 2 months ago... and 12.31 is even older...