使用 UIAutomation 处理警报
我正在尝试使用 UIAutomation 测试 UIAlertView 是否存在,但我的处理程序从未被调用。
在我的 javascript 开头,我写道:
UIATarget.onAlert = function onAlert(alert) {
UIALogger.logMessage("alertShown");
return false;
}
据我了解,一旦我指定了 onAlert 函数,当我的测试期间出现警报视图时,它应该被调用。 所以我运行一个显示alertView的测试,这里是显示警报的代码:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:message message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
alertView.accessibilityLabel = @"alerte d'avertissement";
[alertView show];
我在仪器中运行测试,警报显示但我的处理程序从未被调用。有人能够将事件处理程序与 UIAutomation 一起使用吗?
谢谢, 文森特.
I'm trying to test the presence of an UIAlertView with UIAutomation but my handler never gets called.
At the beginning of my javascript i write :
UIATarget.onAlert = function onAlert(alert) {
UIALogger.logMessage("alertShown");
return false;
}
As i understand it, as soon as i specify my onAlert function, it should get called when an alertView appears during my tests.
So i run a test that shows an alertView, here is the code that shows the alert :
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:message message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
alertView.accessibilityLabel = @"alerte d'avertissement";
[alertView show];
I run my test in instruments, the alert shows up but my handler is never called. Has anybody been able to use event handlers with UIAutomation ?
Thanks,
Vincent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
该文档似乎是错误的。事实证明,警报是在您的脚本尝试运行的同一线程上处理的。因此,如果您希望调用警报处理程序,则需要休眠,例如,
此外,警报的名称和值似乎始终设置为 null。但是,我能够访问包含警报标题的第一个静态文本。
The documentation seems to be wrong. It turns out that alerts are handled on the same thread your script tries to run. So if you want the alert handler to be called, you need to sleep, e.g.,
Also, it appears that the alert's name and value are always set to null. I was, however, able to access the first static text which contained the alert's title.
确保当
UIAlertView
显示时 UI 自动化脚本仍在运行。例如,将以下行添加到脚本末尾将使其保持运行,直到警报可访问或对象解析的宽限期到期。
我通过彻底阅读和解决了这个问题理解 仪器用户指南:自动化 UI 测试,我强烈建议将其作为 UI 自动化的介绍。
查看 UIATarget 类也可能会有所帮助参考,特别是方法
popTimeout
、pushTimeout
、setTimeout
、timeout
和delay
。Make sure the UI Automation script is still running when the
UIAlertView
shows.For example, adding the following line to the end of your script will keep it running until an alert becomes accessible or the grace period for object resolution expires.
I figured this out by thoroughly reading & understanding Instruments User Guide: Automating UI Testing, which I highly recommend doing as an introduction to UI Automation.
It may also be helpful to review the UIATarget Class Reference, specifically the methods
popTimeout
,pushTimeout
,setTimeout
,timeout
, anddelay
.下面的代码对我有用。该函数正在处理警报,并且“已显示警报”会打印在日志上。
The below code works for me. The function is handling the alert and "alert Shown" is printed on the logs.
@vdaubry 解决方案很简单。
根据苹果文档,如果你想手动处理警报,那么你应该在
onAlert(alert)
中返回true
而不是false
@Drew Crawford延迟将不起作用,因为默认情况下 UI 自动化会单击“可以”按钮。文档并没有错,但没有明确解释。
@vdaubry the solution is simple.
According to Apple documentation, if you want to handle alerts manually then you should return
true
instead offalse
inonAlert(alert)
@Drew Crawford the delays will not work because by default can button is clicked by UI Automation. The documentation is not wrong but it is not clearly explained.
我也遇到了“从未调用过警报处理程序”的问题。
只需重新启动苹果的仪器就可以解决这个问题:-)。
I was having "never called alert handler" problem too.
Simply restarting apple's Instruments solved it for me :-).
例如 - onAlert 未被调用
-
例如 - onAlert 被调用
或
尝试一下。
e.g. - onAlert is not called
-
e.g. - onAlert is called
or
Try it out.
以下代码片段适用于 XCode 6.3.1 &仪器(6.3.1 (6D1002)) :
Following snippet works for me on XCode 6.3.1 & Instruments(6.3.1 (6D1002)) :