Best WinAppDriver code snippet using WinAppDriverUIRecorder.UiTreeNode.UiTreeNode
WindowEditNodeAttribute.xaml.cs
Source:WindowEditNodeAttribute.xaml.cs  
...22    /// Interaction logic for WindowEditNodeAttribute.xaml23    /// </summary>24    public partial class WindowEditNodeAttribute : Window25    {26        UiTreeNode uiTreeNode;27        static List<string> listAttrCompareMethod = new List<string>();28        string xpathNode;29        bool? bAddPositionAtribute = null;30        public WindowEditNodeAttribute(UiTreeNode uiNode)31        {32            InitializeComponent();33            uiTreeNode = uiNode;34        }35        private void Window_Loaded(object sender, RoutedEventArgs e)36        {37            if (uiTreeNode == null)38            {39                return;40            }41            if (listAttrCompareMethod.Count == 0)42            {43                // order must be same as  public enum CompareMethod {Equal, StartsWith, Contains};44                listAttrCompareMethod.Add("=");45                listAttrCompareMethod.Add("starts-with");46                listAttrCompareMethod.Add("contains");47            }48            ReadUiTreeNodeValue();49            this.dgNameValue.ItemsSource = uiTreeNode.LoadPropertyData();50        }51        private void ReadUiTreeNodeValue()52        {53            this.textBoxName.Text = uiTreeNode.Name;54            this.textBoxAutoId.Text = uiTreeNode.AutomationId;55            this.textBoxClassName.Text = uiTreeNode.ClassName;56            this.comboBoxName.IsEnabled = !string.IsNullOrEmpty(uiTreeNode.Name);57            this.textBoxName.IsEnabled = !string.IsNullOrEmpty(uiTreeNode.Name);58            if (this.comboBoxName.IsEnabled)59            {60                this.comboBoxName.ItemsSource = listAttrCompareMethod;61                this.comboBoxName.SelectedIndex = (int)uiTreeNode.NameCompareMethod;62            }63            this.comboboxClass.IsEnabled = !string.IsNullOrEmpty(uiTreeNode.ClassName);64            this.textBoxClassName.IsEnabled = !string.IsNullOrEmpty(uiTreeNode.ClassName);65            if (this.comboboxClass.IsEnabled)66            {67                this.comboboxClass.ItemsSource = listAttrCompareMethod;68                this.comboboxClass.SelectedIndex = (int)uiTreeNode.ClassNameCompareMethod;69            }70            this.comboboxAutoId.IsEnabled = !string.IsNullOrEmpty(uiTreeNode.AutomationId);71            this.textBoxAutoId.IsEnabled = !string.IsNullOrEmpty(uiTreeNode.AutomationId);72            if (this.comboboxAutoId.IsEnabled)73            {74                this.comboboxAutoId.ItemsSource = listAttrCompareMethod;75                this.comboboxAutoId.SelectedIndex = (int)uiTreeNode.AutomationIdCompareMethod;76            }77            if (string.IsNullOrEmpty(uiTreeNode.Position) == false)78            {79                this.textBoxPosition.Text = uiTreeNode.Position;80            }81            xpathNode = uiTreeNode.NodePath;82            this.textBoxNode.Text = xpathNode;83        }84        private void buttonOK_Click(object sender, RoutedEventArgs e)85        {86            UpdateUiTreeNodeValue();87            DialogResult = true;88        }89        private void buttonCancel_Click(object sender, RoutedEventArgs e)90        {91            DialogResult = false;92        }93        private void UpdateUiTreeNodeValue()94        {95            if (this.comboBoxName.SelectedValue != null)96            {97                uiTreeNode.Name = this.textBoxName.Text;98                uiTreeNode.NameCompareMethod = UiTreeNode.CompareMethod.Equal;99                if (this.comboBoxName.SelectedValue.ToString() == "starts-with")100                {101                    uiTreeNode.NameCompareMethod = UiTreeNode.CompareMethod.StartsWith;102                }103                else if (this.comboBoxName.SelectedValue.ToString() == "contains")104                {105                    uiTreeNode.NameCompareMethod = UiTreeNode.CompareMethod.Contains;106                }107            }108            if (this.comboboxClass.SelectedValue != null)109            {110                uiTreeNode.ClassName = this.textBoxClassName.Text;111                uiTreeNode.ClassNameCompareMethod = UiTreeNode.CompareMethod.Equal;112                if (this.comboboxClass.SelectedValue.ToString() == "starts-with")113                {114                    uiTreeNode.ClassNameCompareMethod = UiTreeNode.CompareMethod.StartsWith;115                }116                else if (this.comboboxClass.SelectedValue.ToString() == "contains")117                {118                    uiTreeNode.ClassNameCompareMethod = UiTreeNode.CompareMethod.Contains;119                }120            }121            if (this.comboboxAutoId.SelectedValue != null)122            {123                uiTreeNode.AutomationId = this.textBoxAutoId.Text;124                uiTreeNode.AutomationIdCompareMethod = UiTreeNode.CompareMethod.Equal;125                if (this.comboboxAutoId.SelectedValue.ToString() == "starts-with")126                {127                    uiTreeNode.AutomationIdCompareMethod = UiTreeNode.CompareMethod.StartsWith;128                }129                else if (this.comboboxAutoId.SelectedValue.ToString() == "contains")130                {131                    uiTreeNode.AutomationIdCompareMethod = UiTreeNode.CompareMethod.Contains;132                }133            }134            uiTreeNode.NodePath = UpdateNodeXPath();135        }136        private string UpdateNodeXPath()137        {138            xpathNode = $"/{uiTreeNode.Tag}";139            if (!string.IsNullOrEmpty(uiTreeNode.Name) && comboBoxName.SelectedValue != null)140            {141                var nameComboValue = comboBoxName.SelectedValue.ToString();142                if (nameComboValue == "=")143                    xpathNode += $"[@Name=\\\"{this.textBoxName.Text}\\\"]";144                else145                    xpathNode += $"[{nameComboValue}(@Name,\\\"{this.textBoxName.Text}\\\")]";...UiTreeNode.cs
Source:UiTreeNode.cs  
...27        }28        public string PropName { set; get; }29        public string PropValue { set; get; }30    }31    public class UiTreeNode32    {33        public static List<UiTreeNode> s_uiTreeNodes = new List<UiTreeNode>();34        public enum CompareMethod { Equal, StartsWith, Contains };35        public UiTreeNode(UiTreeNode pNode)36        {37            Parent = pNode;38            this.Items = new List<UiTreeNode>();39        }40        public UiTreeNode Parent { get; set; }41        public string Tag { get; set; }42        public string Title { get; set; }43        public string ClassName { get; set; }44        public string Name { get; set; }45        public string AutomationId { get; set; }46        public string Position { get; set; }47        public string Left { get; set; }48        public string Top { get; set; }49        public string Width { get; set; }50        public string Height { get; set; }51        public string RuntimeId { get; set; }52        public RecordedUiTask UiTask { get; set; }53        public List<UiTreeNode> Items { get; set; }54        public CompareMethod NameCompareMethod { get; set; }55        public CompareMethod ClassNameCompareMethod { get; set; }56        public CompareMethod AutomationIdCompareMethod { get; set; }57        public string NodePath { get; set; }58        public ObservableCollection<PropNameValue> LoadPropertyData()59        {60            var listPropValue = new ObservableCollection<PropNameValue>();61            listPropValue.Add(new PropNameValue("Tag", Tag));62            listPropValue.Add(new PropNameValue("ClassName", ClassName));63            listPropValue.Add(new PropNameValue("Name", Name));64            listPropValue.Add(new PropNameValue("AutomationId", AutomationId));65            listPropValue.Add(new PropNameValue("Position", (Position ?? "").ToString()));66            listPropValue.Add(new PropNameValue("Left", (Left ?? "").ToString()));67            listPropValue.Add(new PropNameValue("Top", (Top ?? "").ToString()));68            listPropValue.Add(new PropNameValue("Width", (Width ?? "").ToString()));69            listPropValue.Add(new PropNameValue("Height", (Height ?? "").ToString()));70            listPropValue.Add(new PropNameValue("RuntimeId", RuntimeId));71            listPropValue.Add(new PropNameValue("UiTask", UiTask != null ? UiTask.ToString() : ""));72            return listPropValue;73        }74        public static bool CompareRuntimeId(UiTreeNode nodeLeft, UiTreeNode nodeRight)75        {76            if (nodeLeft == null || nodeRight == null)77            {78                return false;79            }80            else81            {82                return nodeLeft.RuntimeId == nodeRight.RuntimeId;83            }84        }85        static UiTreeNode FindInCachedUiTree(UiTreeNode node, List<UiTreeNode> cachedNode)86        {87            foreach (var snode in cachedNode)88            {89                if (CompareRuntimeId(node, snode))90                {91                    return snode;92                }93            }94            return null;95        }96        public static void AddToUiTree(RecordedUiTask recordedUi)97        {98            if (recordedUi.GetUiTreeNode() == null)99            {100                if (string.IsNullOrEmpty(recordedUi.GetXPath(false)))101                {102                    return;103                }104            }105            var uiNode = recordedUi.GetUiTreeNode();106            if (uiNode == null || uiNode.Items.Count == 0)107            {108                //Log error109                return;110            }111            // Empty root112            if (s_uiTreeNodes.Count == 0)113            {114                s_uiTreeNodes.Add(uiNode);115                return;116            }117            // Add uiNode to s_uiNode118            UiTreeNode node = uiNode;119            UiTreeNode nodeCached = null;120            List<UiTreeNode> listCached = s_uiTreeNodes;121            while (node != null)122            {123                var nodeAddTo = FindInCachedUiTree(node, listCached);124                if (node.Items.Count > 0 && nodeAddTo != null)125                {126                    node = node.Items.First();127                    listCached = nodeAddTo.Items;128                    nodeCached = nodeAddTo;129                }130                else131                {132                    if (nodeCached != null)133                    {134                        node.Parent = nodeCached;135                        nodeCached.Items.Add(node);136                    }137                    break;138                }139            }140        }141        static UiTreeNode FindUiTaskNode(UiTreeNode node, RecordedUiTask uiTaskNode)142        {143            if (node.UiTask == uiTaskNode)144            {145                return node;146            }147            for (int i = 0; i < node.Items.Count; i++)148            {149                var ret = FindUiTaskNode(node.Items[i], uiTaskNode);150                if (ret != null)151                {152                    return ret;153                }154            }155            return null;156        }157        public static void RemoveUiTreeNode(RecordedUiTask uiTaskNode)158        {159            if (s_uiTreeNodes.Count == 0)160            {161                return;162            }163            UiTreeNode uiNode = FindUiTaskNode(s_uiTreeNodes.First(), uiTaskNode);164            if (uiNode == null)165            {166                s_uiTreeNodes.Clear();167                return;168            }169            UiTreeNode uiParent = uiNode.Parent;170            if (uiParent == null)171            {172                s_uiTreeNodes.Clear();173                return;174            }175            while (uiParent != null)176            {177                uiNode.UiTask = null;178                if (uiNode.Items.Count == 0)179                {180                    uiParent.Items.Remove(uiNode);181                }182                if (uiParent.Items.Count > 0)183                {...UiTreeNode
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using WinAppDriverUIRecorder;7{8    {9        static void Main(string[] args)10        {11            UiTreeNode node = new UiTreeNode();12            node.GetUiTree();13        }14    }15}16{17  "BoundingRectangle": {18  },UiTreeNode
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using WinAppDriverUIRecorder;7{8    {9        static void Main(string[] args)10        {11            UiTreeNode node = new UiTreeNode();12            node.GetUiTree();13        }14    }15}16using System;17using System.Collections.Generic;18using System.Linq;19using System.Text;20using System.Threading.Tasks;21using WinAppDriverUIRecorder;22{23    {24        static void Main(string[] args)25        {26            UiTreeNode node = new UiTreeNode();27            node.GetUiTree();28        }29    }30}31using System;32using System.Collections.Generic;33using System.Linq;34using System.Text;35using System.Threading.Tasks;36using WinAppDriverUIRecorder;37{38    {39        static void Main(string[] args)40        {41            UiTreeNode node = new UiTreeNode();42            node.GetUiTree();43        }44    }45}46using System;47using System.Collections.Generic;48using System.Linq;49using System.Text;50using System.Threading.Tasks;51using WinAppDriverUIRecorder;52{53    {54        static void Main(string[] args)55        {56            UiTreeNode node = new UiTreeNode();57            node.GetUiTree();58        }59    }60}61using System;62using System.Collections.Generic;63using System.Linq;64using System.Text;65using System.Threading.Tasks;66using WinAppDriverUIRecorder;67{68    {69        static void Main(string[] args)70        {71            UiTreeNode node = new UiTreeNode();72            node.GetUiTree();73        }74    }75}76using System;77using System.Collections.Generic;78using System.Linq;79using System.Text;UiTreeNode
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.Windows.Automation;7using WinAppDriverUIRecorder;8{9    {10        public static AutomationElement GetAutomationElement(AutomationElement root, string path)11        {12            if (root == null)13                return null;14            if (string.IsNullOrEmpty(path))15                return root;16            var pathParts = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);17            var currentElement = root;18            foreach (var pathPart in pathParts)19            {20                var index = int.Parse(pathPart);21                var children = currentElement.FindAll(TreeScope.Children, Condition.TrueCondition);22                if (index < 0 || index >= children.Count)23                    return null;24                currentElement = children[index];25            }26            return currentElement;27        }28    }29}30using System;31using System.Collections.Generic;32using System.Linq;33using System.Text;34using System.Threading.Tasks;35using System.Windows.Automation;36using WinAppDriverUIRecorder;37{38    {39        public static AutomationElement GetAutomationElement(AutomationElement root, string path)40        {41            if (root == null)42                return null;43            if (string.IsNullOrEmpty(path))44                return root;45            var pathParts = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);46            var currentElement = root;47            foreach (var pathPart in pathParts)48            {49                var index = int.Parse(pathPart);50                var children = currentElement.FindAll(TreeScope.Children, Condition.TrueCondition);51                if (index < 0 || index >= children.Count)52                    return null;53                currentElement = children[index];54            }55            return currentElement;56        }57    }58}59using System;60using System.Collections.Generic;61using System.Linq;62using System.Text;63using System.Threading.Tasks;64using System.Windows.Automation;65using WinAppDriverUIRecorder;66{67    {68        public static AutomationElement GetAutomationElement(AutomationElement root, string path)69        {70            if (root == null)71                return null;72            if (stringUiTreeNode
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.Windows.Automation;7using WinAppDriverUIRecorder;8{9    {10        public AutomationElement Element { get; set; }11        public UiTreeNode(AutomationElement element)12        {13            Element = element;14        }15        public List<UiTreeNode> GetChildren()16        {17            List<UiTreeNode> children = new List<UiTreeNode>();18            var childElements = Element.FindAll(TreeScope.Children, Condition.TrueCondition);19            foreach (AutomationElement childElement in childElements)20            {21                UiTreeNode node = new UiTreeNode(childElement);22                children.Add(node);23            }24            return children;25        }26        public override string ToString()27        {28            return Element.Current.Name;29        }30    }31}32using System;33using System.Collections.Generic;34using System.Linq;35using System.Text;36using System.Threading.Tasks;37using System.Windows.Automation;38using WinAppDriverUIRecorder;39{40    {41        public AutomationElement Element { get; set; }42        public UiTreeNode(AutomationElement element)43        {44            Element = element;45        }46        public List<UiTreeNode> GetChildren()47        {48            List<UiTreeNode> children = new List<UiTreeNode>();49            var childElements = Element.FindAll(TreeScope.Children, Condition.TrueCondition);50            foreach (AutomationElement childElement in childElements)51            {52                UiTreeNode node = new UiTreeNode(childElement);53                children.Add(node);54            }55            return children;56        }57        public override string ToString()58        {59            return Element.Current.Name;60        }61    }62}63using System;64using System.Collections.Generic;65using System.Linq;66using System.Text;67using System.Threading.Tasks;68using System.Windows.Automation;69using WinAppDriverUIRecorder;70{UiTreeNode
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.Windows.Automation;7using System.Windows.Forms;8using System.Xml;9using System.Xml.Serialization;10using System.IO;11using WinAppDriverUIRecorder;12{13    {14        static void Main(string[] args)15        {16            UiTreeNode root = new UiTreeNode();17            AutomationElement rootElement = AutomationElement.RootElement;18            root = root.UiTreeNodeMethod(rootElement);19            XmlSerializer serializer = new XmlSerializer(typeof(UiTreeNode));20            TextWriter textWriter = new StreamWriter("C:\\Users\\user\\Desktop\\test.xml");21            serializer.Serialize(textWriter, root);22            textWriter.Close();23        }24    }25}26using System;27using System.Collections.Generic;28using System.Linq;29using System.Text;30using System.Threading.Tasks;31using System.Windows.Automation;32using System.Windows.Forms;33using System.Xml;34using System.Xml.Serialization;35using System.IO;36using WinAppDriverUIRecorder;37{38    {39        static void Main(string[] args)40        {41            UiTreeNode root = new UiTreeNode();42            AutomationElement rootElement = AutomationElement.RootElement;43            root = root.UiTreeNodeMethod(rootElement);44            XmlSerializer serializer = new XmlSerializer(typeof(UiTreeNode));45            TextWriter textWriter = new StreamWriter("C:\\Users\\user\\Desktop\\test.xml");46            serializer.Serialize(textWriter, root);47            textWriter.Close();48        }49    }50}51using System;52using System.Collections.Generic;53using System.Linq;54using System.Text;55using System.Threading.Tasks;UiTreeNode
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.Windows.Automation;7using WinAppDriverUIRecorder;8{9    {10        static void Main(string[] args)11        {12            {13                var root = AutomationElement.RootElement;14                var tree = new UiTreeNode(root);15                Console.WriteLine(tree.ToString());16            }17            catch (Exception ex)18            {19                Console.WriteLine(ex.Message);20            }21            Console.ReadKey();22        }23    }24}25using System;26using System.Collections.Generic;27using System.Linq;28using System.Text;29using System.Threading.Tasks;30using System.Windows.Automation;31using WinAppDriverUIRecorder;32{33    {34        static void Main(string[] args)35        {36            {37                var root = AutomationElement.RootElement;38                var tree = new UiTreeNode(root);39                Console.WriteLine(tree.ToString());40            }41            catch (Exception ex)42            {43                Console.WriteLine(ex.Message);44            }45            Console.ReadKey();46        }47    }48}49using System;50using System.Collections.Generic;51using System.Linq;52using System.Text;53using System.Threading.Tasks;54using System.Windows.Automation;55using WinAppDriverUIRecorder;56{57    {58        static void Main(string[] args)59        {60            {61                var root = AutomationElement.RootElement;62                var tree = new UiTreeNode(root);63                Console.WriteLine(tree.ToString());64            }65            catch (Exception ex)66            {67                Console.WriteLine(ex.Message);68            }69            Console.ReadKey();70        }71    }72}73using System;74using System.Collections.Generic;75using System.Linq;76using System.Text;77using System.Threading.Tasks;78using System.Windows.Automation;79using WinAppDriverUIRecorder;80{UiTreeNode
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.Windows.Automation;7using WinAppDriverUIRecorder;8{9    {10        static void Main(string[] args)11        {UiTreeNode
Using AI Code Generation
1var UiTreeNode = require('WinAppDriverUIRecorder.UiTreeNode');2var app = Application("C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/Common7/IDE/devenv.exe");3app.wait();4app.start();5var window = app.window("C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/Common7/IDE/devenv.exe");6var node = window.getNode("Window", "Visual Studio");7var treeNode = new UiTreeNode(node);8treeNode.click();9treeNode.typeText("Hello World");10var UiTree = require('WinAppDriverUIRecorder.UiTree');11var app = Application("C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/Common7/IDE/devenv.exe");12app.wait();13app.start();14var window = app.window("C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/Common7/IDE/devenv.exe");15var tree = new UiTree(window);16tree.click("Window", "Visual Studio");17tree.typeText("Hello World");18var UiTree = require('WinAppDriverUIRecorder.UiTree');19var app = Application("C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/Common7/IDE/devenv.exe");20app.wait();21app.start();22var window = app.window("C:/Program Files (x86)/Microsoft Visual Studio/2017Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
