How to handle an iPhone alert with 2 buttons (want

2019-09-03 14:23发布

问题:

I am automating an iPhone appplication
While I type the mail ID in the TO: field and click on send, I get a confirmation alert with two buttons
"Cancel" & "Send".
The default button is "cancel". I'm unable to click on the send button.
Here is the code snippet which I have used. Please help me with this

//Get the handles

window = UIATarget.localTarget().frontMostApp().mainWindow();

target = UIATarget.localTarget();

app = target.frontMostApp();

buttons = window.buttons();

target.delay(2);

//tap the send button on the navigation bar

app.navigationBar().buttons()["Send"].tap(); 

target.delay(2);

//Alert handler

UIATarget.onAlert = function onAlert(alert)

{
   //Unable to enter this portion of the code

   target.delay(2);

   var alerttitle = alert.name();

   UIALogger.logMessage("Alert with title: '"+alerttitle+"' encountered! ");
}

The control is not going inside the handler at all.

回答1:

@kiran - When we print the logElementTree the buttons are shown with the name or null. In your case if you are not able to click on button with name , you can do it by referring in form of array. As all elements on screen are picked in form of array.

In general buttons on screen are arranged in form of array starting from 0 and so on upto number of buttons. In your case u can refer to send button as button[0] or any other index which you have to check on your side.



回答2:

There is a temporary work around which I suppose is not the best approach, but can keep the process going forward

Solution: to tap on the "Send" button using the approximate coordinates of the button relative to the screen.

I have used the following way to achieve that.

//Get the handles

window = UIATarget.localTarget().frontMostApp().mainWindow();

target = UIATarget.localTarget();

app = target.frontMostApp();

//Get the handle for the alert

var alertHandle = app.alert();

//Do a log Element tree for the alert to get the coordinates, height and width of the alert

alertHandle.logElementTree();

//Decide the coordinates of the point based on the value of the alert coordinates

target.tap({x:50,y:150});


回答3:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message: @"some text"
                                                           delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];

[alert show];

[alert release];


- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == [alertView cancelButtonIndex])
        return;


}

using button index you can track which button of alert view was pressed