Getting CTabItem by text or tooltip text (Java, SWT)

Last edited on

Overview

Squish identifies identical CTabItem objects that reside in different CTabFolder objects with different object names, usually by introducing the “occurrence” property, for example:

Symbolic nameReal name
names.Project Explorer{'caption':'Project Explorer', 'type':'org.eclipse.swt.custom.CTabItem'}
names.Project Explorer_2{'caption':'Project Explorer', type='org.eclipse.swt.custom.CTabItem', 'occurrence':1}

This can occur for example when having the same view (in the above object names it is the “Project Explorer” view) visible in different (Eclipse) perspectives.

Solution for Squish 5.1 and higher

Add this property to the CTabItem’s real name (will be done by Squish for newly generated/added object names automatically):

parent.visible='true'

So if the real name is…

{'caption':'Outline', 'type':'org.eclipse.swt.custom.CTabItem'}

…change it to…

{'caption''Outline', 'parent.visible':True, 'type':'org.eclipse.swt.custom.CTabItem'}

Solution for Squish 5.0 and lower

In such a case it may be more convenient to use a function that reliably retrieves a reference to the “currently shown” CTabItem, based on its text or tooltip text.

The examples below demonstrate how such a function could be implemented and used.

Example for JavaScript

function main()
{
    startApplication('eclipse');

    //...

    o = find_first_visible_tabitem("Project Explorer");
    mouseClick(o);

    //...
}

function find_first_visible_tabitem(item_text, item_tooltiptext)
{
    if (item_text == null && item_tooltiptext == null) {
        test.fatal("ERROR: Must specify item_text or item_tooltiptext!");
        return null;
    }

    // At least wait for any CTabFolder instance; you still may
    // need to snooze() before calling this function though
    waitForObject("{isvisible='true' type='org.eclipse.swt.custom.CTabFolder'}");
    snooze(2);

    var i = 0;
    while (true) {
        var n = "{isvisible='true' type='org.eclipse.swt.custom.CTabFolder' occurrence='" + i + "'}";

        if (!object.exists(n)) {
            break;
        }

        var o = findObject(n);
        children = object.children(o);
        for (var j = 0; j < children.length; j++) {
            var c = children[j];
            // Ignore everything except instances of
            // com.froglogic.squish.swt.CTabProxy
            test.log("class: " + c["class"]);
            if (c["class"] != "com.froglogic.squish.swt.CTabProxy") {
                continue;
            }

            var item = c.item;

            // Ignore null items
            if (isNull(item)) {
                continue;
            }

            // Ignore items that are not being shown
            // or whose parent is not visible
            if (!item.showing || !item.parent.visible) {
                continue;
            }

            if (item_text != null && item_text != item.text) {
                continue;
            }
    
            if (item_tooltiptext != null && item_tooltiptext != item.tooltiptext) {
                continue;
            }

            return c;
        }
        i += 1;
    }
    return null;
}
test.js

Example for Python

def main():
    startApplication('eclipse')

    #...
    
    o = find_first_visible_tabitem(item_text='Project Explorer')
    mouseClick(o)

    #...

def find_first_visible_tabitem(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 CTabFolder instance; you still may
    # need to snooze() before calling this function though
    waitForObject("{isvisible='true' type='org.eclipse.swt.custom.CTabFolder'}")
    snooze(2)

    i = 0
    while True:
        n = "{isvisible='true' type='org.eclipse.swt.custom.CTabFolder' occurrence='" + str(i) + "'}"

        if not object.exists(n):
            break

        o = findObject(n)
        children = object.children(o)
        for c in children:
            # Ignore everything except instances of
            # com.froglogic.squish.swt.CTabProxy
            if c["class"] != "com.froglogic.squish.swt.CTabProxy":
                continue

            item = c.item

            # Ignore null items
            if isNull(item):
                continue

            # Ignore items that are not being shown
            # or whose parent is not visible
            if not item.showing or not item.parent.visible:
                continue

            if item_text is not None and item_text != item.text:
                continue

            if item_tooltiptext is not None and item_tooltiptext != item.tooltiptext:
                continue

            return c
        i += 1
    return None
test.py