ios UI自动化测试

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

 

一、一些注意事项:

1.做自动化测试时注意如果是真机话首先要设置不锁屏。

2.自动化测试过程中如果程序后台或崩溃了。脚本运行将会暂停,直到程序再次回到前台。

3.必须明确指定关闭自动测试,测试完成或中断都不会自动关闭测试。

4.测试也是根据视图树的元素位置获取元素进行测试,根视图元素是UIATarget。

 

二、部分功能说明:

1.获取当前程序(在激活状态):

ios UI自动化测试
UIATarget.localTarget().frontMostApp();
ios UI自动化测试

2.获取目标程序的主Window:

ios UI自动化测试
UIATarget.localTarget().frontMostApp().mainWindow();
ios UI自动化测试

3.获取一个cell中的文本元素:

ios UI自动化测试
UIATarget.localTarget().frontMostApp().mainWindow().tableViews()[0].cells()[0].elements()["Chocolate Cake"];
ios UI自动化测试

4.触发一个导航栏中“Add”按钮点击:

ios UI自动化测试
UIATarget.localTarget().frontMostApp().navigationBar().buttons()["Add"].tap();
ios UI自动化测试

5.触发点击屏幕上的某个位置:

ios UI自动化测试
UIATarget.localTarget().doubleTap({x:100, y:200});

UIATarget.localTarget().twoFingerTap({x:100, y:200});
ios UI自动化测试

6.获取tabBar并点击:

ios UI自动化测试
appWindow.tabBar().buttons()["Unit Conversion"].tap();
ios UI自动化测试

7.放大:

ios UI自动化测试
UIATarget.localTarget().pinchOpenFromToForDuration({x:20, y:200}, {x:300, y:200}, 2);
ios UI自动化测试

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

ios UI自动化测试
UIATarget.localTarget().pinchCloseFromToForDuration({x:20, y:200}, {x:300, y:200}, 2);
ios UI自动化测试

8.拖拽和快速滑动:

ios UI自动化测试
UIATarget.localTarget().dragFromToForDuration({x:160, y:200}, {x:160, y:400}, 1);

UIATarget.localTarget().flickFromTo({x:160, y:200}, {x:160, y:400});
ios UI自动化测试

9.为文本框输入内容:

ios UI自动化测试
var recipeName = "Unusually Long Name for a Recipe";
UIATarget.localTarget().frontMostApp().mainWindow().textFields()[0].setValue(recipeName);
ios UI自动化测试

10.在tabBar中导航

ios UI自动化测试
ios UI自动化测试
var tabBar = UIATarget.localTarget().frontMostApp().mainWindow().tabBar();

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

 if (selectedTabName != "Unit Conversion") { 

tabBar.buttons()["Unit Conversion"].tap();

}
ios UI自动化测试
ios UI自动化测试

11.tableview滚动到一个name以“Turtle Pie.”开头的元素:

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

ios UI自动化测试
UIATarget.localTarget().frontMostApp().mainWindow().tableViews()[0].scrollToElementWithPredicate("name beginswith ‘Turtle Pie’");
ios UI自动化测试

不使用预测功能:scrollToElementWithName和scrollToElementWithValueForKey

12.增加时间控制:

ios UI自动化测试
ios UI自动化测试
//压栈时间片:

UIATarget.localTarget().pushTimeout(2);

//接着执行脚本任务;

//时间片出栈

UIATarget.localTarget().popTimeout();
ios UI自动化测试
ios UI自动化测试

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

ios UI自动化测试
UIATarget.localTarget().delay(2);
ios UI自动化测试

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

13.按钮点击:

ios UI自动化测试
UIATarget.localTarget().frontMostApp().mainWindow().buttons()["xxxxx"].tap();
ios UI自动化测试

14截屏功能,事实证明模拟器是能使用截屏功能的:

ios UI自动化测试
UIATarget.localTarget().captureScreenWithName("SS001-2_AddedIngredient");
ios UI自动化测试

15.验证结果:

ios UI自动化测试
ios UI自动化测试
var cell = UIATarget.localTarget().frontMostApp().mainWindow().tableViews()[0].cells().firstWithPredicate("name beginswith ‘Tarte’");

if (cell.isValid()) {

UIALogger.logPass(testName);

}

else {

UIALogger.logFail(testName);

}
ios UI自动化测试
ios UI自动化测试

16.处理弹框,只需指定UIATarget.onAlert:

ios UI自动化测试
ios UI自动化测试
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;

}
ios UI自动化测试
ios UI自动化测试

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

17.模拟后台一段时间:

ios UI自动化测试
UIATarget.localTarget().deactivateAppForDuration(10);
ios UI自动化测试

手机方向旋转:

ios UI自动化测试
UIATarget.localTarget().setDeviceOrientation(UIA_DEVICE_ORIENTATION_PORTRAIT);
ios UI自动化测试

18.拖动?

ios UI自动化测试
ios UI自动化测试
window.tableViews()[0].scrollDown();?

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

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

window.tableViews()[0].scrollRight();
ios UI自动化测试
ios UI自动化测试

19.打印当前屏幕所有空间信息

ios UI自动化测试
UIATarget.localTarget().logElementTree();
ios UI自动化测试

20.记录日志?

ios UI自动化测试
ios UI自动化测试
UIALogger.logStart(“start”);?

UIALogger.logPass(“pass”);?

UIALogger.logWarning(“warning”);?

UIALogger.logFail(“fail”);?

UIALogger.logMessage(“message”);?

UIALogger.logError(“error”);?

UIALogger.logDebug(“debug”);?

UIALogger.logIssue(“issue”);
ios UI自动化测试
ios UI自动化测试

 

21.九宫格搜索输入框?

ios UI自动化测试
UIATarget.localTarget().frontMostApp().mainWindow().searchBars()[0]
ios UI自动化测试

22.模拟键盘操作,

ios UI自动化测试
?UIATarget.localTarget().frontMostApp().keyboard().typeString(“aaa\n”);\n=回车
ios UI自动化测试

23.输入框输入,

ios UI自动化测试
?UIATarget.localTarget().frontMostApp().mainWindow().tableViews()["Empty list"].cells()["用户名:"].textFields()[0].setValue(“abcd”);
ios UI自动化测试

24.获取对象数组长度,

ios UI自动化测试
UIATarget.localTarget().frontMostApp().mainWindow().buttons().length;
ios UI自动化测试

25.获取文本字符串,

ios UI自动化测试
UIATarget.localTarget().frontMostApp().mainWindow().scrollViews()[0].staticTexts()[0].value();
ios UI自动化测试

26.打印当前元素的视图树:

ios UI自动化测试
.logElementTree();
ios UI自动化测试

27.筛选框滚动,

ios UI自动化测试
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});
ios UI自动化测试

 

ios UI自动化测试,布布扣,bubuko.com

ios UI自动化测试

上一篇:英特尔支持下的Open HPC堆栈将于第四季度发布


下一篇:C# Xml 移除指定节点