In C# Coded UI is there a way to wait for a control to be clickable

jgerstle picture jgerstle · Apr 22, 2013 · Viewed 11.6k times · Source

In coded ui there is a way to wait for a control to exist using UITestControl.WaitForControlExist(waitTime);. Is there a way to wait for a control to not exist? The best way I could think of is to create an extension method like this:

public static bool WaitForControlClickable(this UITestControl control, int waitTime = 10000)
    {
        Point p;
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        while (stopwatch.ElapsedMilliseconds < waitTime)
        {
            if (control.TryGetClickablePoint(out p))
            {
                return true;
            }
            Thread.Sleep(500);
        }
        return control.TryGetClickablePoint(out p);
    }

Is there a better way of doing this? Also I am looking for a way to do the opposite.

Answer

Bogdan Gavril MSFT picture Bogdan Gavril MSFT · Apr 22, 2013

So what WaitForControlExists actually does is to call the public WaitForControlPropertyEqual, something like:

return this.WaitForControlPropertyEqual(UITestControl.PropertyNames.Exists, true, timeout);

Your helper can instead call:

 public bool WaitForControlPropertyNotEqual(string propertyName,
                object propertyValue, int millisecondsTimeout)

Also, as Kek points out, there is a WaitForControlNotExist public method.

Note that they all seem to be using the same helper (also public):

 public static bool WaitForCondition<T>(T conditionContext, Predicate<T> conditionEvaluator, int millisecondsTimeout)

this helper essentially does a Thread.Sleep on the current thread, pretty much as you do it.