iOS自动化测试之UI自动化测试学习笔记

本文介绍了iOS自动化测试中的UI自动化测试,包括注意事项和一系列功能的详细说明,如获取当前程序、点击操作、输入内容、屏幕截图等。此外,还讨论了如何处理弹框、模拟后台、模拟键盘操作以及打印视图树等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、注意事项:

  1. 做自动化测试时注意如果是真机话首先要设置不锁屏。
  2. 自动化测试过程中如果程序后台或崩溃了。脚本运行将会暂停,直到程序再次回到前台。
  3. 必须明确指定关闭自动测试,测试完成或中断都不会自动关闭测试。
  4. 测试也是根据视图树的元素位置获取元素进行测试,根视图元素是UIATarget。

二、部分功能说明:

1.获取当前程序(在激活状态):
UIATarget.localTarget().frontMostApp();
2.获取目标程序的主Window:
UIATarget.localTarget().frontMostApp().mainWindow();
3.获取一个cell中的文本元素:
UIATarget.localTarget().frontMostApp().mainWindow().tableViews()[0].cells()[0].elements()["Chocolate Cake"];
4.触发一个导航栏中“Add”按钮点击:
UIATarget.localTarget().frontMostApp().navigationBar().buttons()["Add"].tap();
5.触发点击屏幕上的某个位置:
UIATarget.localTarget().doubleTap({x:100, y:200});

UIATarget.localTarget().twoFingerTap({x:100, y:200});
6.获取tabBar并点击:
appWindow.tabBar().buttons()["Unit Conversion"].tap();
7.放大:
UIATarget.localTarget().pinchOpenFromToForDuration({x:20, y:200}, {x:300, y:200}, 2);

       缩小(后面是个时间参数,表示持续时间):

UIATarget.localTarget().pinchCloseFromToForDuration({x:20, y:200}, {x:300, y:200}, 2);
8.拖拽和快速滑动:
UIATarget.localTarget().dragFromToForDuration({x:160, y:200}, {x:160, y:400}, 1);
UIATarget.localTarget().flickFromTo({x:160, y:200}, {x:160, y:400});
9.为文本框输入内容:
var recipeName = "Unusually Long Name for a Recipe";
UIATarget.localTarget().frontMostApp().mainWindow().textFields()[0].setValue(recipeName);
10.在tabBar中导航
var tabBar = UIATarget.localTarget().frontMostApp().mainWindow().tabBar();

 var selectedTabName = tabBar.selectedButton().name();

 if (selectedTabName != "Unit Conversion") 
 { 
     tabBar.buttons()["Unit Conversion"].tap();
 }
11.tableview滚动到一个name以“Turtle Pie.”开头的元素:

根据name模糊查询控件,firstWithPredicate(“name beginswith ‘xxx’”),根据name完全匹配,firstWithName(“xxxx”),/根据key值匹配,firstWithValueForKey(value,key):

UIATarget.localTarget().frontMostApp().mainWindow().tableViews()[0].scrollToElementWithPredicate("name beginswith ‘Turtle Pie’");

       不使用预测功能:scrollToElementWithNamescrollToElementWithValueForKey

12.增加时间控制:
//压栈时间片:

UIATarget.localTarget().pushTimeout(2);

//接着执行脚本任务;

//时间片出栈

UIATarget.localTarget().popTimeout();

       还有一种方式,采用delay方式:

UIATarget.localTarget().delay(2);

       两种方式的区别是,在时间片内,第一种方法会不断尝试去执行压栈和出栈间的脚本任务,一旦可以执行就执行,不一定在时间片后才执行,而第二种方式是在时间片到后才执行脚本任务。

13.按钮点击:
UIATarget.localTarget().frontMostApp().mainWindow().buttons()["xxxxx"].tap();
14.截屏功能,事实证明模拟器是能使用截屏功能的:
UIATarget.localTarget().captureScreenWithName("SS001-2_AddedIngredient");
15.验证结果:
var cell = UIATarget.localTarget().frontMostApp().mainWindow().tableViews()[0].cells().firstWithPredicate("name beginswith ‘Tarte’");

if (cell.isValid()) {

UIALogger.logPass(testName);

}

else {

UIALogger.logFail(testName);

}
16.处理弹框,只需指定UIATarget.onAlert
UIATarget.onAlert = function onAlert(alert) {

 var title = alert.name();

UIALogger.logWarning("Alert with title '" + title + "' encountered.");

if (title == "The Alert We Expected") {

alert.buttons()["Continue"].tap();

return true; //alert handled, so bypass the default handler

}

// return false to use the default handler

 return false;

}

       返回FALSE代表点击取消,TRUE代表确定。

17.模拟后台一段时间:
UIATarget.localTarget().deactivateAppForDuration(10);
手机方向旋转:

UIATarget.localTarget().setDeviceOrientation(UIA_DEVICE_ORIENTATION_PORTRAIT);

####18.拖动


window.tableViews()[0].scrollDown();


window.tableViews()[0].scrollUp();


window.tableViews()[0].scrollLeft();


window.tableViews()[0].scrollRight();
19.打印当前屏幕所有空间信息
UIATarget.localTarget().logElementTree();

####20.记录日志


UIALogger.logStart(“start”);


UIALogger.logPass(“pass”);


UIALogger.logWarning(“warning”);


UIALogger.logFail(“fail”);


UIALogger.logMessage(“message”);


UIALogger.logError(“error”);


UIALogger.logDebug(“debug”);


UIALogger.logIssue(“issue”);

####21.九宫格搜索输入框


UIATarget.localTarget().frontMostApp().mainWindow().searchBars()[0]
22.模拟键盘操作,

UIATarget.localTarget().frontMostApp().keyboard().typeString(“aaa\n”);\n=回车
23.输入框输入,

UIATarget.localTarget().frontMostApp().mainWindow().tableViews()["Empty list"].cells()["用户名:"].textFields()[0].setValue(“abcd”);
24.获取对象数组长度,
UIATarget.localTarget().frontMostApp().mainWindow().buttons().length;
25.获取文本字符串,
UIATarget.localTarget().frontMostApp().mainWindow().scrollViews()[0].staticTexts()[0].value();
26.打印当前元素的视图树:
.logElementTree();
27.筛选框滚动,
UIATarget.localTarget().frontMostApp().mainWindow().pickers()[0].wheels()[0].dragInsideWithOptions({startOffset:{x:0.38, y:0.66}, endOffset:{x:0.38, y:0.12}, duration:1.6});

       转载自:http://www.cnblogs.com/dokaygang128/p/3517674.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值