Best FlaUI code snippet using FlaUI.Core.AutomationElements.ComboBoxItem.ComboBoxItem
MainWindow.xaml.cs
Source:MainWindow.xaml.cs  
...28    /// Interaction logic for MainWindow.xaml29    /// </summary>30    public static class TestMethod31    {32        public static void ActionSelectComboBoxItem(this AutomationElement comboBoxElement, int indexToSelect)33        {34            if (comboBoxElement == null)35                throw new Exception("Combo Box not found");36            //Get the all the list items in the ComboBox37            //Expand the combobox38            ExpandCollapsePattern expandPattern = (ExpandCollapsePattern)comboBoxElement.GetCurrentPattern(ExpandCollapsePattern.Pattern);39            expandPattern.Expand();40            AutomationElementCollection comboboxItem = comboBoxElement.FindAll(TreeScope.Children, new System.Windows.Automation.PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem));41            //Index to set in combo box42            AutomationElement itemToSelect = comboboxItem[indexToSelect];43            //Finding the pattern which need to select44            SelectionItemPattern selectPattern = (SelectionItemPattern)itemToSelect.GetCurrentPattern(SelectionItemPattern.Pattern);45            selectPattern.Select();46        }47        #region 1st Way to select combobox item48        //public static void SetSelectedComboBoxItem(this AutomationElement comboBox, string item)49        //{50        //    AutomationPattern automationPatternFromElement = GetSpecifiedPattern(comboBox, "ExpandCollapsePatternIdentifiers.Pattern");51        //    ExpandCollapsePattern expandCollapsePattern = comboBox.GetCurrentPattern(automationPatternFromElement) as ExpandCollapsePattern;52        //    expandCollapsePattern.Expand();53        //    expandCollapsePattern.Collapse();54        //    AutomationElement listItem = comboBox.FindFirst(TreeScope.Subtree, new System.Windows.Automation.PropertyCondition(AutomationElement.NameProperty, item));55        //    automationPatternFromElement = GetSpecifiedPattern(listItem, "SelectionItemPatternIdentifiers.Pattern");56        //    SelectionItemPattern selectionItemPattern = listItem.GetCurrentPattern(automationPatternFromElement) as SelectionItemPattern;57        //    selectionItemPattern.Select();58        //}59        #endregion60        #region 2nd Way to select combobox item61        public static void SetSelectedComboBoxItem(this AutomationElement comboBoxElement, string item)62        {63            if (comboBoxElement == null)64                throw new Exception("Combo Box not found");65            //Get the all the list items in the ComboBox66            //Expand the combobox67            ExpandCollapsePattern expandPattern = (ExpandCollapsePattern)comboBoxElement.GetCurrentPattern(ExpandCollapsePattern.Pattern);68            expandPattern.Expand();69            expandPattern.Collapse();70            AutomationElementCollection comboboxItem = comboBoxElement.FindAll(TreeScope.Subtree, new System.Windows.Automation.PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem));71            int i = 0;72            //try to get patterns73            //foreach(AutomationElement cbcItem in comboboxItem)74            //{75            //    foreach (AutomationPattern ap in cbcItem.GetSupportedPatterns())76            //    {77            //        MessageBox.Show(ap.ProgrammaticName);78            //    }79            //}80            foreach (AutomationElement cbxItem in comboboxItem)81            {82                if(cbxItem.FindFirst(TreeScope.Children, System.Windows.Automation.Condition.TrueCondition).Current.Name==item)83                {84                    break;85                }86                i++;87            }88            //Index to set in combo box89            AutomationElement itemToSelect = comboboxItem[i];90            //Finding the pattern which need to select91            SelectionItemPattern selectPattern = (SelectionItemPattern)itemToSelect.GetCurrentPattern(SelectionItemPattern.Pattern);92            selectPattern.Select();93        }94        #endregion95        public static AutomationPattern GetSpecifiedPattern( this AutomationElement element, string patternName)96        {97            AutomationPattern[] supportedPattern = element.GetSupportedPatterns();98            foreach (AutomationPattern pattern in supportedPattern)99            {100                if (pattern.ProgrammaticName == patternName)101                    return pattern;102            }103            return null;104        }105        public static List<AutomationElement> GetAllDescendants(this AutomationElement element, int depth = 0, int maxDepth = 4)106        {107            var allChildren = new List<AutomationElement>();108            if (depth > maxDepth)109            {110                return allChildren;111            }112            AutomationElement sibling = TreeWalker.RawViewWalker.GetFirstChild(element);113            while (sibling != null)114            {115                allChildren.Add(sibling);116                allChildren.AddRange(sibling.GetAllDescendants(depth + 1, maxDepth));117                sibling = TreeWalker.RawViewWalker.GetNextSibling(sibling);118            }119            return allChildren;120        }121        [DllImport("USER32.DLL")]122        public static extern bool SetForegroundWindow(IntPtr hWnd);123    }124    public partial class MainWindow : Window125    {126        public MainWindow()127        {128            InitializeComponent();129        }130        private void button_Click(object sender, RoutedEventArgs e)131        {132            #region oldcode133            AutomationElement target = null;134            AutomationElementCollection automationCollection = AutomationElement.RootElement.FindAll(TreeScope.Children, System.Windows.Automation.Condition.TrueCondition);135            foreach (AutomationElement automation in automationCollection)136            {137                if (automation.Current.Name == "Há» Thá»ng Quản Lý Bán Lẻ")// sá»a lại thành cá»a sá» Äang cần spy138                {139                    target = automation;140                    break;141                }142            }143            Process[] flexproc = Process.GetProcessesByName("FlexBARMS");// Sá»a lại tên app144            TestMethod.SetForegroundWindow(flexproc[0].MainWindowHandle);145            Thread.Sleep(1000);146            var automationlist = TestMethod.GetAllDescendants(target);147            int i = 0;148            foreach (AutomationElement a in automationlist)149            {150                listBox.Items.Add(i+a.Current.AutomationId+"_"+a.Current.Name+"_"+a.Current.ControlType.LocalizedControlType);151                if (a.Current.AutomationId == "PersonalCountryCmb")// sá»a lại thành PersonalCountryCmb152                //if (i == 63)153                {154                    try155                    {156                        //a.ActionSelectComboBoxItem(2);157                        //Thread.Sleep(1000);158                        ((ValuePattern)a.GetCurrentPattern(ValuePattern.Pattern)).SetValue("Russia");159                    }160                    catch (Exception error)161                    {162                        MessageBox.Show(error.Message);163                    }164                }165                i++;166            }167            #endregion168            //var items = a.FindAll(TreeScope.Descendants, new System.Windows.Automation.PropertyCondition(AutomationElement.AutomationIdProperty, "PART_Item"));169            //var items = a.FindAll(TreeScope.Descendants, System.Windows.Automation.Condition.TrueCondition);170            //foreach (AutomationElement item in items)...ComboBoxItem.cs
Source:ComboBoxItem.cs  
...3{4    /// <summary>5    /// Class to interact with a combobox item element.6    /// </summary>7    public class ComboBoxItem : SelectionItemAutomationElement8    {9        /// <summary>10        /// Creates a <see cref="ComboBoxItem"/> element.11        /// </summary>12        public ComboBoxItem(FrameworkAutomationElementBase frameworkAutomationElement) : base(frameworkAutomationElement)13        {14        }15        /// <summary>16        /// Gets the text of the element.17        /// </summary>18        public virtual string Text19        {20            get21            {22                if (FrameworkType == FrameworkType.Wpf)23                {24                    // In WPF, the Text is actually an inner content only (text) element25                    // which can be accessed only with a raw walker.26                    var rawTreeWalker = Automation.TreeWalkerFactory.GetRawViewWalker();...ComboBoxItem
Using AI Code Generation
1using FlaUI.Core.AutomationElements;2using FlaUI.Core.AutomationElements.Infrastructure;3using FlaUI.Core.Definitions;4using FlaUI.Core.Input;5using FlaUI.Core.WindowsAPI;6using FlaUI.UIA3;7using System;8using System.Windows.Automation;9{10    {11        static void Main(string[] args)12        {13            using (var automation = new UIA3Automation())14            {15                var app = FlaUI.Core.Application.Launch(@"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\devenv.exe");16                var window = app.GetMainWindow(automation);17                var comboBox = window.FindFirstDescendant(cf => cf.ByAutomationId("ComboBox")).AsComboBox();18                comboBox.Select(0);19                comboBox.Select(1);20                comboBox.Select(2);21                comboBox.Select(3);22                comboBox.Select(4);23                comboBox.Select(5);24                comboBox.Select(6);25                comboBox.Select(7);26                comboBox.Select(8);27                comboBox.Select(9);28                comboBox.Select(10);29                comboBox.Select(11);30                comboBox.Select(12);31                comboBox.Select(13);32                comboBox.Select(14);33                comboBox.Select(15);34                comboBox.Select(16);35                comboBox.Select(17);36                comboBox.Select(18);37                comboBox.Select(19);38                comboBox.Select(20);39                comboBox.Select(21);40                comboBox.Select(22);41                comboBox.Select(23);42                comboBox.Select(24);43                comboBox.Select(25);44                comboBox.Select(26);45                comboBox.Select(27);46                comboBox.Select(28);47                comboBox.Select(29);48                comboBox.Select(30);49                comboBox.Select(31);50                comboBox.Select(32);51                comboBox.Select(33);52                comboBox.Select(34);53                comboBox.Select(35);54                comboBox.Select(36);55                comboBox.Select(37);56                comboBox.Select(38);57                comboBox.Select(39);58                comboBox.Select(40);59                comboBox.Select(41);60                comboBox.Select(42);61                comboBox.Select(43);62                comboBox.Select(44);63                comboBox.Select(45);64                comboBox.Select(46);65                comboBox.Select(47);66                comboBox.Select(48);67                comboBox.Select(49);68                comboBox.Select(50);69                comboBox.Select(51);70                comboBox.Select(52);71                comboBox.Select(53);72                comboBox.Select(54);73                comboBox.Select(55);74                comboBox.Select(56);ComboBoxItem
Using AI Code Generation
1using System;2using FlaUI.Core.AutomationElements;3using FlaUI.Core.AutomationElements.Infrastructure;4using FlaUI.Core.Definitions;5using FlaUI.Core.Input;6using FlaUI.Core.WindowsAPI;7using FlaUI.UIA3;8using System.Windows.Automation;9{10    {11        static void Main(string[] args)12        {13            var application = FlaUI.Core.Application.Launch("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");14            var automation = new UIA3Automation();15            var mainWindow = application.GetMainWindow(automation);16            var comboBox = mainWindow.FindFirstDescendant(cf => cf.ByAutomationId("cmbLanguages")).AsComboBox();17            var comboBoxItem = comboBox.FindFirstDescendant(cf => cf.ByAutomationId("en-US")).AsComboBoxItem();18            comboBoxItem.Select();19            Console.ReadLine();20        }21    }22}23using System;24using FlaUI.Core.AutomationElements;25using FlaUI.Core.AutomationElements.Infrastructure;26using FlaUI.Core.Definitions;27using FlaUI.Core.Input;28using FlaUI.Core.WindowsAPI;29using FlaUI.UIA3;30using System.Windows.Automation;31{32    {33        static void Main(string[] args)34        {35            var application = FlaUI.Core.Application.Launch("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");36            var automation = new UIA3Automation();37            var mainWindow = application.GetMainWindow(automation);38            var comboBox = mainWindow.FindFirstDescendant(cf => cf.ByAutomationId("cmbLanguages")).AsComboBox();39            var comboBoxItem = comboBox.FindFirstDescendant(cf => cf.ByAutomationId("en-US")).AsComboBoxItem();40            comboBoxItem.Select();41            Console.ReadLine();42        }43    }44}45using System;46using FlaUI.Core.AutomationElements;47using FlaUI.Core.AutomationElements.Infrastructure;48using FlaUI.Core.Definitions;49using FlaUI.Core.Input;50using FlaUI.Core.WindowsAPI;51using FlaUI.UIA3;52using System.Windows.Automation;53{54    {55        static void Main(string[] args)56        {ComboBoxItem
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using FlaUI.Core.AutomationElements;7using FlaUI.Core.AutomationElements.Infrastructure;8using FlaUI.Core.Definitions;9using FlaUI.Core.Input;10using FlaUI.Core.Tools;11using FlaUI.Core.WindowsAPI;12using FlaUI.UIA3;13using System.Windows.Automation;14using System.Windows.Automation.Provider;15using System.Windows.Controls;16using System.Windows.Controls.Primitives;17{18    {19        static void Main(string[] args)20        {21            var app = FlaUI.Core.Application.Launch(@"C:\Windows\System32\calc.exe");22            var automation = new UIA3Automation();23            var window = app.GetMainWindow(automation);24            var comboBox = window.FindFirstDescendant(cf => cf.ByAutomationId("ComboBox1")).AsComboBox();25            var comboBoxItem = comboBox.FindAllDescendant(cf => cf.ByControlType(ControlType.ListItem)).ToList()[2].AsComboBoxItem();26            comboBoxItem.Select();27        }28    }29}ComboBoxItem
Using AI Code Generation
1using System;2using FlaUI.Core;3using FlaUI.Core.AutomationElements;4using FlaUI.Core.AutomationElements.Infrastructure;5using FlaUI.Core.Conditions;6using FlaUI.Core.Definitions;7using FlaUI.Core.EventHandlers;8using FlaUI.Core.Input;9using FlaUI.Core.WindowsAPI;10using FlaUI.UIA3;11{12    {13        static void Main(string[] args)14        {15            var app = Application.Launch(@"C:\Windows\System32\calc.exe");16            var automation = new UIA3Automation();17            var mainWindow = app.GetMainWindow(automation);18            var button = mainWindow.FindFirstDescendant(cf => cf.ByControlType(ControlType.Button).And(cf.ByName("One")));19            button.Click();20            var button1 = mainWindow.FindFirstDescendant(cf => cf.ByControlType(ControlType.Button).And(cf.ByName("Two")));21            button1.Click();22            var button2 = mainWindow.FindFirstDescendant(cf => cf.ByControlType(ControlType.Button).And(cf.ByName("Three")));23            button2.Click();24            var button3 = mainWindow.FindFirstDescendant(cf => cf.ByControlType(ControlType.Button).And(cf.ByName("Four")));25            button3.Click();26            var button4 = mainWindow.FindFirstDescendant(cf => cf.ByControlType(ControlType.Button).And(cf.ByName("Five")));27            button4.Click();28            var button5 = mainWindow.FindFirstDescendant(cf => cf.ByControlType(ControlType.Button).And(cf.ByName("Six")));29            button5.Click();30            var button6 = mainWindow.FindFirstDescendant(cf => cf.ByControlType(ControlType.Button).And(cf.ByName("Seven")));31            button6.Click();ComboBoxItem
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using FlaUI.Core.AutomationElements;7using FlaUI.Core.Definitions;8using FlaUI.Core.Tools;9{10    {11        static void Main(string[] args)12        {13            var application = FlaUI.Core.Application.Launch(@"C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE");14            Retry.WhileException(() => application.GetMainWindow(Automation15                .Instance), TimeSpan.FromSeconds(10), TimeSpan.FromMilliseconds(100));16            var mainWindow = application.GetMainWindow(Automation.Instance);17            var comboBox = mainWindow.FindFirstDescendant(cf => cf.ByAutomationId("FontColorGallery")).AsComboBox();18            var comboBoxItem = comboBox.FindFirstDescendant(cf => cf.ByAutomationId("FontColorGalleryItem1")).AsComboBoxItem();19            var comboBoxItemValue = comboBoxItem.ComboBoxItemValue;20            comboBoxItem.Select();21            Console.WriteLine("Value of the comboboxitem is: " + comboBoxItemValue);22            Console.ReadLine();23        }24    }25}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!!
