WinAppDriver
WinAppDriver copied to clipboard
Failed to locate opened application window with appId
I am using C# and WinAppDriver to automate UI tests on a desktop application, however, I continue to have an intermittent issue when launching the WPF application. I see there are similar issue on here but I have not found a solution.
When opening the application there is a login screen. When the correct login credentials are entered there is splash screen for approximately 5 seconds before the application opens. I typically see the issue right before the login screen opens.
I continue to get the following exception when opening the application; it appears to be random. Every time I think I found a pattern I find another data point that conflict with the pattern I thought I identified.
Exception
Message:
OpenQA.Selenium.WebDriverException : Failed to locate opened application window with appId: C:\Program Files (x86)\<path to .exe>\, and processId: 14124
Stack Trace:
RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
AppiumDriver`1.Execute(String driverCommandToExecute, Dictionary`2 parameters)
RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
RemoteWebDriver.ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
AppiumDriver`1.ctor(Uri remoteAddress, ICapabilities appiumOptions)
WindowsDriver`1.ctor(Uri remoteAddress, AppiumOptions AppiumOptions)
Application Launch Code
if (session == null){
AppiumOptions appOptions = new AppiumOptions();
appOptions.AddAdditionalCapability("app", Path);
appOptions.AddAdditionalCapability("platformName", "Windows");
appOptions.AddAdditionalCapability("deviceName", "WindowsPC");
Thread.Sleep(TimeSpan.FromSeconds(5));
session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appOptions);
Assert.IsNotNull(session);
Thread.Sleep(TimeSpan.FromSeconds(10));
var allWindowHandles = session.WindowHandles;
session.SwitchTo().Window(allWindowHandles[0]);
session.Manage().Timeouts().ImplicitWait = TimeSpan.FromMinutes(1);
//Enter password and login
session.FindElementByAccessibilityId("tbPwd").SendKeys("password");
session.FindElementByAccessibilityId("btnLogin").Click();
Thread.Sleep(TimeSpan.FromSeconds(5));
session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appOptions);
session.Manage().Timeouts().ImplicitWait = TimeSpan.FromMinutes(1);
}
Tear down code after each test case
public void TearDown()
{
AppiumOptions appOptions = new AppiumOptions();
appOptions.AddAdditionalCapability("app", "Root");
session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appOptions);
// Use the session to control the desktop
session.Keyboard.PressKey(Keys.Command + "r");
session.Keyboard.SendKeys(Keys.Null);
session.Keyboard.SendKeys(@"C:\path of taskkill file.bat"+ Keys.Enter);
//End current session instance
session.Quit();
}
Livin54 Sometimes session may not create from first time try to use try cache and attach to running app if created session was failed from first time https://github.com/microsoft/WinAppDriver/blob/master/Samples/C%23/StickyNotesTest/StickyNotesSession.cs
Could you highlight code on what step you test was failed
@Shakevg It fails as soon as the login screen appears when opening the application.
I believe this line of code is what opens the application, is that correct? I am new at UI automation and still learning.
session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appOptions);
In the try block that is in the sample code you sent, is the 'StickNotesAppId' the path to the .exe?
appCapabilities.SetCapability("app", StickyNotesAppId);
I appreciate your help.
In the try block that is in the sample code you sent, is the 'StickNotesAppId' the path to the .exe?
appCapabilities.SetCapability("app", StickyNotesAppId);
It is a windows app Id but you can use the path to the .exe
@Shakevg I implemented the sample code and so far it is working as expected. I am going to run a lot of tests tomorrow to see if it occurs again.
Thanks you
@Shakevg This fixed the issue! Thank you!
@Livin54 How did you fix it?
In the try block that is in the sample code you sent, is the 'StickNotesAppId' the path to the .exe?
appCapabilities.SetCapability("app", StickyNotesAppId);
It is a windows app Id but you can use the path to the .exe
Hi, I tried the same on my app but still get error Failed to locate opened application window with appId: C:\Program Files (x86)\...
do I need notice anything?
The same problem. If I open my app by my own - tests works good. But it can't open exe automatically(
@Livin54 can you help us with you fix?
This worked for me:
options.AddAdditionalCapability("ms:waitForAppLaunch", "3");