为什么编码 UI 模块无法在 Internet Explorer 浏览器中看到 UI 控件元素?

Why is Coded UI Module failing to see UI Control Elements showing up in Internet Explorer browser?

我们的自动化 UI 测试代码需要搜索并找到 link,其中包含 "p" html 元素以及名为 Customers 的文本。最后,我们需要自动化uit测试代码点击前面提到的link.

<div data-bind="foreachRibbonItem: items">
 blah blah
 blah blah blah
<a class="btn txt-color-white" href="#" data-bind="click: content,      tooltip: {title : description, placement: 'bottom' }, keyTipsGroup : {         parentGroup: ribbonTabKeyTip, group: text }" data-original-title="" title="">
 <span class="txt-color-blueDark">
 <p data-bind="html: text">Customers</p>
  </a>
 blah blah
 blah blah blah
 </div>

以下是我的单元测试模块中使用 Microsoft Coded UI 技术的 C# 代码:

             url = new Uri("http://localhost:2816/");
             BrowserWindow brwsWin = BrowserWindow.Launch(url);
             brwsWin.Maximized = true;
             brwsWin.WaitForControlReady();
             UITestControl doc = brwsWin.CurrentDocumentWindow;
             HtmlControl control = new HtmlControl(doc);

            // Our application has a Rich User Interface with a lot of
            // UI controls so we have a quasi-placeholder UI div element          
             // associated with a class called "loading-screen" which just shows up
             // temporarily in the webpage, but disappears after all the visible UI
             // dev elements are loaded up.
             control.SearchProperties[HtmlControl.PropertyNames.ClassName] = "loading-screen";

            UITestControlCollection collection = control.FindMatchingControls();

             foreach (UITestControl loadScreen in collection)
            {
                  loadScreen.WaitForControlNotExist();
            }
            // The following Removes the "loading-screen" ClassName from our
             // search criteria
              control.SearchProperties.Remove(HtmlControl.PropertyNames.ClassName);

             // Let's just reinstantiate the control HtmlControl UI Element.
                       control = new HtmlControl(doc);
             // Now I'm just adding "div" tagname to the search criteria.
               control.SearchProperties[HtmlControl.PropertyNames.TagName] = "div";

             foreach (HtmlControl div in secondCollection)
            {
                div.WaitForControlExist();

                     div.SearchProperties[HtmlControl.PropertyNames.ClassName] = "btn txt-color-white";
                UITestControlCollection thirdCollection =      div.FindMatchingControls();
                foreach (UITestControl aLink in thirdCollection)
                {
                    //cast the item to HtmlHyperlink type
                     HtmlHyperlink mylink = (HtmlHyperlink)aLink;

                     HtmlControl paraFinder = new HtmlControl(mylink);

                          paraFinder.SearchProperties[HtmlControl.PropertyNames.TagName] = "p";
                      paraFinder.SearchProperties[HtmlControl.PropertyNames.InnerText] = "Customers";

                     UITestControlCollection fourthCollection =     paraFinder.FindMatchingControls();

                    if (fourthCollection.Capacity == 1)
                     {
                         Mouse.Click(mylink);
                     }
                  }

             }

上述代码的问题是它在代码运行时无法拾取任何 UI 控件元素。 有人可以帮我确定哪里出了问题吗? 我的直觉是,我实例化 HtmlControl 的方式以及所述 HtmlControl 与 "Website Under Test"

的关联方式可能有问题
           url = new Uri("http://localhost:2816/");
            BrowserWindow brwsWin = BrowserWindow.Launch(url);
            brwsWin.Maximized = true;
             brwsWin.WaitForControlReady();
             UITestControl doc = brwsWin.CurrentDocumentWindow;
            HtmlControl control = new HtmlControl(doc);

有人可以指出上面的代码有什么问题吗?

获取 link 的更简单方法:

public HtmlHyperlink link()
{
    HtmlControl paragraph = new HtmlControl(brwsWin);
    paragraph.SearchProperties["InnerText"] = "Customers";
    HtmlHyperlink target = (HtmlHyperlink)paragraph.GetParent();
    return target;
}

这样您现在就可以 Mouse.Click(link());

我一直喜欢使用 BrowserWindow 对象本身作为父对象,但根据定义,使用 doc 对象不会给您带来任何问题。

为了提高速度,将识别标签添加到您的 HTML 元素中会有所帮助,例如 ID 或名称,这些标签对于相关元素是唯一的。我发现 Coded-UI 引擎可以比使用 InnerText 属性 搜索得更快。另一个提示是在更高级别过滤父对象,以便该工具可以首先找到更高级别的元素,而不必在整个页面中搜索 "customers"。例如,您的 link 在树中:

<body>
    <div id='div1'>
        <div id='div2'>
            <div id='div3'>
                <a id='customerLink'>Customers</a>
            </div>
        </div>
    </div>
<body>

可以通过指定每个父 DIV 元素轻松找到。

public HtmlDiv div1()
{
    HtmlDiv div = new HtmlDiv(brwsWin);
    div.SearchProperties["ID"] = "div1";
    return div;
}

public HtmlDiv div2()
{
    HtmlDiv div = new HtmlDiv(div1);
    div.SearchProperties["id"] = "div2";
    return div;
}

为每个父对象执行此操作,在实例化它时将其正上方的 div 指定为父对象(就像我们对上面的 brwsWin 所做的那样),然后在 link 上:

public HtmlHyperlink link()
{
    HtmlHyperlink target = new HtmlHyperlink(div3);
    target.SearchProperties["id"] = "customerLink";
    return target;
}