3

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"}}
user2091773
  • 41
  • 1
  • 6
  • You probably need to create a [desktop session](https://github.com/microsoft/WinAppDriver/wiki/Frequently-Asked-Questions#when-and-how-to-create-a-desktop-session). – PixelPlex Oct 16 '19 at 07:25

3 Answers3

1

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();
user2091773
  • 41
  • 1
  • 6
  • Apart from the code it would help if you would quickly explain what exactly was the issue and how you solved it so other people don't need to go through both code sections and try to spot the differences – huserben Oct 17 '19 at 17:37
  • @huserben, see [this](https://github.com/microsoft/WinAppDriver/wiki/Frequently-Asked-Questions#what-to-do-when-an-application-has-a-splash-screen-or-winappdriver-fails-to-recognize-the-correct-window) section on the winappdriver github wiki. That's the problem the OP had. Winappdriver keeps separate windows in a list of window handles. When the focus changes to another window, you need to switch to the correct window handle manually. – PixelPlex Oct 21 '19 at 13:09
1

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));
deHaar
  • 17,687
  • 10
  • 38
  • 51
shatulsky
  • 306
  • 2
  • 10
0

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();
 }
Dan
  • 1,805
  • 2
  • 18
  • 21