views:

271

answers:

2

We have an old legacy application we need to automate. It uses MDI Windows.

We're using UIAutomation and I can succesfully get the appropriate AutomationElement for each MDI Child window. What I cannot do is bring that element into focus.

Here is some example code that I tried, that fails:

        var desktop = AutomationElement.RootElement;
        var dolphin = desktop.FindFirst(TreeScope.Children,
                new PropertyCondition(AutomationElement.NameProperty,
                    "Dolphin for Windows",
                    PropertyConditionFlags.IgnoreCase));
        dolphin.SetFocus();

        var workspace = dolphin.FindFirst(TreeScope.Children,
                new PropertyCondition(AutomationElement.NameProperty,
                    "Workspace",
                    PropertyConditionFlags.None));

        var childWindow = workspace.FindFirst(TreeScope.Children, new
                PropertyCondition(AutomationElement.NameProperty, "Sharp   "));
        childWindow.SetFocus();

The last line in this code fails with System.InvalidOperationException

Experimenting, I tried finding a control on the childWindow, and calling SetFocus on it. It DID correctly set the focus on the right control, but it did not bring the MDI window to the foreground.

Any ideas?

A: 

Have you tried "BringToFront" before you set focus? I can imagine that the top-level control (mdi-parent) won't allow focus on children or is unable to do so when the child (mdi-child) when it's not visible.

riffnl
I can't find BringToFront. What class is this a member of?
Scott Ferguson
more information for WPF BringToFront can be found: http://social.msdn.microsoft.com/Forums/en/wpf/thread/5a76aa4f-0521-44a5-989e-ed5068fc2b8d and http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/90cb3bf6-ca7a-4e8a-a539-957de1f939d9 BringToFront is a windows-form native method (hence the "")
riffnl
Thanks for your help, but sorry, this won't work in our situation. We need to use UIAutomation to interect with an external application.
Scott Ferguson
A: 

Hi,

In my opinion the UI under test does not support the IsKeyboardFocusable property hence you are getting this exception.

Rajneesh

Rajneesh