使用给定的搜索参数 Wpf UI 测试无法在页面上找到元素

Posted

技术标签:

【中文标题】使用给定的搜索参数 Wpf UI 测试无法在页面上找到元素【英文标题】:An element could not be located on the page using the given search parameters Wpf UI test 【发布时间】:2020-06-09 09:26:27 【问题描述】:

我已经实现了 WPF、CLR 应用程序,现在我想对我的应用程序进行用户界面测试。我创建了一个单元测试,并且在单元测试中我使用“WindowsDriver”定义了一个会话。 当我运行单元测试时,它会启动我的应用程序,但是当它想要找到一个元素并单击它时,它找不到该元素。

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);
        

【问题讨论】:

【参考方案1】:

使用下面的代码:

[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);
            
        
    

【讨论】:

【参考方案2】:
[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);
        
    

【讨论】:

以上是关于使用给定的搜索参数 Wpf UI 测试无法在页面上找到元素的主要内容,如果未能解决你的问题,请参考以下文章

如何按名称或类型查找WPF控件?

CSV数据无法在DataGrid的WPF ui上呈现[重复]

WPF 如何创建这样的横向菜单(Modern-UI)

我无法在 WPF 的现代 UI 中启用我的列表框(vs2013 C#)

使用Teamcity进行编码的UI测试

WPF 动画/UI 功能性能和基准测试