示例#1
0
 private static void UpdateStatus(UIATestObject testObject, ElementFindingStatusEnum findStatus)
 {
     if (StatusUpdate != null)
     {
         StatusUpdate(testObject, findStatus);
     }
 }
示例#2
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);
        }
示例#3
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);
        }
示例#4
0
        public static string DumpTestObject(ITestObject testObject)
        {
            UIATestObject to = (UIATestObject)testObject;

            ObjectDescriptor od = to.GetDescriptor();

            ObjectDescriptor descriptor = (ObjectDescriptor)od.Clone();

            string result = JsonUtil.SerializeObject(descriptor, true, true);

            return(result);
        }
示例#5
0
        static ILogger _Logger = LogFactory.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);//"LPReplayCore.UIA.UIAFinder");

        public static UIATestObject CreateTestObject(ObjectDescriptor descriptor, ModelLoadLevel loadLevel)
        {
            UIATestObject uia = new UIATestObject(descriptor);

            //if (loadLevel == null)
            //{
            //TODO: load additional details into context objects
            //1.Load cached properties
            //2.Load recorded images
            //}

            return(uia);
        }
示例#6
0
        private static List <AutomationElement> FilterWithLocators(UIATestObject testObject, List <AutomationElement> elements)
        {
            List <AutomationElement> matchedElements = elements;

            foreach (KeyValuePair <string, LocatorFunc> pair in locators)
            {
                string      locatorName = pair.Key;
                LocatorFunc locator     = pair.Value;

                matchedElements = locator(testObject, elements);

                //TODO refine the logging
                _Logger.WriteDebug(locatorName + " returns count: " + matchedElements.Count.ToString());
            }

            return(matchedElements);
        }
        public void Click()
        {
            Assert.IsTrue(this.Parent != null);
            UIATestObject parentObject = (UIATestObject)this.Parent;

            Rect?tempRect = UIAHighlight.HighlightVirtualControl(parentObject.AutomationElement, this.BoundingRect);

            if (tempRect == null)
            {
                return;
            }

            Rect rect = (Rect)tempRect;

            ClickOnPointTool.ClickOnPoint((IntPtr)null, new System.Windows.Point(rect.Left + 10, rect.Top + 10));
            //InvokePattern invokePattern = (InvokePattern)parentObject.AutomationElement.GetCurrentPattern(InvokePattern.Pattern);
        }
示例#8
0
        //get the automation object from testObject
        public static AutomationElement Find(UIATestObject testObject)
        {
            UpdateStatus(testObject, ElementFindingStatusEnum.Searching);

            List <AutomationElement> matchedElements = FindAll(testObject);

            if (matchedElements.Count >= 1)
            {
                UpdateStatus(testObject, ElementFindingStatusEnum.Found);
                return(matchedElements[0]);
            }
            else
            {
                UpdateStatus(testObject, ElementFindingStatusEnum.NotFound);
            }

            return(null);
        }
示例#9
0
        private static List <AutomationElement> TopWindowTitleLocator(UIATestObject testObject, List <AutomationElement> elements)
        {
            _Logger.WriteDebug("TopWindowTitleLocator enter");
            if (testObject.ControlType != ControlType.Window)
            {
                return(elements);
            }

            string title = testObject.HasProperty(UIAControlKeys.Title) ? testObject.Properties[UIAControlKeys.Title] : null;

            if (string.IsNullOrEmpty(title))
            {
                return(elements);
            }

            _Logger.WriteDebug("title is " + title);

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

            int index      = testObject.Index;
            int matchIndex = 0;

            foreach (AutomationElement element in elements)
            {
                string caption = element.Current.Name;
                if (Utility.StringSimiliarityCompare(title, caption, 50))
                {
                    if (index < 0 || index == matchIndex)
                    {
                        _Logger.WriteDebug("matched caption is " + caption);
                        matchedElements.Add(element);
                    }
                    else
                    {
                        matchIndex++;
                    }
                }
            }
            return(matchedElements);
        }
示例#10
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);
        }
示例#11
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();
             * }
             * */
        }
        public void HighlightControl()
        {
            UIATestObject parentObject = (UIATestObject)this.Parent;

            UIAHighlight.HighlightVirtualControl(parentObject.AutomationElement, this.BoundingRect);
        }
 private System.Drawing.Point GetPointToClick(UIATestObject parentObject, Rect rect)
 {
     throw new NotImplementedException();
 }