Skip to content

Commit

Permalink
Top-level windows are not always of type Window.
Browse files Browse the repository at this point in the history
  • Loading branch information
imsardine committed May 5, 2015
1 parent ed04c56 commit 32180ba
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 31 deletions.
3 changes: 1 addition & 2 deletions src/WinAppDriver/FindElementHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public object Handle(Dictionary<string, string> urlParams, string body, ref Sess
{
FindElementRequest request = JsonConvert.DeserializeObject<FindElementRequest>(body);

AutomationElement start;
this.uiAutomation.TryGetFocusedWindowOrRoot(out start);
var start = this.uiAutomation.GetFocusedWindowOrRoot();
if (urlParams.ContainsKey("id"))
{
start = session.GetUIElement(int.Parse(urlParams["id"]));
Expand Down
3 changes: 1 addition & 2 deletions src/WinAppDriver/FindElementsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public object Handle(Dictionary<string, string> urlParams, string body, ref Sess
{
FindElementRequest request = JsonConvert.DeserializeObject<FindElementRequest>(body);

AutomationElement start;
this.uiAutomation.TryGetFocusedWindowOrRoot(out start);
var start = this.uiAutomation.GetFocusedWindowOrRoot();
if (urlParams.ContainsKey("id"))
{
start = session.GetUIElement(int.Parse(urlParams["id"]));
Expand Down
5 changes: 1 addition & 4 deletions src/WinAppDriver/GetSourceHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ public GetSourceHandler(IUIAutomation uiAutomation)

public object Handle(Dictionary<string, string> 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());
}
}
}
2 changes: 1 addition & 1 deletion src/WinAppDriver/IUIAutomation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace WinAppDriver

internal interface IUIAutomation
{
bool TryGetFocusedWindowOrRoot(out AutomationElement window);
AutomationElement GetFocusedWindowOrRoot();

string DumpXml(AutomationElement start);

Expand Down
5 changes: 2 additions & 3 deletions src/WinAppDriver/UAC/UACPromptHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
27 changes: 8 additions & 19 deletions src/WinAppDriver/UIAutomation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 32180ba

Please sign in to comment.