[Collapse]TestComplete FAQ
 [Collapse]Writing Automated Test Script Code
   How do I call procedures or variables declared in another unit?/support/viewarticle.aspx?aid=8921
   How can I close a modal dialog which was invoked by a method called from a script?/support/viewarticle.aspx?aid=8922
   How can I pass function parameters by reference in JScript (C++Script, C#Script)?/support/viewarticle.aspx?aid=8923
   I can use the same name for two procedures that are in different units. Is this correct?/support/viewarticle.aspx?aid=8924
   I cannot find functions to deal with time. Where should I search for them?/support/viewarticle.aspx?aid=8925
   Is there a way to call routines defined in another project?/support/viewarticle.aspx?aid=8926
 [Expand]Working With Files, Databases and Excel Sheets
 [Expand]Web Testing - Functional Testing of Web Pages
 [Expand]TestComplete Trial Version Questions
 [Expand]Test Log and Results
 [Expand]Supported Applications, Operating Systems and Development Tools
 [Expand]Recording, Running and Debugging Automated Tests
 [Expand]Objects, Files and Images Comparison
 [Expand]Miscellaneous
 [Expand]Load Testing of Web Servers
 [Expand]Licensing Questions
 [Expand]General Questions About Automated Testing
 [Expand]Automated Testing Tasks - Basic
 [Expand]Automated Testing Tasks - Advanced
 [Expand]Accessing Application's Internal Objects, Methods and Properties
Updated: 7/26/2010 Applies To: TestComplete
Rating: PoorPoorPoorPoorPoor 3 vote(s)
Click to rate:
PoorNot badAverageGoodExcellent
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:

VBScriptCopy Code

'USEUNIT Unit_Name

Be sure you specify the apostrophe ( ' ) before USEUNIT.

To link to several units, use the following syntax:

VBScriptCopy Code

'USEUNIT Unit1_Name
'USEUNIT Unit2_Name

In DelphiScript projects place the cursor at the beginning of the unit and add the following string:

DelphiScriptCopy Code

uses Unit_Name;

If you need to link several units, separate their names with commas, that is

DelphiScriptCopy Code

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:

JScriptCopy Code

//USEUNIT Unit_Name;

Be sure to specify the double slash ( // ) before USEUNIT.

You can link several units in the following manner:

JScriptCopy Code

//USEUNIT Unit1_Name
//USEUNIT Unit2_Name
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:

VBScriptCopy Code

UnitA
'USEUNIT UnitB
...
UnitB
'USEUNIT UnitA
...

DelphiScriptCopy Code

UnitA
uses UnitB;
...
UnitB
uses UnitA;
...

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.

VBScriptCopy Code

Call Runner.CallMethod("UnitA.MyScript")

JScriptCopy Code

Runner.CallMethod("UnitA.MyScript")

DelphiScriptCopy Code

Runner.CallMethod('UnitA.MyScript')

C++Script, C#ScriptCopy Code

Runner["CallMethod"]("UnitA.MyScript")


© 2010 AutomatedQA Corp.
Email Send feedback on this document
 
© 2010 SmartBear Software. All rights reserved.
Home | Privacy | Terms of Use | About | Contact Us | Site Map | Print