Disable Hardware Keyboard for iOS Simulator using UIAutomation

蓝咒 提交于 2019-12-21 12:59:10

问题


I'm doing some automated tests in the iOS simulator using UIAutomation.

In Xcode 6, the iOS simulator's keyboard behavior changed to be similar to a real device, and now there is a menu item to connect/disconnect your Mac's keyboard to the simulator: Hardware > Keyboard > Connect Hardware Keyboard.

I don't mind this, but what happens when your Mac's keyboard is connected is that the simulator will no longer show the software keyboard. When you run a test script with UIAutomation, calls like UIATarget.localTarget().frontMostApp().keyboard().typeString("myString"); will fail because the keyboard doesn't appear, even when you've made a text field the first responder.

This is annoying, because if I do any manual testing in the simulator, I will need to remember to disable the hardware keyboard before I run any of my UIAutomation tests, or they will all fail.

Is there any way, from within a UIAutomation JS script, to check hardware keyboard settings and disable them? Or, any way to do this from the command line, prior to executing the UIAutomation script?


回答1:


If I understood your question correctly, you need bulletproof way to enter text. I just use setValue for that. Like this: UIATarget.localTarget().frontMostApp().textFields().Login.setValue('sofa');




回答2:


Because Ian's script is not visible in the net anymore , here my variant of the Apple script switching off the hardware keyboard.

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



回答3:


I wrote an AppleScript that sets the state of the hardware keyboard [other versions], which you'd execute like this:

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$

It's available in Illuminator as target().connectHardwareKeyboard(), so to make sure that the hardware keyboard was disabled before each test run you'd do this in your setup:

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


来源:https://stackoverflow.com/questions/27973891/disable-hardware-keyboard-for-ios-simulator-using-uiautomation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!