简体   繁体   English

在iOS模拟器上首次启动时关闭警报

[英]Dismiss alert on initial launch on iOS simulator

I'm working on the automated UI tests for my app and I'm having trouble when trying to set up the environment for running the tests. 我正在为我的应用程序进行自动UI测试,尝试设置运行测试的环境时遇到了麻烦。 The plan is roughly this: 该计划大致是这样的:

  • build the application 编译应用
  • shutdown simulator if running 关闭模拟器(如果正在运行)
  • erase the simulator to make a clean install 擦除模拟器以进行全新安装
  • install my app on the simulator 在模拟器上安装我的应用
  • run UIAutomation tests 运行UIAutomation测试

Everything is working except when the application is launched by instruments to execute the tests, the alert appears to ask if the user allows notifications. 一切正常,除非仪器通过仪器启动应用程序以执行测试,但警报似乎会询问用户是否允许通知。 This is all as expected, but I can't find the way to get rid of the alert. 这一切都符合预期,但是我找不到摆脱警报的方法。

Things I have already tried: 我已经尝试过的事情:

  • creating onAlert as a first thing in my test script, in case it appears before the my alert callback is defined 在我的测试脚本中首先创建onAlert,以防它在定义我的警报回调之前出现
  • delay the target by 5 seconds in case the tests actually run even before the UI of the app is visible in the simulator 万一测试实际运行,即使在模拟器中看不到应用程序的界面之前,也可以将目标延迟5秒

I also went through all the permutations of the above that can be found on SO, I never get my onAlert callback invoked, no matter what I do. 我还仔细阅读了上面所有可以在SO上找到的排列,无论我做什么,我都永远不会调用onAlert回调。 So another thing I tried was: 所以我尝试的另一件事是:

  • try dismissing the alert with applescript 尝试使用applescript消除警报

The script I wrote: 我写的脚本:

tell application "System Events"
    tell process "iOS Simulator"
        set allUIElements to entire contents of window 1
        repeat with anElement in allUIElements
            try
                log anElement
            end try
        end repeat
    end tell
end tell

and it displays: 它显示:

static text “MyApp” Would Like to Send You Notifications of window iOS Simulator - iPhone 6 - iPhone 6 / iOS 8.1 (12B411) of application process iOS Simulator
static text Notifications may include alerts, sounds, and icon badges. These can be configured in Settings. of window iOS Simulator - iPhone 6 - iPhone 6 / iOS 8.1 (12B411) of application process iOS Simulator
UI element 3 of window iOS Simulator - iPhone 6 - iPhone 6 / iOS 8.1 (12B411) of application process iOS Simulator

Looks like the buttons are placed inside the "UI element 3" but I can't retrieve any elements from inside it, let alone clicking on it. 看起来这些按钮位于“ UI元素3”内部,但是我无法从其中检索任何元素,更不用说单击它了。 So I checked with Accessibility Manager: 所以我检查了辅助功能管理器:

Accessibility Manager中的UI元素3

It sits there as one of the children, the other ones are notification title and message. 它作为孩子之一坐在那里,其他孩子是通知标题和消息。 But when I go to that element, it is highlighted and I see this: 但是当我转到该元素时,该元素将突出显示,并且我看到以下内容:

在此处输入图片说明在此处输入图片说明

It is identified as generic element, it doesn't have any children... The interesting thing is when I choose the OK button in the Accessibility Inspector, I can actually see it's a child of the window, yet it is never listed: 它被标识为通用元素,没有任何子项。有趣的是,当我在Accessibility Inspector中选择“确定”按钮时,我实际上可以看到它是窗口的子项,但从未列出:

在此处输入图片说明

Can someone please shed some light on what is going on here? 有人可以说明一下这里发生的事情吗? How can I press that button with Applescript? 如何使用Applescript按下该按钮?

If you are doing automation using Instrument, the you will need to register callback (onAlert) for performing any action on alerts. 如果使用Instrument进行自动化,则需要注册回调(onAlert)以对警报执行任何操作。

But the problem in your case is that the alert appears before your script actually start executing and at that time no callback is registered for alert. 但是,您遇到的问题是,警报在脚本实际开始执行之前出现,并且那时没有为警报注册回调。

So if the alert can come with a delay of around 10 sec when you start application, then only it can be handled. 因此,如果启动应用程序时警报可能会延迟10秒钟左右,则只能对其进行处理。 But this can only be controlled through source code and not by your Automation code. 但这只能通过源代码控制,而不能由您的自动化代码控制。

So only option which is left is you need to manual dismiss the alert once fresh application is installed 因此,剩下的唯一选择是一旦安装了新的应用程序,您就需要手动消除警报

I am also facing same problem and found it to be a limitation of the tool 我也面临着同样的问题,并发现这是该工具的局限性

There are too many limitaion of this tool and thats why i shifted to UFT 这个工具的局限性太大,这就是为什么我转向UFT的原因

I had a similar problem. 我有一个类似的问题。 I just wanted position of the last control on the alert. 我只是想要警报上最后一个控件的位置。 So I came up with following piece of code: 所以我想出了以下代码:

on get_simulator_last_object_rect(simulator_index)

tell application "System Events"
    set ProcessList to (unix id of processes whose name is "iOS Simulator")
    set myProcessId to item simulator_index of ProcessList
    tell window 1 of (processes whose unix id is myProcessId)

        -- Forcefully print the UI elements so that Accessibility hierarchy is built
        UI elements
        -- Then wait precisely to let the Accessibility view hierarchy is ready
        delay 0.5

        set lowest_label_lowest_position to 0
        set _x to 0
        set _y to 0
        set _width to 0
        set _height to 0

        repeat with element in UI elements

            set {_x, _y} to position of element
            set {_width, _height} to size of element
            set current_control_lowest_position to _y + _height

            if current_control_lowest_position > lowest_label_lowest_position then set lowest_label_lowest_position to current_control_lowest_position - _height / 2

        end repeat
        return {{_x, _y}, {_width, lowest_label_lowest_position}}
    end tell
end tell
end get_simulator_alert_ok_button_position

I have a desktop app to control my actions. 我有一个桌面应用程序来控制我的操作。 I use this apple script in my Desktop app to get the frame of the last control. 我在桌面应用程序中使用此Apple脚本来获取最后一个控件的框架。 Now that I have the frame, I create a mouse event and perform click on the frame, after activating the simulator. 现在有了框架,在激活模拟器之后,我将创建一个鼠标事件并在该框架上单击。

Although I have not yet tried, but I am pretty sure that you can create mouse events from apple script and perform click on the frame / center of the frame. 尽管我还没有尝试过,但是我很确定您可以从apple脚本创建鼠标事件并在框架/框架中央单击。

Hope this helps. 希望这可以帮助。

Thanks, RKS 谢谢,RKS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM