wpfclruitest

An element could not be located on the page using the given search parameters Wpf UI test


I have implemented a WPF, CLR application and now I want to have a user interface test for my application. I have created an unit test and in the unit test I have defined a Session by using of "WindowsDriver". When I run the unit test, it lunches my application but when it wants to find an element and click on it, it can not find the element.

private const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
        private const string AlarmClockAppId = @"D:\misSoloution\x64\Debug\XNAV.exe";

        protected static WindowsDriver<WindowsElement> session;
        protected static RemoteTouchScreen touchScreen;

public static void Setup(TestContext context)
        {
            // Launch Alarms & Clock application if it is not yet launched
            if (session == null || touchScreen == null)
            {
                TearDown();

                // Create a new session to bring up the Alarms & Clock application
                var appiumOptions = new AppiumOptions();
                appiumOptions.AddAdditionalCapability("app", AlarmClockAppId);
                appiumOptions.AddAdditionalCapability("deviceName", "WindowsPC");
                appiumOptions.AddAdditionalCapability("appWorkingDir", @"D:\misSoloution\CreateMISRelease_local\");
                session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appiumOptions);

                //DesiredCapabilities appCapabilities = new DesiredCapabilities();
                //appCapabilities.SetCapability("app", AlarmClockAppId);
                //appCapabilities.SetCapability("appWorkingDir", @"D:\misSoloution\CreateMISRelease_local\");
                //appCapabilities.SetCapability("deviceName", "Windows 10");
                //session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);
                Assert.IsNotNull(session);
                Assert.IsNotNull(session.SessionId);

                // Set implicit timeout to 1.5 seconds to make element search to retry every 500 ms for at most three times
                session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1.5);

                // Initialize touch screen object
                touchScreen = new RemoteTouchScreen(session);
                Assert.IsNotNull(touchScreen);
            }
        }

public virtual void TestInit()
        {
            WindowsElement alarmButtonElement = null;

            // Attempt to go back to the main page in case Alarms & Clock app is started in EditAlarm view
            try
            {
                alarmButtonElement = session.FindElementByAccessibilityId("ExitButton");
            }
            catch
            {
                // Click cancel button if application is in a nested page such as New Alarm or New Timer
                session.FindElementByAccessibilityId("ExitButton").Click();
                Thread.Sleep(TimeSpan.FromSeconds(1));
                alarmButtonElement = session.FindElementByAccessibilityId("ExitButton");
            }

            // Verify that the app is in the main view showing alarmButtonElement
            Assert.IsNotNull(alarmButtonElement);
            Assert.IsTrue(alarmButtonElement.Displayed);
        }

l

enter image description here


Solution

  • use below code:

    [TestClass]
        public class WpfTest
        {
           protected const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
           private const string WpfAppId = @"WpfUiTesting_e627vcndsd2rc!App";
    
           protected static WindowsDriver<WindowsElement> session;
           protected static WindowsDriver<WindowsElement> DesktopSession;
    
            public static void Setup(TestContext context)
            {
                if (session == null)
                {
                    var appiumOptions = new AppiumOptions();
                    appiumOptions.AddAdditionalCapability("app", WpfAppId);
                    appiumOptions.AddAdditionalCapability("deviceName", "WindowsPC");
                    DesktopSession = null;
                    try
                    {
                        Console.WriteLine("Trying to Launch App");
                        DesktopSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appiumOptions);
                    }
                    catch
                    {
                        Console.WriteLine("Failed to attach to app session (expected).");
                    }
    
                    appiumOptions.AddAdditionalCapability("app", "Root");
                    DesktopSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appiumOptions);
                    var mainWindow = DesktopSession.FindElementByAccessibilityId("WpfUITestingMainWindow");
                    var mainWindowHandle = mainWindow.GetAttribute("NativeWindowHandle");
                    mainWindowHandle = (int.Parse(mainWindowHandle)).ToString("x"); // Convert to Hex
                    appiumOptions = new AppiumOptions();
                    appiumOptions.AddAdditionalCapability("appTopLevelWindow", mainWindowHandle);
                    session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appiumOptions);
                }
            }
        }