instruments如何处理alert对话框

被测应用中有出现alert对话框,需要点击处理。绕了很多弯路,回头想想其实很简单。
1. onAlert事件要放在执行代码前面
2. onAlert事件中return值需要为true。如果是false,系统自动点击cancel按钮,如果cancel按钮不存在,则点击默认按钮。
3. 按顺序写执行代码即可

var target = UIATarget.localTarget();

UIATarget.onAlert = function onAlert(alert) {
var title = alert.name();
UIALogger.logWarning(“Alert with title ‘” + title + “‘ encountered.”);
// return false to use the default handler
return true;
}

target.frontMostApp().mainWindow().tableViews()[“空列表”].cells()[“xxx, 用户注销”].tap();
// Alert detected. Expressions for handling alerts should be moved into the UIATarget.onAlert function definition.
target.frontMostApp().alert().defaultButton().tap();

instruments学习笔记(一)

//调用外部js
#import “../common/common.js”

//导航栏操作
UIATarget.localTarget().frontMostApp().navigationBar().leftButton().tap(); //点击左侧按钮
UIATarget.localTarget().frontMostApp().navigationBar().rightButton().tap(); //点击右侧按钮

//按钮点击
UIATarget.localTarget().frontMostApp().mainWindow().buttons()[“xxxxx”].tap();

//坐标点击
UIATarget.localTarget().tap({x:30,y:131});

//等待n秒,delay(n)
UIATarget.localTarget().delay(3);

//截图
UIATarget.localTarget().captureScreenWithName(“picname”);

//获取对象数组长度,length
UIATarget.localTarget().frontMostApp().mainWindow().buttons().length;

//获取文本字符串,value()
UIATarget.localTarget().frontMostApp().mainWindow().scrollViews()[0].staticTexts()[0].value();

//滚轮滚动,dragInsideWithOptions()
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});

//根据name模糊查询控件,firstWithPredicate(“name beginswith ‘xxx'”)
obj= window.tableViews()[0].cells().firstWithPredicate(“name beginswith ‘hello'”);

//根据name完全匹配,firstWithName(“xxxx”)
obj= window.tableViews()[0].cells().firstWithName(“hello world”);

//根据key值匹配,firstWithValueForKey(value,key)
obj= window.tableViews()[0].cells().firstWithValueForKey(“hello world”,”name”);

//拖动
window.tableViews()[0].scrollDown();
window.tableViews()[0].scrollUp();
window.tableViews()[0].scrollLeft();
window.tableViews()[0].scrollRight();

//打印当前屏幕所有空间信息
UIATarget.localTarget().logElementTree();

//记录日志
UIALogger.logStart(“start”);
UIALogger.logPass(“pass”);
UIALogger.logWarning(“warning”);
UIALogger.logFail(“fail”);
UIALogger.logMessage(“message”);
UIALogger.logError(“error”);
UIALogger.logDebug(“debug”);
UIALogger.logIssue(“issue”);

//九宫格搜索输入框
UIATarget.localTarget().frontMostApp().mainWindow().searchBars()[0]

//模拟键盘操作,keyboard().typeString(“xxx”)
UIATarget.localTarget().frontMostApp().keyboard().typeString(“aaa\n”);

\n=回车

//输入框输入,setValue()
UIATarget.localTarget().frontMostApp().mainWindow().tableViews()[“Empty list”].cells()[“用户名:”].textFields()[0].setValue(“abcd”);