Avoiding problems with ToolItems in RCP and SWT applications

Last edited on


Because tool bars (org.eclipse.swt.widgets.ToolBar) and tool items (org.eclipse.swt.widgets.ToolItem) can appear in multiple perspectives/tabs this can make it hard for Squish to identify the respective objects uniquely. Often one will end up with object names that contain "occurrence" properties, which tend to be unreliable.

To avoid this problem area you can use the below function find_first_swt_toolitem() to look up the respective ToolItem via its text or tooltiptext:

import names
def main():

    # Initially recorded, but object name has/relies
    # on occurrence, so look up ToolItem via its text
    # instead 
    #mouseClick(waitForObject(names.Debug_ToolItem_2), 35, 8, 0, Button.Button1)
    mouseClick(find_first_swt_toolitem("Debug"), 35, 8, 0, Button.Button1)

def find_first_swt_toolitem(item_text=None, item_tooltiptext=None):
    if item_text is None and item_tooltiptext is None:
        test.fatal("ERROR: Must specify item_text or item_tooltiptext!")
        return None

    # At least wait for any ToolBar instance; you still may
    # need to snooze() before calling this function
    waitForObject({"isvisible": True, "type": "org.eclipse.swt.widgets.ToolBar"})

    i = 0
    while True:
        n = {"isvisible": True, "type": "org.eclipse.swt.widgets.ToolBar", "occurrence": i}
        if not object.exists(n):
        o = findObject(n)
        children = object.children(o)
        for c in children:
            if item_text is not None and item_text != c.text:
            if item_tooltiptext is not None and item_tooltiptext != c.tooltiptext:
            return c
        i += 1
    test.fatal('ERROR: Could not find ToolItem with text "' + str(item_text) + '" and tooltiptext "' + str(item_tooltiptext) + '"'")
    return None