Q.:
How can I call procedures or variables declared in another unit? I need to call a procedure declared in UnitA from UnitB and vice versa.
A.: By default, a script can call routines declared within
the same unit. To call routines in another unit, the current one must be
“linked” to it:
In VBScript projects place the cursor at the beginning of the
unit and add the following string:
Be sure you specify the apostrophe ( ' ) before USEUNIT.
To link to several
units, use the following syntax:
In DelphiScript projects place the cursor at the beginning
of the unit and add the following string:
If you need to link several units, separate their names with
commas, that is
uses Unit1_Name, Unit2_Name;
In JScript, C++Script or C#Script projects place the cursor at the beginning
of the
unit and add the following string:
Be sure to specify the double slash ( // ) before USEUNIT.
You can link several units in the following manner:
| Note: |
The USEUNIT statement does not allow tab or space characters at the beginning of the line and between the USEUNIT and the comment operator (' in VBScript and // in JScript).
|
Units can refer to each other. In other words, circular
references are allowed. For instance, if you need to call scripts
declared in UnitA from UnitB, and to
call scripts declared in UnitB from UnitA,
you must add references to both units:
Circular references are not allowed in JScript, C++Script and C#Script.
In these languages, circular links may cause errors in the jscript.dll (C++Script and C#Script are based on JScript and use this dll as well), which causes errors in TestComplete.
Another solution is to use the Runner.CallMethod function. It calls any routine specified by its full name (that is, unit_name.routine_name). If you call a script routine using this function, there is no need to include the unit specified by unit_name in the uses (USEUNIT) clause of the current unit.
Call Runner.CallMethod("UnitA.MyScript")
Runner.CallMethod("UnitA.MyScript")
Runner.CallMethod('UnitA.MyScript')
Runner["CallMethod"]("UnitA.MyScript")