Пример #1
0
        public static List <AutomationElement> FindAll(UIATestObject testObject)
        {
            _Logger.WriteLog(LogTypeEnum.Debug, () => "FindAll:\r\n" + UIAUtility.DumpTestObject(testObject));

            List <AutomationElement> matchedElements =
                ConditionLocator(testObject);

            if (matchedElements.Count == 0)
            {
                _Logger.WriteWarning("Can not find child in the parent container:" + testObject.ControlName);
                return(matchedElements);
            }

            /*
             * // if window, just check the caption, 60% match will be recognized.
             * call the following lines in sequence
             * matchedElements = TopWindowLocator(testObject, matchedElements);
             * matchedElements = LeftRightLocator(testObject, matchedElements);
             * matchedElements = IndexLocator(testObject, matchedElements);
             */
            matchedElements = FilterWithLocators(testObject, matchedElements);

            if (matchedElements.Count == 1)
            {
                return(matchedElements);                            //ignore other optional parameters
            }
            matchedElements = FindInMatchedCollection(testObject, matchedElements);

            _Logger.WriteDebug("FindAll returns count:" + matchedElements.Count);

            return(matchedElements);
        }
Пример #2
0
        public static UIATestObject CreateTestObject(AutomationElement element, string nodeName = null)
        {
            UIATestObject testObject = new UIATestObject();

            testObject.AutomationElement = element;
            testObject.ControlType       = element.Current.ControlType;

            string automationId = element.Current.AutomationId;

            if (!string.IsNullOrEmpty(automationId))
            {
                testObject.AddProperty(UIAControlKeys.AutomationId, automationId);
            }

            string className = element.Current.ClassName;

            if (!string.IsNullOrEmpty(className))
            {
                testObject.AddProperty(UIAControlKeys.ClassName, className);
            }

            //"" is different from null.
            if (element.Current.Name != null)
            {
                if (testObject.ControlType == ControlType.Window)
                {
                    testObject.Properties[UIAControlKeys.Title] = element.Current.Name;
                }
                else
                {
                    testObject.ControlName = element.Current.Name;
                }
            }

            string name = UIAUtility.GetName(element);

            if (!String.IsNullOrEmpty(name))
            {
                if (testObject.ControlType == ControlType.Hyperlink)
                {
                    testObject.Properties[UIAControlKeys.AttachedText] = name;
                }
                else
                {
                    testObject.Properties[ControlKeys.Text] = name;
                }
            }

            if (!String.IsNullOrEmpty(name = element.Current.HelpText))
            {
                testObject.Properties[UIAControlKeys.HelpText] = name;
            }

            if (!string.IsNullOrEmpty(nodeName))
            {
                testObject.NodeName = nodeName;
            }
            return(testObject);
        }
Пример #3
0
        public static List <AutomationElement> LeftRightLocator(ITestObject testObject, List <AutomationElement> matchedElements)
        {
            if (testObject.Relation == null)
            {
                return(matchedElements);
            }

            IRelation relation = testObject.Relation;

            if (relation.Left == null && relation.Right == null)
            {
                return(matchedElements);
            }

            AutomationElement leftElementNext = null, rightElementPrevious = null;

            //check if the left or right condtion exist
            //get the left and right element
            if (relation.Right != null)
            {
                ITestObject rightTO = relation.Right;

                AutomationElement rightElement = Find((UIATestObject)rightTO);
                if (rightElement == null)
                {
                    //TODO, log error
                    goto errorCase;
                }
                rightElementPrevious = UIAUtility.GetPreviousElement(rightElement);

                if (rightElementPrevious == null)
                {
                    //TODO, log error
                    goto errorCase;
                }
            }

            if (relation.Left != null)
            {
                ITestObject leftTO = relation.Left;

                AutomationElement leftElement = Find((UIATestObject)leftTO);

                if (leftElement == null)
                {
                    //TODO, log error
                    goto errorCase;
                }

                leftElementNext = UIAUtility.GetNextElement(leftElement);
                if (leftElementNext == null)
                {
                    //TODO, log error
                    goto errorCase;
                }
            }

            AutomationElement elementFound = null;

            foreach (AutomationElement element in matchedElements)
            {
                bool leftCriteria = leftElementNext == null ||
                                    (leftElementNext != null && UIAUtility.AreEqual2(leftElementNext, element));

                bool rightCriteria = rightElementPrevious == null ||
                                     (rightElementPrevious != null && UIAUtility.AreEqual2(rightElementPrevious, element));

                if (leftCriteria && rightCriteria)
                {
                    elementFound = element;
                    break;
                }
            }

            if (elementFound != null)
            {
                matchedElements.Clear();
                matchedElements.Add(elementFound);
            }
            else
            {
                Debug.Assert(false, "Reach here means either the left/right are not null, but cannot found element");
            }

            return(matchedElements);

errorCase:
            matchedElements.Clear();

            return(matchedElements);
        }
Пример #4
0
        public static List <AutomationElement> ConditionLocator(UIATestObject testObject)
        {
            _Logger.WriteDebug("ConditionLocator start");
            Condition condition = testObject.GetCondition();

            if (condition == null)
            {
                condition = TreeWalker.ControlViewWalker.Condition;
            }

            List <AutomationElement> elementList = new List <AutomationElement>();

            UIATestObject parentTestObject = (UIATestObject)testObject.Parent;

            if (parentTestObject == null)
            {
                //root object
                AutomationElement windowElement = AutomationElement.RootElement.FindFirst(TreeScope.Children,
                                                                                          condition);
                if (windowElement != null)
                {
                    elementList.Add(windowElement);
                }

                /*
                 * AutomationElementCollection windowElements = AutomationElement.RootElement.FindAll(TreeScope.Children,
                 * condition);
                 * if (windowElements != null)
                 * {
                 *  foreach (AutomationElement element in windowElements)
                 *  {
                 *      elementList.Add(element);
                 *  }
                 * }
                 * else
                 * {
                 *  _Logger.WriteLog(LogTypeEnum.Debug, () => "Cannot locate object:\r\n" + UIAUtility.DumpTestObject(testObject));
                 * }*/

                goto ConditionLocatorEnd;
            }

            AutomationElementCollection matchedElements = null;

            AutomationElement parentElement = parentTestObject.AutomationElement;

            if (parentElement == null)
            {
                //TODO, refine log
                _Logger.WriteWarning("Cannot identify the parent object");
                goto ConditionLocatorEnd;
            }

            matchedElements = UIAUtility.FindDescendantElements(parentTestObject.AutomationElement, condition);

            elementList.Capacity = matchedElements.Count;
            foreach (AutomationElement element in matchedElements)
            {
                elementList.Add(element);
            }

ConditionLocatorEnd:
            _Logger.WriteDebug("ConditionLocator end");
            _Logger.WriteDebug("ConditionLocator returns count:" + elementList.Count);
            return(elementList);
        }
Пример #5
0
        public static bool ExpandMenuItem(AutomationElement element)
        {
            if (ControlType.MenuItem != element.Cached.ControlType)
            {
                return(false);
            }
            bool   isKeyboardFocusable                  = false;
            object expandCollapsePatternObject          = null;
            ExpandCollapsePattern expandCollapsePattern = null;

            try
            {
                isKeyboardFocusable = (bool)element.GetCurrentPropertyValue(AutomationElement.IsKeyboardFocusableProperty, true);
            }
            // Object doesn't support the ExpandCollapsePattern control pattern.
            catch (Exception e)
            {
                Console.WriteLine("{1} {0} element is not focusable - the element may be not visible", element.Current.Name, e.Message);
            }

            try
            {
                if (element.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out expandCollapsePatternObject))
                {
                    expandCollapsePattern = expandCollapsePatternObject as ExpandCollapsePattern;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("{1} {0} element doesn't have ExpandCollapse pattern", element.Current.Name, e.Message);
                return(false);
            }


            if (null != expandCollapsePattern && ExpandCollapseState.LeafNode != expandCollapsePattern.Current.ExpandCollapseState)
            {
                try
                {
                    if (expandCollapsePattern.Current.ExpandCollapseState == ExpandCollapseState.Collapsed ||
                        expandCollapsePattern.Current.ExpandCollapseState == ExpandCollapseState.PartiallyExpanded)
                    {
                        if (isKeyboardFocusable)
                        {
                            element.SetFocus();
                        }
                        //    expandCollapsePattern.Expand();
                        if (element.Current.FrameworkId.Equals("WPF"))
                        {
                            expandCollapsePattern.Expand();
                        }
                        else
                        {
                            AutomationElement aeParent = UIAUtility.GetParentElement(element);
                            if (aeParent.Current.ControlType != ControlType.MenuBar)
                            {
                                expandCollapsePattern.Expand();
                            }
                        }
                        Thread.Sleep(500);
                    }
                    return(true);
                }
                // Object doesn't support the ExpandCollapsePattern control pattern.
                catch (InvalidOperationException)
                {
                    Console.WriteLine("Expand menu item InvalidOperationException {0}", element.Current.Name);
                }
            }
            else
            {
                Console.WriteLine("{0} element is leaf node", element.Current.Name);
            }
            return(false);
        }
Пример #6
0
        public static void InitProperties(string nodeName, AutomationElement element, UIATestObject testObject)
        {
            testObject.AutomationElement = element;
            testObject.ControlType       = element.Current.ControlType;
            testObject.NodeName          = nodeName;

            string automationId = element.Current.AutomationId;

            if (!string.IsNullOrEmpty(automationId))
            {
                testObject.AddProperty(UIAControlKeys.AutomationId, automationId);
            }

            string className = element.Current.ClassName;

            if (!string.IsNullOrEmpty(className))
            {
                testObject.AddProperty(UIAControlKeys.ClassName, className);
            }

            //"" is different from null.
            if (element.Current.Name != null)
            {
                if (testObject.ControlType == ControlType.Window)
                {
                    testObject.Properties[UIAControlKeys.Title] = element.Current.Name;
                }
                else
                {
                    testObject.ControlName = element.Current.Name;
                }
            }

            string name = UIAUtility.GetName(element);

            if (!String.IsNullOrEmpty(name))
            {
                if (testObject.ControlType == ControlType.Hyperlink)
                {
                    testObject.Properties[UIAControlKeys.AttachedText] = name;
                }
                else
                {
                    testObject.Properties[ControlKeys.Text] = name;
                }
            }

            if (!String.IsNullOrEmpty(name = element.Current.HelpText))
            {
                testObject.Properties[UIAControlKeys.HelpText] = name;
            }

            //TODO determine the right logic to create index, currently it takes too long

            /*
             *
             * UIATestObject parentTestObject = (UIATestObject) testObject.Parent;
             *
             * AutomationElement parentElement;
             *
             * if (parentTestObject == null)
             * {
             *  parentElement = AutomationElement.RootElement;
             * }
             * else
             * {
             *  parentElement = parentTestObject.AutomationElement;
             * }
             *
             * AutomationElementCollection matchedElement;
             * if (testObject.ControlType == ControlType.Custom)
             * {
             *  matchedElement = parentElement.FindAll(TreeScope.Descendants, TreeWalker.ControlViewWalker.Condition);
             * }
             * else
             * {
             *  Condition c = new PropertyCondition(AutomationElement.ControlTypeProperty, element.Current.ControlType);
             *  matchedElement = parentElement.FindAll(TreeScope.Descendants, c);
             * }
             *
             * int objectIndex = -1;
             * for (int i = 0; i < matchedElement.Count; i++)
             * {
             *  if (UIAUtility.AreEqual(matchedElement[i], element))
             *  {
             *      objectIndex = i;
             *      break;
             *  }
             * }
             * if (objectIndex != -1)
             * {
             *  testObject.Properties[ControlKeys.Index] = objectIndex.ToString();
             * }
             * */
        }
Пример #7
0
 public UIATestObject CreateTestObject(ObjectDescriptor descriptor, ModelLoadLevel loadLevel = ModelLoadLevel.ReplayOnly)
 {
     return(UIAUtility.CreateTestObject(descriptor, loadLevel));
 }