Best FlaUI code snippet using FlaUI.Core.AutomationElementXPathNavigator.AutomationElementXPathNavigator
AutomationElementXPathNavigator.cs
Source:AutomationElementXPathNavigator.cs  
...10    /// <summary>11    /// Custom implementation of a <see cref="XPathNavigator" /> which allows12    /// selecting items by xpath by using the <see cref="ITreeWalker" />.13    /// </summary>14    public class AutomationElementXPathNavigator : XPathNavigator15    {16        private const int NoAttributeValue = -1;17        private readonly AutomationElement _rootElement;18        private readonly ITreeWalker _treeWalker;19        private AutomationElement _currentElement;20        private int _attributeIndex = NoAttributeValue;21        public AutomationElementXPathNavigator(AutomationElement rootElement)22        {23            _treeWalker = rootElement.Automation.TreeWalkerFactory.GetControlViewWalker();24            _rootElement = rootElement;25            _currentElement = rootElement;26        }27        private bool IsInAttribute => _attributeIndex != NoAttributeValue;28        /// <inheritdoc />29        public override bool HasAttributes => !IsInAttribute;30        /// <inheritdoc />31        public override string Value => IsInAttribute ? GetAttributeValue(_attributeIndex) : _currentElement.ToString();32        /// <inheritdoc />33        public override object UnderlyingObject => _currentElement;34        /// <inheritdoc />35        public override XPathNodeType NodeType36        {37            get38            {39                if (IsInAttribute)40                {41                    return XPathNodeType.Attribute;42                }43                if (_currentElement.Equals(_rootElement))44                {45                    return XPathNodeType.Root;46                }47                return XPathNodeType.Element;48            }49        }50        /// <inheritdoc />51        public override string LocalName => IsInAttribute ? GetAttributeName(_attributeIndex) : _currentElement.Properties.ControlType.ValueOrDefault.ToString();52        /// <inheritdoc />53        public override string Name => LocalName;54        /// <inheritdoc />55        public override XmlNameTable NameTable => throw new NotImplementedException();56        /// <inheritdoc />57        public override string NamespaceURI => String.Empty;58        /// <inheritdoc />59        public override string Prefix => String.Empty;60        /// <inheritdoc />61        public override string BaseURI => String.Empty;62        /// <inheritdoc />63        public override bool IsEmptyElement => false;64        /// <inheritdoc />65        public override XPathNavigator Clone()66        {67            var clonedObject = new AutomationElementXPathNavigator(_rootElement)68            {69                _currentElement = _currentElement,70                _attributeIndex = _attributeIndex71            };72            return clonedObject;73        }74        /// <inheritdoc />75        public override bool MoveToFirstAttribute()76        {77            if (IsInAttribute)78            {79                return false;80            }81            _attributeIndex = 0;82            return true;83        }84        /// <inheritdoc />85        public override bool MoveToNextAttribute()86        {87            if (_attributeIndex >= Enum.GetNames(typeof(ElementAttributes)).Length - 1)88            {89                // No more attributes90                return false;91            }92            if (!IsInAttribute)93            {94                return false;95            }96            _attributeIndex++;97            return true;98        }99        /// <inheritdoc />100        public override string GetAttribute(string localName, string namespaceUri)101        {102            if (IsInAttribute)103            {104                return String.Empty;105            }106            var attributeIndex = GetAttributeIndexFromName(localName);107            if (attributeIndex != NoAttributeValue)108            {109                return GetAttributeValue(attributeIndex);110            }111            return String.Empty;112        }113        /// <inheritdoc />114        public override bool MoveToAttribute(string localName, string namespaceUri)115        {116            if (IsInAttribute)117            {118                return false;119            }120            var attributeIndex = GetAttributeIndexFromName(localName);121            if (attributeIndex != NoAttributeValue)122            {123                _attributeIndex = attributeIndex;124                return true;125            }126            return false;127        }128        /// <inheritdoc />129        public override bool MoveToFirstNamespace(XPathNamespaceScope namespaceScope) => throw new NotImplementedException();130        /// <inheritdoc />131        public override bool MoveToNextNamespace(XPathNamespaceScope namespaceScope) => throw new NotImplementedException();132        /// <inheritdoc />133        public override void MoveToRoot()134        {135            _attributeIndex = NoAttributeValue;136            _currentElement = _rootElement;137        }138        /// <inheritdoc />139        public override bool MoveToNext()140        {141            if (IsInAttribute) { return false; }142            var nextElement = _treeWalker.GetNextSibling(_currentElement);143            if (nextElement == null)144            {145                return false;146            }147            _currentElement = nextElement;148            return true;149        }150        /// <inheritdoc />151        public override bool MoveToPrevious()152        {153            if (IsInAttribute) { return false; }154            var previousElement = _treeWalker.GetPreviousSibling(_currentElement);155            if (previousElement == null)156            {157                return false;158            }159            _currentElement = previousElement;160            return true;161        }162        /// <inheritdoc />163        public override bool MoveToFirstChild()164        {165            if (IsInAttribute) { return false; }166            var childElement = _treeWalker.GetFirstChild(_currentElement);167            if (childElement == null)168            {169                return false;170            }171            _currentElement = childElement;172            return true;173        }174        /// <inheritdoc />175        public override bool MoveToParent()176        {177            if (IsInAttribute)178            {179                _attributeIndex = NoAttributeValue;180                return true;181            }182            if (_currentElement.Equals(_rootElement))183            {184                return false;185            }186            _currentElement = _treeWalker.GetParent(_currentElement);187            return true;188        }189        /// <inheritdoc />190        public override bool MoveTo(XPathNavigator other)191        {192            var specificNavigator = other as AutomationElementXPathNavigator;193            if (specificNavigator == null)194            {195                return false;196            }197            if (!_rootElement.Equals(specificNavigator._rootElement))198            {199                return false;200            }201            _currentElement = specificNavigator._currentElement;202            _attributeIndex = specificNavigator._attributeIndex;203            return true;204        }205        /// <inheritdoc />206        public override bool MoveToId(string id)207        {208            return false;209        }210        /// <inheritdoc />211        public override bool IsSamePosition(XPathNavigator other)212        {213            var specificNavigator = other as AutomationElementXPathNavigator;214            if (specificNavigator == null)215            {216                return false;217            }218            if (!_rootElement.Equals(specificNavigator._rootElement))219            {220                return false;221            }222            return _currentElement.Equals(specificNavigator._currentElement)223                && _attributeIndex == specificNavigator._attributeIndex;224        }225        private string GetAttributeValue(int attributeIndex)226        {227            switch ((ElementAttributes)attributeIndex)...AutomationElementXPathNavigator
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using FlaUI.Core;7using FlaUI.Core.AutomationElements;8using FlaUI.Core.Definitions;9using FlaUI.Core.Tools;10using FlaUI.UIA3;11using System.Xml.XPath;12{13    {14        static void Main(string[] args)15        {16            var application = Application.Launch("notepad.exe");17            var automation = new UIA3Automation();18            var window = application.GetMainWindow(automation);19            var editBox = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Edit)).AsTextBox();20            editBox.Enter("Hello World");21            var path = "/Window[@Name='Untitled - Notepad']/Edit";22            var element = window.FindFirstDescendant(cf => cf.ByXPath(path));23            Console.WriteLine("XPath: {0}", path);24            Console.WriteLine("AutomationId: {0}", element.Properties.AutomationId);25            Console.WriteLine("ControlType: {0}", element.Properties.ControlType);26            Console.WriteLine("Name: {0}", element.Properties.Name);27            Console.WriteLine("ClassName: {0}", element.Properties.ClassName);28            Console.WriteLine("FrameworkId: {0}", element.Properties.FrameworkId);29            Console.WriteLine("ProcessId: {0}", element.Properties.ProcessId);30            Console.WriteLine("NativeWindowHandle: {0}", element.Properties.NativeWindowHandle);31            Console.WriteLine("IsKeyboardFocusable: {0}", element.Properties.IsKeyboardFocusable);32            Console.WriteLine("HasKeyboardFocus: {0}", element.Properties.HasKeyboardFocus);33            Console.WriteLine("IsEnabled: {0}", element.Properties.IsEnabled);34            Console.WriteLine("IsOffscreen: {0}", element.Properties.IsOffscreen);35            Console.WriteLine("IsPassword: {0}", element.Properties.IsPassword);36            Console.WriteLine("IsRequiredForForm: {0}", element.Properties.IsRequiredForForm);37            Console.WriteLine("ItemStatus: {0}", element.Properties.ItemStatus);38            Console.WriteLine("ItemType: {0}", element.Properties.ItemType);39            Console.WriteLine("BoundingRectangle: {0}", element.Properties.BoundingRectangle);40            Console.WriteLine("AcceleratorKey: {0}", element.Properties.AcceleratorKey);41            Console.WriteLine("AccessKey: {0}", element.Properties.AccessKey);42            Console.WriteLine("LocalizedControlType: {0}", element.Properties.LocalizedControlType);43            Console.WriteLine("AutomationElementXPathNavigator
Using AI Code Generation
1using System;2using FlaUI.Core;3using FlaUI.Core.AutomationElements;4using FlaUI.Core.AutomationElements.Infrastructure;5using FlaUI.Core.Definitions;6using FlaUI.Core.Input;7using FlaUI.Core.Tools;8using FlaUI.UIA3;9using FlaUI.Core.Conditions;10using FlaUI.Core.Patterns;11using FlaUI.Core.WindowsAPI;12using System.Windows.Automation;13using System.Diagnostics;14using System.Threading;15using System.Collections.Generic;16using System.Windows.Automation.Text;17using System.Windows.Automation.Provider;18using System.Windows.Automation.Peers;19using System.Windows;20using System.Windows.Controls;21using System.Windows.Media;22using System.Windows.Shapes;23using System.Windows.Threading;24using System.Windows.Input;25using System.Windows.Automation;26using System.Windows.Automation.Text;27using System.Windows.Automation.Provider;28using System.Windows.Automation.Peers;29using System.Windows.Controls.Primitives;30using System.Windows.Media;31using System.Windows.Shapes;32using System.Windows.Threading;33using System.Windows.Input;34using System.IO;35using System.Xml;36using System.Xml.XPath;37using System.Xml.Linq;38using System.Xml.Serialization;39using System.Linq;40using System.Text;41{42    {43        static void Main(string[] args)44        {45            {46                var app = FlaUI.Core.Application.Launch(@"C:\Windows\System32\calc.exe");47                var automation = new UIA3Automation();48                var window = app.GetMainWindow(automation);49                var root = window.FindFirstDescendant();50                var nav = new AutomationElementXPathNavigator(root);51                nav.MoveToRoot();52                nav.MoveToFirstChild();53                nav.MoveToFirstChild();AutomationElementXPathNavigator
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 FlaUI.Core;8using FlaUI.Core.Definitions;9using FlaUI.Core.Input;10using FlaUI.Core.Tools;11using FlaUI.UIA3;12using FlaUI.UIA2;13using FlaUI.Core.AutomationElements;14using FlaUI.Core.AutomationElements.Infrastructure;15using FlaUI.Core.AutomationElements.PatternElements;16using FlaUI.Core.Conditions;17using FlaUI.Core.WindowsAPI;18{19    {20        static void Main(string[] args)21        {22            var application = FlaUI.Core.Application.Launch(@"C:\Windows\System32\calc.exe");23            var automation = new UIA3Automation();24            var mainWindow = application.GetMainWindow(automation);25            var result = mainWindow.FindFirstDescendant(cf => cf.ByAutomationId("CalculatorResults"));26            var xpathNavigator = result.AsXPathNavigable().CreateNavigator();27            var xpathNodeIterator = xpathNavigator.Select(xpathExpression);28            var automationElement = (AutomationElement)xpathNodeIterator.Current.UnderlyingObject;29            Console.WriteLine(automationElement.Current.Name);30            Console.ReadLine();31        }32    }33}34using System;35using System.Collections.Generic;36using System.Linq;37using System.Text;38using System.Threading.Tasks;39using System.Windows.Automation;40using FlaUI.Core;41using FlaUI.Core.Definitions;42using FlaUI.Core.Input;43using FlaUI.Core.Tools;44using FlaUI.UIA3;45using FlaUI.UIA2;46using FlaUI.Core.AutomationElements;47using FlaUI.Core.AutomationElements.Infrastructure;48using FlaUI.Core.AutomationElements.PatternElements;49using FlaUI.Core.Conditions;50using FlaUI.Core.WindowsAPI;51{52    {53        static void Main(string[] args)54        {55            var application = FlaUI.Core.Application.Launch(@"C:\Windows\System32\calc.exe");56            var automation = new UIA3Automation();57            var mainWindow = application.GetMainWindow(automation);58            var result = mainWindow.FindFirstDescendant(cf => cf.ByAutomationId("CalculatorResults"));AutomationElementXPathNavigator
Using AI Code Generation
1using System.Windows.Automation;2using FlaUI.Core;3using FlaUI.Core.AutomationElements;4using FlaUI.Core.AutomationElements.Infrastructure;5using FlaUI.Core.Definitions;6using FlaUI.Core.Input;7using FlaUI.Core.WindowsAPI;8using FlaUI.UIA3;9using System;10using System.Collections.Generic;11using System.Linq;12using System.Text;13using System.Threading.Tasks;14using System.Windows.Automation;15using FlaUI.Core;16using FlaUI.Core.AutomationElements;17using FlaUI.Core.AutomationElements.Infrastructure;18using FlaUI.Core.Definitions;19using FlaUI.Core.Input;20using FlaUI.Core.WindowsAPI;21using FlaUI.UIA3;22{23    {24        static void Main(string[] args)25        {26            var app = Application.Launch(@"C:\Program Files (x86)\Microsoft Office\Office16\WINWORD.EXE");27            var automation = new UIA3Automation();28            var window = app.GetMainWindow(automation);29            var element = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Button).And(cf.ByName("File")));30            element.AsButton().Invoke();31            var element1 = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Button).And(cf.ByName("New")));32            element1.AsButton().Invoke();33            var element2 = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Button).And(cf.ByName("Blank document")));34            element2.AsButton().Invoke();35            var element3 = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Button).And(cf.ByName("Close")));36            element3.AsButton().Invoke();37            var element4 = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Button).And(cf.ByName("File")));38            element4.AsButton().Invoke();39            var element5 = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Button).And(cf.ByName("Open")));40            element5.AsButton().Invoke();41            var element6 = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Button).And(cf.ByName("Document")));42            element6.AsButton().Invoke();43            var element7 = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Button).And(cf.ByName("Close")));44            element7.AsButton().Invoke();45            var element8 = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Button).And(cf.ByNameAutomationElementXPathNavigator
Using AI Code Generation
1using System;2using System.Windows.Automation;3using FlaUI.Core;4using FlaUI.UIA2;5using FlaUI.Core.AutomationElements.Infrastructure;6using FlaUI.Core.Definitions;7using FlaUI.Core.Conditions;8using System.Windows.Forms;AutomationElementXPathNavigator
Using AI Code Generation
1using FlaUI.Core;2using FlaUI.Core.AutomationElements;3using FlaUI.Core.AutomationElements.Infrastructure;4using FlaUI.Core.Definitions;5using FlaUI.Core.Patterns;6using FlaUI.Core.Tools;7using FlaUI.UIA3;8using System;9using System.Collections.Generic;10using System.Diagnostics;11using System.Linq;12using System.Text;13using System.Threading.Tasks;14{15    {16        static void Main(string[] args)17        {18            Process process = Process.Start(@"C:\Windows\System32\calc.exe");19            var automation = new UIA3Automation();20            var mainWindow = automation.WaitForDesktopWindow("Calculator", InitializeOption.NoCache);21            Console.WriteLine(element.Current.Name);22            Console.ReadLine();23        }24    }25}AutomationElementXPathNavigator
Using AI Code Generation
1using FlaUI.Core;2using FlaUI.Core.AutomationElements;3using FlaUI.Core.AutomationElements.Infrastructure;4using FlaUI.Core.Definitions;5using FlaUI.Core.Tools;6using System;7using System.Collections.Generic;8using System.Linq;9using System.Text;10using System.Threading.Tasks;11using System.Windows.Automation;12using System.Windows.Automation.Text;13using System.Xml.XPath;14{15    {16        static void Main(string[] args)17        {18            var application = FlaUI.Core.Application.Launch("notepad.exe");19            var mainWindow = application.GetMainWindow(FlaUI.Core.Automation.AutomationFactory.Instance);20            var mainAutomationElement = mainWindow.AutomationElement;21            var buttonAutomationElement = mainAutomationElement.FindFirst(FlaUI.Core.Definitions.TreeScope.Descendants, new FlaUI.Core.Conditions.AndCondition(new FlaUI.Core.Conditions.PropertyCondition(FlaUI.Core.Definitions.AutomationElement.ControlTypeProperty, FlaUI.Core.Definitions.ControlType.Button), new FlaUI.Core.Conditions.PropertyCondition(FlaUI.Core.Definitions.AutomationElement.NameProperty, "Save")));22            var buttonAutomationElement1 = mainAutomationElement.FindFirst(FlaUI.Core.Definitions.TreeScope.Descendants, new FlaUI.Core.Conditions.AndCondition(new FlaUI.Core.Conditions.PropertyCondition(FlaUI.Core.Definitions.AutomationElement.ControlTypeProperty, FlaUI.Core.Definitions.ControlType.Button), new FlaUI.Core.Conditions.PropertyCondition(FlaUI.Core.Definitions.AutomationElement.NameProperty, "Save")));23            var buttonAutomationElement2 = mainAutomationElement.FindFirst(FlaUI.Core.Definitions.TreeScope.Descendants, new FlaUI.Core.Conditions.AndCondition(new FlaUI.Core.Conditions.PropertyCondition(FlaUI.Core.Definitions.AutomationElement.ControlTypeProperty, FlaUI.Core.Definitions.ControlType.Button), new FlaUI.Core.Conditions.PropertyCondition(FlaUIAutomationElementXPathNavigator
Using AI Code Generation
1using System;2using System.Windows.Automation;3using FlaUI.Core;4using FlaUI.UIA3;5using System.Threading;6using System.Collections.Generic;7{8    {9        static void Main(string[] args)10        {11            System.Diagnostics.Process.Start("notepad.exe");12            Thread.Sleep(1000);13            var automation = new UIA3Automation();14            var window = automation.GetDesktop();15            var editor = window.FindFirstDescendant(FlaUI.Core.Definitions.TreeScope.Descendants, new FlaUI.Core.Conditions.AndCondition(FlaUI.Core.Conditions.ConditionFactory.ByAutomationId("15"), FlaUI.Core.Conditions.ConditionFactory.ByName("Text Editor")));16            var textbox = editor.FindFirstDescendant(FlaUI.Core.Definitions.TreeScope.Descendants, new FlaUI.Core.Conditions.AndCondition(FlaUI.Core.Conditions.ConditionFactory.ByAutomationId("1001"), FlaUI.Core.Conditions.ConditionFactory.ByName("")));17            textbox.AsTextBox().Text = "Hello World";18            Thread.Sleep(1000);19            window.Close();20            Thread.Sleep(1000);21        }22    }23}24FlaUI.Core.AutomationElement.FindFirstDescendant(FlaUI.Core.Definitions.TreeScope, FlaUI.Core.Conditions.AndCondition)Learn 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!!
