使用 UI自动化测试的好处就是在代码逻辑中写好 case 后,来实现 “一劳永逸” 的作用,并且自动化测试能够模拟人工达不到要求,比如快速切换页面、快速点击按钮等,对于提高软件的稳定性很有帮助。

安装的软件:

1、WinAppDriver

   github : https://github.com/microsoft/winappdriver

  上面有些 demo。Application Driver直接安装地址:https://github.com/Microsoft/WinAppDriver/releases

  安装完成后,默认在系统的:C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe

  这个工具的作用是你写的测试工程,通过本地 localhost(127.0.0.1)以 json 方式与 WinAppDriver 进行通信,使 WinAppDriver调用 win32 api来模拟屏幕操作,如果鼠标点击、拖拽、触屏手势等,后续据说会支持 xbox 手柄、hololens 等操作。

2、 inspect.exe

 这个工具是用来查看运行软件(uwp、win32、win form、wpf)的 UI 元素的 Name、ID、Text 等等。包含在 Windows SDK 中。

 安装完 Visual Studio2015后,可以在 C盘下找到:C:\Program Files (x86)\Windows Kits\10\bin\x64\inspect.exe

 元素对照表:

Client API Locator Strategy Matched Attribute Example
FindElementByAccessibilityId accessibility id AutomationId AppNameTitle
FindElementByClassName class name ClassName TextBlock
FindElementById id RuntimeId (decimal) 42.333896.3.1
FindElementByName name Name Calculator
FindElementByTagName tag name LocalizedControlType (upper camel case) Text

这里以爱奇艺uwp作为示例,大概描述一下使用方式:

1、首先在启动 WinAppDriver.exe,运行界面:

  路径:C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe

如果不运行它的话,UI自动化工程在运行单元测试的时候,vs 会抛异常:

/* 先启动 C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe,否则会抛异常
Unexpected error. System.Net.WebException: 无法连接到远程服务器 ---> System.Net.Sockets.SocketException: 由于目标计算机积极拒绝,无法连接。 127.0.0.1:4723
在 System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
在 System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- 内部异常堆栈跟踪的结尾 ---
在 OpenQA.Selenium.Appium.Service.AppiumCommandExecutor.Execute(Command commandToExecute)
在 OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
*/

  

2、使用 inspect.exe 工具,确定软件运行时,需要触发事件 UI元素的 ID/Name 

  路径:C:\Program Files (x86)\Windows Kits\10\bin\x64\inspect.exe

  例如,在 xaml 中,添加两个 Button:

第一个设置附加属性:AutomationProperties.AutomationId="button_1"

第二个设置: x:Name=“button_2”

 <StackPanel Width="500" Height="500" Background="Blue" Grid.RowSpan="3">
<StackPanel.Resources>
<Style TargetType="Button">
<Setter Property="Margin" Value="10"/>
</Style>
</StackPanel.Resources>
<Button AutomationProperties.AutomationId="button_1">AutomationId="button_1"</Button>
<Button x:Name="button_2">x:Name="button_2"</Button>
</StackPanel>

用 inspect 观测:

可以看出来,在 inspect 中 Name显示为 Button.Content 属性,如果是 TextBlock控件,Name则显示 TextBlock.Text 属性。

3、创建测试工程

1)创建普通的 C# 类库工程即可:

2)通过 nuget 引用相关类库:

3)获取 uwp 的启动 id,一个的方法右键点击 app 的快捷方式,选择属性:

3)工程中添加两个类 QyClientTestBase 和 PlayerPageScenarios,实现启动 爱奇艺uwp,从首页随机点击一个视频,跳转到播放页,播放3秒后,拖拽进度条到中间位置,然后再随机播放选集视频,在播放页递归播放。

public class QyClientTestBase
{
// Note: append /wd/hub to the URL if you're directing the test at Appium
protected const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723"; protected static WindowsElement Hanburg; protected static WindowsElement BackButton;
protected static WindowsDriver<WindowsElement> QyClientSession;
protected static string OriginalCalculatorMode; public static void BaseSetup(TestContext context)
{
if (QyClientSession == null)
{
// 启动爱奇艺 app
DesiredCapabilities appCapabilities = new DesiredCapabilities();
appCapabilities.SetCapability("app", "0C72C7CD.Beta_atj5cpqqdhzyp!App");
appCapabilities.SetCapability("deviceName", "WindowsPC");
QyClientSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);
Assert.IsNotNull(QyClientSession);
QyClientSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds()); Hanburg = QyClientSession.FindElementByName("Menu"); // 已经在播放页时
//BackButton = QyClientSession.FindElementByAccessibilityId("Back");
} // 在首页时,汉堡按钮显示;在播放页时,Back按钮显示
Assert.IsNotNull(Hanburg);
} public static void BaseTearDown()
{
if (QyClientSession != null)
{
Hanburg = null;
QyClientSession.Dispose();
QyClientSession = null;
}
}
}
 [TestClass]
public class PlayerPageScenarios : QyClientTestBase // 子类型需要设置为 public,否则“测试资源管理器”中看不到
{
[ClassInitialize] //每个单元测试方法执行前,调用的初始化方法
public static void Setup(TestContext context)
{
BaseSetup(context);
} [ClassCleanup] //每个单元测试方法执行完成后,调用的清理方法
public static void TearDown()
{
BaseTearDown();
} bool isPlaying = false; [TestMethod]
public void SliderClick()
{ if (isPlaying == false)
{
// 先从 app首页,随机播放一个视频,跳转到播放页
PlayBRandom(); Thread.Sleep(TimeSpan.FromSeconds());
isPlaying = true;
} ShowControls(); //元素需要在页面中显示,如 Visibility="Collapsed" 则获取不到
WindowsElement slider = QyClientSession.FindElementByAccessibilityId("slider_position"); //进度条 Assert.IsNotNull(slider); //视频进度条拖动到中间位置
QyClientSession.Mouse.MouseMove(slider.Coordinates, slider.Size.Width / , ); // seek进度
slider.Click();
Thread.Sleep(TimeSpan.FromSeconds()); //随机播放页面中的一个视频
PlayBRandom(); Thread.Sleep(TimeSpan.FromSeconds()); // 递归调用,在当前页面切换选集播放
SliderClick();
} /// <summary>
/// 播放当前页面中的任意一个 B 对象
/// </summary>
void PlayBRandom()
{
var list = QyClientSession.FindElementsByName("QyClient._3._0.Model._B"); if (list.Count > )
{
int min = isPlaying ? : ; //在app首页时,播放 30以后的 B对象 int index = new Random().Next(min, list.Count - ); WindowsElement e = list.ElementAt(index);
e.Click();
}
Assert.AreNotEqual(list.Count, ); Thread.Sleep();
} /// <summary>
/// 显示播控栏,否则播控按钮获取不到
/// </summary>
void ShowControls()
{
WindowsElement win = QyClientSession.FindElementByAccessibilityId("mediaPlayer");
win.Click();
Thread.Sleep();
}
}

4)在“测试资源管理器” 中可以运行和断点调试测试:

5)运行测试,循环切换选集,播放视频

示例工程代码

相关参考:

UI Testing: What's new with WinAppDriver:https://channel9.msdn.com/Events/Build/2017/P4084

Improving App Quality with UI Automation:https://channel9.msdn.com/Events/Connect/2016/202?ocid=player

Automate Windows And Mac Apps With The WebDriver Protocol - Dan Cuellar, FOODIt:https://www.youtube.com/watch?v=MgBRvQOZhec

StarDriver Enterprise Appium to the Future | Jonathan Lipps:https://www.youtube.com/watch?v=e61OhZzbsEI

github WinAppDriver: https://github.com/microsoft/winappdriver

Modern Dev Practices: Unit Testing:https://channel9.msdn.com/Shows/Visual-Studio-Toolbox/Modern-Dev-Practices-Unit-Testing

Inspect.exe: https://msdn.microsoft.com/en-us/library/windows/desktop/dd318521(v=vs.85).aspx

Windows SDK and emulator archive : https://developer.microsoft.com/en-us/windows/downloads/sdk-archive

Windows Automation API: UI Automation:https://msdn.microsoft.com/zh-cn/library/ms726294(vs.85).aspx

AutomationID :https://docs.microsoft.com/en-us/dotnet/framework/ui-automation/use-the-automationid-property

UI Automation Overview: 
https://docs.microsoft.com/en-us/dotnet/framework/ui-automation/ui-automation-overview
https://msdn.microsoft.com/zh-cn/library/ms728097(v=vs.85).aspx

21、uwp UI自动化测试(WinAppDriver)的更多相关文章

  1. 对Windows桌面应用程序进行UI自动化测试

    题记:本文简述如何利用appium对Windows桌面应用程序进行UI自动化测试. 所谓UI自动化测试,就是模拟一个用户,对应用程序的UI进行操作,以完成特定场景的功能性集成测试. 要对Windows ...

  2. UI自动化测试框架(项目实战)python、Selenium(日志、邮件、pageobject)

    其实百度UI自动化测试框架,会出来很多相关的信息,不过就没有找到纯项目的,无法拿来使用的:所以我最近就写了一个简单,不过可以拿来在真正项目中可以使用的测试框架. 项目的地址:https://githu ...

  3. 关于Selenium remote模式分布式执行UI自动化测试必定面临的性能问题

    1.大部分自动化测试人员都是在本地执行UI自动化测试,也就是代码和浏览器必须在同一台机器上,这样的的缺陷很多,无法多任务并发执行UI自动化测试用例,效率极低 2.正是如此,Selenium 的remo ...

  4. 网易与Google合作,于GDC开幕首日发布开源UI自动化测试方案

    [TechWeb报道]美西时间3月19日,在GDC开幕第一天的Google开发者专场,Google发布了一款由网易研发的UI自动化测试方案:Airtest Project. Google方面评价,这可 ...

  5. UI自动化测试框架:关键字驱动+数据驱动

    1. 关键字驱动框架简介 2. 工程结构说明 3. 工程代码实现 action 包  page_action.py business_process 包 case_process.py data_so ...

  6. 腾讯优测优分享 | 游戏的UI自动化测试可以这样开展

    腾讯优测是专业的自动化测试平台,提供自动化测试-全面兼容性测试,云真机-远程真机租用,漏洞分析等多维度的测试服务,让测试更简单! 对于目前的两大游戏引擎cocos-2dx.unity3D,其UI自动化 ...

  7. 使用WatiN进行UI自动化测试

    Watin是一个UI自动化测试工具,支持ie/firefox,官方网站:http://watin.org/. 主要有以下特点: 支持主要的html元素,见:http://watin.org/docum ...

  8. 如何正确选择UI自动化测试

    近年流行一个词-UI,和UI搭边好像都那么高大上,软件测试行业也不例外,比如UI自动化测试. 常见的UI自动化测试程序有哪些呢? l  带UI的Unit Test,比如mock掉底层代码,仅仅测试UI ...

  9. 【转】Web UI自动化测试原理

    目前市面上有很多Web UI自动化测试框架,比如WatiN, Selinimu,WebDriver,还有VS2010中的Coded UI等等.  这些框架都可以操作Web中的控件,模拟用户输入,点击等 ...

随机推荐

  1. PHP哈希表碰撞攻击

    哈希表是一种查找效率极高的数据结构,PHP中的哈希表是一种极为重要的数据结构,不但用于表示数组,关联数组,对象属性,函数表,符号表,还在Zend虚拟机内部用于存储上下文环境信息(执行上下文的变量及函数 ...

  2. ubuntu 安装 oracle-xe-universal

    安装oracle-xe-universal第一个我们要考虑的就是交换分区是否足够大, 如果你直接安装,可能会出现下面的英文提示: This system does not meet the minim ...

  3. Indy Changed from Indy10

    Indy Changed from Indy10 http://stackoverflow.com/questions/16339656/delphi-xe4-indy-compatibility-i ...

  4. [Spring学习笔记 1 ] Spring 简介,初步知识--Ioc容器详解 基本原理。

    一.Spring Ioc容器详解(1) 20131105 1.一切都是Bean Bean可是一个字符串或者是数字,一般是一些业务组件. 粒度一般比较粗. 2.Bean的名称 xml配置文件中,id属性 ...

  5. POJ 3683 Priest John's Busiest Day (2-SAT)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6900   Accept ...

  6. 《安卓考试》通用版ver-2,截图和源码

    首先放出源码:https://github.com/chenyoca/AndroidTopicBankEx 然后是实际应用运行效果截图: 最后说明一下: 1.应用没有完成,应用的数据是模拟的. 2.应 ...

  7. mvc 模型验证及正则表达式

    ASP.NET MVC3中的Model是自验证的,这是通过.NET4的System.ComponentModel.DataAnnotations命名空间完成的. 我们要做的只是给Model类的各属性加 ...

  8. 【Eclipse】Eclipse如何如何集成Tomcat服务器

    需要的环境 下载和配置JDK 读者可参见JDK的安装与配置 下载和配置Tomcat 读者可参见Tomcat的下载和配置 下载Eclipse 读者可参见Eclipse官方网站 Eclipse 4.4.0 ...

  9. macOS SIP 权限设置

    1.macOS SIP 权限设置 对于 macOS 10.11+ 用户,由于系统启用了 SIP(System Integrity Protection), 导致 root 用户也没有权限修改 /usr ...

  10. 简单获取cpu使用率,以及后台运行的问题

    做了一个运维平台,有一个功能定时执行一个脚本,获取cpu使用率和内存使用情况到监控平台. 获取cpu使用率使用的是top中的信息.直接运行没有问题.通过nohup xxx.sh & 之后获取不 ...