Forum Discussion

kcarr86's avatar
kcarr86
Occasional Contributor
2 years ago

Designing a for/while loop to expand varying objects within objects

I'm trying to design a loop that will use the Expand method to open each "+" sign in the posted screenshot. Here are the issues I'm having:

 

1. I don't know how to teach TC to recognize the "+" sign as an Object. TC recognizes the overall window as an Object (which I'll refer to as WindowObject) but I want to focus specifically on the "+" signs within the window. Is there a way to select an object within an object? So far the closest I've gotten to identifying individual "+" signs is by calling out the window object's row (i.e. The first "+" will expand if I use WindowObject.Expand(0), the second will expand if I include an additional WindowObject.Expand(1) line, etc). 

 

2. While I can successfully expand those "+" signs by specifically referencing each row of WindowObject, I plan on testing 100s of these windows and they all have different numbers of rows. Ideally, I want my loop to be able to break if there are no more "+" signs to expand but I'm struggling to design my loop in a way that won't generate errors. This is what I've come up with so far when attempting to finetune my test in Javascript

 

var i = 0;

while (i < 3);  //The 3 is arbitrary, I just wanted to create a boundary to stop the loop

WindowObject.Expand(i);

i++;

 

Running that test will successfully expand the three "+" signs in the screenshot but will also terminate on an error indicating that "Row 3 is out of bounds" since there isn't a 4th "+" to expand. 

 

I'm still fairly new to TestComplete so please let me know if there is anything I need to clarify or if I've left out some important information that could resolve this issue.

7 Replies

  • scottroutesmart's avatar
    scottroutesmart
    Occasional Contributor

    I wrote custom code to detect the rows in a table and then click on a button within another cell of the table.  Here is a basic script I use to identify then iterate through the rows to find the desired value, then click on the corresponding button...

    let TotalRows = Aliases.browser.PAGENAME.TABLENAME.ChildCount;
    // Log.Message(TotalRows, "", 0);

    for (let i = 1; i < TotalRows; i++) { //Starts on the first data row, rather than the header row.
      if (Aliases.browser.PAGENAME.TABLENAME.Cell(i, 0).contentText == DESIREDVALUE) {
        Aliases.browser.PAGENAME.TABLENAME.Cell(i, 3).FindElement("[type='button']:nth-child(1)").Click();
        return true;
      }
    }

    I have also found running loops within loops tends to slow down TestComplete greatly, but haven't foun a great workaround to speed it up.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Get the number of rows of your grid control. Iterate through each row and expand the row. The Expand method may return a value, which you need to check - you should be able to use this to break the loop. Or you could use IsExpandable/IsExpandable or something similar to check the state of the row.

    • kcarr86's avatar
      kcarr86
      Occasional Contributor

      Thank you for your suggestions. When you say "get the number of rows of your grid control" are you saying I should know the number of rows in each of my test cases first or that there's a method in TestComplete that will count and return the number of rows?

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    If you use the Object Spy tool on your control and have a look at the properties. There may be a property that indicates the number of rows.

     

    For example,

    This shows the number of items in the list control.

  • kcarr86's avatar
    kcarr86
    Occasional Contributor

    Thanks for both of your replies! 

     

    I was able to take your suggestions and utilize them in the following code:

     

    var RC, i;

    RC = WindowObject.RowCount;

    for (i = 0; i<RC; i++)

       WindowObject.Expand(i)

     

    Using that code enabled TC to expand each occurrence of "+" in the WindowObject grid!

     

    However, I ran into an issue where one of the test cases contained a parent row with expandable child rows (please see attached photo). I tried using the code above and it didn't work but when I included additional code to expand the child rows, the parent row expanded and the child rows remained collapsed. The amended code in bold is below:

     

    var RC, i;

    RC = WindowObject.wChildView(0, "Employees").RowCount;

    for (i = 0; i<RC; i++)

       WindowObject.wChildView(0, "Employees").Expand(i)

     

    Does that mean that in order to open those child rows, I would have to declare a child row within a child row? And since the first parameter of wChildView is asking for the row number, is there a way to iterate on this value within my loop to expand any additional rows?

     

    rraghvani, when reviewing a grid that contained 1 parent row and 2 child rows, ObjectSpy revealed a total of 1 rows. However, the ChildCount is 14 so I'm wondering if that figure includes the expandable rows I'm trying to open. 

    • scottroutesmart's avatar
      scottroutesmart
      Occasional Contributor

      I would guess you probably need to find an identifier for the child row + boxes, separate from those of the parent row.  Use the Object Spy to see if there is a unique identifier you can use to expand those children rows.  You can also ask your dev team to add a specific identifier to the code so that you know exactly what to look for, such as "childexpander"  Based on your screenshot, I see about 12-14 rows total depending on how the structure of table is laid out.  So, I think your figure probably includes the expandable rows you are trying to open.

       

      I was going to suggest that you may need to refresh TC's cache when you open the menu, but if it is already aware of those rows then that is likely not needed.  I have found when I go to a page and perform an action which makes an API call to the server, that after the UI content is refreshed that I need to refresh TCs cache by doing something like this...

      Aliases.browser.PAGENAME.TABLENAME.Refresh();

      I would suggest using the "Object Browser" to figure out the structure of the page you are using and how rows are structured within.  It takes some time to expand the objects and find the specific rows you need, but it often helps me know what children are being identified at each level.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    What you have written so far, you're on the right line - it's best to understand how the control behaves, i.e. use the Object Spy tool to see what properties are exposed, when the control is expanded/collapsed. If the childcount remains the same, then the control is showing all the rows regardless of it being expanded/collapsed.