播放未能在 Coded UI 中找到具有给定搜索属性的控件

The playback failed to find the control with the given search properties in Coded UI

我是 Coded UI 测试的新手,所以这是一个 "simple" 问题:

尝试浏览菜单选项时,我记录了操作并试图回放。我收到以下消息:The playback failed to find the control with the given search properties

录制工具生成的代码如下:

public void NavegarSituacao()
        {
            #region Variable Declarations
            HtmlCustom uINotíciasCustom = this.UIHttpcmshomepsafecomIWindow.UIHttpcmshomepsafecomDocument.UINotíciasCustom;
            HtmlCustom uIEntretenimentoCustom = this.UIHttpcmshomepsafecomIWindow.UIHttpcmshomepsafecomDocument.UIEntretenimentoCustom;
            HtmlCustom uIMulherCustom = this.UIHttpcmshomepsafecomIWindow.UIHttpcmshomepsafecomDocument.UIMulherCustom;
            HtmlCustom uIEsportesCustom = this.UIHttpcmshomepsafecomIWindow.UIHttpcmshomepsafecomDocument.UIEsportesCustom;
            HtmlCustom uIHomemCustom = this.UIHttpcmshomepsafecomIWindow.UIHttpcmshomepsafecomDocument.UIHomemCustom;
            HtmlCustom uITecnologiaCustom = this.UIHttpcmshomepsafecomIWindow.UIHttpcmshomepsafecomDocument.UITecnologiaCustom;
            HtmlCustom uIVídeosCustom = this.UIHttpcmshomepsafecomIWindow.UIHttpcmshomepsafecomDocument.UIVídeosCustom;
            #endregion

            // Click 'Notícias' custom control
            Mouse.Click(uINotíciasCustom, new Point(89, 21));

            // Click 'Entretenimento' custom control
            Mouse.Click(uIEntretenimentoCustom, new Point(90, 15));

            // Click 'Mulher' custom control
            Mouse.Click(uIMulherCustom, new Point(90, 9));

            // Click 'Esportes' custom control
            Mouse.Click(uIEsportesCustom, new Point(84, 18));

            // Click 'Homem' custom control
            Mouse.Click(uIHomemCustom, new Point(82, 16));

            // Click 'Tecnologia' custom control
            Mouse.Click(uITecnologiaCustom, new Point(85, 8));

            // Click 'Vídeos' custom control
            Mouse.Click(uIVídeosCustom, new Point(70, 11));
        }

有没有办法通过某种定位器捕获这些元素(这些元素没有 ID)?像这样:

public HtmlCustom UIHomemCustom
        {
            get
            {
                if ((this.mUIHomemCustom == null))
                {
                    this.mUIHomemCustom = new HtmlCustom(this);
                    #region Search Criteria
                    this.mUIHomemCustom.SearchProperties["TagName"] = "LI";
                    this.mUIHomemCustom.SearchProperties["Id"] = null;
                    this.mUIHomemCustom.SearchProperties[UITestControl.PropertyNames.Name] = null;
                    this.mUIHomemCustom.FilterProperties["Class"] = null;
                    this.mUIHomemCustom.FilterProperties["ControlDefinition"] = "data-value=\"201405231131464799\"";
                    this.mUIHomemCustom.FilterProperties["InnerText"] = "Homem";
                    this.mUIHomemCustom.FilterProperties["TagInstance"] = "8";
                    this.mUIHomemCustom.FilterProperties["Xpath"] = "#default > div.wrapper > div.menu > div > ul > li:nth-child(5)";
                    this.mUIHomemCustom.WindowTitles.Add("http://cms.home.psafe.com/");
                    #endregion
                }
                return this.mUIHomemCustom;
            }
        }

这是菜单:

CODEDUI回放的一个有​​趣之处在于它不一定考虑用户在录制过程中的时间因素。这些是我多年来对此类错误的了解...

  1. 当前 window 或控件不是最顶层 window。
  2. 搜索条件太严格了,问问自己这个搜索条件有没有和录音不一样的地方。在这种情况下,我可以看到一个可能的问题:"data-value=\"201405231131464799\"" 那个数字每次都一样吗?
  3. 在准备完成之前搜索控件。这是通过调用(在你的情况下) Custom.WaitForReady()
  4. 来解决的
  5. 这个我也看到了(不是很懂)因为这些记录没有X,Y坐标;除了第二个参数中定义的点。如果录制屏幕尺寸和播放屏幕尺寸不一样,有时(但不总是)播放 "can't find the control"。您可以尝试省略点参数。
  6. 元素根本不可见。

进入 UI 地图并将 InnerTextFilterProperty 更改为 SearchProperty。首先应用搜索属性——如果它找到一个完全匹配,它甚至不会查看过滤器属性。在这种情况下,控件(文本值)最重要的是过滤器 属性。

它正在尝试查找没有 ID 的 <LI> 标签。它无疑会找到多个匹配项。然后它应用过滤器属性,这些属性可能因页面加载而异。

您还可以将 ID 属性 应用到 <LI> 标签,然后更新 UI 地图搜索属性,以便它搜索特定的 ID,这也能解决问题。

一般来说,当您将 Coded UI 用于 Web 应用程序时,最好确保页面上的所有内容都具有唯一的 "ID" 属性。这使得 Coded UI 更容易将您尝试与之交互的页面元素归零。

我遇到了类似的问题并遇到了这个问题。就我而言,我使用的是较旧的 CodedUI 测试,这些测试是用稍旧的版本 (12.0.21005.1) 记录的。相同的错误消息 ("The playback failed to find the control with the given search properties") 并且该错误特别提到了在页面上找不到的文本输入。

我的答案是,当页面更改时(在单击前一页上的 link 之后),BrowserWindow 对象正在丢失它的引用。我不知道为什么。

解决方案是在日志语句中调用 BrowserWindow.TryFind(),然后一切正常,如先前记录的那样。

只是想我会分享以防其他人遇到这个问题。