public static AutomationElement Normalize(AutomationElement element) { if (element.Current.ControlType == ControlType.Window) return element; TreeWalker walker = new TreeWalker(ConditionBuilder.ToCondition(a => a.Current.ControlType == ControlType.Window)); return walker.Normalize(element); }
public void NormalizeTest () { Condition groupCondition = new PropertyCondition (AEIds.ControlTypeProperty, ControlType.Group); TreeWalker groupWalker = new TreeWalker (groupCondition); AssertRaises<ArgumentNullException> ( () => groupWalker.Normalize (null), "passing null to TreeWalker.Normalize"); Assert.AreEqual (groupBox1Element, groupWalker.Normalize (groupBox1Element), "If element matches, return it"); Assert.AreEqual (groupBox1Element, groupWalker.Normalize (button2Element), "When element does not match, return first matching ancestor"); // This is according to MSDN: // http://msdn.microsoft.com/en-us/library/system.windows.automation.treewalker.normalize.aspx //Assert.AreEqual (AutomationElement.RootElement, // groupWalker.Normalize (button1Element), // "When neither elment nor ancestors match, return RootElement"); // This is how Microsoft actually implemented it: Assert.IsNull (groupWalker.Normalize (button1Element), "When neither element nor ancestors match, return null"); Condition noNameCondition = new PropertyCondition (AEIds.NameProperty, string.Empty); TreeWalker noNameWalker = new TreeWalker (noNameCondition); Assert.AreEqual (AutomationElement.RootElement, noNameWalker.Normalize (button1Element), "When RootElement matches, it should be returned"); Assert.AreEqual (AutomationElement.RootElement, TreeWalker.RawViewWalker.Normalize (AutomationElement.RootElement), "When RootElement matches, it should be returned"); Assert.AreEqual (null, new TreeWalker (Condition.FalseCondition).Normalize (AutomationElement.RootElement), "When RootElement does not match, null should be returned"); }