使用 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. hihocoder第229周:最大连续字母个数

    题目链接 给定一个仅包含小写字母的字符串s(长度小于1e5),你可以交换任意两个字符的位置,现在允许交换k次,要求交换之后,s中最长的连续相同字符个数尽量多,求这个最长连续区间的长度. 样例 输入 1 ...

  2. 【总结】java regex 正则表达式 提取数字和去除数字,过滤数字,提取价格

    @Test public void test33() { String phoneString = "哈哈,13888889999"; // 提取数字 Pattern patter ...

  3. Mysql模糊查询 select count(*) from sys_invitation where from_id like '%1006%';

    select count(*) from sys_invitation where from_id like '%1006%'; 查询结果 select * from sys_invitation w ...

  4. linux运维常见英文报错中文翻译(菜鸟必知)

    linux常见英文报错中文翻译(菜鸟必知) 1.command not found  命令没有找到 2.No such file or directory  没有这个文件或目录 3.Permissio ...

  5. Magento模块升级修改数据库表结构

    Magento支持在模块中创建.删除.更新数据库表及字段.通过该方式,在插件开发的过程中,添加和修改字段变成了一件非常轻松的事情.只需要修改模块的版本号,同时在文件中添加一个符合命名规则的脚本就可以了 ...

  6. Fastjson是一个Java语言编写的高性能功能完善的JSON库。

    简介 Fastjson是一个Java语言编写的高性能功能完善的JSON库. 高性能 fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库,包括曾经号称最快的jackson. ...

  7. 【C语言】练习2-8

     题目来源:<The C programming language>中的习题P38  练习2-1:  编写一个函数rightrot(x,n),该函数返回将x循环右移(即从最右端移除的位将从 ...

  8. Oracle->mysql碰到的问题

    1.大小写敏感的区别(如果服务器OS是linux).    在oracle中一般情况下不区分大小写.有时候我们在使用oracle不注意大小写的问题,表名和字段名不加双引号是不区分大小写的,像这样:in ...

  9. [MSP430] 对MSP430单片机__delay_cycles精确延时的说明及改正

    在这里, 我来讨论一下关于MSP430单片机使用__delay_cycles延时的问题. IAR for MSP430编译器提供了一个编译器内联的精确延时函数(并非真正的 函数)以提供用户精确延时使用 ...

  10. ORA-01917: user or role 'PDB_DBA' does not exist

    在使用seed PDB创建新的PDB的时候,报了以下错误提示: SQL> create pluggable database pdb2 admin user admin1 identified ...