ios UI自动化测试

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

 

一、一些注意事项:

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’");

不使用预测功能:scrollToElementWithName和scrollToElementWithValueForKey

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
”);
=回车

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});
原文地址:https://www.cnblogs.com/ccxniit2004/p/3669004.html