microsoft / WinAppDriver

Windows Application Driver
MIT License
3.69k stars 1.4k forks source link

Unable to run WinAppDriver with https://github.com/microsoft/WinUI-3-Demos #1261

Open llongley opened 4 years ago

llongley commented 4 years ago

(Ported from this issue.)

WinAppDriver appears to be unable to find the window when it launches a WinUI 3 desktop application. I can't know for sure since the code isn't open source, but the problem appears to be on the WinAppDriver side - we're internally able to find and manipulate a WinUI 3 desktop application window using Microsoft.Windows.Apps.Test (which I believe is what WinAppDriver is using), and I'm also able to interact with a pre-opened WinUI 3 desktop application using the method under Attaching to an Existing App Window. I suspect the problem may stem from the fact that the window class name is different for desktop applications - for UWP apps, the window class name is Windows.UI.Core.CoreWindow, whereas for desktop apps, it's WinUIDesktopWin32WindowClass. Pointing our test code at the latter class name made us able to retrieve the window of the app we launched.

anunay1 commented 4 years ago

Is inspect able to find the controls.

PureWeen commented 3 years ago

From what I can tell you can inspect the controls

Here's the code I'm using to workaround this for the time being. This will let you start the app via code and then use the workaround to find the window

        public static void StartupApplication()
        {
            AppiumOptions options = new AppiumOptions();
            options.AddAdditionalCapability("app", "0d4424f6-1e29-4476-ac00-ba22c3789cb6_ph1m9x8skttmg!App");

            try
            {
                Session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), options);
            }
            catch {
                // This crashes because it can't find the window but it will at least start the application
            }
        }

        public static WindowsElement GetWindowsElement()
        {
            AppiumOptions options = new AppiumOptions();
            options.AddAdditionalCapability("app", "Root");
            Session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), options);

            new Actions(Session)
                    .SendKeys(Keys.Meta + "s" + Keys.Meta)
                    .Perform();

            try
            {
                return Session.FindElementByName("WinUI Desktop");
            }
            catch { }

            StartupApplication();
            return GetWindowsElement();
        }

        public static WindowsDriver<WindowsElement> CreateWindowsDriver()
        {
            var topLevelWindowHandle = GetWindowsElement().GetAttribute("NativeWindowHandle");
            topLevelWindowHandle = (int.Parse(topLevelWindowHandle)).ToString("x"); // Convert to Hex

            AppiumOptions options = new AppiumOptions();
            options.AddAdditionalCapability("appTopLevelWindow", topLevelWindowHandle);
            options.AddAdditionalCapability("appArguments", "RunningAsUITests");

            Session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), options);

            Assert.IsNotNull(Session);
            Session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1);
            Reset();

            return Session;
        }