WinAppDriver, Appium, C#, Connecting To An Already Running Window on Windows 10

1.7k Views Asked by At

I'm trying to automate the use of an application with WinAppDriver and Appium on Windows 10 using C# .Net.

I'm trying to attach to an already running Window and I've been failing to find a way. I've tried many examples on the web, including from the github site for the WinAppDriver.

https://github.com/microsoft/WinAppDriver

I finally found a way to get a handle for the process, but when I try to connect it fails. It claims it's not a top level window.

The specific error is: "OpenQA.Selenium.WebDriverException: 'b2c is not a top level window handle'"

I've tried the "app" entry instead of "appTopLevelWindow", and it did not work.

Here is the code I'm running:

    private void grabWindowsProcesses2()
    {
        int number = 0;
        _txtBxOutput.Text = "";
        IntPtr myAppTopLevelWindowHandle = new IntPtr();
        foreach (Process clsProcess in Process.GetProcesses())
        {
            number++;

            _txtBxOutput.Text += number.ToString() + ") " + clsProcess.ProcessName + "\n";
            if (clsProcess.ProcessName.Contains("VisualBoyAdvance"))
            {
                myAppTopLevelWindowHandle = clsProcess.Handle;
                break;
            }
        }

        var appTopLevelWindowHandleHex = myAppTopLevelWindowHandle.ToString("x");

        AppiumOptions appCapabilities = new AppiumOptions();           
        appCapabilities.AddAdditionalCapability("platformName", "Windows");            
        appCapabilities.AddAdditionalCapability("deviceName", "WindowsPC");
        appCapabilities.AddAdditionalCapability("appTopLevelWindow", appTopLevelWindowHandleHex);

        /*
         * Error I get here is: OpenQA.Selenium.WebDriverException: 'b2c is not a top level window handle'
         */
        _visualBoyAdvance = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appCapabilities);            
    }

Does anyone have any ideas about what I'm doing wrong? I'm fairly new to automating a desktop. I don't know, maybe there is a better way.

0

There are 0 best solutions below