Disable Hardware Keyboard for iOS Simulator using UIAutomation
Asked Answered
H

4

7

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?

Hubbell answered 15/1, 2015 at 22:7 Comment(0)
C
4

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');

Carhart answered 2/2, 2015 at 21:51 Comment(1)
Seems fine. Sorry for taking so long to accept this :)Hubbell
K
3

Updated AppleScript of Leo's answer for Xcode 11 where "Hardware" was changed to "I/O". To get this working it Xcode 11.4.1 I needed a bash script wrapper to call the AppleScript scpt file.

Files
./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
Kareem answered 1/5, 2020 at 16:58 Comment(0)
F
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
Faux answered 2/5, 2018 at 16:41 Comment(0)
K
1

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);
 });
Krutz answered 6/4, 2015 at 21:39 Comment(2)
What is Illuminator?Personal
It was an apache-licensed framework I wrote to do iOS automation back when that was javascript-based. It was later updated to be a Swift library although certain features like the bridge and tagging aren't possible. github.com/paypal/IlluminatorKrutz

© 2022 - 2024 — McMap. All rights reserved.