【问题标题】:What is the difference between find and findmatchingcontrolsfind 和 findmatchingcontrols 有什么区别
【发布时间】:2015-02-05 08:02:07
【问题描述】:

最近我开始尝试手动编码 Coded-UI 测试,但遇到了 Find 方法的问题。

我使用的代码:

    Dim usernameControl As New UITestControl
    usernameControl.TechnologyName = "MSAA"
    usernameControl.SearchProperties.Add(WinWindow.PropertyNames.ControlName, "user")
    usernameControl.Find()
    Dim usernameEdit As New WinEdit(usernameControl)
    usernameEdit.text = "myusername"

    Dim passwordControl As New UITestControl()
    passwordControl.TechnologyName = "MSAA"
    passwordControl.SearchProperties.Add(WinWindow.PropertyNames.ControlName, "password")
    passwordControl.Find()
    Dim passwordEdit As New WinEdit(passwordControl)
    passwordEdit.text = "mypassword"

出于某种原因,passwordEdit.text 函数设置了 usernameEdit 字段的文本。在我用.FindMatchingControls() 替换.Find() 后,它开始工作了。

    usernameControl.Find() VS usernameControl.FindMatchingControls()

documentation of these functions 非常轻,所以轻我无法理解其中的区别。

引出以下问题:

  • 这两个函数有什么区别?
  • 能否举例说明何时使用 which?
  • 是否有更好的 Coded-UI 方法文档?

【问题讨论】:

  • 以下问题类似,答案包含有关这些功能的更多信息,但我仍然不明白为什么一个有效而另一个无效。 stackoverflow.com/questions/19962011/…
  • 请显示有效且使用FindMatchingControls的代码。
  • 只需将 2x .Find() 替换为 .FindMatchingControls()
  • Find() 仅在 完全 一项匹配时成功,然后返回该项。 FindMatchingControls() 返回一个可能为空的控件列表。我建议您在调用FindMatchingControls() 之后添加代码以显示找到的项目数,或者使用调试器查看usernameEditpasswordEdit 包含的内容。我怀疑返回了一个空列表,因此您的代码实际上是Dim xxx As New WinEdit(null)
  • 感谢您的反馈,我认为我的问题出在其他地方。

标签: coded-ui-tests


【解决方案1】:

由于我看不到您尝试测试的代码,这将是一个猜测:

您正在寻找名为 password 的控件,因为您没有指定它是 WinEdit,它可能只会找到第一个名为 password 的控件。如果我的猜测是正确的,那可能是密码字段上方的文本。

两个选项,将密码控件重命名为 PasswordInput 并专门搜索。

第二个选项是搜索 winEdit 控件而不是 UiTestControl:

Dim passwordEdit As New WinEdit()
passwordEdit.TechnologyName = "MSAA"
passwordEdit.SearchProperties.Add(WinWindow.PropertyNames.ControlName, "password")
passwordEdit.Find()
passwordEdit.text = "mypassword"

【讨论】:

  • 谢谢,是的,给元素提供更好的唯一名称会有所帮助,但是当我正在为遗留应用程序编写测试时,我想尝试而不是更改应用程序代码,除非有必要。我觉得我什至不需要 .Find() 函数,只要搜索只返回一个控件,但不知何故,带有 ControlName 的 MSAA 技术会返回随机控件。 :) 其他技术似乎效果更好。
  • 尝试使用 .DrawHighlight();在 .Find() 之前和之后,在添加文本之前查看测试是否找到了正确的控件。
  • 哇,.DrawHighlight() 非常方便调试!
  • 不过仅用于调试,它会在 7 秒或更长时间内造成播放性能损失!
【解决方案2】:

回答UITestControl.FindUITestControl.FindMatchingControls之间的主要问题区别

在对特定控件应用搜索或过滤器后,如果您希望触发对该控件的搜索,则使用书写类型为 Void 的 Find。 而 FindMatchingControls 还触发搜索,返回类型为 UITestControlCollection 的集合,其中包含与给定搜索或过滤器匹配的所有控件。

但是,还有一个控件返回 bool,即 TryFind

【讨论】:

  • 谢谢,这很有帮助。除了 Find 之外,我会返回一些东西,我想我必须使用 TryFind。
猜你喜欢
  • 2017-03-19
  • 2018-01-13
  • 1970-01-01
  • 2016-05-20
  • 1970-01-01
  • 1970-01-01
  • 2011-02-13
  • 2016-08-30
  • 1970-01-01
相关资源
最近更新 更多