Forum Discussion

felix_ossigbona's avatar
felix_ossigbona
New Contributor
12 years ago

Objects - Iterating and Deleting

I have just started using TestComplete so am not overly familiar with it. I have managed to read in some data from an excel file using the sample cade here (http://support.smartbear.com/viewarticle/56908/) but I am now trying to create an array of objects I can use later in my script.



I have worked out how to create a class, an object of that class and set the properties:



function test(){



  // Create Class

  TestClassObj = ODT.Classes.Declare("TestClass");

  TestClassObj.AddProperty("username", "");

  TestClassObj.AddProperty("password", "");

 

  // Create an Object of TestClass

  TestObj = ODT.Classes.New("TestClass");

 

  // Set the properties of the object

  TestObj.username = "felix";

  TestObj.password = "mypassword";

}



For this to work I need to create an ODT item - I did this by right-clicking the Advanced folder and adding a new ODT item (i'm not sure if this can be done in code).



One problem I am having at present is taht I need to manually delete the class from the project before I can run the script again. I have tried deletin the class using:

  ODT.Classes.Delete("TestClass");



But this gives me the following error:

    "Cannot delete the TestClass class, since there are objects that belong to this class"

I'm not sure how to delete an object (I tried the 'delete' keyword, but it gave me the same error)





I also need to be able to iteralte/loop through the objects. For example I want to do something like the following.



{

  num_of_objs = 7;

  objects = [];



  for i = 1:num_of_objs:

    objects.append(ODT.Classes.New("TestClass"));

   ...

   for i in enumerate(particles):

      do stuff to each object

   ...

   for i in enumerate(particles):

      delete objects (so I can delete the class).

}



If someone could help me with the code to do the above it would be greatly appreciated.



Regards,

Felix

2 Replies

  • Ok, I have found out one way to create an array of objects. You first need to create a Group:



      // First create Array to put the Objects in

      data = ODT.Data.AddGroup("TestData");

      arr = data.AddVarOfArrayType("TestArray");

     

    You can then use the AddItemOfClassType method to append objects:



      // Populate array with Objects

      obj = arr.AddItemOfClassType("TestClass");

      obj.username = "felix";

      obj.password = "mypassword";

     

      // Populate array with Objects

      obj = arr.AddItemOfClassType("TestClass");

      obj.username = "frank";

      obj.password = "mypass";



    The only way I have found to iterate throught the objects is to use the .Run() method which means I don't really have control over the loop.



    Hopefully there is another way...

  • I have solved the problem of deleating the objects and classes: use the following before or after you create the class and objects:



      ODT.Data.Clear();

      ODT.Classes.Clear();