Windows Driver doesn't connect to Application 50% of time

Hi,

I’ve started using the WinAppDriver to start testing the windows desktop application CCleaner using the WinAppDriver.

Approximately 50% of the time I get an issue during the launch of the application, and I can’t figure out what the problem is. Can anyone see where I’m going wrong? It seems random, sometimes Appium restart fixes it, sometimes not, but I’d like to resolve this so I can start using these tests in a CI process.

Here’s the code in question:

public static class Application
{
    private const string AppDriverUrl = "http://127.0.0.1:4723/wd/hub";
    private const string CCleanerAppId = @"C:\Program Files\CCleaner\CCleaner64.exe";
    
    private static WindowsDriver<WindowsElement> CCleanerSession;

    private static void initialiseAppiumDriver()
    {   
        DesiredCapabilities appCapabilities = new DesiredCapabilities();
        appCapabilities.SetCapability("app", CCleanerAppId);
        appCapabilities.SetCapability("platformName", "Windows");
        appCapabilities.SetCapability("deviceName", "WindowsPC");
                    
        CCleanerSession = new WindowsDriver<WindowsElement>(new Uri(AppDriverUrl), appCapabilities);

When the Appium session launches, CCleaner starts up, but it feels like either Appium ‘loses’ its connection to the app, or my CCleanerSession doesn’t get created properly. In the code when debuggin the CCleanerSession above is null.

Error from Debug:

An unknown server-side error occurred while processing the command. Original error: [object Object]

at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary2 parameters) at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities) at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities) at OpenQA.Selenium.Appium.AppiumDriver1…ctor(Uri remoteAddress, ICapabilities desiredCapabilities)
at OpenQA.Selenium.Appium.Windows.WindowsDriver`1…ctor(Uri remoteAddress, DesiredCapabilities desiredCapabilities)
at Framework.Application.initialiseAppiumDriver() in C:\Dev\Repos\CC_UI\CCleaner_Framework\Application.cs:line 37
at Framework.Application.Open() in C:\Dev\Repos\CC_UI\CCleaner_Framework\Application.cs:line 27
at CCleanerTests.Specs.TestCase1.Setup() in C:\Dev\Repos\CC_UI\CCleaner.Specs\TestCase1.cs:line 13

In the Appium console I see this:

[HTTP] <-- POST /wd/hub/session 500 5348 ms - 152
[HTTP] --> POST /wd/hub/session {“desiredCapabilities”:{“app”:“C:\Program Files\CCleaner\CCleaner64.exe”,“platformName”:“Windows”,“deviceName”:“WindowsPC”}}
[debug] [MJSONWP] Calling AppiumDriver.createSession() with args: [{“app”:“C:\Program Files\CCleaner\CCleaner64.exe”,“platformName”:“Windows”,“deviceName”:“WindowsPC”},null,null,null,null,null,null,null,null,null,null,null,null]
[Appium] Creating new WindowsDriver session
[Appium] Capabilities:
[Appium] app: ‘C:\Program Files\CCleaner\CCleaner64.exe’
[Appium] platformName: ‘Windows’
[Appium] deviceName: ‘WindowsPC’
[BaseDriver] The following capabilities were provided, but are not recognized by appium: app.
[BaseDriver] Session created with session id: 4ac8329e-8949-41e7-80c9-74998a5f5551
[WinAppDriver] Verifying WinAppDriver is installed with correct checksum
[debug] [WinAppDriver] WinAppDriver changed state to ‘starting’
[WinAppDriver] Killing any old WinAppDrivers, running: FOR /F “usebackq tokens=5” %a in (netstat -nao ^| findstr /R /C:"4823 ") do (FOR /F “usebackq” %b in (TASKLIST /FI "PID eq %a" ^| findstr /I winappdriver.exe) do (IF NOT %b=="" TASKKILL /F /PID %a))
[WinAppDriver] No old WinAppDrivers seemed to exist
[WinAppDriver] Spawning winappdriver with: undefined 4823/wd/hub
[WinAppDriver] [STDOUT] Windows Application Driver Beta listening for requests at: http://127.0.0.1:4823/wd/hub
[debug] [WinAppDriver] WinAppDriver changed state to ‘online’
[debug] [JSONWP Proxy] Proxying [POST /session] to [POST http://127.0.0.1:4823/wd/hub/session] with body: {“desiredCapabilities”:{“app”:“C:\Program Files\CCleaner\CCleaner64.exe”,“platformName”:“Windows”,“deviceName”:“WindowsPC”}}
[WinAppDriver] [STDOUT]
[WinAppDriver] [STDOUT] Address ‘http://127.0.0.1:4823/wd/hub’ is already in use
[WinAppDriver] [STDOUT]
[WinAppDriver] [STDOUT] Press ENTER to exit.
[WinAppDriver] [STDOUT]
[WinAppDriver] [STDOUT]
[WinAppDriver] [STDOUT] ==========================================
[WinAppDriver] [STDOUT] POST /wd/hub/session HTTP/1.1
[WinAppDriver] [STDOUT] Accept: /
[WinAppDriver] [STDOUT] Connection: keep-alive
[WinAppDriver] [STDOUT] Content-Length: 127
[WinAppDriver] [STDOUT] Content-Type: application/json
[WinAppDriver] [STDOUT] Host: 127.0.0.1:4823
[WinAppDriver] [STDOUT] User-Agent: appium
[WinAppDriver] [STDOUT]
[WinAppDriver] [STDOUT] {“desiredCapabilities”:{“app”:“C:\Program Files\CCleaner\CCleaner64.exe”,“platformName”:“Windows”,“deviceName”:“WindowsPC”}}
[WinAppDriver] [STDOUT] SessionManager - Creating session
[WinAppDriver] [STDOUT] SessionManager - WinAppDriver succeeded loading MitaBroker
[WinAppDriver] [STDOUT] HTTP/1.1 500 Internal Error
[WinAppDriver] [STDOUT] Content-Length: 133
[WinAppDriver] [STDOUT] Content-Type: application/json
[WinAppDriver] [STDOUT]
[WinAppDriver] [STDOUT] {“status”:13,“value”:{“error”:“unknown error”,“message”:“An unknown error occurred in the remote end while processing the command.”}}
[debug] [WinAppDriver] Deleting WinAppDriver session
[MJSONWP] Encountered internal error running command: {“jsonwp”:{“status”:13,“value”:{“error”:“unknown error”,“message”:“An unknown error occurred in the remote end while processing the command.”}}} ProxyRequestError: Could not proxy command to remote server. Original error: 500 - {“status”:13,“value”:{“error”:“unknown error”,“message”:“An unknown error occurred in the remote end while processing the command.”}}
at JWProxy.proxy$ (…/…/…/lib/jsonwp-proxy/proxy.js:126:13)
at tryCatch (C:\Users\phil.losty\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (C:\Users\phil.losty\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (C:\Users\phil.losty\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:100:21)
at GeneratorFunctionPrototype.invoke (C:\Users\phil.losty\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37)
[HTTP] <-- POST /wd/hub/session 500 5238 ms - 152

Many thanks, sorry if its something obvious I’ve missed!

Phil

Phil,

I am having the exact same issue using WinApp. Were you able to solve this issue?

Best,

Matty

Hi @caulfield2

We kind of worked around it - our application launched 2 processes, the app and a ‘monitor’ process that you can switch off; so we just switched off the setting in the program that launched the monitor. That was why it failed ~50% of the time.

This workaround resolved the issue for us to run our tests, so we never found a way to identify the exact process we need and switch it in Appium.

Before the winappdriver was released I tried using testStack.White (http://teststackwhite.readthedocs.io/en/latest/) where I got around the problem by going through each window, checking the name, then selecting that one, but I didn’t find a way to replicate that in WinAppDriver/Appium.

Hope that helps a bit?

Thanks,

Phil

Thanks for the reply @plosty,

That definitely helps me a bit. I’m thinking that my issues may have something to do with launching multiple processes as well. When your app wouldn’t launch, did it throw an InvalidOperationException? I get that error most times when I try and launch in [ClassInitialize] and strangely enough my app will still start just fine. I just use a try-catch to handle the exception. Additionally, I too get the null Session that throws a NullReferenceException when I try and execute my test method. Was this solved when you turned off that second process?

Thanks again,

Matty

Hi @caulfield2,

Sorry it was a few months ago now; when the process connected to the ‘correct’ process then the test ran correctly. We initially caught it and tried relaunching it, but that retest would also have 50% chance of failing. I think it was some strange null pointer exception being thrown as the monitor process had no windows to connect to, and we never found a way to detect if we’d connected to the right version, and if not how to change the appium process to connect to another running process.