JavaScript for JScript Users

Applies to TestComplete 15.47, last modified on January 20, 2023

Converting JScript Projects to JavaScript

In TestComplete, you can convert your JScript projects to JavaScript. This action cannot be undone, but you can create a backup before the conversion.

Conversion changes the project’s scripting language to JavaScript, but does not modify your code. You may need to fix certain things in your code manually (see below).

To convert a JScript project to JavaScript:

  1. Right-click your JScript project in the Project Explorer and select Convert to JavaScript from the context menu.

    Converting a JScript project to JavaScript
  2. (Recommended.) Select the option to Back up the project before conversion.

  3. Click Yes.

  4. If you selected to create a backup, choose the folder to save the backup to.

To undo the conversion, you need to delete the converted project, and then add the project backup as an existing project.

Differences Between JavaScript and JScript

JavaScript is very similar to JScript, so, in most cases, code written in JScript will work in JavaScript with little to no changes. However, there are some differences and specifics you should keep in mind when migrating from JScript to JavaScript:

  • JavaScript is not supported on Windows XP and Windows Server 2003 (see JavaScript - Specifics of Usage).

  • The new ActiveXObject() statement is not supported in JavaScript. Use getActiveXObject("prog-id") instead. To call methods and properties of the obtained objects, we recommend that you use the $get, $set and $call methods. This way the calls will be faster.

  • GetObject() is not supported in JavaScript. Use getActiveXObject("prog-id") instead.

    For example, to access the Windows Management Instrumentation (WMI) service, you can use getActiveXObject to get a helper object and then use the object’s method to connect to the server:

    JavaScript

    let Locator = getActiveXObject("WbemScripting.SWbemLocator");
    let WmiService = Locator.$call("ConnectServer", Computer-Name, "\\root\\cimv2", "", "");  

    Note that we used the $call method to invoke the ConnectServer method.

  • JavaScript in TestComplete can work with SAFEARRAY arrays (VBScript-format arrays) directly, so there is no need to convert them by using new VBArray(arr).toArray(). This includes arrays returned from the Find methods, such as FindAllChildren, EvaluateByXPath, and others. However, the VBArray object is also available in JavaScript in TestComplete, so your JScript code that uses VBArray will work without changes in JavaScript.

  • JavaScript equality operations == and === may return wrong result when comparing values obtained from:

    • TestComplete test objects (objects in tested applications),
    • TestComplete scripting objects, such as Sys or aqEnvironment,
    • COM objects.

    For example (but not limited to), when comparing these values to null, undefined or objects. To avoid errors, use the equal or strictEqual functions instead:

    JavaScript

    let links = page.EvaluateXPath("//A");
    if (!equal(links, null))
    {
      // found
    }

  • To refer to another unit in JavaScript, use require("UnitName") instead of //USEUNIT. See JScript - Calling Routines and Variables Declared in Another Unit.

  • //USEUNIT is supported for compatibility with JScript, but works slightly differently in JavaScript:

    • To modify a variable declared in another unit, you must use the UnitName.VariableName syntax. This works the same way in JavaScript and JScript.

      If you omit the unit name before the variable name, JavaScript thinks it refers to the variable in the current unit, whereas JScript first looks for this variable in the referenced units.

      JavaScript, JScript

      [Unit1]
      var foo = 0;

      [Unit2]
      //USEUNIT Unit1
      function test()
      {
        Unit1.foo = 42; // OK

        foo = 15; // In JScript, this changes Unit1.foo
                  // In JavaScript, this changes the variable foo in the current unit
      }

    • JavaScript supports circular references between units, unlike JScript.

    • JavaScript can handle exceptions thrown in other units, though errors may occur if you use the instanceof statement to check the exception type (see JavaScript - Specifics of Usage).

  • JScript lets you assign values to parameterized properties directly:

    JScript

    obj.wValue(3, 5) = newValue;

    JavaScript does not support this syntax. You need to use the $set method instead:

    JavaScript

    obj.$set("wValue", 3, 5, newValue);

  • JScript lets you access collection items directly by specifying indexes for the collection. For example, to access the items of the Cells collection:

    JScript

    Excel.Cells(1, 4);

    In JavaScript, you need to explicitly specify the property name (Item or Items) to access the collection items:

    JavaScript

    Excel.Cells.Items(1, 4);

  • The JavaScript Error object does not have the description and number properties. To get the error message in JavaScript, use the message property. If you need to use the number property, you can add it to the Error object instance manually before throwing the error. For example:

    JavaScript

    try
    {
      let MyError = new Error("My Error Message");
      MyError.number = 404;
      throw MyError;
    } catch (e) {
      Log.Error(e.number + ": " + e.message);
    }

  • In JavaScript, you can iterate through an iterable object by using the for...of loop instead of the Enumerator object:

    JavaScript

    for (let process of Sys)
      Log.Message(process.Name);

    However, JavaScript in TestComplete also supports the Enumerator object, so the corresponding JScript code will work without changes in JavaScript:

    JavaScript, JScript

    var iterator = new Enumerator(Sys);
    for (; !iterator.atEnd(); iterator.moveNext())
      Log.Message(iterator.item().Name);

  • In JavaScript, the caller property works properly. In JScript, it contains the null value.

  • Script extensions can be written in JScript but not JavaScript.

  • JScript’s CollectGarbage function is not available in JavaScript in TestComplete, but it has a similar function - gc(). However, we do not recommend using gc() because it may cause stability issues, and it may be removed in future TestComplete releases. If you absolutely have to use the garbage collection, you can call it as follows:

    JavaScript

    if (gc)
      gc();

Detecting JavaScript and JScript

If you use the same script unit in both JScript and JavaScript projects (via Add Existing Item), you may need to check the active project’s language to use different language features for JScript and JavaScript. To detect the language, you can use this function:

JavaScript, JScript

// Returns true if the project language is JavaScript, or false if it is JScript
function isJavaScript()
{
  return (typeof Symbol === 'function') && (typeof Symbol.toStringTag === 'symbol');
}

See Also

Specifics of Usage Common for All Languages
JavaScript - Specifics of Usage
JScript, C#Script and C++Script - Specifics of Usage
Selecting the Scripting Language
Converting Scripts From One Scripting Language To Another

Highlight search results