Search code examples
vbscriptautomationtabindexautomated-teststestcomplete

How to raise a click event when selecting a tab in TTabSet via TabIndex?


I have a TestComplete test that selects a tab in a TTabSet using the TabIndex property:

Tab.TabIndex = 1

That works great. However, I am noticing that some of the objects on that tab require the click event to occur to be enabled. That said, how can I select the tab item via the TabIndex with some sort of a click event involved?


Solution

  • Instead of assigning a value to TabIndex, you can call TTabSet's ItemRect method to get the coordinates of a tab by its index, and then pass these coordinates to TestComplete's Click method. Here's an example:

    Sub Main
      Set tabSet = Sys.Process("Project1").VCLObject("Form1").VCLObject("TabSet1")
    
      For i = 0 To tabSet.Tabs.Count
        ClickTab tabSet, i
        Delay 1000
      Next
    End Sub
    
    Sub ClickTab(TTabSet, ItemIndex)
      Dim rect, x, y
      Set rect = TTabSet.ItemRect(ItemIndex)
      x = (rect.Left + rect.Right)  / 2
      y = (rect.Top  + rect.Bottom) / 2
    
      TTabSet.Click x, y 
    End Sub
    

    However, this approach requires that:

    • Your tested application is built with debug information (this makes public members, including ItemRect, available to TestComplete).
    • The application code contains the ItemRect method call (otherwise this method will be left out of the EXE by Delphi's smart linker, so it will be unavailable to TestComplete).

    Also, if your tab control has more tabs than are displayed (that is, Tabs.Count > VisibleTabs), you'll need to scroll it to make the needed tab visible first. You can do this, for example, using the FirstIndex property:

    Sub ClickTab(TTabSet, ItemIndex)
      If ItemIndex >= TTabSet.FirstIndex + TTabSet.VisibleTabs Then
        TTabSet.FirstIndex = ItemIndex
      End If
    
      Dim index, rect, x, y
      index = ItemIndex - TTabSet.FirstIndex
    
      Set rect = TTabSet.ItemRect(index)
      x = (rect.Left + rect.Right)  \ 2
      y = (rect.Top  + rect.Bottom) \ 2
    
      TTabSet.Click x, y 
    End Sub