如何实现UI自动化?DevExpress Winforms帮你忙
DevExpress Winforms Controls 内置140多个UI控件和库,完美构建流畅、美观且易于使用的应用程序。无论是Office风格的界面,还是分析处理大批量的业务数据,DevExpress WinForms都能轻松胜任。DevExpress广泛应用于ECM企业内容管理、 成本管控、进程监督、生产调度,在企业/政务信息化管理中占据一席重要之地。
【适用范围】:各种桌面、Web应用程序开发,尤其是WinForms应用程序开发。
在针对Visual Studio 2019的发行说明中,Microsoft 宣布Coded UI测试的生命周期终止。
Microsoft建议将Appium with WinAppDriver 一起用于测试桌面和UWP应用,此消息引起广大用户的兴趣:DevExpress控件是否与Appium兼容?经过DevExpress团队的反复测试,答案是肯定的!使用Appium创建自动UI测试的方法如下。
1. 跳转到 https://github.com/Microsoft/WinAppDriver/releases然后下载两个APP,
- WinAppDriver - 允许您运行测试,需要安装。
- WinAppDriver UI Recorder - 允许您在运行时记录测试,不需要安装 - 将下载的存档解压到任何文件夹。
2. 在Windows中打开Developer Mode。
3. 以管理员身份运行WinAppDriver.exe并使其运行,请注意应用程序正在侦听的地址,稍后您将需要它。

4. 打开您要测试的Visual Studio解决方案,或创建一个新的示例解决方案。
5. 将新的单元测试项目添加到解决方案。

6. 在Solution Explorer中右键单击Unit Test project,然后选择“Manage NuGet Packages…”,安装最新的稳定Appium.WebDriver程序包。
7. 打开Unit Test项目的UnitTest1.cs文件,并添加两个类:MainDemoSession(定义开始和结束测试会话的方法)和Helper(包含查找被测试的UI元素的方法),将步骤3中的地址用作WindowsApplicationDriverUrl值。
- public class MainDemoSession{protected const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";private const string ApplicationPath = @"C:\Users\...\AppiumTest.exe";
- protected static WindowsDriver<WindowsElement> desktopSession;
- public static void Setup(TestContext context)
- {
- // Launch a new instance of the tested application
- if (desktopSession == null)
- {
- // Create a new session to launch the tested application
- AppiumOptions options = new AppiumOptions();
- options.AddAdditionalCapability("app", ApplicationPath);
- desktopSession = new WindowsDriver<WindowsElement>(
- new Uri(WindowsApplicationDriverUrl), options);
- Assert.IsNotNull(desktopSession);
- Assert.IsNotNull(desktopSession.SessionId);
- // Set implicit timeout to 1.5 seconds
- //to make element search to retry every 500 ms
- //for at most three times
- desktopSession.Manage().Timeouts().ImplicitWait =
- TimeSpan.FromSeconds(1.5);
- }
- }
- public static void TearDown()
- {
- // Close the application and delete the session
- if (desktopSession != null)
- {
- desktopSession.Close();
- desktopSession.Quit();
- desktopSession = null;
- }
- }
- }
- public static class Helper
- {
- public static WindowsElement FindElementByAbsoluteXPath(
- this WindowsDriver<WindowsElement> desktopSession,
- string xPath,
- int nTryCount = 3)
- {
- WindowsElement uiTarget = null;
- while (nTryCount-- > 0) {
- try {
- uiTarget = desktopSession.FindElementByXPath(xPath);
- }
- catch {
- }
- if (uiTarget != null) {
- break;
- }
- else {
- System.Threading.Thread.Sleep(400);
- }
- }
- return uiTarget;
- }
- }
8. 修改自动生成的UnitTest1类,如下所示:
- [TestClass]public class UnitTest1 : MainDemoSession{[TestMethod]public void TestMethod1(){//test start
- //test finish
- }
- [ClassInitialize]
- public static void ClassInitialize(TestContext context)
- {
- Setup(context);
- }
- [ClassCleanup]
- public static void ClassCleanup()
- {
- TearDown();
- }
- }
9. 运行您的应用程序,并将其拖到主系统显示屏上(如果您具有多屏幕设置)。
10. 启动WinAppDriver UI Recorder然后点击“Record”, 将鼠标悬停在要与之交互的第一个UI元素上,然后等待它开始闪烁蓝色。Recorder的状态栏会将其文本从“Active”更改为“XPath Ready”。

11. 当该元素闪烁时,recorder已准备就绪,您可以执行UI操作:单击此元素、将其拖动、输入新值等。完成此元素后,将鼠标悬停在另一个UI元素上,等待 recorder的确认并重复该过程。
12. 记录了要重现的一系列步骤后,请在recorder中单击“Pause”,您可以打开actions selector确保已记录所有UI操作。

13. 单击“Generate and copy C# code to Clipboard”按钮来复制所有记录的操作代码,将此代码粘贴到UnitTest1.TestMethod1方法中。 例如,下面的代码选择“Job”标签。

- [TestMethod]
- public void TestMethod1()
- {
- //test start
- // LeftClick on TabItem "Job" at (20,31)
- Console.WriteLine("LeftClick on TabItem \"Job\" at (20,31)");
- string xpath_LeftClickTabItemJob_20_31 = "/Pane\[@ClassName=\"#32769\"\][@Name=\"Desktop 1\"]/Window\[starts-with(@AutomationId,\"XtraForm\")]/Pane[@Name=\"The XtraLayoutControl\"\][starts-with(@AutomationId,\"dataLayoutControl\")]/Table[@Name=\"Root\"]/Table[@Name=\"autoGeneratedGroup0\"]/Table[@Name=\"Root\"]/Table[@Name=\"Photo\"]/Table[@Name=\"FirstAndLastName\"]/Tab[@Name=\"Tabs\"]/TabItem[@Name=\"Job\"]";
- var winElem_LeftClickTabItemJob_20_31 = desktopSession.FindElementByAbsoluteXPath(xpath_LeftClickTabItemJob_20_31);
- if (winElem_LeftClickTabItemJob_20_31 != null)
- {
- winElem_LeftClickTabItemJob_20_31.Click();
- }
- else
- {
- Console.WriteLine($"Failed to find element using xpath: {xpath_LeftClickTabItemJob_20_31}");
- return;
- }
- //test finish
- }
14. 在内部测试期间,自动生成的代码可能无法通过其完整路径找到UI元素:
/Pane\[@ClassName=\"#32769\"\][@Name=\"Desktop 1\"]/Window[starts-with…
如果发生这种情况,请缩短所有元素路径,使其以“ / Window”开头。
string xpath_LeftClickTabItemJob_20_31 = "/Window[starts-with(@AutomationId...";
此外,您可以使用Assert.Fail而不是Console.WriteLine来调试测试(如果找不到UI元素,则可以)。
Assert.Fail($"Failed to find element...");
15. 在Visual Studio中右键单击Unit Test project,然后单击“Run Tests”。测试将启动您的应用程序,重复所有记录的步骤,然后关闭应用程序。 所有测试操作都记录在步骤3中启动的WinAppDriver控制台中。

您可以通过与Coded UI相同的方式启动Appium测试,唯一的区别是您需要在测试执行计算机上运行WinAppDriver。
DevExpress v19.2全新发布,最新动态请持续关注DevExpress中文网!
DevExpress中文网官网QQ群:540330292 欢迎一起进群讨论
如何实现UI自动化?DevExpress Winforms帮你忙的更多相关文章
- 甘特图控件如何自定义绘图?DevExpress Winforms帮你忙
DevExpress Winforms Controls 内置140多个UI控件和库,完美构建流畅.美观且易于使用的应用程序.无论是Office风格的界面,还是分析处理大批量的业务数据,DevExpr ...
- .NET Core 3时代如何转换.NET 4项目,DevExpress Winforms帮你忙!
DevExpress Winforms Controls 内置140多个UI控件和库,完美构建流畅.美观且易于使用的应用程序.无论是Office风格的界面,还是分析处理大批量的业务数据,DevExpr ...
- 2020即将到来!DevExpress Winforms开发有哪些新功能值得期待?
下载DevExpress v19.2完整版 DevExpress Winforms Controls 内置140多个UI控件和库,完美构建流畅.美观且易于使用的应用程序.DevExpress Winf ...
- DevExpress WinForms使用教程:皮肤颜色和LookAndFeel
[DevExpress WinForms v18.2下载] v18.2版本中更改了控制背景颜色和皮肤一起处理的方式.在v18.1中引入了Project Settings页面,其中包含一个skin se ...
- 手机APP自动化之uiautomator2 +python3 UI自动化
题记: 之前一直用APPium直到用安卓9.0 发现uiautomatorviewer不支持安卓 9.0,点击截屏按钮 一直报错,百度很久解决方法都不可以,偶然间看见有人推荐:uiautomator ...
- DevExpress WinForms使用教程:SVG图库和Image Picker
[DevExpress WinForms v18.2下载] 每个新版本都在几个新控件中引入了矢量图标支持. 对于v18.2,这是列表: BackstageViewControl及其项目 RecentI ...
- DevExpress WinForms v18.2新版亮点(八)
买 DevExpress Universal Subscription 免费赠 万元汉化资源包1套! 限量15套!先到先得,送完即止!立即抢购>> 行业领先的.NET界面控件2018年第 ...
- DevExpress WinForms v18.2新版亮点(六)
行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WinForms v1 ...
- DevExpress WinForms v18.2新版亮点(五)
行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WinForms v1 ...
随机推荐
- SpringBoot+Vue前后端分离,使用SpringSecurity完美处理权限问题
原文链接:https://segmentfault.com/a/1190000012879279 当前后端分离时,权限问题的处理也和我们传统的处理方式有一点差异.笔者前几天刚好在负责一个项目的权限管理 ...
- chapter01图像基本操作
刚刚开始学习opencv,来记录一下自己的学习笔记,也向各位大牛虚心求教 一.图片的基本知识 只要是彩色的图片都有三个颜色通道,即RGB,用三个矩阵来表示. 三个矩阵的同一个坐标位置上的数值描述的是一 ...
- LeNet-5 pytorch+torchvision+visdom
# ====================LeNet-5_main.py=============== # pytorch+torchvision+visdom # -*- coding: utf- ...
- MongoDB 聚合函数及排序
聚合函数 最大值 $max db.mycol.aggregate([{$group : {_id : "$by_user", num_max : {$max: "$li ...
- QMetaMethod 获取成员函数的元信息
在上一篇中,我们将的是QMetaEnum类,它可以获得一个类中由Q_ENUM宏或Q_FLAG宏声明的枚举类型的元信息.同样,QMetaMethod类是用来获取成员方法的元信息的一个类.通过该类,我们可 ...
- hdu 1875 最小生成树 prime版
最小生成树prime版 大致的步骤 首先选取一个到集合最近的点 然后标记起在集合内部 然后更新最短距离 畅通工程再续 Time Limit: 2000/1000 MS (Java/Others) ...
- ES 6 日期util.js =>
微信小程序demo const formatTime = date => { const year = date.getFullYear() const month = date.getMont ...
- C++编译 C # 调用方法
C++编译 C # 调用方法 编译时使用 public ref class ABC { ... }; 调用时 右键---引用 --- 添加dll引用 即可
- ligerui.grid.extend.rowSpan
扩展LigerUI的Grid中的相同列合并行功能,代码如下:$.extend($.ligerui.controls.Grid.prototype, { _getHtmlFromData:functio ...
- java jdk1.8 API
里面有 中英文 jdk 1.8 API 还有 jdk1.6 和1.7 英文 API 链接:https://pan.baidu.com/s/1tchABVX7htJCaO3quENP1g提取码:y ...