Objects.Compare Method

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

Description

The behavior of the Compare method depends on whether the update feature is enabled.

The Compare method performs a property-by-property comparison of two objects and returns True if all the values are identical. All child properties stored in the collection are compared as well. If neither object is found during the comparison, Compare returns False and generates an error string, which you can retrieve by using the LastError property of the Objects object and post this string to the test log.

The Compare method also lets you update the stored property collection during the script execution. TestComplete will do that if the update feature is enabled, that is if both the global Update files option and the stored collection’s Update option are checked. In this case, Objects.Compare works similar to the Objects.Update method - it replaces property values of the stored object and all its child objects with values of the corresponding properties of the specified object and its children. After that, Compare will post a message reporting that the collection was updated to the log and return True.

Declaration

Objects.Compare(AObject, Name, ReportDifference, MessageType)

AObject [in]    Required    A tested object    
Name [in]    Required    String    
ReportDifference [in]    Optional    Boolean Default value: True   
MessageType [in]    Optional    Variant Default value: lmWarning   
Result Boolean

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

AObject

The tested object whose properties you want to compare with those of the object referred to by the Name item of the Objects collection item. If the update feature is enabled, AObject specifies the tested object whose property values will replace the corresponding property values of the stored object specified by the Name parameter.

Name

Unique name of the stored object’s property collection.

ReportDifference

Specifies whether the Compare method posts a message to the test log if the comparison fails.

MessageType

The type of message to be posted to the test log in case the comparison fails and ReportDifference is True. Possible values:

Constant Value Description
lmNone 0 Do not post any message.
lmMessage 1 Post an informative message.
lmWarning 2 Post a warning message.
lmError 3 Post an error message.

Result Value

True if the compared objects are identical and False otherwise.

Also True, if Compare has updated the stored property collection.

Remarks

To save collections of an object’s properties, use the Objects.Save method.

You can also use the Stored.Object method to check object properties.

Example

The following code demonstrates how you can save properties of the desired object to an item of the Objects collection and compare the values of the stored properties with the actual values of the properties.

JavaScript, JScript

function Test()
{
  var w, PropertyNames, CollectionName;
  w = Sys.Desktop.ActiveWindow();

  // Prepares the property list
  PropertyNames = "Top Left Height Width";
  // Sets the name of the property collection
  CollectionName = "ActiveWindowProps";
  // Saves the values of the properties specified in PropertyNames.
  // Note that the Save method also has the fourth parameter
  // that lets you not to save certain properties.
  Objects.Save(w, CollectionName, PropertyNames);
  ...
  // Compares property values
  if (! Objects.Compare(w, CollectionName))
    Log.Message("Properties have been changed.");
}

Python

def Test():
   w = Sys.Desktop.ActiveWindow()
   # Prepares the property list
   PropertyNames = "Top Left Height Width"
   # Sets the name of the property collection
   CollectionName = "ActiveWindowProps"
   # Saves the values of the properties specified in PropertyNames.
   #Note that the Save method also has the fourth parameter
   # that lets you not to save certain properties.
   Objects.Save(w, CollectionName, PropertyNames)
   # ...
   # Compares property values
   if not Objects.Compare(w, CollectionName):
     Log.Message("Properties have been changed.")

VBScript

Sub Test
  Set w = Sys.Desktop.ActiveWindow

  ' Prepares the property list
  PropertyNames = "Top Left Height Width"
  ' Sets the name of a property collection
  CollectionName = "ActiveWindowProps"
  ' Saves the values of the properties specified in PropertyNames.
  ' Note that the Save method also has the fourth parameter
  ' that lets you not to save certain properties.
  Objects.Save w, CollectionName, PropertyNames
  ...
  ' Compares property values
  If Not Objects.Compare(w, CollectionName) Then
    Log.Message "Properties have been changed."
  End If
End Sub

DelphiScript

var
  w, PropertyNames, CollectionName : OleVariant;
begin
  w := Sys.Desktop.ActiveWindow();
  // Prepares the property list
  PropertyNames := 'Top Left Height Width';
  // Sets the name of the property collection
CollectionName := 'ActiveWindowProps';
  
  // Saves the values of the properties specified in PropertyNames.
  // Note that the Save method also has the fourth parameter
  // that lets you not to save certain properties.
Objects.Save(w, CollectionName, PropertyNames);
  ...
  // Compares property values
  if not Objects.Compare(w, CollectionName) then
    Log.Message('Properties have been changed.');
end;

C++Script, C#Script

function Test()
{
  var w, PropertyNames, CollectionName;
  
  w = Sys["Desktop"]["ActiveWindow"]();

  // Prepares the property list
  PropertyNames = "Top Left Height Width";
  // Sets the name of the property collection
  CollectionName = "ActiveWindowProps";
  // Saves the values of the properties specified in PropertyNames.
  // Note that the Save method also has the fourth parameter
  // that lets you not to save certain properties.
  Objects["Save"](w, CollectionName, PropertyNames);
  ...
  // Compares property values
  if (! Objects["Compare"](w, CollectionName) )
    Log["Message"]("Properties have been changed.");
}

See Also

Load Method
Save Method
Update Method
About Objects Collection
Adding Property Collections to Stores
About Object Checkpoints
Alternatives to Property Checkpoints

Highlight search results