【问题标题】:Disable Hardware Keyboard for iOS Simulator using UIAutomation使用 UIAutomation 禁用 iOS 模拟器的硬件键盘
【发布时间】:2015-03-14 11:07:32
【问题描述】:

我正在使用 UIAutomation 在 iOS 模拟器中进行一些自动化测试。

在 Xcode 6 中,iOS 模拟器的 keyboard behavior changed 类似于真实设备,现在有一个菜单项可以将 Mac 的键盘连接/断开连接到模拟器:硬件 > 键盘 > 连接硬件键盘。

我不介意,但是当您的 Mac 键盘连接时,模拟器将不再显示软件键盘。当您使用 UIAutomation 运行测试脚本时,像 UIATarget.localTarget().frontMostApp().keyboard().typeString("myString"); 这样的调用将失败,因为键盘没有出现,即使您已将文本字段设置为第一响应者。

这很烦人,因为如果我在模拟器中进行任何手动测试,我需要记住在运行任何 UIAutomation 测试之前禁用硬件键盘,否则它们都会失败。

有什么方法可以在 UIAutomation JS 脚本中检查硬件键盘设置并禁用它们?或者,在执行 UIAutomation 脚本之前,有什么方法可以从命令行执行此操作?

【问题讨论】:

    标签: ios keyboard xcode6 simulator ios-ui-automation


    【解决方案1】:

    如果我正确理解了您的问题,您需要防弹的方式来输入文字。我只是为此使用 setValue 。像这样:UIATarget.localTarget().frontMostApp().textFields().Login.setValue('sofa');

    【讨论】:

    • 看起来不错。很抱歉花了这么长时间才接受这个:)
    【解决方案2】:

    更新了 Leo 对 Xcode 11 的回答的 AppleScript,其中“硬件”更改为“I/O”。为了让它在 Xcode 11.4.1 中运行,我需要一个 bash 脚本包装器来调用 AppleScript scpt 文件。

    文件
    ./禁用硬件键盘
    ./disable-hardware-keyboard.scpt

    tell application "Simulator"
        activate
    end tell
    
    tell application "System Events"
        set hwKB to value of attribute "AXMenuItemMarkChar" of menu item "Connect Hardware Keyboard" of menu 1 of menu item "Keyboard" of menu 1 of menu bar item "I/O" of menu bar 1 of application process "Simulator"
        if ((hwKB as string) is equal to "missing value") then
            do shell script "echo 'hardware keyboard is off'"
        else
            click menu item "Connect Hardware Keyboard" of menu 1 of menu item "Keyboard" of menu 1 of menu bar item "I/O" of menu bar 1 of application process "Simulator"
        end if
    end tell
    
    #!/bin/bash
    cd $(dirname $0)
    osascript disable-hardware-keyboard.scpt
    

    【讨论】:

      【解决方案3】:

      因为 Ian 的脚本在网络中不再可见,这里我的 Apple 脚本变体关闭了硬件键盘。

      tell application "Simulator"
          activate
      end tell
      
      tell application "System Events"
          set hwKB to value of attribute "AXMenuItemMarkChar" of menu item "Connect Hardware Keyboard" of menu 1 of menu item "Keyboard" of menu 1 of menu bar item "Hardware" of menu bar 1 of application process "Simulator"
          if ((hwKB as string) is equal to "missing value") then
            do shell script "echo 'hardware keyboard is off'"
          else
            click menu item "Connect Hardware Keyboard" of menu 1 of menu item "Keyboard" of menu 1 of menu bar item "Hardware" of menu bar 1 of application process "Simulator"
          end if
      end tell
      

      【讨论】:

        【解决方案4】:

        我写了一个AppleScript that sets the state of the hardware keyboard [other versions],你可以这样执行:

        bash$ osascript scripts/set_hardware_keyboard.applescript 0
        menu item Connect Hardware Keyboard of menu Keyboard of menu item Keyboard of menu Hardware of menu bar item Hardware of menu bar 1 of application process iOS Simulator
        bash$
        

        它在 Illuminator 中以 target().connectHardwareKeyboard() 的形式提供,因此要确保在每次测试运行之前禁用硬件键盘,您需要在设置中执行此操作:

        automator.setCallbackPrepare(function () {
                 target().connectHardwareKeyboard(false);
         });
        

        【讨论】:

        • 什么是照明器?
        • 这是一个 apache 许可的框架,当它是基于 javascript 的时候,我写它来做 iOS 自动化。它后来被更新为一个 Swift 库,尽管某些功能(如桥接和标记)是不可能的。 github.com/paypal/Illuminator
        猜你喜欢
        • 2019-09-14
        • 1970-01-01
        • 2019-11-25
        • 2014-11-13
        • 1970-01-01
        • 2022-01-26
        • 2017-05-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多