Search code examples
winappdriver

Windows Application Driver, error "Could not find any recognizable digits." when connecting to session (driver)


I know how to launch a windows application using the filepath to launch it and that works (working example below). I am writing tests and they work too but my question is this: If the application is running already, how do I create my "session" (often called "driver") for the currently running application?

I have read this article that explains how you would connect a new session to Cortana which is already running. It's a great example but my app is an exe that has been launched and is not part of windows and I'm getting the error "Could not find any recognizable digits.".

What am I doing wrong?

WORKING CODE THAT LAUNCHES THE APP AND CREATES THE "session":

private const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
protected static WindowsDriver<RemoteWebElement> session;

    public static void Setup(TestContext context)
        {
            // Launch app and populate session
            if (session == null)
            {
                // Create a new sessio
                DesiredCapabilities appCapabilities = new DesiredCapabilities();
                appCapabilities.SetCapability("app", filepath /*The exeecutable's filepath on c drive*/);

                //LaunchWPF app and wpf session                
                session = new WindowsDriver<RemoteWebElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);
                session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
        }
}

PROBLEM CODE :

   [TestMethod()]
    public void Common_CreateSession_ForAlreadyRunningmyApp()
    {
        string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";

        IntPtr myAppTopLevelWindowHandle = new IntPtr();
        foreach (Process clsProcess in Process.GetProcesses())
        {
            if (clsProcess.ProcessName.Contains("MyApp.Client.Shell"))
            {
                myAppTopLevelWindowHandle = clsProcess.Handle;
            }
        }

        DesiredCapabilities appCapabilities = new DesiredCapabilities();
        appCapabilities.SetCapability("appTopLevelWindow", myAppTopLevelWindowHandle);

        //Create session for app that's already running (THIS LINE FAILS, ERROR: : 'Could not find any recognizable digits.')               
        session = new WindowsDriver<RemoteWebElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);
        session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);

} }


Solution

  • There's now an answer on github here. You can see on github I have made 3 tweaks to the answer given by moonkey124, 2 of them were obvious (my aplication name and a little sleep command), 1 of them was to adapt the answer to a WPF application under test...