14

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.

nschum
  • 15,322
  • 5
  • 58
  • 56
vdaubry
  • 11,369
  • 7
  • 54
  • 76

7 Answers7

19

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.

Drew Crawford
  • 226
  • 3
  • 4
  • In many cases when element has no name specified but has static text inside, this text can be used like name property... for me that worked for buttons, views, tables.... – yoosiba Mar 16 '11 at 21:39
  • 2
    hi, im also having the same problem. can u tel me how do u get the static txt from alertview? thanks in advance – cancerian Oct 18 '11 at 06:53
  • 1
    @cancerian inside the `onAlert` callback function, try `alert.logElementTree()`. From there, I see that `alert.name()` is its `title` and `alert.elements()[2].name()` is its `message`. – ma11hew28 Oct 04 '12 at 18:55
  • Instead of `delay(4)`, `frontMostApp().alert()` is faster: http://stackoverflow.com/a/12726891/242933 – ma11hew28 Oct 04 '12 at 18:57
  • Drew, you mean the alerts are handled on a **different** thread than the main automation script, correct? (Because the main script keeps going while the alert is being addressed.) @MattDiPasquale, I did not find `app.alert()` in the main thread to work particularly; `target.delay(4)` had much better results. – Matt Mc Jan 17 '15 at 03:46
6

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.

ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • This fixed my problem. Calling `.alert()` will wait up to 5 seconds (by default, configurable with `target.setTimeout`) for it to show up. – Kevin Borders Jul 23 '13 at 23:17
4

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();
Gabber
  • 5,152
  • 6
  • 35
  • 49
Sai Jithendra
  • 439
  • 5
  • 16
3

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

abarisone
  • 3,707
  • 11
  • 35
  • 54
Sumit
  • 29
  • 10
0

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.

fumiharu
  • 1
  • 1
0

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();
Hitesh Savaliya
  • 1,336
  • 13
  • 15
0

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

BamboOS
  • 478
  • 5
  • 13