通过过滤编码ui中的属性来选择UI元素

时间:2016-05-05 16:20:26

标签: coded-ui-tests

我有一个网络应用程序。我正在使用编码ui编写自动化测试来测试应用程序。

我有一个带文本框的下拉列表。在输入文本框中的值时,下拉列表中的值会根据输入的文本进行过滤。

enter image description here

如果我在文本框中输入“' Admin'”,我会在下面输入以下选项:

enter image description here

我需要捕捉显示的两个选项。

但是使用IE Developer工具(F12),我无法捕获过滤的选项,因为显示的选项没有任何唯一属性(如下所示)。并且未显示的选项具有class =" hidden"财产

通过应用某种类型的过滤器来捕捉显示的元素的任何方式,例如选择其类别的ui元素!=隐藏'

提前致谢!!

enter image description here

2 个答案:

答案 0 :(得分:0)

嗨请尝试下面的代码它是否适合你。通过所有那些有类="隐藏"

的控件遍历
WpfWindow mainWindow = new WpfWindow();
            mainWindow.SearchProperties.Add(HtmlControl.PropertyNames.ClassName, "hidden");
            UITestControlCollection collection = mainWindow.FindMatchingControls();
            foreach (UITestControl links in collection)
            {

                HtmlHyperlink mylink = (HtmlHyperlink)links;

                Console.WriteLine(mylink.InnerText);
            }

答案 1 :(得分:0)

我不确定搜索属性是否有办法实现,但还有其他方法。

一种方法是对集合进行暴力破坏。找到所有列表项,然后找到隐藏的列表项并做出改变。

HtmlControl listControl = /* find the UL somehow */

HtmlControl listItemsSearch = new HtmlControl(listControl);
listItemsSearch.SearchProperties.Add(HtmlControl.PropertyNames.TagName, "li");

HtmlControl hiddenListItemsSearch = new HtmlControl(listControl);
hiddenListItemsSearch.SearchProperties.Add(HtmlControl.PropertyNames.TagName, "li");
hiddenListItemsSearch.SearchProperties.Add(HtmlControl.PropertyNames.ClassName, "hidden");

var listItems = listItemsSearch.FindMatchingControls().Except(hiddenListItemsSearch.FindMatchingControls());

您只能迭代此集合一次,因此如果您需要多次迭代,请创建一个返回此搜索的函数。

var listItemsFunc = () => listItemsSearch.FindMatchingControls().Except(hiddenListItemsSearch.FindMatchingControls());

foreach(var listItem in listItemsFunc()){
 // iterate 1
}

foreach(var listItem in listItemsFunc()){
 // iterate 2
}

我认为这样做的另一种方法是根据具有可点击点的控件进行过滤,并占用屏幕上的空间(即不隐藏)。

listItemsSearch.FindMatchingControls().Where(x => {
    try { x.GetClickablePoint(); return x.Width > 0 && x.Height > 0; } catch { return false; }
});