Using WinAppDriver, I need help on how to change focus when a new window is opened in the WPF application

Viewed 7510

I need help on how to change focus when a new window is opened in the WPF application with WinAppDriver

With WinAppDriver I am able to open a WPF application and test using the code below, I need help on how to change focus when a new window is opened in the WPF application. the Problem with WPF applications is that you cannot use the standard method, as the driver will not recognize the app. there should be a way to help switch focus or at least open an existing WPS application window which I cannot do right now. if anybody could help, I would greatly appreciate it.

// to open the standard app
AppiumOptions options2 = new AppiumOptions();
options2.AddAdditionalCapability("app", "Application new window");
options2.AddAdditionalCapability("Window", "WindowsPC");

//to open a WPS application
AppiumOptions options = new AppiumOptions();
options.AddAdditionalCapability("app", @"C:\Path\Debug\Application.exe");
options.AddAdditionalCapability("deviceName", "WindowsPC");
_driver = new WindowsDriver(new Uri("http://127.0.0.1:4723"), options);

{"desiredCapabilities":{"app":"[@Name =\"Loan Folder 14847\"]","Window":"WindowsPC","platformName":"Windows"},"capabilities":{"firstMatch":[{"platformName":"Windows"}]}}
HTTP/1.1 500 Internal Error
Content-Length: 101
Content-Type: application/json

{"status":13,"value":{"error":"unknown error","message":"The system cannot find the file specified"}}
3 Answers

I was able to figure out how to focus on new windows being open in a WPF application and wanted to post my solution to help someone Below:

//Setup
private WindowsDriver _driver;

AppiumOptions options = new AppiumOptions();
options.AddAdditionalCapability("app", @"C:\Desktop\Debug\application.exe");
options.AddAdditionalCapability("deviceName", "WindowsPC");

_driver = new WindowsDriver(new Uri("http://127.0.0.1:4723"), options);
_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);

//Test
_driver.FindElementByAccessibilityId("Login").Click();
Thread.Sleep(5000);
_driver.FindElementByName("button").Click();
//Opens a new window
_driver.FindElementByName("Open").Click();
Thread.Sleep(2000);
//Solution - switches to the latest window
_driver.SwitchTo().Window(_driver.WindowHandles.First());
//can click on buttons in the new Window
_driver.FindElementByName("buttons").Click();
//Opens another new Window
_driver.FindElementByName("Open another window").Click();
Thread.Sleep(2000);
//Solution - switches to the latest window
_driver.SwitchTo().Window(_driver.WindowHandles.First());
//can click on buttons in the new Window
_driver.FindElementByName("button3").Click();

You can use WinAPI.

[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(IntPtr hWnd); 

hWnd is NativeWindowHandle attribute of Window.

string windowHandle = windowElement.GetAttribute("NativeWindowHandle");
return new IntPtr(long.Parse(windowHandle));

An alternative to using WindowHandles is to find the window element through the WindowsElement API. You can then use the element API to find children of the window.

The example below opens a new window and then closes it by finding the Close control on the new window.

[Test]
public void TestAboutWindow()
{
    // Open About window through menu
    _session.FindElementByName("Help").Click();
    _session.FindElementByName("About").Click();

    // Close child window through WindowElement API
    var aboutWindow = _session.FindElementByName("AboutWindow");
    aboutWindow.FindElementByName("Close").Click();
 }
Related