转自:http://blog.youkuaiyun.com/close_marty/article/details/37907323
测试的过程中弹出Alert不会中断测试的执行,因为测试工具会自动处理弹框,它的处理方法就是点击弹框的取消按钮。
贴上一段代码
- UIATarget.onAlert = function onAlert(alert){
- var title = alert.name();
- var message = alert.scrollViews()[0].staticTexts()[1].name();
- UIALogger.logWarning("Alert with title " + title + new Date().getTime());
- if (title == "努力加载中,是否继续等待?"){
- alert.buttons()[1].tap();
- return true;
- }
- if (message == "抱歉,加载详情页时间过长") {
- alert.buttons()["取消"].tap();
- detailFail = 1;
- return true;
- }
- if (message == "抱歉,获取视频信息失败,请重试") {
- alert.buttons()["确定"].tap();
- detailFail = 1;
- return true;
- }
- return false;
- }
我们通过自己指定的onAlert()这么一个function给automation的UIATarget.onAlert,实现了对弹框的手动处理。
alert.name()通常可以获取到弹框的内容,当然有时候弹框里面会有多个staticTexts,可以具体alert.logElementTree()查看它的构成。
然后我们可以通过alert.buttons(),对指定的按钮进行操作。
这个方法默认的返回是false,这就是告诉automation "这个弹框我不处理,交给你来",
return true 就是 “这个弹框我已经处理过了,你就不要再管了”。
这个方法是不需要手动调用的,当有弹框出现时,automation会自动调用这个方法,所以你只需要在你的脚本开头,写上这个方法就可以了。
***在写UIAutomation相关代码时,由于有时候会开启instruments,这时再用instruments -w -t -e的方式运行脚本就会产生异常:UIAScriptAgentSignaledException ,所以看到该异常时关掉instruments即可。