Accessing Native Methods of Java Objects

Applies to TestComplete 15.40, last modified on July 05, 2022

TestComplete provides you with access to individual objects of Java applications, their internal properties and methods. The following sections describe how you can access these native properties and methods from your tests:

About Accessing Native Methods

TestComplete identifies individual GUI objects in Java applications and provides special methods and properties for automating various operations over these objects, getting object data, checking the state and so on. However, if these predefined properties and methods are insufficient for your testing needs, you can use native methods of Java objects to complete the desired tasks. These are the same methods that are available in the tested application’s source code as well as custom methods implemented by the application developers. This way, you can perform almost any operations in the tested Java application, even those that cannot be accessed via the application’s GUI.

The Object Browser marks objects of Java applications with the glyph. To view native methods available for a Java object, use the Object Spy or Object Browser in the Advanced view mode. Note that TestComplete gives you access to private, protected and public methods and fields of Java application objects. In the Object Browser, native methods of Java objects are displayed under the Java category (see Important Notes for details):

Native Properties of a Java Menu Object in Object Browser

Click the image to enlarge it.

For more information on how to refer to objects in Java applications, see Addressing Objects in Java Applications.

Calling Native Object Methods

When testing a Java application, you can call its object methods from your tests.

To call an object’s method from script code, you use the dot operator "." (in VBScript, JScript, Python and DelphiScript projects) or the square bracket notation [" "] (in C++Script and C#Script projects) to specify the method name after the object name.

For example, in script code you can call the native setLabel method of a Java menu as follows:

JavaScript, JScript

Aliases.java.frame0.Menu.setLabel("New menu label");

Python

Aliases.java.frame0.Menu.setLabel("New menu label");

VBScript

Aliases.java.frame0.Menu.setLabel("New menu label")

DelphiScript

Aliases.java.frame0.Menu.setLabel('New menu label');

C++Script, C#Script

Aliases["java"]["frame0"]["Menu"]["setLabel"]("New menu label");

In keyword tests, you can use the On-Screen Action operation to call an object’s native method. For more information, see Calling Object Methods.

Invoking a Java object’s native method

Click the image to enlarge it.

Important Notes

  • In the Object Spy or Object Browser, native fields and methods of Java objects are displayed only in the Advanced view mode.

  • If a native method (or field) has the same name as the test object method (or field) provided by TestComplete, the native method (field) can be accessed via the NativeJavaObject property. For example, you can access the native validate method of a Java application’s form as follows:

    Aliases.java.frame0.NativeJavaObject.validate

  • TestComplete also supports access to array objects. To address array items, it adds the Items and Length properties to the appropriate scripting object. Length specifies the array size and Items provides access to elements by their indexes (zero-based), for instance, javaArrObj.Items(2).

    Multi-dimensional arrays are represented as arrays, in which every item, in its turn, is also an array. For instance, if javaArrObj is an array that has three dimensions, you can access its elements in the following way:

    javaArrObj.Items(1).Items(2).Items(3)

  • If an object contains overloaded methods (methods with the same name but with different parameters and they return value types), TestComplete appends indexes to method names in order to distinguish them. For example, GetMousePosition() and GetMousePosition_2(). To determine which index-suffixed method name corresponds to which overloaded method, examine the method’s parameter list in the Object Spy, Object Browser or Code Completion.

  • The leading underscore ( _ ) in property and method names is replaced with the character z. For example, a property named _flag is accessible as zflag, a method named __reset() is accessed as z_reset(), and so on.

  • Some native methods of Java objects are unavailable to TestComplete. For more information, see Object Properties, Fields and Methods That Are Unavailable to TestComplete.

See Also

Testing Java Applications

Highlight search results