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); }
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(); * } * */ }