AddProperty Method

Applies to TestComplete 15.64, last modified on May 16, 2024

The object-driven testing (ODT) functionality is deprecated. Do not use it to create new tests. It will be removed from the product in one of the future releases. As an alternative, you can create custom classes in your scripts. For more information, see Alternatives to the ODT functionality.

Description

The Object.AddProperty method adds a new property to the object and returns this property as the Property object.

Declaration

ObjectObj.AddProperty(Name, Value)

ObjectObj An expression, variable or parameter that specifies a reference to an Object object
Name [in]    Required    String    
Value [in]    Optional    Variant    
Result A Property object

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

Name

Specifies the name of the new property. This name must be unique within the object. If the object already contains a method or property with the specified name, an error will occur. Since the property name is used to address the property in scripts, it must match the naming rules of the selected scripting language (it must be a valid identifier).

Value

Specifies the property value: string, integer, date, object, etc. Value is used as a default property value. For instance, if Value specifies an object, the property will hold a reference to this object.

Note: To add an array property, use the AddPropOfArrayType method. To add a property that will hold an instance of an ODT class, use the AddPropOfClassType method.

Result Value

The new property represented as a Property object.

Remarks

Note that a new property is added to the Object_Obj object only, it is not added to other objects or to the class on which Object_Obj is based.

Example

The following code snippet declares a new class, adds two properties to it, creates an object that contains an instance of the new class and adds another property to the object. After that, the code iterates through the object’s properties and posts them to the test log.

JavaScript, JScript

function ObjectSample()
{
  var NewClass, MyObject, Prop;
  // Declares a new class
  NewClass = ODT.Classes.Declare("NewClass");
  // Adds properties to the new class
  NewClass.AddProperty("Property1", 10);
  NewClass.AddProperty("Property", "TestString");

  // Creates an object
  MyObject = ODT.Classes.New("NewClass");
  // Adds a new property to the object
  MyObject.AddProperty("Property3", true);

  // Iterates through the object’s properties
  for (var i = 0; i < MyObject.PropertyCount; i++)
  {
    // Posts the property’s name and value to the test log
    Prop = MyObject.Properties(i);
    Log.Message(Prop.Name + " : " + Prop.Value);
  }

}

VBScript

Sub ObjectSample

  ' Declares a new class
  Set NewClass = ODT.Classes.Declare("NewClass")
  ' Adds properties to the new class
  Call NewClass.AddProperty("Property1", 10)
  Call NewClass.AddProperty("Property", "TestString")

  ' Creates an object
  Set MyObject = ODT.Classes.New("NewClass")
  ' Adds a new property to the object
  Call MyObject.AddProperty("Property3", True)

  ' Iterates through the object’s properties
  For i = 0 To MyObject.PropertyCount - 1
    ' Posts the property’s name and value to the test log
    Set Prop = MyObject.Properties(i)
    Log.Message Prop.Name & " : " & Prop.Value
  Next

End Sub

DelphiScript

procedure ObjectSample();
var NewClass, MyObject, Prop, i;
begin
  // Declares a new class
  NewClass := ODT.Classes.Declare('NewClass');
  // Adds properties to the new class
  NewClass.AddProperty('Property1', 10);
  NewClass.AddProperty('Property', 'TestString');

  // Creates an object
  MyObject := ODT.Classes.New('NewClass');
  // Adds a new property to the object
  MyObject.AddProperty('Property3', true);

  // Iterates through the object’s properties
  for i := 0 to MyObject.PropertyCount - 1 do
  begin
    // Posts the property’s name and value to the test log
    Prop := MyObject.Properties[i];
    Log.Message(Prop.Name + ' : ' + aqConvert.VarToStr(Prop.Value));
  end;

end;

C++Script, C#Script

function ObjectSample()
{
  var NewClass, MyObject, Prop;
  // Declares a new class
  NewClass = ODT["Classes"]["Declare"]("NewClass");
  // Adds properties to the new class
  NewClass["AddProperty"]("Property1", 10);
  NewClass["AddProperty"]("Property", "TestString");

  // Creates an object
  MyObject = ODT["Classes"]["New"]("NewClass");
  // Adds a new property to the object
  MyObject["AddProperty"]("Property3", true);

  // Iterates through the object’s properties
  for (var i = 0; i < MyObject["PropertyCount"]; i++)
  {
    // Posts the property’s name and value to the test log
    Prop = MyObject["Properties"](i);
    Log["Message"](Prop.Name + " : " + Prop["Value"]);
  }

}

See Also

AddPropOfArrayType Method
AddPropOfClassType Method
DeleteProperty Method
Properties Property
AddMethod Method
AddProperty Method

Highlight search results