Testing WPF Tooltips

Last edited on

Overview

Majority of WPF controls have the tooltip property in Squish. That property, of course, can be used for testing. Sometimes, however, the tooltips can be complex objects. For example, in the picture below, the tooltip is composed of two Labels.

In the cases like this a little bit of scripting is needed and the main idea goes as follows:

You can of course apply this on simple tooltips as well, in order to verify that they are actually displayed.

Python Example

def trigger_tooltip(object_name):
    """ Triggers tooltip display for a given object."""
    obj = waitForObject(object_name)

    # Move the mouse cursor over the object center:
    mouseMove(obj, obj.width / 2, obj.height / 2)

    # Wait a bit until the tooltip is displayed:
    snooze(1)

def find_tooltip():
    """ Returns the tooltip component if displayed."""
    for top in object.topLevelObjects():
        if top['class'] == 'System.Windows.Controls.ToolTip':
            return top
    return None

def find_labels(obj):
    """ Returns all children of type Label for the given object."""
    labels = []
    if obj:
        if obj.type == 'Label':
            labels = [ obj ]
        for child in object.children(obj):
            labels += find_labels(child)
    return labels

def get_text(labels, separator=" "):
    """ Returns the text of all Labels as one string."""
    text = [ label.text for label in labels ]
    return separator.join(text)

def check_tooltip(expected_text):
    """ Checks whether the current tooltip has the expected text."""
    tooltip = find_tooltip()
    labels = find_labels(tooltip)
    text = get_text(labels)
    test.compare(text, expected_text)

# Other methods ...
def main():
    startApplication("<your_application>")

    # Some other code ...
    trigger_tooltip("<your_component>")          
    check_tooltip("<Expected Tooltip Text>")
    
    # Some other code ...
test.py

JavaScript Example

/**
 * Triggers tooltip display for a given object.
 */
function trigger_tooltip(object_name) {
    var obj = waitForObject(object_name);

    // Move the mouse cursor over the object center:
    mouseMove(obj, obj.width / 2, obj.height / 2);

    // Wait a bit until the tooltip is displayed:
    snooze(1);
}

/**
 * Returns the tooltip component if displayed.
 */
function find_tooltip() {
    var topLevels = object.topLevelObjects();
    for (var i = 0; i < topLevels.length; ++i) {
        var top = topLevels[i];
        if (top['class'] == 'System.Windows.Controls.ToolTip') {
            return top;
        }
    }
    return undefined;
}

/**
 * Returns all children of type Label for the given object
 */
function find_labels(obj) {
    var labels = [];
    if (obj) {
        if (obj.type == 'Label') {
            labels = [ obj ];
        }
        var children = object.children(obj);
        for (var i = 0; i < children.length; ++i) {
            labels = labels.concat(find_labels(children[i]))
        }
    }
    return labels;
}

/**
 * Returns the text of all Labels as one string.
 */
function get_text(labels, separator=" ") {
    var text = [];
    for (var i = 0; i < labels.length; ++i) {
        text.push(labels[i].text);
    }
    return text.join(separator);
}

/**
 * Checks whether the current tooltip has the expected text.
 */
function check_tooltip(expected_text) {
    var tooltip = find_tooltip();
    var labels = find_labels(tooltip);
    var text = get_text(labels);
    test.compare(text, expected_text);
}

// Other methods ...

function main() {
    startApplication("<your_application>");

    // Some other code ...

    trigger_tooltip("<your_component>");
    check_tooltip("<Expected Tooltip Text>");
    
    // Some other code ...
}
test.js