diff --git a/src/WinAppDriver/FindElementHandler.cs b/src/WinAppDriver/FindElementHandler.cs index a2c2764..fceba6b 100755 --- a/src/WinAppDriver/FindElementHandler.cs +++ b/src/WinAppDriver/FindElementHandler.cs @@ -19,8 +19,7 @@ public object Handle(Dictionary urlParams, string body, ref Sess { FindElementRequest request = JsonConvert.DeserializeObject(body); - AutomationElement start; - this.uiAutomation.TryGetFocusedWindowOrRoot(out start); + var start = this.uiAutomation.GetFocusedWindowOrRoot(); if (urlParams.ContainsKey("id")) { start = session.GetUIElement(int.Parse(urlParams["id"])); diff --git a/src/WinAppDriver/FindElementsHandler.cs b/src/WinAppDriver/FindElementsHandler.cs index 9dc8c45..3c56097 100755 --- a/src/WinAppDriver/FindElementsHandler.cs +++ b/src/WinAppDriver/FindElementsHandler.cs @@ -20,8 +20,7 @@ public object Handle(Dictionary urlParams, string body, ref Sess { FindElementRequest request = JsonConvert.DeserializeObject(body); - AutomationElement start; - this.uiAutomation.TryGetFocusedWindowOrRoot(out start); + var start = this.uiAutomation.GetFocusedWindowOrRoot(); if (urlParams.ContainsKey("id")) { start = session.GetUIElement(int.Parse(urlParams["id"])); diff --git a/src/WinAppDriver/GetSourceHandler.cs b/src/WinAppDriver/GetSourceHandler.cs index 0394236..be06963 100755 --- a/src/WinAppDriver/GetSourceHandler.cs +++ b/src/WinAppDriver/GetSourceHandler.cs @@ -15,10 +15,7 @@ public GetSourceHandler(IUIAutomation uiAutomation) public object Handle(Dictionary urlParams, string body, ref Session session) { - AutomationElement start; - this.uiAutomation.TryGetFocusedWindowOrRoot(out start); - - return this.uiAutomation.DumpXml(start); + return this.uiAutomation.DumpXml(this.uiAutomation.GetFocusedWindowOrRoot()); } } } \ No newline at end of file diff --git a/src/WinAppDriver/IUIAutomation.cs b/src/WinAppDriver/IUIAutomation.cs index 55b303c..ae5c7f9 100755 --- a/src/WinAppDriver/IUIAutomation.cs +++ b/src/WinAppDriver/IUIAutomation.cs @@ -5,7 +5,7 @@ namespace WinAppDriver internal interface IUIAutomation { - bool TryGetFocusedWindowOrRoot(out AutomationElement window); + AutomationElement GetFocusedWindowOrRoot(); string DumpXml(AutomationElement start); diff --git a/src/WinAppDriver/UAC/UACPromptHandler.cs b/src/WinAppDriver/UAC/UACPromptHandler.cs index f808f87..f334678 100755 --- a/src/WinAppDriver/UAC/UACPromptHandler.cs +++ b/src/WinAppDriver/UAC/UACPromptHandler.cs @@ -78,15 +78,14 @@ private bool DismissUACPrompts() { logger.Info("Trying to find the (focused) UAC elevation prompt."); - AutomationElement window = null; - if (this.uiAutomation.TryGetFocusedWindowOrRoot(out window)) + AutomationElement dialog = this.uiAutomation.GetFocusedWindowOrRoot(); { var conditions = new AndCondition( Automation.ContentViewCondition, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Pane), new PropertyCondition(AutomationElement.ClassNameProperty, "CtrlNotifySink")); - var elements = window.FindAll(TreeScope.Descendants, conditions); + var elements = dialog.FindAll(TreeScope.Descendants, conditions); if (elements.Count > 0) { logger.Info("The UAC elevation prompt found."); diff --git a/src/WinAppDriver/UIAutomation.cs b/src/WinAppDriver/UIAutomation.cs index 6154cd9..8f70ca1 100755 --- a/src/WinAppDriver/UIAutomation.cs +++ b/src/WinAppDriver/UIAutomation.cs @@ -8,30 +8,19 @@ namespace WinAppDriver internal class UIAutomation : IUIAutomation { - public bool TryGetFocusedWindowOrRoot(out AutomationElement window) + public AutomationElement GetFocusedWindowOrRoot() { - window = AutomationElement.RootElement; - var walker = TreeWalker.ContentViewWalker; - var parent = AutomationElement.FocusedElement; - while (parent != null) + var node = AutomationElement.FocusedElement; + var parent = node; + + while (parent != AutomationElement.RootElement) { - if (parent == AutomationElement.RootElement) - { - return false; - } - else if (parent.Current.ControlType == ControlType.Window) - { - window = parent; - return true; - } - else - { - parent = walker.GetParent(parent); - } + node = parent; + parent = walker.GetParent(node); } - return false; + return node; } public string DumpXml(AutomationElement start)