6

I'm currently creating automated test cases for a WPF application using Microsoft UIAutomation (UIA) framework. I'm able to locate objects/elements using AutomationElement class but sometimes application take more time to load and this fails my test cases, because test cases are trying to click an object which is not yet visible on the screen.

I thought of implementing implicit wait in my test cases (don't want to use Thread.Sleep) where my test cases will first wait for the object to appear and then perform action. But unfortunately I'm not able to find any way of implementing implicit wait.

How can I implement Implicit Wait in my test cases using UIA?

2 Answers 2

5
+50

There might be better ways (code-wise), but avoiding Thread.Sleep can be easily done by using SpinWait.SpinUntil which is in the System.Threading namespace.

It will loop until either true, or the set timeout has passed (then the code execution simply continues).

An idea for your implementation in half pseudo-code:

public void ClickWhenVisible(this Element el)
{
  SpinWait.SpinUntil(() => el.IsVisible(), 10000);
  el.Click();
}

Edit: also, if you look at Selenium source code, the Wait.Until method also essentially is a loop using Thread.Sleep but a very brief one. So it's dynamic in the sense that the condition is checked often and the loop breaks when the condition has been met. So you could implement a similar thing (even very basic with a counter in a do-while will suffice).

1
  • Edited with another alternative suggestion
    – FDM
    Commented Sep 21, 2017 at 4:03
2

One way to solve this is to listen to UI automation events This is somewhat similar to how UI automation is done on OSX by enabling and listening to accessibility events

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.