Exemple #1
0
        public static void xtSelectFromComboBox(this IUIAutomationElement element, string Option)
        {
            var childrenOfCombobox = element.xtGetAllChildren();
            IUIAutomationElement listItemToSelect = null;

            bool IsInsideTable = element.xtGetRelative(RelativeType.Parent).CurrentLocalizedControlType == "dataitem";

            //	Seeing if the intended Option to Select even exists
            bool found = false;

            for (int i = 0; i < childrenOfCombobox.Length; i++)
            {
                if (childrenOfCombobox.GetElement(i).CurrentName == Option)
                {
                    found            = true;
                    listItemToSelect = (IUIAutomationElement)childrenOfCombobox.GetElement(i);
                    break;
                }
            }
            if (!found)
            {
                Console.WriteLine($"List Item is not an Option!!! ComboBox Name: [{element.CurrentName}] - ComboBox ID: [{element.CurrentAutomationId}] - List Item Selection: [{Option}] .");
                throw new Exception($"List Item is not an Option!!! ComboBox Name: [{element.CurrentName}] - ComboBox ID: [{element.CurrentAutomationId}] - List Item Selection: [{Option}] .");
            }

            //	If intended list item is already selected - just skip everything - no need
            if (listItemToSelect.xtIsItemSelected())
            {
                return;
            }

            //	Seeing if a blank option exists
            bool blankOptionExists = false;

            for (int i = 0; i < childrenOfCombobox.Length; i++)
            {
                if (childrenOfCombobox.GetElement(i).CurrentName == " ")
                {
                    blankOptionExists = true;
                    break;
                }
            }

            // Block to Complete selection if the combo box is inside a table row
            if (IsInsideTable)
            {
                try { element.xtScrollIntoView(); } catch (Exception e) { }
                Thread.Sleep(50);
                element.xtFocus();
                Thread.Sleep(50);
                element.xtClickCenterOfBounds();

                for (int i = 0; i < (childrenOfCombobox.Length + 2); i++)
                {
                    Keyboard.Instance.PressSpecialKey(TestStack.White.WindowsAPI.KeyboardInput.SpecialKeys.UP);
                }
                for (int i = 0; i < (childrenOfCombobox.Length + 2); i++)
                {
                    if (listItemToSelect.xtIsItemSelected())
                    {
                        break;
                    }
                    Keyboard.Instance.PressSpecialKey(TestStack.White.WindowsAPI.KeyboardInput.SpecialKeys.DOWN);
                }
                goto czCheckpoint;
            }


            //	Block to complete the ComboBox Selection
            if (blankOptionExists)
            {
                Thread.Sleep(50);
                element.SetFocus();
                Thread.Sleep(50);
                Keyboard.Instance.PressSpecialKey(TestStack.White.WindowsAPI.KeyboardInput.SpecialKeys.SPACE);
                element.xtCollapse();

                for (int i = 0; i < childrenOfCombobox.Length + 2; i++)
                {
                    Keyboard.Instance.PressSpecialKey(TestStack.White.WindowsAPI.KeyboardInput.SpecialKeys.DOWN);
                    if (listItemToSelect.xtIsItemSelected())
                    {
                        break;
                    }
                }
            }
            else if (!blankOptionExists)
            {
                Thread.Sleep(50);
                element.SetFocus();
                Thread.Sleep(50);

                element.SetFocus();

                for (int i = 0; i < (childrenOfCombobox.Length + 2); i++)
                {
                    Keyboard.Instance.PressSpecialKey(TestStack.White.WindowsAPI.KeyboardInput.SpecialKeys.UP);
                }
                for (int i = 0; i < (childrenOfCombobox.Length + 2); i++)
                {
                    Thread.Sleep(50);
                    if (listItemToSelect.xtIsItemSelected())
                    {
                        break;
                    }
                    Keyboard.Instance.PressSpecialKey(TestStack.White.WindowsAPI.KeyboardInput.SpecialKeys.DOWN);
                }
            }


czCheckpoint:

            if (!listItemToSelect.xtIsItemSelected())
            {
                Console.WriteLine($"ComboBox list item was not properly selected!!! Element Name: [{element.CurrentName}] - Element ID: [{element.CurrentAutomationId}] - List Item Selection: [{Option}] .");
                throw new Exception($"ComboBox list item was not properly selected!!! Element Name: [{element.CurrentName}] - Element ID: [{element.CurrentAutomationId}] - List Item Selection: [{Option}] .");
            }

            Keyboard.Instance.PressSpecialKey(TestStack.White.WindowsAPI.KeyboardInput.SpecialKeys.TAB);
            Thread.Sleep(100);
        }