使用 UIAutomation 处理警报

发布于 2024-09-18 01:07:02 字数 646 浏览 5 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

油饼 2024-09-25 01:07:02

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

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

此外,警报的名称和值似乎始终设置为 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.,

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

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.

尸血腥色 2024-09-25 01:07:02

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

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

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

我通过彻底阅读和解决了这个问题理解 仪器用户指南:自动化 UI 测试,我强烈建议将其作为 UI 自动化的介绍。

查看 UIATarget 类也可能会有所帮助参考,特别是方法popTimeoutpushTimeoutsetTimeouttimeoutdelay

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.

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

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, and delay.

陈甜 2024-09-25 01:07:02

下面的代码对我有用。该函数正在处理警报,并且“已显示警报”会打印在日志上。

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();

The below code works for me. The function is handling the alert and "alert Shown" is printed on the logs.

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();
软甜啾 2024-09-25 01:07:02

@vdaubry 解决方案很简单。

根据苹果文档,如果你想手动处理警报,那么你应该在 onAlert(alert) 中返回 true 而不是 false

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

@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 of false in onAlert(alert)

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

@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.

素年丶 2024-09-25 01:07:02

我也遇到了“从未调用过警报处理程序”的问题。
只需重新启动苹果的仪器就可以解决这个问题:-)。

I was having "never called alert handler" problem too.
Simply restarting apple's Instruments solved it for me :-).

橘亓 2024-09-25 01:07:02

例如 - onAlert 未被调用

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

-

例如 - 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()

尝试一下。

e.g. - onAlert is not called

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

-

e.g. - onAlert is called

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

or

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

Try it out.

暖伴 2024-09-25 01:07:02

以下代码片段适用于 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();

Following snippet works for me on XCode 6.3.1 & Instruments(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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文