使用UIAutomation禁用iOS模拟器的硬件键盘

时间:2015-01-15 22:07:43

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

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

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

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

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

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

4 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:2)

因为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

答案 2 :(得分:1)

更新了Leo针对Xcode 11的答案的AppleScript,其中“硬件”已更改为“ I / O”。为了使其正常工作,Xcode 11.4.1我需要一个bash脚本包装器来调用AppleScript scpt文件。

文件
./disable-hardware-keyboard
./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 :(得分:0)

我写了一个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);
 });