diff --git a/src/WinAppDriver/FindElementHandler.cs b/src/WinAppDriver/FindElementHandler.cs index 1617605..a2c2764 100755 --- a/src/WinAppDriver/FindElementHandler.cs +++ b/src/WinAppDriver/FindElementHandler.cs @@ -19,16 +19,17 @@ public object Handle(Dictionary urlParams, string body, ref Sess { FindElementRequest request = JsonConvert.DeserializeObject(body); - var root = AutomationElement.RootElement; + AutomationElement start; + this.uiAutomation.TryGetFocusedWindowOrRoot(out start); if (urlParams.ContainsKey("id")) { - root = session.GetUIElement(int.Parse(urlParams["id"])); + start = session.GetUIElement(int.Parse(urlParams["id"])); } AutomationElement element = null; if (request.Strategy == "xpath") { - element = this.uiAutomation.FindFirstByXPath(root, request.Locator); + element = this.uiAutomation.FindFirstByXPath(start, request.Locator); } else { @@ -43,7 +44,7 @@ public object Handle(Dictionary urlParams, string body, ref Sess property = AutomationElement.ClassNameProperty; } - element = root.FindFirst( + element = start.FindFirst( TreeScope.Descendants, new PropertyCondition(property, request.Locator)); } diff --git a/src/WinAppDriver/FindElementsHandler.cs b/src/WinAppDriver/FindElementsHandler.cs index a0888bb..9dc8c45 100755 --- a/src/WinAppDriver/FindElementsHandler.cs +++ b/src/WinAppDriver/FindElementsHandler.cs @@ -20,16 +20,17 @@ public object Handle(Dictionary urlParams, string body, ref Sess { FindElementRequest request = JsonConvert.DeserializeObject(body); - var root = AutomationElement.RootElement; + AutomationElement start; + this.uiAutomation.TryGetFocusedWindowOrRoot(out start); if (urlParams.ContainsKey("id")) { - root = session.GetUIElement(int.Parse(urlParams["id"])); + start = session.GetUIElement(int.Parse(urlParams["id"])); } IEnumerable elements = null; if (request.Strategy == "xpath") { - elements = this.uiAutomation.FindAllByXPath(root, request.Locator); + elements = this.uiAutomation.FindAllByXPath(start, request.Locator); } else { @@ -43,7 +44,7 @@ public object Handle(Dictionary urlParams, string body, ref Sess property = AutomationElement.ClassNameProperty; } - elements = root.FindAll( + elements = start.FindAll( TreeScope.Descendants, new PropertyCondition(property, request.Locator)); } diff --git a/src/WinAppDriver/GetSourceHandler.cs b/src/WinAppDriver/GetSourceHandler.cs index 837d30f..0394236 100755 --- a/src/WinAppDriver/GetSourceHandler.cs +++ b/src/WinAppDriver/GetSourceHandler.cs @@ -15,7 +15,10 @@ public GetSourceHandler(IUIAutomation uiAutomation) public object Handle(Dictionary urlParams, string body, ref Session session) { - return this.uiAutomation.DumpXml(AutomationElement.RootElement); + AutomationElement start; + this.uiAutomation.TryGetFocusedWindowOrRoot(out start); + + return this.uiAutomation.DumpXml(start); } } } \ No newline at end of file diff --git a/src/WinAppDriver/IUIAutomation.cs b/src/WinAppDriver/IUIAutomation.cs index 0b03a8b..55b303c 100755 --- a/src/WinAppDriver/IUIAutomation.cs +++ b/src/WinAppDriver/IUIAutomation.cs @@ -5,12 +5,14 @@ namespace WinAppDriver internal interface IUIAutomation { - string DumpXml(AutomationElement root); + bool TryGetFocusedWindowOrRoot(out AutomationElement window); - string DumpXml(AutomationElement root, out IList elements); + string DumpXml(AutomationElement start); - AutomationElement FindFirstByXPath(AutomationElement root, string xpath); + string DumpXml(AutomationElement start, out IList elements); - IList FindAllByXPath(AutomationElement root, string xpath); + AutomationElement FindFirstByXPath(AutomationElement start, string xpath); + + IList FindAllByXPath(AutomationElement start, string xpath); } } \ No newline at end of file diff --git a/src/WinAppDriver/Program.cs b/src/WinAppDriver/Program.cs index 1d4422b..0e59105 100755 --- a/src/WinAppDriver/Program.cs +++ b/src/WinAppDriver/Program.cs @@ -24,7 +24,7 @@ private static RequestManager InitRequestManager(SessionManager sessionManager) var utils = new Utils(); var keyboard = new Keyboard(new KeyboardWrap(), new KeyInteropWrap(), new WinUserWrap()); var uiAutomation = new UIAutomation(); - var uacHandler = new UACPromptHandler(keyboard); + var uacHandler = new UACPromptHandler(uiAutomation, keyboard); manager.AddHandler(new ClickElementHandler()); manager.AddHandler(new DeleteSessionHandler(sessionManager)); diff --git a/src/WinAppDriver/UAC/UACPromptHandler.cs b/src/WinAppDriver/UAC/UACPromptHandler.cs index dac20b8..f808f87 100755 --- a/src/WinAppDriver/UAC/UACPromptHandler.cs +++ b/src/WinAppDriver/UAC/UACPromptHandler.cs @@ -8,6 +8,8 @@ internal class UACPromptHandler : IUACPomptHandler { private static ILogger logger = Logger.GetLogger("WinAppDriver"); + private IUIAutomation uiAutomation; + private IKeyboard keyboard; private Thread threadLazy; @@ -18,8 +20,9 @@ internal class UACPromptHandler : IUACPomptHandler private bool allowed; // true = Yes, false = No - public UACPromptHandler(IKeyboard keyboard) + public UACPromptHandler(IUIAutomation uiAutomation, IKeyboard keyboard) { + this.uiAutomation = uiAutomation; this.keyboard = keyboard; this.threadLazy = null; this.stopped = false; @@ -76,7 +79,7 @@ private bool DismissUACPrompts() logger.Info("Trying to find the (focused) UAC elevation prompt."); AutomationElement window = null; - if (this.TryGetFocusedWindow(out window)) + if (this.uiAutomation.TryGetFocusedWindowOrRoot(out window)) { var conditions = new AndCondition( Automation.ContentViewCondition, @@ -101,32 +104,6 @@ private bool DismissUACPrompts() return false; } - private bool TryGetFocusedWindow(out AutomationElement window) - { - window = null; - - var walker = TreeWalker.ContentViewWalker; - var parent = AutomationElement.FocusedElement; - while (parent != null) - { - if (parent == AutomationElement.RootElement) - { - return false; - } - else if (parent.Current.ControlType == ControlType.Window) - { - window = parent; - return true; - } - else - { - parent = walker.GetParent(parent); - } - } - - return false; - } - private Thread GetThread() { if (this.threadLazy == null) diff --git a/src/WinAppDriver/UIAutomation.cs b/src/WinAppDriver/UIAutomation.cs index f3d5848..6154cd9 100755 --- a/src/WinAppDriver/UIAutomation.cs +++ b/src/WinAppDriver/UIAutomation.cs @@ -8,21 +8,47 @@ namespace WinAppDriver internal class UIAutomation : IUIAutomation { - public string DumpXml(AutomationElement root) + public bool TryGetFocusedWindowOrRoot(out AutomationElement window) { - return this.DumpXmlImpl(root, null); + window = AutomationElement.RootElement; + + var walker = TreeWalker.ContentViewWalker; + var parent = AutomationElement.FocusedElement; + while (parent != null) + { + if (parent == AutomationElement.RootElement) + { + return false; + } + else if (parent.Current.ControlType == ControlType.Window) + { + window = parent; + return true; + } + else + { + parent = walker.GetParent(parent); + } + } + + return false; } - public string DumpXml(AutomationElement root, out IList elements) + public string DumpXml(AutomationElement start) + { + return this.DumpXmlImpl(start, null); + } + + public string DumpXml(AutomationElement start, out IList elements) { elements = new List(); - return this.DumpXmlImpl(root, elements); + return this.DumpXmlImpl(start, elements); } - public AutomationElement FindFirstByXPath(AutomationElement root, string xpath) + public AutomationElement FindFirstByXPath(AutomationElement start, string xpath) { IList nodes; - string xml = this.DumpXml(root, out nodes); + string xml = this.DumpXml(start, out nodes); var doc = new XPathDocument(new StringReader(xml)); XPathNavigator node = doc.CreateNavigator().SelectSingleNode(xpath); @@ -37,10 +63,10 @@ public AutomationElement FindFirstByXPath(AutomationElement root, string xpath) } } - public IList FindAllByXPath(AutomationElement root, string xpath) + public IList FindAllByXPath(AutomationElement start, string xpath) { IList elements; - string xml = this.DumpXml(root, out elements); + string xml = this.DumpXml(start, out elements); var doc = new XPathDocument(new StringReader(xml)); XPathNodeIterator nodes = doc.CreateNavigator().Select(xpath); @@ -66,7 +92,7 @@ private string DumpXmlImpl(AutomationElement start, IList ele writer.WriteStartDocument(); writer.WriteStartElement("WinAppDriver"); - this.WalkTree(AutomationElement.RootElement, walker, writer, elements); + this.WalkTree(start, walker, writer, elements); writer.WriteEndDocument(); return stringWriter.ToString(); @@ -74,26 +100,26 @@ private string DumpXmlImpl(AutomationElement start, IList ele private void WalkTree(AutomationElement parent, TreeWalker walker, XmlWriter writer, IList elements) { - var child = walker.GetFirstChild(parent); - while (child != null) + var info = parent.Current; + writer.WriteStartElement(info.ControlType.ProgrammaticName); + if (elements != null) { - var info = child.Current; - writer.WriteStartElement(info.ControlType.ProgrammaticName); - if (elements != null) - { - writer.WriteAttributeString("_index_", elements.Count.ToString()); - elements.Add(child); - } + writer.WriteAttributeString("_index_", elements.Count.ToString()); + elements.Add(parent); + } - writer.WriteAttributeString("id", info.AutomationId); - writer.WriteAttributeString("name", info.Name); - writer.WriteAttributeString("class", info.ClassName); + writer.WriteAttributeString("id", info.AutomationId); + writer.WriteAttributeString("name", info.Name); + writer.WriteAttributeString("class", info.ClassName); + var child = walker.GetFirstChild(parent); + while (child != null) + { this.WalkTree(child, walker, writer, elements); - writer.WriteEndElement(); - child = walker.GetNextSibling(child); } + + writer.WriteEndElement(); } } } \ No newline at end of file