9

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.

4

3 回答 3

3

关于Find()TryFind()方法的部分答案。

在为控件设置类实例中的各种搜索属性后,该Find()方法会实际搜索要匹配的控件。SearchProperties用于尝试查找控件。如果没有找到控件,则搜索失败 - 忘记究竟发生了什么,可能会引发异常,但文档没有说明。如果找到一个控件,则Find()完成。如果找到两个或更多,则通过使用FilterProperties将找到的控件数量减少到一个来继续搜索。

Coded UI 记录器生成样式代码,UIControl aControl = this.UIMap.uione.uitwo.uithree;这导致了如何uione获取引用控件的值以便uitwo可以评估的问题?我找到的唯一答案是在http://blogs.msdn.com/b/balagans/archive/2009/12/28/9941582.aspx的描述部分,它说“搜索控件开始(通过 Find 明确() 或通过在操作或属性验证中对控件的任何使用隐含) "。

因此Find()执行对控件的搜索,并且可以显式或隐式调用它。

TryFind()基本相同,Find()只是它返回一个布尔值,指示是否找到控件。同样,文档很差,但我相信如果恰好找到一个控件,则TryFind()返回,否则。truefalse

另一个有用的 find 方法是FindMatchingControls返回匹配搜索条件的所有控件的(可能为空)集合。

根据yonitdm的回答,BoundingRectangle当有多个匹配但大多数未显示的项目时,使用可以提供帮助。和的值TopLeft可以使用。做一个FindMatchingControls并筛选结果以忽略任何负面TopLeft可能有效的东西。

在开发测试时,该DrawHighlight方法很有用,它会在控件周围绘制一个矩形。使用十字准线工具记录断言时绘制的相同类型的矩形。

Coded UI 内容索引有很多很好的信息。指向“UI 测试框架如何查找(搜索)控件”的链接可能对您特别有帮助。

于 2013-11-13T22:42:19.037 回答
1

我们没有使用 obj.Exists(),而是编写了自己的存在方法,该方法使用EnsureClickable()BoundingRectangle.Width >0 的组合方法来确保控件具有屏幕点。

ETA-哎呀,抱歉遗漏了一个重要部分。更新为添加 .Width 以确保它大于 0,如果您的宽度无法正常工作,您可能需要使用长度。

于 2013-11-14T22:17:02.590 回答
1

我正在使用 tryfind() .. 它工作正常。

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

早些时候我使用的是 obj_webor.GenLink.exist().. 但如果控制不存在并且发生异常,则会给出错误。tryfind 没问题

于 2015-08-11T10:38:14.063 回答