我需要在Coded UI Test自动化中选择treeview中的项目。我提出了自己的解决方案。我看到一些性能问题,因为它运行缓慢。有关改善表现的任何建议吗?
这个我写的扩展方法,我可以使用它与Treeview控件内联。
此外,节点路径是自定义的,假设“>”不在任何项目文本中使用。我在摘要
中给出了节点文本的示例 /// <summary>
/// Selects item in a tree view control.
/// Eg nodePath: 2017>Jan>21. This will expand 2017 then Jan and select 21st date in treeview
/// </summary>
/// <param name="parentControl">Tree view control</param>
/// <param name="nodePath">Path where the item to be selected is located. Parent and child are separated with '>'</param>
public static void SelectItemInTreeView(this WpfTree parentControl, string nodePath)
{
char splitter = '>';
string[] treePath = nodePath.Split(splitter);
WpfTreeItem parentNode = null;
UITestControlCollection childNodes = parentControl.Nodes;
bool itemSelected = false;
string root = treePath[0];
string lastItemInTreePath = treePath[treePath.Length - 1];
foreach (string pathElement in treePath)
{
bool found = false;
foreach (WpfTreeItem item in childNodes)
{
UITestControl itemCheck = item.GetChildren().Where(x => x.ControlType == ControlType.Text && x.Name == pathElement).FirstOrDefault();
if (itemCheck != null)
{
found = true;
WpfTreeItem currentItem = null;
if (itemCheck.Name.Equals(lastItemInTreePath))
{
item.Selected = true;
itemSelected = true;
}
else
{
currentItem = (parentNode == null) ? new WpfTreeItem(parentControl) : new WpfTreeItem(parentNode);
currentItem.TechnologyName = "UIA";
currentItem.SearchProperties[WpfTreeItem.PropertyNames.Name] = item.Name;
currentItem.SearchConfigurations.Add(SearchConfiguration.ExpandWhileSearching);
currentItem.Expanded = true;
parentNode = currentItem;
}
break;
}
}
if (!found)
{
Assert.Fail("Error in Tree view path. Correct your input Treeview path: {0}", nodePath);
}
else
{
if (!itemSelected)
{
childNodes = parentNode.Nodes;
}
}
}
}