使用UIAutomation处理警报

时间:2010-09-06 12:10:50

标签: iphone ios-ui-automation

我正在尝试使用UIAutomation测试UIAlertView的存在,但我的处理程序永远不会被调用。

在我的javascript开头我写道:

UIATarget.onAlert = function onAlert(alert) {
    UIALogger.logMessage("alertShown");
    return false;
}

据我所知,只要我指定了onAlert函数,就应该在我的测试期间出现alertView时调用它。 所以我运行一个显示alertView的测试,这是显示警报的代码:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:message message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
alertView.accessibilityLabel = @"alerte d'avertissement";
[alertView show];

我在乐器中运行我的测试,警报出现但我的处理程序从未被调用过。有没有人能够在UIAutomation中使用事件处理程序?

谢谢, 文森特。

7 个答案:

答案 0 :(得分:19)

文档似乎有误。事实证明,警报是在脚本尝试运行的同一个线程上处理的。因此,如果您希望调用警报处理程序,则需要休眠,例如

UIATarget.onAlert = { ... }
window.buttons().triggerAlertButton.tap();
UIATarget.localTarget().delay(4);

此外,警报的名称和值似乎始终设置为null。但是,我能够访问包含警报标题的第一个静态文本。

答案 1 :(得分:6)

确保在UIAlertView显示时,UI自动化脚本仍在运行。

例如,将以下行添加到脚本末尾将使其保持运行,直到可以访问警报或对象解析的宽限期到期为止。

// Wait for UIAlert to appear so that UIATarget.onAlert gets called.
target.frontMostApp().alert();

我通过彻底阅读& amp;理解Instruments User Guide: Automating UI Testing,我强烈建议您将其作为UI自动化的介绍。

审核UIATarget Class Reference,特别是方法popTimeoutpushTimeoutsetTimeouttimeoutdelay也可能会有所帮助

答案 2 :(得分:4)

以下代码适合我。该功能正在处理警报和"警告显示"打印在日志上。

var target = UIATarget.localTarget();
var application = target.frontMostApp();
var window = application.mainWindow();

UIATarget.onAlert = function onAlert(alert){
    UIALogger.logMessage("alert Shown");    
}

target.frontMostApp().mainWindow().tableViews()[0]
    .cells()["Fhgui"].buttons()["Images"].tap();
// Alert detected. Expressions for handling alerts 
// should be moved into the UIATarget.onAlert function definition.
target.frontMostApp().alert().defaultButton().tap();

答案 3 :(得分:3)

@vdaubry解决方案很简单。

根据Apple文档,如果您想手动处理提醒,那么您应该在true

中返回false而不是onAlert(alert)
 UIATarget.onAlert = function onAlert(alert) {
    UIALogger.logMessage("alertShown");
    return true;
}

@Drew Crawford延迟将无法正常工作,因为默认情况下,UI Automation会单击该按钮。文档没有错,但没有明确解释。

答案 4 :(得分:0)

我也有“从未调用过警报处理程序”的问题。 只需重启苹果的仪器解决了它:-)。

答案 5 :(得分:0)

e.g。 - onAlert未被称为

var target = UIATarget.localTarget(); 
target.buttons()["ShowAlert"].tap()
UIAtarget.onAlert = function onAlert(alert)
{...}

-

e.g。 - onAlert被称为

var target = UIATarget.localTarget(); 
UIAtarget.onAlert = function onAlert(alert)
{......}
target.buttons()["ShowAlert"].tap()

#import "onAlert.js"
var target = UIATarget.localTarget(); 
target.buttons()["ShowAlert"].tap()

尝试一下。

答案 6 :(得分:0)

以下代码片段适用于XCode 6.3.1&仪器(6.3.1(6D1002)):

    var target = UIATarget.localTarget();

    // Following line shows an internal alert with 'Cancel' & 'Continue' buttons
    target.frontMostApp().mainWindow().buttons()["ShowAlert"].tap();

    // Handle an internal alert
    UIATarget.onAlert = function onAlert(alert) {
            return true;
     }

    // Perform Tap on alert.
    target.frontMostApp().alert().buttons()["Continue"].tap();