【问题标题】:How to handle an iPhone alert with 2 buttons (want to click the non default button)如何使用 2 个按钮处理 iPhone 警报(想要单击非默认按钮)
【发布时间】:2011-09-06 15:39:06
【问题描述】:

我正在自动化 iPhone 应用程序
当我在 TO: 字段中输入邮件 ID 并单击发送时,我收到带有两个按钮的确认警报
“取消”和“发送”。
默认按钮是“取消”。我无法点击发送按钮。
这是我使用的代码 sn-p。请帮我解决这个问题

//获取句柄

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

target = UIATarget.localTarget();

app = target.frontMostApp();

buttons = window.buttons();

target.delay(2);

//点击导航栏上的发送按钮

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

target.delay(2);

//警报处理程序

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! ");
}

控件根本不在处理程序内部。

【问题讨论】:

    标签: javascript iphone cocoa-touch automation


    【解决方案1】:

    @kiran - 当我们打印 logElementTree 时,按钮以名称或 null 显示。在您的情况下,如果您无法单击带有 name 的按钮,您可以通过以数组的形式引用来完成。因为屏幕上的所有元素都是以数组的形式选取的。

    通常屏幕上的按钮以数组的形式排列,从 0 开始直到按钮的数量。在您的情况下,您可以将发送按钮称为按钮 [0] 或您必须检查的任何其他索引。

    【讨论】:

    • 感谢老兄的回复。是的,我同意你的观点,我能够弄清楚。其中的基础是使用 UIATarget.localTarget().frontMostApp().alert().buttons()[Buttonname].tap() 你也可以使用 UIATarget.localTarget() .frontMostApp().alert().buttons()[ButtonIndex].tap()
    • 我又读了一篇 qt 以使用 javascript 重新启动应用程序。我目前也在做同样的事情。你能帮我解决这个问题吗? DeactivateAppForDuration 从同一点再次重新启动它。但我想从头开始重新启动它。
    • 您是否使用 Instruments 进行自动化?
    • 如果是这样,很遗憾没有办法重新启动它,伙计:(这是Apple的限制。
    • @kiran 是的,我正在使用这些仪器。看起来你已经做了很多工作。您是否曾尝试暂停正在导入到我们在仪器中运行的脚本的 .js?
    【解决方案2】:

    有一个临时工作,我认为这不是最好的方法,但可以使过程继续进行

    解决方案:使用按钮相对于屏幕的大致坐标点击“发送”按钮。

    我使用了以下方法来实现。

    //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;
    
    
    }
    

    使用按钮索引,您可以跟踪按下警报视图的哪个按钮

    【讨论】:

    • 默认情况下,当脚本手动/由 Instruments 结束时,会按下取消按钮。那是因为调用了默认的警报处理程序,它按下了默认按钮(即取消)。但我想覆盖默认处理程序并单击“发送”按钮。
    • 我没有完全理解你的意思。默认情况下如何按下按钮。你是通过你的代码来做的吗?
    • 不...我不是通过我的代码来做的。这就是我困惑的地方。在我发布的代码 sn-p 中,点击“发送”按钮后,我会弹出一个带有“取消”和“发送”按钮的两个按钮。
      buttons = window.buttons();
      按钮["发送"].tap();对我不起作用。
      但是,对于不同页面中的同一应用程序,我会收到一条带有单个确定按钮的警报消息。对于那个警报
      buttons["OK"].tap();
      正在工作。
    • 看看尝试按照我在要创建两个按钮警报的答案中发布的方式创建警报
    • 希望我能做到。但我没有编写应用程序,我只是使用 javascript 自动化应用程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 2011-11-15
    • 2018-05-01
    相关资源
    最近更新 更多