Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAutomation : Cancel button on Alert view is tapped without actually doing it

I'm facing this weird problem in UIAutomation.

I am checking an alert. In that, I am trying to log alert title and alert message. My code for this is:

UIATarget.onAlert = function onAlert(alert) {
UIALogger.logMessage("alert Shown");
UIALogger.logMessage(frontApp.alert().name());
UIALogger.logMessage(frontApp.alert().staticTexts()[1].value());
}

var target = UIATarget.localTarget().frontMostApp().mainWindow();
target.scrollViews()[0].buttons()["saveB"].tap();
UIATarget.localTarget().delay(2);

I am not tapping on cancel button in the alert to dismiss it. But, it is getting tapped automatically. I don't know why. Even in the logMessages, I see

target.frontMostApp().alert().cancelButton().tap()

this line getting executed automatically. I don't have this line anywhere in my script file. Is it a bug in iOS?

like image 794
user1982519 Avatar asked Jan 12 '23 21:01

user1982519


1 Answers

The cancel button on an alert is always tapped to keep the application from blocking unless the onAlert callback returns true. By returning true, you are telling the alert handling mechanism that you will handle tapping the appropriate button to dismiss the alert.

Change your alert callback to look like this:

UIATarget.onAlert = function onAlert(alert) {
    UIALogger.logMessage("alert Shown");
    UIALogger.logMessage(frontApp.alert().name());
    UIALogger.logMessage(frontApp.alert().staticTexts()[1].value());
    return true;   // <-- Adding this line
}

Conversely, returning false or leaving out a return value altogether signals to the alert handling mechanism that the cancel button should be tapped.

like image 72
Jonathan Penn Avatar answered Jan 26 '23 22:01

Jonathan Penn