0

I have a controller that creates and displays an alert when a user clicks a button ["Delete"]. The alert view prompts the user for confirmation, and either cancels the deletion, or completes the operation.

When I run the code in my iPhone or even in the iPhone Simulator, it runs fine - the item gets deleted if/when the user clicks the "Yes" button on the UIAlertView.

However, I am trying to create UI Automation tests using Instruments. When I run the following script, the alert is shown as expected, and the log shows that in fact the "Yes" button is tapped during the simulation. However, even though the alert is dismissed and the "Yes" button is tapped, the delegate method "clickedButtonAtIndex" method does not get called in my app's controller - so the deletion never happens.

Thinking that it is a timing issue, I have tried adding a delay after tapping the "Yes" button (on top of waiting for the "Yes" button to become invalid), but it has not made any difference.

NOTE: - The alert view should remain in memory until the controller is deallocated, as the alert view is a strong property of the controller class. - The controller is setting itself as the delegate of the alert - as evidenced by the app performing as expected when NOT simulating.

Any ideas?

Here is the script:

UIATarget.onAlert = function onAlert(alert) {

    var title = alert.name();

    if (title == expectedAlertMessage) {
    expectedAlertMessage = "";
        return true;  
    }
    return false; 
}

function testDeleteMyFavoriteStore(target, app) {

    var mainWindow = new MainWindow(target, app);             //MainWindow is defined in a separate js file
    var storeNameToDelete = "My Favorite Grocery Store"

    mainWindow.StorePicker().wheels()[0].selectValue(storeNameToDelete);
    mainWindow.EditStoreButton().tap();
    mainWindow.EditStoreButton().waitForInvalid();

    var editStoreWindow = new EditStoreWindow(target, app);   //EditStoreWindow is defined in a separate js file
    expectedAlertMessage = "Delete Store";   
    editStoreWindow.DeleteStoreButton().tap(); 
    editStoreWindow.DeleteStoreButton().waitForInvalid();

    var yesButton = app.alert().buttons()["Yes"];
    yesButton.tap();
    yesButton.waitForInvalid(); 
}
Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
Ana M
  • 467
  • 5
  • 13

2 Answers2

0

Can you try giving delays as any of the follows -

[1] target.delay(5);   //delay for 5 sec - can use this after your alert appears

[2] Use pushTimeout and popTimeout -

    UIATarget.localTarget().pushTimeout(20);
    window.navigationBar().name()["Welcome"];
    UIATarget.localTarget().popTimeout();

[3] Use isVisible check -

    UIATarget.localTarget().pushTimeout(20);
    window.navigationBar().name()["Welcome"].withValueForKey(1, "isVisible");
    UIATarget.localTarget().popTimeout();


Note : [2] and [3] will make instruments wait up to 20 seconds until the name of the navigation bar has changed to "Welcome."
Smriti
  • 1,552
  • 3
  • 15
  • 29
  • I have tried both (1) and (2) before - although never with more than a value of 5 seconds... I just retried with 20 seconds. I still get the same result. (3) keeps giving me an undefined error... not sure why yet. What is strange is that the delays that I put in place seem to pause the application before the tap() rather than after. I.e., even though I insert the delay after the tap(), the simulator seems to pause while the alert is still visible. Once the alert disappears, the app goes immediately back to the previous window. – Ana M Mar 21 '14 at 02:29
0

Did you assigned the Controller delegate of the Alert View and implemented the UIAlertViewDelegate protocol?

santibernaldo
  • 825
  • 1
  • 11
  • 26
  • Yes, I did both. The app functions correctly when run normally. It is only when I profile the app using the automation tool that the delegate does not get called. – Ana M Mar 21 '14 at 01:51