jgerstle
jgerstle

Reputation: 1684

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

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.

Upvotes: 5

Views: 11991

Answers (3)

AdrianHHH
AdrianHHH

Reputation: 14086

There is a family of WaitForControl...() methods, including the WaitForControlNotExist() method.

The full set of these Wait... methods is:

WaitForControlEnabled()
WaitForControlExist()
WaitForControlNotExist()
WaitForControlPropertyEqual()
WaitForControlPropertyNotEqual()
WaitForControlReady()

There are also the WaitForCondition() and WaitForControlCondition() methods that can be used to wait for more complicated conditions.

Upvotes: 1

knighter
knighter

Reputation: 1227

If you just want to wait for a specific length of time, you can add to your test:

Playback.Wait(3000);

with the time in milliseconds.

Upvotes: 2

Bogdan Gavril MSFT
Bogdan Gavril MSFT

Reputation: 21498

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.

Upvotes: 3

Related Questions