-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy pathTreeNodes.cs
More file actions
74 lines (67 loc) · 2.97 KB
/
TreeNodes.cs
File metadata and controls
74 lines (67 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Automation;
using TestStack.White.AutomationElementSearch;
using TestStack.White.Factory;
using TestStack.White.UIItems.Actions;
namespace TestStack.White.UIItems.TreeItems
{
public class TreeNodes : UIItemList<TreeNode>
{
public TreeNodes(AutomationElementFinder finder, IActionListener actionListener)
: base(finder.Children(AutomationSearchCondition.ByControlType(ControlType.TreeItem)), actionListener) {}
/// <summary>
/// Gets the TreeNode matching the path. If multi-level find is specified in arguments then in process of finding the TreeNode it would also expand the TreeNodes.
/// </summary>
/// <param name="path">e.g. when arguments are ("Parent", "Child", "GrandChild") it would return the TreeNode "GrandChild" which is under "Child", which
/// in turn is under "Parent", root node. To get the "Parent" node one needs to just specify ("Parent") as argument.</param>
/// <returns>true if it finds such node, false otherwise</returns>
public virtual TreeNode GetItem(params string[] path)
{
var nodePath = new List<string>(path);
if (nodePath.Count == 0) return null;
string nodeText = nodePath[0];
TreeNode node = MatchingNode(nodeText);
nodePath.RemoveAt(0);
if (nodePath.Count == 0) return node;
if (node == null) throw new UIItemSearchException(string.Format("Could not find node {0}", nodeText));
return node.GetItem(nodePath.ToArray());
}
private TreeNode MatchingNode(string nodeText)
{
return Find(treeNode => Regex.IsMatch(treeNode.AutomationElement.Current.Name, nodeText, RegexOptions.IgnoreCase));
}
/// <summary>
/// Get the list of tree nodes which would be traversed to reach the node supplied
/// </summary>
/// <param name="treeNode"></param>
/// <returns></returns>
public virtual List<TreeNode> GetPathTo(TreeNode treeNode)
{
var nodePath = new List<TreeNode>();
Path(treeNode, nodePath);
return nodePath;
}
private bool Path(TreeNode treeNode, List<TreeNode> treeNodes)
{
foreach (TreeNode currentNode in this)
{
if (currentNode.Equals(treeNode))
{
treeNodes.Add(currentNode);
return true;
}
treeNodes.Add(currentNode);
bool pathFound = currentNode.Nodes.Path(treeNode, treeNodes);
if (pathFound) return true;
treeNodes.Remove(currentNode);
}
return false;
}
public virtual void Visit(ITreeNodeVisitor visitor)
{
foreach (TreeNode treeNode in this)
treeNode.Visit(visitor);
}
}
}