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