iOS模拟器发送通知和UI测试
iOS模拟器发送通知和UI测试
背景
我们可能通过点击通知直接跳转到页面指定页面,或者点击通知打开web页面,更或者通过其他应用启动 app打开指定页面。面对这种跳转指定页面我们应该如何做 UI测试了?
向模拟器发送通知
从 Xcode11.4中,用户可以想 iOS 模拟器推送通知,包含以下两种方式:
- 终端使用 Xcode 命令执行,发送通知。
- 将 apns 文件拖放到目标模拟器。
apns 文件
首先我们需要定义 apns 发送文件,并命名ExamplePush.apns
。
定义格式如下:
{"Simulator Target Bundle": "xxxx(包名)","aps": {"badge": 0,"alert": {"title": "打开百度","subtitle": "url加载测试","body": "点击跳转App web 容器并且打开百度搜索",},"sound":"default",},"route": {"url":"https://www.baidu.com/"}
}
至此,你可以填写正确的应用包名,通过拖拽到模拟器来发送通知了。
Xcode 命令发送
在项目已经正确配置好通知所需要的条件后,保证待测模拟器的通知权限已经开启。
发送命令如下:
xcrun simctl push <device> com.example.my-app ExamplePush.apns
如命令所见,我们需要模拟器标识符,应用包名和 apns 文件。
同理,我们还可以通过
xcrun simctl openurl booted <Universal Link>
来发送通用链接和 scheme 跳转,请参考文章IOS Deep linking: URL Scheme vs Universal Links
- 获取模拟器设备标识符
- 应用包名
在结合之前我们定义的 apns 文件,发送通知的命令如下:
xcrun simctl push 942A6B1C-BE29-4E05-A075-5146088F7C9E com.compass.MusselExample PushNotificationTest.apns
UI 测试发送通知
可惜的是 XCodeTest 并没有提供在测试中发送通知的方式。
接下来该怎么办了?
方法1:【推荐】借助其他库实现,比如Mussel(可以在测试用例中发送通知、Scheme Link 和通用链接,最低支持 iOS11)、NWPush(配置麻烦,需要各种证书,教程示例)等库
方法2:自己写 shell 脚本和命令
使用 Mussel 发送通知
Mussel github 仓库:https://github.com/UrbanCompass/Mussel
自定义脚本发送通知
原理:在编译 UI测试 target 期间,添加运行脚本来发送通知
缺点:每次编译都会发送通知(需要手动控制参数)
编写推送通知脚本
#!/bin/bash
#推送和通用链接
#是否运行推送(以便关闭通知)1:打开 0关闭
isAllowPushNotification=1
# 获取脚本所在路径
DIR="$( cd "$( dirname "$0" )" && pwd )"
#进入目录,以便发送 apns 文件
echo ${DIR}pushNotification(){# 模拟器 iddiviceId=xxxxx# 应用包名appName=xxxxxcrun simctl push $diviceId $appName ./ExamplePush.apns
}if [ $isAllowPushNotification == 1 ]; then
pushNotification
fi
暂且命名为 pushNotification.sh 并把相应的权限修改了chmod +x pushNotification.s
(不然没有执行权限)。
为 UITest 添加运行脚本
将所需的脚本和 apns 文件都添加到测试 target 目录下后,选中UI 测试 target。 添加脚本。
这样每次编译 UI 测试 target 就会发送通知了。
打开通知页面的测试操作
注意:我这里为了方便测试,关闭了通知以组显示的方式。
参数和setUp 方法
@interface OpentNotificationUITests : XCTestCase
@property(nonatomic, strong) XCUIApplication *app; //当前应用
@property(nonatomic, strong) XCUIApplication *springboard; //系统页面
@end@implementation OpentNotificationUITests- (void)setUp {// Put setup code here. This method is called before the invocation of each test method in the class.// In UI tests it is usually best to stop immediately when a failure occurs.self.continueAfterFailure = NO;// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.self.app = [[XCUIApplication alloc] init];self.springboard = [[XCUIApplication alloc] initWithBundleIdentifier:@"com.apple.springboard"];[self.app launch];// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}
测试方法
- (void)testOpenUrlFromNotification{[self openNotificationCenterTitle:@"打开百度"];NSArray<XCUIElement *> *webViews = [[self.app webViews] allElementsBoundByIndex]; //业务中已处理(点击会通过 webViewController 去加载 url)XCTAssertTrue((webViews.count >= 1), @"应该打开 web 页面"); // 或者使用XCTAssertTrue([webViews[0] waitForExistenceWithTimeout:10], @"应该打开 web 页面");
}
辅助方法
我这里是通过通知的标题来获取对应通知,然后进行点击打开。
#pragma mark - helper
/// 相当于按 Home 键
- (void)toHome{[[XCUIDevice sharedDevice] pressButton:XCUIDeviceButtonHome];
}
/// 打开通知, 注意 需要保证模拟器通知方式[取消以组显示通知],通知 title 也需要不一致。
- (void)openNotificationCenterTitle:(NSString * _Nonnull)title {[self toHome];XCUIApplication *app = self.springboard;XCUICoordinate *coord1 = [app coordinateWithNormalizedOffset:CGVectorMake(0.1, 0.01)];XCUICoordinate *coord2 = [app coordinateWithNormalizedOffset:CGVectorMake(0.1, 0.8)];[coord1 pressForDuration:0.1 thenDragToCoordinate:coord2]; // 滑动显示通知XCUIElement *mainWindow = [[app windows] elementMatchingType:XCUIElementTypeWindow identifier:@"SBCoverSheetWindow"];NSArray<XCUIElement *> *notiCells = [[mainWindow scrollViews] allElementsBoundByIndex];XCUIElement *scroll = notiCells[0];NSArray<XCUIElement *> *allNotiCells = [[scroll scrollViews] allElementsBoundByIndex];XCUIElement *aimElement = nil;for (int i=0; i < allNotiCells.count; i++) {XCUIElement *cell = allNotiCells[i];NSLog(@"cellLabel:%@",cell.label);if ([cell.label containsString:title]) { // title = 打开百度 这里我是通过是否包含来找到某个通知。aimElement = cell;break;}}XCTAssertNotNil(aimElement,@"应该存在待测通知");[aimElement tap];XCUICoordinate *openCoordinate = [aimElement coordinateWithNormalizedOffset:CGVectorMake(0.2, 0.35)];[openCoordinate tap]; // 位置点击(模拟器通知 有 open按钮,相当点击了open)
}
消息点击效果
iOS模拟器发送通知和UI测试相关推荐
- 02- web UI测试与UI Check List
UI英文是 user interface .所以UI测试就是用户界面测试. Web UI测试 用户界面测试:user interface testing,UI Testing指软件中的可见外观及其与用 ...
- 《iOS移动开发从入门到精通》图书连载7:iOS模拟器的使用
酷课堂(ID:coolketang)独家文章,其他媒体转载请注明出处 本期导读 当您在使用Xcode软件开发iOS平台的应用程序时,可以使用Apple提供的iOS模拟器进行应用程序的测试.Apple提 ...
- iOS 单元测试和UI测试教程
原文:iOS Unit Testing and UI Testing Tutorial 作者:Audrey Tam 译者:kmyhy 编写测试不是为了追求刺激,测试是为了避免你崭新的 App 变成了充 ...
- 有没有测试ipad的软件,4个用于测试iPhone iPad应用程序的iOS模拟器 | MOS86
如果您正在寻找在Mac或PC上测试iOS应用的方法,则需要使用模拟器. 仿真器与仿真器的不同之处在于它们并非旨在复制硬件,而是对硬件的基础状态进行建模. 好的仿真器可以很好地对这些条件进行建模,以使仿 ...
- iOS单元测试和UI测试全面解析
编写测试可不是一项迷人的工作;然而,由于测试可以避免使你的宝贝应用程序变成一块充斥错误的大垃圾场,所以编写测试又是一项非常有必要做的工作.如果你正在阅读本文,那么你应当已经知道你应该为您的代码和用户界 ...
- ios ui自动化测试_Xcuitest的ios自动化ui测试
ios ui自动化测试 Who knew automated UI Testing could be so easy! Well, I guess Apple did. Automated UI Te ...
- LayaAir3.0beta5新增3D UI、支持FairyGUI导入、支持iOS模拟器、IDE多开、列表指定子项等...
今天,LayaAir3.0的beta5版本出来了,虽然为了IDE插件等大模块功能的正式版推出,最近一直停留在beta版,但我们依然每次添加大量的新功能.例如本次的3D UI.FairyGUI适配.支持 ...
- iOS 单元测试和 UI 测试快速入门
iOS 单元测试和 UI 测试快速入门 前言 平时写完业务代码的时候都会去自己测试一遍,后面每次有修改都需要重复测,不管是一个业务流程还是一个工具类,其实都可以通过测试框架来帮助我们完成测试,特别是一 ...
- iOS通知和KVO的区别和用法
[NSNotification.delegate和KVO的区别] 1.效率肯定是delegate比nsnotification高. 2. delegate方法比notification更加直接,最典型 ...
- 向 iOS 设备发送推送通知
向 iOS 设备发送推送通知 直接向 iOS 应用程序提供丰富的内容 Michael Yuan 2012 年 5 月 25 日发布 背景 SMS 和 MMS 消息是由无线运营商通过设备的电话号码向特定 ...
最新文章
- shiro系列二、身份验证和授权
- 最新版,别的可以不用看了,zabbix 监控 esxi
- POJ3614奶牛晒阳光DINIC或者贪心
- PHP安装zip拓展,以及libzip安装问题
- Java技术小册(基础篇)
- Java面试题之有没有有顺序的Map实现类,如果有,他们是怎么实现有序的?
- 布法罗博士计算机专业回国人员,四名UW学生参加爱达荷州国家实验室的实习计划...
- imx6的Linux默认颜色,MY-IMX6 Linux-3.14 测试手册(1)
- webstorm使用前的准备——Node.js安装及配置
- mysql sql 备份数据_mysql怎么进行数据库备份和还原,以及自动备份
- linux定时任务总结。
- eclipse远程调试liunx下的tomcat
- 网络编程----心得体会
- 用VS运行程序错误,提示0x000007b
- VirtualBox安装教程及使用(Windows)
- 墨者学院—网络安全篇3
- 本地nodejs+gulp完成字体转换 ttf转成woff2格式
- RSTP快速生成树简介、RSTP与STP的区别、RSTP BPDU字段信息、RSTP角色与端口状态介绍、RSTP工作过程之P/A机制详细分解)
- 联想小新V1070-FXSE(FX版)怎么重装系统教程
- Alphabetical list of part-of-speech tags used in the Penn Treebank Project:
热门文章
- 基于vhdl的分频器设计
- 量子统计巨正则系综应用理想费米气体与波色气体性质详解
- 常用值得收藏的网站/软件 持续更新中
- sourceoffsite,sos_collab,版本控制,安装过程中不能创建数据,相关服务无法启动
- Thinkphp聊天室H5实时聊天室群聊聊天室自动分配账户完群组私聊禁言等功能全开源运营版本
- roseha linux,RoseHA 9.0 for Linux快速安装说明_v2.0-2015-04.pdf
- 【Python实战系列】串口实时接收数据并基于pyqtgraph绘图
- ajax上传图片 java_Java+ajax+图片上传
- 服务器性能测试 iometer 测试io
- High-Dimensional Statistics A Non-Asymptotic Viewpoint by Martin J. Wainwright Exercise7.3