I'm currently trying to help automate some coded UI tests using C# for a web application. A frequent problem I'm encountering is that it can be extremely difficult to determine if a UITestControl
object exists on the page or not. Unfortunately, Microsoft's documentation on their MSDN website for anything regarding coded UI tests is practically non-existant (see here their page for UITestControl
).
Basically what I'm asking is:
- What is the best way to determine if a
UITestControl
exists on the page or not? - How does the
UITestControl.Exists
property work? - What does the
UITestControl.Find()
method do? - How does the
UITestControl.TryFind()
method work?
How I've tried to handle it:
As I mentioned earlier, the documentation on all of these classes and methods is mostly blank. The most you can get to describe any of the methods and properties is a 1 line description in Intellisense, so I've been experimenting with the methods that are listed.
First I tried checking if the UITestControl.Exists
property was true, but over time and consulting others' experience with it, it became apparent that it always returns true, even if the browser isn't open. Since the option that seemed most obvious wasn't working, I tried using the UITestControl.Find()
method, but since it takes no arguments and returns nothing I couldn't figure out what it did. I tried using the UITestControl.TryFind()
method, and occasionally it worked, but I found that it only seemed to return false when I wasn't on the correct page; it always returned true otherwise. Clearly I had no idea how it worked, and shouldn't use it as a test.
I figured if I couldn't get the provided methods to do their job, I'd have to try to make my own tools. I most recently tried using Mouse.Hover(UITestControl)
in a try/catch block to determine if the control exists like so:
public bool DoesExist(UITestControl control){
if(control == null)
return false;
try{ Mouse.Hover(control); }
catch (UITestException)
{
return false;
}
return true;
}
It works sometimes, but in certain situations it seems to return false positives for reasons I don't understand. I'm still flying blind, and I'm nearly out of ideas.
I am using Visual Studio 2012, and Microsoft .NET Framework version 4.5.50709.
TryFind()
andFind()
is that it's hard to discern how it interacts with procedurally-generated elements, from my limited understanding it seems to often fail to find elements that exist, and successfully find elements that have stopped existing or never existed to begin with. – Einsteinium