Winappdriver: I need help on how to change focus when a new window is opened in the WPS application

Created on 14 Oct 2019  路  5Comments  路  Source: microsoft/WinAppDriver

Hi,
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 WPS application.
the Problem with WPS applications is that you cannot use the standard, 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\DebugApplication.exe");
options.AddAdditionalCapability("deviceName", "WindowsPC");
_driver = new WindowsDriver(new Uri("http://127.0.0.1:4723"), options);

Help Wanted

Most helpful comment

Hi Guys,
I was able to figure out how to focus on new windows being open in a WPF application and wanted to post my solution 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();

All 5 comments

Can you give an example of a WPS application? I'm assuming you mean WPF?

Launching WPF should be supported, but if the app fails to launch, you could try creating a desktop session and then attaching to your WPF app session.

Hi, thanks for your comment, (sorry Yes I am talking about a WPF application), in theory, that should work, but in actuality, that doesn't work with WPF applications.

I am able to attach to a regular app session(like a Windows calculator) but to launch a WPF application with WinAppDriver, you need to use a path like: options.AddAdditionalCapability("app", @"C:\Path\DebugApplication.exe");

I am not able to attach to a WPF app session, becuase the driver cannot find the sub-root window.

I have tried to select the pop-up window by clicking on the xpath of the window, but that also seems to not work with WPF apps but does work with WinForms.

Please if you actually have experience testing WPF applications with WinAppDriver please comment:

Capture

Could you provide the snippet of your current attaching solution?

Hi Guys,
I was able to figure out how to focus on new windows being open in a WPF application and wanted to post my solution 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();

can you try this after launching application?
and adding some wait Thread.Sleep(TimeSpan.FromSeconds(10));

var allWindowHandles = session.WindowHandles; //hold focus on the active window
session.SwitchTo().Window(allWindowHandles[0]);
Assert.IsNotNull(session);

Was this page helpful?
0 / 5 - 0 ratings