froglogic / Blog / Tip of the Week / Testing WPF Popups and Tooltips

Testing WPF Popups and Tooltips

Introduction

By their nature, popups and tooltips disappear as soon as they lose keyboard focus, making it a bit hard to inspect their structure during test development. WPF Popups and Tooltips will get their own Squish types in Squish’s next major release: Popup and Tooltip, respectively, making working with them a bit easier. This blog will show you how to test these controls in the newest Squish.

Triggering Display

In order to test popups and tooltips, you first have to display them. For tooltips, that can usually be done by moving the mouse cursor over a control. The script function for that could be:

def trigger_tooltip(object_name):
    obj = waitForObject(object_name)          
    mouseMove(obj, obj.width / 2, obj.height / 2)
WPF Tooltip

For popups, you can also try to move the mouse cursor, but often the popups are triggered just by a mouse click. So, the standard Squish function clickButton can be used for that:

clickButton(waitForObject(names.Show_Popup_Button))
Wpf Popup

Finding Labels

Once the tooltip and popup appear on the screen, you can start searching for the labels inside of them. The following script code could be used for that purpose:

lables = findAllObjects({"container": {"type": "ToolTip"}, "type": "Label"})
...
lables = findAllObjects({"container": {"type": "Popup"}, "type": "Label"})

If you would like to see the structure of these controls when displayed, you can use the Squish function: saveObjectSnapshot:

tooltip = waitForObjectExists({"type": "ToolTip"})
saveObjectSnapshot(tooltip, "tooltip_snapshot.xml")
...
popup = waitForObjectExists({"type": "Popup"})
saveObjectSnapshot(popup, "popup_snapshot.xml")

Later on, you can inspect these snapshots and figure out the structure of the controls, which is often useful if the structure is unknown or more complex.

For searching the labels you can also use the function find_labels from our Knowledge Base article, Testing WPF Tooltips. This function can give you a bit more flexibility and control if you need it.

And, of course, you can also put {"type": "ToolTip"} and {"type": "Popup"} into the scripted object map so that you have something like names.tooltip and names.popup, which is a bit nicer and easier to maintain.

Verifying Text

Once you get the labels, you can obtain their text by using the following function:

def get_text(labels, separator=" "):
    text = [ label.text for label in labels ]
    return separator.join(text)

The function will iterate over the labels array and collect the text property of the labels.

Finally, once you have the text, you can use the standard Squish functions for verifications, as in the example below:

test.compare(get_text(lables), "Example Popup Popup Content")

Or, if you would like to have a different separator between the labels, you can use:

test.compare(get_text(lables, "/"), "Example Popup/Popup Content")

Example

The example application and Squish test suite used in this blog post can be download here: WpfPopupAndTooltip.zip

Leave a Reply

Your email address will not be published. Required fields are marked *

Copy link
Powered by Social Snap