Avoiding problems with ToolItems in RCP and SWT applications

Last edited on

Overview

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():
    startApplication('my_eclipse_or_rcp_application')

    # 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"})
    snooze(2)

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