Accessing Native Properties and Methods of .NET Objects

Applies to TestComplete 15.63, last modified on April 23, 2024

TestComplete provides you with access to individual objects of .NET 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 Properties and Methods

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

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

Native Properties of a .NET ListView Object in Object Browser

Click the image to enlarge it.

Note: Besides properties and methods of .NET GUI objects, TestComplete lets you access properties and methods of any .NET class defined in the application and its referenced assemblies. To learn how to do that, see the following topics:

Accessing Non-Visual Objects in .NET Applications

Creating Instances of .NET Classes Defined in Applications

Calling Functions From .NET Assemblies

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

Getting and Setting Native Object Properties

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

For example, you can get and set a .NET form’s title using its native Text property in the following way:

JavaScript, JScript

var title = Aliases.Orders.MainForm.Text;
Aliases.Orders.MainForm.Text = "New Window Title";

Python

title = Aliases.Orders.MainForm.Text;
Aliases.Orders.MainForm.Text = "New Window Title";

VBScript

Dim title
title = Aliases.Orders.MainForm.Text
Aliases.Orders.MainForm.Text = "New Window Title"

DelphiScript

procedure Test;
var title;
begin
  …
  title := Aliases.Orders.MainForm.Text;
  Aliases.Orders.MainForm.Text := 'New Window Title';
  …
end;

C++Script, C#Script

var title = Aliases["Orders"]["MainForm"]["Text"];
Aliases["Orders"]["MainForm"]["Text"] = "New Window Title";

In keyword tests, you can retrieve an object’s property value and save it to a test variable using the Set Variable Value operation. To change a property value, you can use the On-Screen Object Action operation to call the property’s [Set] method. For more information, see Getting and Setting Object Property Values.

Saving a .NET object's native property value to a test variable

Click the image to enlarge it.

Setting a new value for a .NET object's native property

Click the image to enlarge it.

You can also create checkpoints to verify values of native object properties.

Calling Native Object Methods

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, you can call the native Focus method of a .NET button in the following way:

JavaScript, JScript

Aliases.Orders.OrderForm.ButtonOK.Focus();

Python

Aliases.Orders.OrderForm.ButtonOK.Focus();

VBScript

Aliases.Orders.OrderForm.ButtonOK.Focus

DelphiScript

Aliases.Orders.OrderForm.ButtonOK.Focus();

C++Script, C#Script

Aliases["Orders"]["OrderForm"]["ButtonOK"]["Focus"]();

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

Invoking a .NET object's native method

Click the image to enlarge it.

Data Type Casting

In .NET, all data types are objects. As a result, there are some specifics of using values obtained from .NET objects in your tests:

  • .NET integer, double and boolean values are compatible with TestComplete data types and scripting data types, and can be directly used in tests.

  • Values of .NET Decimal and DateTime objects and enumeration members should be accessed via the OleValue property. For example, you can get the window state of a .NET form in the following way:

    Aliases.Orders.MainForm.WindowState.OleValue

    In certain cases OleValue should also be used with .NET strings (System.String objects) - for example, it may be needed to pass this value as a parameter to a user-defined script function or compare the value with another string.

  • TestComplete also extends single-dimensional .NET arrays with the OleValue property that returns a Variant-compatible array. This lets you use native array operations and functions of the scripting languages with the returned array. For example, in VBScript you can use the LBound and UBound functions to determine the array’s lower and upper bounds and access array elements using the parenthesis syntax: arr(index).

    Note that two- and multidimensional .NET arrays do not have the OleValue property. To get elements of an array, use its native Get(index1, index2, ..., indexN) method.

  • To work with other objects, use their internal properties and methods.

Important Notes

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

  • TestComplete provides access to native object properties, fields and methods of any access type - public, protected, private, internal as well as protected internal.

    However, private and protected properties are not displayed by default because a property’s get accessor defined by the application developer can perform critical or unsafe operations in the application that may change the application’s current state. If you need to view private and protected properties and their values, enable the Show hidden properties option in the TestComplete Options dialog.

  • If a native property (or method) has the same name as a test object property (method) provided by TestComplete, the native property (method) can be accessed via the NativeClrObject property. For example, you can access the native Name property of a .NET application’s form in the following way:

    JavaScript, JScript

    Aliases.Orders.MainForm.NativeClrObject.Name

    Python

    Aliases.Orders.MainForm.NativeClrObject.Name

    VBScript

    Aliases.Orders.MainForm.NativeClrObject.Name

    DelphiScript

    Aliases.Orders.MainForm.NativeClrObject.Name

    C++Script, C#Script

    Aliases["Orders"]["MainForm"]["NativeClrObject"]["Name"]

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

  • If the object contains overloaded methods (methods with the same name but different parameters and return value types), TestComplete appends indexes to method names in order to distinguish between them. For example, GetChildAtPoint() and GetChildAtPoint_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.

  • If you need to pass an object as a parameter to a specific .NET application routine, make sure the object meets the following requirements:
    • It must be a .NET object, otherwise, a type mismatch error will occur.
    • The desired .NET class instance must be created on the same domain.
    For more information, see the Creating Instances of .NET Classes Defined in Applications topic.
  • Class constructors are available as methods named zctor(), zctor_2() and so on. For more information on calling class constructors and creating instances of .NET classes, see Creating Instances of .NET Classes Defined in Applications and Calling Functions From .NET Assemblies.

  • For Strip Menu controls: When you access the native Visible property of a Strip menu’s item, the property returns the value that indicates whether the menu item is visible on the screen. This value can differ from the one originally set for the item in your development tool. This happens because, at design time, the value specifies whether the item will be visible when the menu will pop up. During the run time, the value indicates whether the menu item is visible on the screen.

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

See Also

Testing .NET Applications
About Testing .NET Applications

Highlight search results