WinAppDriver icon indicating copy to clipboard operation
WinAppDriver copied to clipboard

Failed to locate opened application window with appId

Open Livin54 opened this issue 2 years ago • 10 comments

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 avatar Jun 21 '22 01:06 Livin54

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 avatar Jun 21 '22 05:06 Shakevg

@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.

Livin54 avatar Jun 21 '22 17:06 Livin54

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 avatar Jun 21 '22 20:06 Shakevg

@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

Livin54 avatar Jun 21 '22 22:06 Livin54

@Shakevg This fixed the issue! Thank you!

Livin54 avatar Jun 23 '22 19:06 Livin54

@Livin54 How did you fix it?

SahityaShetty avatar Feb 24 '23 16:02 SahityaShetty

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?

qubaoqa avatar Apr 21 '23 14:04 qubaoqa

The same problem. If I open my app by my own - tests works good. But it can't open exe automatically(

dolbilov avatar May 30 '23 08:05 dolbilov

@Livin54 can you help us with you fix?

petrisorionel avatar Sep 26 '23 16:09 petrisorionel

This worked for me:

options.AddAdditionalCapability("ms:waitForAppLaunch", "3");

gabrieldelaparra avatar Jan 11 '24 14:01 gabrieldelaparra