Cannot determine if specific UITestControl exists in my web application

I'm currently trying to help automate some coded UI tests using C # for a web application. A common problem that I am facing is that it is very difficult to determine if a UITestControl object UITestControl on the page or not. Unfortunately, the Microsoft documentation on its MSDN website for virtually everything related to coded user interface tests practically does not exist ( see their page for UITestControl ).

I basically ask:

  • What is the best way to determine if a UITestControl on a page?
  • How does the UITestControl.Exists property UITestControl.Exists ?
  • What does the UITestControl.Find() method UITestControl.Find() ?
  • How does the UITestControl.TryFind() method UITestControl.TryFind() ?

How I tried to handle this:

As I mentioned earlier, the documentation for all of these classes and methods is mostly empty. The most that you can describe with any of the methods and properties is a 1-line description in Intellisense, so I experimented with the methods listed below.

At first I tried to check if the UITestControl.Exists property UITestControl.Exists true, but over time and with the experience of other people with it it became apparent that it always returns true, even if the browser is not open. Since the option that seemed most obvious didn't work, I tried using the UITestControl.Find() method, but since it takes no arguments and returns nothing, I could not understand what it did. I tried to use the UITestControl.TryFind() method, and sometimes it worked, but I found that it only returned to false when I was not on the correct page; he always returned otherwise. Clearly, I had no idea how this works, and should not be used as a test.

I decided that if I could not get the provided methods to do my work, I would have to try to create my own tools. I recently tried using Mouse.Hover(UITestControl) in a try / catch block to determine if such a control exists:

 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 some situations it seems to return false positives for reasons that I don't understand. I'm still flying blind, and I have almost no ideas.

I am using Visual Studio 2012 and Microsoft.NET Framework version 4.5.50709.

+8
c # web-applications visual-studio-2012 coded-ui-tests
source share
3 answers

A partial answer about the Find() and TryFind() methods.

After setting various search properties in the class instance for the control, the Find() method actually searches for the control. SearchProperties are used to search for a control. If no controls are found, no search is possible - forget exactly what happens then, maybe an exception is thrown, but this is not indicated in the documentation. If one control is detected, terminate Find() . If two or more are found, the search continues with FilterProperties to reduce the number of controls found.

The coded user interface generates UIControl aControl = this.UIMap.uione.uitwo.uithree; style UIControl aControl = this.UIMap.uione.uitwo.uithree; Which leads to the question of how uione get the value related to the control such that uitwo can be called? The only answer I found is in the description part of http://blogs.msdn.com/b/balagans/archive/2009/12/28/9941582.aspx , which says that "the search for the control starts (explicitly with using Find () or implicit for any use of the control in actions or property checks).

So, Find() searches for the control, and you can call it explicitly or implicitly.

TryFind() is basically the same as Find() , except that it returns a boolean indicating whether the control was found. Again, the documentation is bad, but I believe that TryFind() returns true if only one control is found, false otherwise.

Another useful search method is FindMatchingControls , which returns a (possibly empty) collection of all controls that match your search criteria.

According to yonitdm's answer , using a BoundingRectangle can help when there are several elements that match, but most of them are not displayed. The values Top and Left can also be used. Performing FindMatchingControls and screening the results to ignore anything with a negative Top or Left may work.

When developing tests, the DrawHighlight method DrawHighlight useful; it draws a rectangle around the control. The same rectangle that is drawn when recording statements using the crosshair tool.

The Coded UI content index has a lot of good information. The link to "How the UI Test Framework Finds (Search) for a Control" may be especially useful to you.

+3
source share

Instead of using obj.Exists (), we encoded our own existence method, which uses the combination approach of EnsureClickable () and BoundingRectangle.Width > 0 to make sure the control has a screen point.

ETA-oops, sorry, left the important part. Updated to add .Width to make sure it is greater than 0, you may need to use length if your width somehow doesn't work.

+1
source share

I use tryfind () .. it works fine.

 if (obj_webor.GenLink.TryFind()) { logdata.WriteLine(obj_webor.GenInnerText + " Exist !"); } else { logdata.WriteLine(obj_webor.GenInnerText + " Does Not Exist"); } 

I used to use obj_webor.GenLink.exist () .. but an error was given if it was a control that does not exist and an exception is thrown.

+1
source share

All Articles