English 中文(简体)
Handling Alert with 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.

最佳回答

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.

问题回答

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.

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

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

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

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.

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




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签