Forum Discussion

pcherng's avatar
pcherng
Occasional Contributor
13 years ago

Problem with Find method returning objects with the wrong indexes

I am writing a connected application with C# in VS2010 and TestComplete v8.6 for testing an application using lots of Java Swing components. I was trying to find a particular table using the Find method and click on the last row of the table using the ClickCell method. I also had the program print the FullName of the found object to the console.



var table= Sys["Find"]("Name", "[\"SwingObject\"](\"GenericTable\", \"\", 0)", 20000, true);

string test = table["FullName"];


Console.WriteLine(test);

int last_row_index = table["getRowCount"]() - 1;


table["ClickCell"](last_row_index, 0);




 However, I get an error when it tries to click on the cell because the object apparently doesn't exist. Upon checking the console output, I see that it did indeed find the object I intended to find, but when I compare the FullName that is printed on the console against the FullName that is displayed using Object Spy/Object Browser, I notice that several of the index parameters for SwingObject have discrepancies. In particular, all of the index parameters that are printed out to console are 0, which is not the case when looking in the Object Browser.



I further tested by simply copying and pasting the FullName from ObjectBrowser into my code for the assignment of var table, and the code worked fine. However, this is not an ideal solution because the index parameters are not reliable as they can change from run to run and I do not wish to repeatedly change my code just to run this task. 



Am I doing something wrong? Please advise.



Thanks,

-Paul Cherng

4 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Well, again, I think your problem is that you're using the Name property instead of a mapping of multiple properties.  Because you're using the "name" property, you're having to put the name in which, in the object browser, includes the index.  If, however, you use things like WndClass, WndCaption, etc, as a set of properties and values in your Find statement INSTEAD of the Name, perhaps that will work better.



    If you were just calling SwingObject as a method, you could leave off the index, but because of your implementation, that's not going to work.  



    So, to go with what you're looking for,  you could do





    var MyObject = Sys.Find(["Class", "Caption"], ["GenericTable", ""], 20)




    This will search the tree to a depth of 20 objects looking for all objects with a class of GenericTable and a caption that is blank and return it to you (assuming, of course, that the proper property names are "Class" and "Caption" and not something like WndClass and WndCaption or other wise).



    In short: as a rule of thumb, I never use Name, FullName, or MappedName in any of the Find methods as they are already dependant upon other internal workings of TestComplete for "finding" those objects.
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    You're using a single property and value for finding your object.  You might be better off using an array of properties and subsequent values. (see the article http://smartbear.com/support/viewarticle/11728/).



    So, you might end up with something like





    var table= Sys["Find"](["Name","Caption", "[\"SwingObject\", "MyTable"](\"GenericTable\", \"\", 0)", 20000, true);




    Of course, not knowing your object, this is just a mock up to demonstrate.  The more properties and values you use, the more you can narrow down your search parameters.  The art is finding JUST the right combination where you're not using as few properties as possible and still guarenteeing a successful find.



    Might I suggest you replace Find with FindChild since you're looking specifically for a child object?  See http://smartbear.com/support/viewarticle/11716/
  • pcherng's avatar
    pcherng
    Occasional Contributor
    Thanks for the response Robert. However, I am fairly certain that it is finding the right object with just that single property and value because I know for a fact that there are no other objects on this screen with a similar FullName. For instance, in the FullName of the table object displayed in ObjectBrowser I wish to find, there is a part that reads:



    ["SwingObject"]("JPanel", "active table", 1)



    But in the console output FullName, that same part of the FullName reads:



    ["SwingObject"]("JPanel", "active table", 0)



    There is only one object in the entire object browser tree that is a JPanel with caption "active table", so I don't understand why it is even finding another object that has the same class and caption, but different index which doesn't even exist according to the object browser. Is there any way for me to just wildcard the index?



    I tried both of your suggestions and I ran into the same problems.
  • pcherng's avatar
    pcherng
    Occasional Contributor
    Oh, I see. I didn't realize that Names were bad to use as property and value parameters. After using different identifiers as parameters for FindChild, I was able to successfully find and click the object. Thanks!