TestComplete 8 Sneak Peek: Calling Methods of Java Applications
  April 07, 2010

TestComplete 8 provides a new feature that allows you to call methods of Java classes from your test scripts. Now all Process objects that correspond to Java applications have the JavaRuntime method that provides access to a Java virtual machine that exists in a Windows process. The object returned by this 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 the java.lang.Runtime class, as well as specific properties provided by TestComplete. The object’s JavaClasses property provides access to packages and classes defined in the Java application. By using this property, you can access static non-visual objects and even create instances of tested application’s classes directly from your script code.

To call a specific method of Java classes from test script, use the following syntax:



TestComplete also includes a new JavaClasses object that provides access to Java classes, types and type members. In order for you to be able to call routines from a Java class via this object, the following settings must be specified:

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.

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 test scripts through the JavaClasses object and paths to these classes:

  • Right-click your project in the Project Explorer panel and select Edit from the ensuing context menu or simply double-click the project in the Project Explorer. TestComplete will show the project editor in the Workspace panel.
  • Click Properties at the bottom of the project editor to display the Project Properties page.
  • Select Java Bridge from the list on the left of this page. This will open the Java Bridge settings for editing. These settings specify classes whose functions can be called from test scripts and the location where these classes are stored.

    Specifying Java classes

  • 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.
  • To specify the path to the specified classes, click the Add Directory or Add JAR File button to the right of the Class Paths list and select the desired folder or file from the subsequent dialog.

After specifying the classes in the Java Bridge options, they become available in test scripts as child objects of the JavaClasses object. The types defined in a specific class are 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 window

To call a specific method using the JavaClasses object, use the following syntax:


Note that this syntax only lets you call 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. 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.

As you can see, TestComplete 8 provides you with the ability to get access to Java classes. We hope this feature helps you better test your Java applications and make your automated tests more powerful and effective. If you are interested in trying TestComplete 8, apply for the Beta version.

See also:


[dfads params='groups=937&limit=1&orderby=random']