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

Posted

技术标签:

【中文标题】为啥编码的 UI 模块无法在 Internet Explorer 浏览器中看到 UI 控件元素?【英文标题】:Why is Coded UI Module failing to see UI Control Elements showing up in Internet Explorer browser?为什么编码的 UI 模块无法在 Internet Explorer 浏览器中看到 UI 控件元素? 【发布时间】:2015-01-26 14:04:44 【问题描述】:

我们的自动化 UI 测试代码需要搜索并找到包含“p”html 元素以及名为“客户”的文本的链接。最后,我们需要自动化 uit 测试代码来点击上述链接。

<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 与“待测网站”的关联方式可能有问题

           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);

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

【问题讨论】:

@adrianhhh 嘿,我试图缩小我在 UI 测试中面临的技术问题的具体细节。你能看一下帖子吗?提前致谢。 @adrianhhh 我现在面临运行测试速度有多慢的问题。我的项目正在使用真正的 bootstraphunter.com/smartadmin-product.php UI 界面,它有很多 UI控件。问题是 Microsoft Coded UI 需要很长时间才能搜索 UI 元素。Cud 你请建议我如何提高测试速度? 【参考方案1】:

获取链接的更简单方法:

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 属性更快地进行搜索。另一个提示是在更高级别上过滤父对象,以便该工具可以首先找到更高级别的元素,而不必在整个页面中搜索“客户”。例如,您在树中的链接:

<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 所做的那样),然后在链接上:

public HtmlHyperlink link()

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

【讨论】:

Ryan,谢谢您的帮助。我现在面临运行测试速度有多慢的问题。我的项目正在使用真正的bootstraphunter.com/smartadmin-product.php UI 界面,它有很多 UI 控件.问题是Microsoft Coded UI需要很长时间才能搜索UI元素。Cud你请建议我如何提高测试速度? 希望有帮助。根据我的经验,Coded UI 往往比你想象的要慢。老实说,我使用 Selenium WebDriver 工具获得了更好的结果(速度方面)。

以上是关于为啥编码的 UI 模块无法在 Internet Explorer 浏览器中看到 UI 控件元素?的主要内容,如果未能解决你的问题,请参考以下文章