Addressing Objects in JavaFX Applications

Applies to TestComplete 15.20, last modified on January 19, 2022

There are several ways to refer to objects in JavaFX applications. If you use Name Mapping in your test project, you can refer to the application objects using their names specified in Name Mapping. For objects that are not in Name Mapping, you can use the naming syntax that directly includes identification properties and values of these objects. This topic explains both approaches.

About Object Names in the Object Browser

You can explore the object structure of the tested application in the Object Browser. Below is a sample view of a JavaFX application's hierarchy displayed in the Object Browser:

A view of a JavaFX application in the object tree

Click the image to enlarge it.

Here, the Process("java") branch of the Sys object corresponds to your tested JavaFX application and the other objects correspond to the application’s controls and objects.

To refer to an individual object in the tested JavaFX application, select the needed object in the Object Browser or Object Spy. These tools display the selected object’s name above the member list. From the right-click menu, you can copy this name to the clipboard and then insert it into your test.

Copying the object name from the Object Browser

Click the image to enlarge it.

You may notice that this name differs from what you see in the object tree on the left of the Object Browser. This is because the name displayed at the top of the panel uses object aliases if they are defined, whereas the object tree uses the naming syntax that includes identification properties. The differences between the two object naming syntaxes are explained in the Addressing JavaFX Objects Using Name Mapping and Aliases and Addressing JavaFX Objects Without Name Mapping sections below.

You can also learn the object names in different syntaxes by looking at the following object properties:

  • MappedName - the object’s fully-qualified alias (including the aliases of all parent objects). This name is used for the object by default in your tests. Note that if the selected object is not in Name Mapping, this property is empty.

  • Name - the object name that includes the object's identification properties. This is the same name you see in the Object Browser’s object tree. For an explanation of this syntax, see below.

  • FullName - the object’s fully-qualified name in the syntax that includes the identification properties. This name consists of the object’s Name property value and the Names property value of all of the object's parent objects. See below.

Addressing JavaFX Objects Using Name Mapping and Aliases

By default, TestComplete uses object names from Name Mapping (the object repository) to refer to individual objects in JavaFX applications under test. These names are called aliases. When you are recording a test, TestComplete automatically adds the recorded objects to Name Mapping and generates aliases for them.

You can see the available object aliases and add new aliases in the Name Mapping editor.

Sample Name Mapping for a JavaFX application

Here, Mapped Objects and Aliases display the hierarchy of mapped objects and their aliases, respectively. The fully-qualified alias that can be used to refer to an object is displayed in the top right part of the Name Mapping editor (see the image above). It starts with Aliases and includes aliases of all of the object’s parent objects separated by dots (in VBScript, JScript, Python and DelphiScript projects) or square brackets (in C++Script and C#Script projects). For example, in the image above you can see that you can refer to the checkbox check box object using the following name:

JavaScript, JScript

Aliases.java.Stage.checkbox

Python

Aliases.java.Stage.checkbox

VBScript

Aliases.java.Stage.checkbox

DelphiScript

Aliases.java.Stage.checkbox

C++Script, C#Script

Aliases["java"]["Stage"]["checkbox"]

You can copy object aliases from the Name Mapping editor to the clipboard and use them when manually adding test operations over the tested application’s objects.

The following example shows a sample item selection operation over the ComboBox combo box control specified by an alias:

A keyword test operation over a JavaFX object addressed by using its alias
Note: For simplicity, keyword tests do not display the Aliases identifier at the beginning of object aliases.

Also, keyword tests may not display intermediate objects in the application’s object hierarchy. To view the full name of an operation’s target object, double-click the operation or increase the Maximum depth option in the Keyword Test Editor - Groups options.

JavaScript, JScript

Aliases.java.Stage.checkbox.ClickButton(cbChecked);

Python

Aliases.java.Stage.checkbox.ClickButton(cbChecked);

VBScript

Call Aliases.java.Stage.checkbox.ClickButton(cbChecked)

DelphiScript

Aliases.java.Stage.checkbox.ClickButton(cbChecked);

C++Script, C#Script

Aliases["java"]["Stage"]["checkbox"]["ClickButton"](cbChecked)

Note that the fully-qualified mapped name for this object is the following:

JavaScript, JScript

NameMapping.Sys.java.Stage.Scene.checkbox

Python

NameMapping.Sys.java.Stage.Scene.checkbox

VBScript

NameMapping.Sys.java.Stage.Scene.checkbox

DelphiScript

NameMapping.Sys.java.Stage.Scene.checkbox

C++Script, C#Script

NameMapping["Sys"]["java"]["Stage"]["Scene"]["checkbox"]

Since the Scene object is of no interest for our test, we exclude it from the path to the checkbox object in the Alias tree, so the object hierarchy becomes shorter. Using aliases lets you make your scripts shorter and more readable.

For more information about using Name Mapping and aliases, see Name Mapping and Access Objects by Aliases.

Addressing JavaFX Objects Without Name Mapping

Your tests can work not only with applications and objects added to Name Mapping, but also with any other running applications, their windows and controls. These objects can be addressed by using a syntax that directly includes identification property values.

Consider the following sample code and keyword test that illustrate how you can refer to an object in your JavaFX application:

A keyword test operation over a JavaFX object that is not in Name Mapping

JavaScript, JScript

Sys.Process("java").JavaFXObject("Stage", "JavaFX").JavaFXObject("Scene", "").JavaFXObject("button").ClickButton(cbChecked);

Python

Sys.Process("java").JavaFXObject("Stage", "JavaFX").JavaFXObject("Scene", "").JavaFXObject("button").ClickButton(cbChecked);

VBScript

Call Sys.Process("java").JavaFXObject("Stage", "JavaFX").JavaFXObject("Scene", "").JavaFXObject("button").ClickButton(cbChecked)

DelphiScript

Sys.Process('java').JavaFXObject('Stage', 'JavaFX').JavaFXObject('Scene', '').JavaFXObject('button').ClickButton(cbChecked);

C++Script, C#Script

Sys["Process"]("java")["JavaFXObject"]("Stage", "JavaFX")["JavaFXObject"]("Scene", "")["JavaFXObject"]("button")["ClickButton"](cbChecked);

To learn the exact syntax for referring to a particular object, use the Object Browser - the object tree and the FullName property of objects always use this naming syntax. See the About Object Names in the Object Browser section above.

Let’s have a closer look at this notation and its components.

  • The notation starts with Sys object that represents the operating system.

  • The next object, Process("java"), corresponds to the Java virtual machine on which the Java code is executed.
  • The objects represent windows, top-level visual containers and visual objects in the JavaFX application. TestComplete uses the following default syntax for these objects:

    JavaFXObject(NativeName)

    Here, NativeName is the object name as it is specified in the tested application’s source code (the string that is returned by the JavaFXObjectName object’s property).

    Another possible syntax for JavaFX objects is:

    JavaFXObject(JavaFXClassName, Text, [Index])

    Instead of the object’s native name, it uses its JavaFX class name (the class name as it is specified by JavaClassName), window caption (the value returned by the JavaFXObjectText property) and (optionally) index among sibling objects with the same class and text.

    The Index parameter is used only if the object contains two or more child objects that have the same class name and caption.

    This syntax is used in the following cases:

    • TestComplete cannot determine the object name that is specified in the application code.

    • There are several objects with the same name.

    For more information about the JavaFX object addressing syntax and its parameters, see the description of the JavaFXObject notation.

For examples of this syntax applied to objects in your tested JavaFX application, examine your application in the Object Browser. You can also copy this syntax from the Object Browser and paste it to your test in order not to manually type it.

Remarks
  • The JavaFXObject notation is used for JavaFX GUI objects of any type - forms, buttons, combo boxes, grids and so on. The word Object here does not mean that TestComplete does not identify the object type. Just the opposite, TestComplete supports many different types of JavaFX GUI objects and provides high-level methods and properties for automating operations over them. See Support for JavaFX Applications' Controls for more information.

  • If the Use native object names for TestComplete object names setting is disabled in your test project, TestComplete uses the JavaFXObject(JavaFXClassName, Text, [Index]) rather than JavaFXObject(NativeName) notation for JavaFX objects by default. For example, it uses this notation in the Object Browser and also during test recording when Name Mapping is not used.

    Note, however, that this setting affects only the default object naming syntax used in TestComplete panels and dialogs. When creating tests manually, you can use both the JavaFXObject(JavaFXClassName, Text, [Index]) and JavaFXObject(NativeName) notations equally well, regardless of this setting.

Specifics of the JavaFX Object Hierarchy

JavaFX applications can contain hundreds of objects and some of them can actually be of no interest, since they do not affect your tests in any way. You may need to exclude unnecessary objects in order to reduce the object hierarchy, improve the tests' readability and speed up the playback.

TestComplete projects contain the JavaFX nodes property that specifies JavaFX objects to be included in the object hierarchy. This property affects both the recording and playback. To view or modify this property:

  1. Double-click your project in the Project Explorer.

  2. In the project editor, switch to the Properties tabbed page.

  3. In the tree on the left, select Open Applications | JavaFX.

The JavaFX nodes list contains class names of JavaFX internal objects that TestComplete will expose. The list includes several default presents. It also includes the * preset that exposes all the hierarchy of JavaFX applications. We do not recommend that you use this preset, because it takes long to expose all the objects and can slow down your tests.

Using the JavaFX nodes property you can make the application hierarchy more plain and readable and exclude objects that are of no interest from the hierarchy.

Keep in mind that adding or removing classes inherited from javafx.scene.Parent affects the object position and indexes in the object hierarchy.

Below is an example of the application hierarchy exposed by TestComplete with different settings enabled:

All presets are disabled
All presets are disabled
Default presets are enabled
Default presets are enabled
* preset is enabled
* preset is enabled

The object hierarchy can change significantly when you add new classes or remove the existing ones. Configure the JavaFX nodes list before you start creating tests. If you change the list after creating a test, the test may fail.

Combining Mapped and Unmapped Object Names

You can combine aliases specified in Name Mapping and the JavaFXObject notation when referencing an object hierarchy in tests. For example, you can use aliases for parent objects and the JavaFXObject notation for child objects:

JavaScript, JScript

Aliases.java.stage.JavaFXObject("Scene", "").JavaFXObject("button").ClickButton(cbChecked);

Python

Aliases.java.stage.JavaFXObject("Scene", "").JavaFXObject("button").ClickButton(cbChecked);

VBScript

Aliases.java.stage.JavaFXObject("Scene", "").JavaFXObject("button").ClickButton(cbChecked)

DelphiScript

Aliases.java.stage.JavaFXObject('Scene', '').JavaFXObject('button').ClickButton(cbChecked);

C++Script, C#Script

Aliases["java"]["stage"]["JavaFXObject"]("Scene", "")["JavaFXObject"]("button")["ClickButton"](cbChecked);

However, keep in mind that aliases must always precede the JavaFXObject notation in the object hierarchy. Aliases cannot be used after the JavaFXObject notation within the same statement. That is, the following statement is invalid:

JavaScript, JScript

Sys.Process("java").JavaFXObject("Stage", "JavaFX").Scene.button.ClickButton(cbChecked);

Python

Sys.Process("java").JavaFXObject("Stage", "JavaFX").Scene.button.ClickButton(cbChecked);

VBScript

Sys.Process("java").JavaFXObject("Stage", "JavaFX").Scene.button.ClickButton(cbChecked)

DelphiScript

Sys.Process('java').JavaFXObject('Stage', 'JavaFX').Scene.button.ClickButton(cbChecked);

C++Script, C#Script

Sys["Process"]("java")["JavaFXObject"]("Stage", "JavaFX")["Scene"]["button"]["ClickButton"](cbChecked);

Accessing Non-Visual Objects in JavaFX Applications

JavaFX applications can include non-visual objects that are not displayed in the Object Browser. However, you can access these objects from your tests.

To get access to a non-visual object, you need to find a property, field or method of another object (visual or non-visual) that would return a reference to the desired object. To find the needed property or method, examine the tested JavaFX application in the Object Browser or ask the application developers where you can find it.

See Also

Testing JavaFX Applications
Testing JavaFX Applications - Overview
Support for JavaFX Applications' Controls
Accessing Native Methods of JavaFX Objects

Highlight search results