Getting Tooltip Element in UI Automation Tests using TestStack (White) framework

We use TestStack (White) UI Automation framework for our UI Automation tests, we have few tests that requires getting the error message from the tooltip to validate what is being displayed to the user. These tests were running fine but failing on the Build Server

The Problem

When trying to get the ToolTip using TestStack (White) framework documented way, we get null. Increasing the timeout setting (as documented here) did not help.

The Solution

Adding retrial mechanisms, and few other attempts did not work. After doing some reading about Windows Forms and Tooltips, I found that Windows says a tooltip will only shown when the user hover over a UI Element and pause and that is what was missing.

I started looking at the source code of TestStack (White) and how it tries to get the tooltip text, and apparently it was only trying to get the mouse over the ui element, then get the tooltip directly after moving the tooltip, without any wait, and this was the problem.
The other thing that I found was, the source code moves the mouse over the UI element and reads the tooltip. This might work if the course (mouse pointer) was somewhere else, but if the pointer was already over the UI ELement, then it would not work. You would need to move the mouse away and move it back on top of the UI Element. This was another issue that we had with our retrial mechanism.

In the end, I ended up creating a custom UI element and overriding the ErrorProviderMessage() method as below.

    [ControlTypeMapping(CustomUIItemType.Text)]
    public class MyAutomationLabel : CustomUIItem
    {
        public MyAutomationLabel(AutomationElement automationElement, ActionListener actionListener) : base(automationElement, actionListener)
        {
        }

        protected MyAutomationLabel()
        { 
        }

        public override string ErrorProviderMessage(Window window)
        {

            // 1. Hover away from the label
            window.Mouse.Location = new Point(this.Location.X -20, this.Location.Y - 20);

            // 2. move the mouse on top of the label
            window.Mouse.Location = new Point(this.Bounds.Right + 10, this.Bounds.Top + 10);
            this.actionListener.ActionPerformed(White.Core.UIItems.Actions.Action.WindowMessage);

            // 3. Wait for tooltip to show up
            Thread.Sleep(300);

            return window.ToolTip.Text;
        }
    }

Then you could use this when you poll the form for a label (with an ErrorProvider/tooltip) and that should work šŸ™‚

Hope you find this useful, would love to hear from you if you have any questions or concerns.

3 Replies to “Getting Tooltip Element in UI Automation Tests using TestStack (White) framework”

  1. Hi Has,

    Great article, but I am having trouble locating the TalAutomationLabel in the White framework.
    Is that part of a new release? We are using 0.10.3.118.

    Cheers

    1. Hi Karimahta,

      Sorry that was my mistake when I changed the name of the class (when making this post), I forgot to change the constructor. Please have a look at the CustomUI element code now and you should not have an issue.
      Remember that you would need to use this type when you do poll<> on your pages to find the label
      Good luck

  2. How to pass ActionListener parameter. I mean which method returns ActionListener so that we can pass that argument.

Leave a Reply

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