Calling Functions From Java Classes

Applies to TestComplete 14.71, last modified on April 22, 2021
Information in this topic applies to desktop applications only.

You can call routines that reside in any Java class from your scripts. The following sections provide detailed information about this feature:

Requirements

  • A license for TestComplete Desktop module.

  • The Java Classes Support plugin enabled in TestComplete. The plugin is installed and enabled automatically.

    To check whether the plugin is available, select File | Install Extensions from the TestComplete main menu and search for the plugin in the subsequent dialog. If the plugin is not there, re-install TestComplete.

Calling Java Routines via the JavaClasses Object

You can use the JavaClasses object to access Java classes, their methods and properties. The setup is as follows:

  1. Specify the module of a Java virtual machine.

    In order for the script engine to be able to call functions of Java classes, you should specify the Java virtual machine module that will be used to host the classes. To do this, use the Java Bridge Options dialog. To call the dialog, select Tools | Options from the main menu and then select Engines | Java Bridge from the tree displayed on the left side of the resulting Options dialog. The dialog includes the Java Virtual Machine module location option that allows you to specify the path to the desired file of a Java virtual machine module.

    Specifying the Java Virtual Machine module location.
  2. Make the class available to the script engine.

    To do this, add the desired class to the list in the project’s Java Bridge group of settings. This group contains the list of Java classes whose functions will be available to your scripts through the JavaClasses object and paths to these classes:

    • Select Tools | Current Project Properties from the TestComplete main menu.

    • Select the Java Bridge category.

      Specifying Java classes

      Click the image to enlarge it.

    • To add a new class name to the Java Classes list, click the Add button to the right of the list and then specify the desired class name.

      Use the full class name, including the package name.
    • To specify the path to the specified classes, click the Add Directory or Add JAR Files button to the right of the Class Paths list and select the desired folder or JAR files from the subsequent dialog.

      The Class Path list contains directories and JAR files. Java classes are specified in the Java Classes list above. If you want to add a new Java class to the project settings, click Add to the right of the Java Classes list and the specified class will be automatically added to this list.
    • (Optional.) To modify or rebuild the JAR file added to TestComplete project’s settings, click Unload Classes to the right of the Java Classes list. This button will clear the Java Classes list. The fact is that TestComplete blocks access to JAR files specified in the Java Bridge project settings until the project suite that contains the corresponding project is closed. So, if you try to delete or rebuild this file while the corresponding project is opened in TestComplete, you will get the "Access is denied." prompt. To get access to the specified JAR file, you can either click Unload Classes or close the project suite.

Classes added to the Java Bridge options become available in scripts as child objects of the JavaClasses object. The types defined in a specific class are, in their turn, child objects of the class node. The object corresponding to a type provides access to subtypes, static members and constructors defined in this type. All available classes, types, and type members are displayed in the Code Completion window:

Code Completion with Java Classes

Click the image to enlarge it.

To call a specific method from scripts, use the following syntax:

JavaClasses.package.class.subclass.method(parameters)

Simple data types (such as strings, integers, booleans) passed as parameters are automatically converted to instances of the appropriate Java classes. Parameters of the Object type can be passed to the Java Bridge only if they are descendants of java.lang.Object, otherwise, a type mismatch error occurs.

As you can see, the package and class name (as well as the subclass name) must be separated by dots. Also, note that the dots used in the package name must be replaced with an underscore character.

The syntax above lets you call only static members of Java classes. In order to use non-static methods and properties in scripts, you should first create an instance of the desired class by calling the class constructor or a static member that creates a new class instance.

Note: Normally, a Java constructor has the same name as the name of the class to which it belongs. A class can have any number of constructors all of which have the same name. In order to avoid naming conflicts, TestComplete changes the names of these methods to newInstance. Thus, class constructors will have the following names: newInstance, newInstance_2, newInstance_3, etc.

You can work with objects returned by methods and properties of Java classes in the same way as with other objects. Some value type instances, such as numeric and Boolean values, are OLE-compatible and can be used in scripts directly. To make the String, Decimal, DateTime objects, enumeration values and arrays OLE-compatible, TestComplete adds a special OleValue property to them. To work with structures and reference types (except for strings and arrays), use their internal properties and members.

Calling Java Routines via the JavaRuntime Object

All process objects that correspond to Java applications have the JavaRuntime method that provides access to the Java virtual machine that exists in a Windows process. The object returned by the JavaRuntime method is an instance of the java.lang.Runtime class of the desired Java virtual machine. This object contains the same properties and methods as java.lang.Runtime does, as well as specific properties provided by TestComplete. The objects’JavaClasses property provides access to packages and classes defined in the Java application.

The JavaRuntime.JavaClasses property is similar to the JavaClasses object, and the syntax that is used to call a class member via JavaRuntime is similar to the syntax you use with the JavaClasses object:

Sys.Process("MyJavaApp").JavaRuntime.JavaClasses.package.class.subclass.method(parameters)

Just the same, to call a non-static method or property, you should first create a class instance using the class constructor or a special static member.

The advantage of the JavaRuntime.JavaClasses property over the JavaClasses object is that it lets you create instances of the application’s classes without having to add the classes to the project’s Java Bridge options.

Handling Exceptions That Occurred in Java Applications

TestComplete allows catching and handling exceptions that occurred in Java applications’ code. You can catch these exceptions using standard script statements, such as try … catch and others. For more information on catching exceptions from TestComplete scripts, see Handling Exceptions in Scripts.

TestComplete automatically catches an exception that occurs while testing a Java application and returns a description of it in the following format:

Exception Type: Exception Cause

For instance, the following exception occurs when the Java application under test detects division by zero - java.lang.ArithmeticException: / by zero.

Limitations

TestComplete cannot get access to internal methods and properties of Java objects, if the tested application was launched with a -verbose or -verbose:class command-line arguments. So, if you try to get access to the JavaVMCount or JavaVMVersion property of a Java object, and the tested application was launched with one of these command-line arguments, an error will occur.

See Also

Using External Functions
Project Properties - CLR Bridge Options
Testing .NET Applications
Calling DLL Functions From Tests
TestComplete Helper Objects

Highlight search results