无法确定我的Web应用程序中是否存在某个UITestControl [英] Can't determine if a certain UITestControl exists in my web app
问题描述
我目前正在尝试使用针对Web应用程序的C#自动化一些编码的UI测试。我经常遇到的一个问题是,很难确定页面上是否存在 UITestControl
对象。不幸的是,Microsoft在其MSDN网站上几乎没有关于编码UI测试的文档(请在此处查看其页面以获取 UITestControl
)。
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
).
我要问的基本上是:
- 确定页面上是否存在
UITestControl
的最佳方法是什么? -
UITestControl.Exists
属性起作用吗? -
UITestControl.Find()
方法有什么作用? -
UITestControl.TryFind()
方法如何工作?
- 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?
我如何处理它:
正如我之前提到的,所有这些文档类和方法大多为空白。您最多可以描述任何方法和属性,是Intellisense中的1行描述,因此我一直在尝试使用列出的方法。
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.
首先我尝试检查 UITestControl.Exists
属性是否为真,但是随着时间的流逝并参考其他人的经验,很明显即使浏览器不支持,它也总是返回true。没开由于似乎最明显的选项无效,因此我尝试使用 UITestControl.Find()
方法,但是由于它没有参数且没有返回值,所以我无法确定搞清楚它做了什么。我尝试使用 UITestControl.TryFind()
方法,但偶尔会起作用,但是我发现,只有当我不在正确的页面上时,它似乎只会返回false。否则始终返回true。显然,我不知道它是如何工作的,也不应该将其用作测试。
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.
我发现如果无法获得提供的方法来完成任务,我必须尝试制作自己的工具。我最近尝试在try / catch块中使用 Mouse.Hover(UITestControl)
来确定控件是否像这样存在:
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.
我正在使用Visual Studio 2012和Microsoft .NET Framework 4.5.50709版。
I am using Visual Studio 2012, and Microsoft .NET Framework version 4.5.50709.
推荐答案
关于 Find()
和的部分答案TryFind()
方法。
在类实例中为控件设置各种搜索属性后, Find()
方法实际搜索要匹配的控件。 SearchProperties
用于尝试查找控件。如果未找到控件,则搜索将失败-完全忘记随后会发生什么,可能会引发异常,但文档中没有说明。如果找到一个控件,则 Find()
完成。如果找到两个或更多,则继续搜索,方法是使用 FilterProperties
将找到的控件数量减少到一个。
After setting the various search properties in the class instance for the control the Find()
method does the actual searching for a control to match. The SearchProperties
are used to try and find a control. If no controls are found then the search fails - forget exactly what happens then, possibly an exception is thrown but the documentation does not state that. If one control is found that the Find()
completes. If two or more are found then the search continues by using FilterProperties
to reduce the number of controls found to one.
编码的UI记录器生成的样式为 UIControl aControl = this.UIMap.uione.uitwo.uithree;
的代码会导致的问题uione
是否获得引用控件的值,以便可以评估 uitwo
?我找到的唯一答案是在
The Coded UI recorder generates code of the style UIControl aControl = this.UIMap.uione.uitwo.uithree;
which leads to the question of how does uione
get a value referring to a control such that uitwo
can be evauated? The only answer I have found is in the Description part of http://blogs.msdn.com/b/balagans/archive/2009/12/28/9941582.aspx which says "the search for the control starts ( explicit by Find() or implicit by any usage of the control in actions or property validations )".
所以 Find()
执行搜索控件,可以显式或隐式调用它。
So Find()
performs the search for a control and it can be called explicitly or implicitly.
TryFind()
基本上与 Find()
相同,只是它返回一个布尔值,指示是否找到控件。同样,文档很差,但我相信 TryFind()
如果找到了恰好一个控件,则返回 true
。 c $ c> false 否则。
TryFind()
is basically the same as Find()
except that it returns a boolean indicating whether the control was found. Again, the documentation is poor but I believe that TryFind()
returns true
if exactly one control is found, false
otherwise.
另一个有用的查找方法是 FindMatchingControls
Another useful find method is FindMatchingControls
which returns a (possibly empty) collection of all controls that match the search criteria.
根据 yonitdm 的答案,使用 BoundingRectangle
在有多个匹配项但大多数未显示时可以提供帮助。也可以使用 Top
和 Left
的值。进行 FindMatchingControls
并筛选结果以忽略 Top
或 Left
可能起作用。
As per yonitdm's answer, using the BoundingRectangle
can help when there are multiple items that match but most are not on display. The values of Top
and Left
can also be used. Doing a FindMatchingControls
and screening the results to ignore anything with negative Top
or Left
may work.
在进行开发测试时, DrawHighlight
方法很有用,它会在控制。使用十字准线工具记录断言时所绘制的矩形也是如此。
When developing tests the DrawHighlight
method is useful, it draws a rectangle around a control. The same sort of rectangle that is drawn when recording assertions with the cross-hairs tool.
编码的UI内容索引包含很多有用的信息。指向 UI测试框架如何找到(搜索)控件的链接的链接可能对您特别有用。
The Coded UI content index has lots of good information. The link to "How does UI Test Framework find (search) for a control" may be particularly helpful for you.
这篇关于无法确定我的Web应用程序中是否存在某个UITestControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!