无法确定某个 UITestControl 是否存在于我的 Web 应用程序中 [英] Can't determine if a certain UITestControl exists in my web app

查看:22
本文介绍了无法确定某个 UITestControl 是否存在于我的 Web 应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用 C# 为 Web 应用程序帮助自动化一些编码的 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 属性是否为真,但随着时间的推移并咨询其他人的经验,很明显它总是返回真,即使浏览器不是打开.由于似乎最明显的选项不起作用,我尝试使用 UITestControl.Find() 方法,但由于它不接受任何参数且不返回任何内容,因此我无法弄清楚它做了什么.我尝试使用 UITestControl.TryFind() 方法,偶尔它会起作用,但我发现它似乎只有在我不在正确的页面上时才返回 false;否则它总是返回真.显然我不知道它是如何工作的,不应该将它用作测试.

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() 方法的部分答案.

Partial answer about the Find() and TryFind() methods.

在类实例中为控件设置各种搜索属性后,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 进行评估?我找到的唯一答案是在说明部分rel="nofollow">http://blogs.msdn.com/b/balagans/archive/2009/12/28/9941582.aspx 表示开始搜索控件(通过Find() 或在操作或属性验证中使用控件隐式 )".

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 如果恰好找到一个控件,则返回 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 可以在有多个项目匹配但大多数未显示时提供帮助.也可以使用 TopLeft 的值.执行 FindMatchingControls 并筛选结果以忽略具有负 TopLeft 的任何内容.

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.

这篇关于无法确定某个 UITestControl 是否存在于我的 Web 应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆