Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Hardware Keyboard for iOS Simulator using UIAutomation

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?

like image 276
Matt Mc Avatar asked Jan 15 '15 22:01

Matt Mc


People also ask

How to fix iOS simulator keyboard hidden issue?

Click the simulator menu Hardware —> Keyboard, check the Connect Hardware Keyboard sub-menu, and then uncheck it at once. Now the keyboard will be prompted automatically when you click the text field or text view UI component in the iOS simulator. 2. Fix iOS Simulator Keyboard Hidden Issue Method Two.

How do I enable keyboard in iOS simulator?

Select the iOS simulator window by click it. Click the simulator menu Hardware —> Keyboard, check the Connect Hardware Keyboard sub-menu, and then uncheck it at once. Now the keyboard will be prompted automatically when you click the text field or text view UI component in the iOS simulator.

How to enable instruments to automate the iOS simulator?

For multiple simulator support, you will need to upgrade to the XCUITest driver). To allow the iOS simulator to be automated by Instruments, you need to modify the authorization database for the system. Appium provides an easy way to do this by installing and running an authorization script: sudo authorize-ios.

How to fix text field not showing in iOS simulator?

Below are the steps to fix this issue. Select the iOS simulator window by click it. Click the simulator menu Hardware —> Keyboard, check the Connect Hardware Keyboard sub-menu, and then uncheck it at once. Now the keyboard will be prompted automatically when you click the text field or text view UI component in the iOS simulator.


2 Answers

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

like image 140
Sofa Avatar answered Sep 27 '22 20:09

Sofa


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
like image 42
Iain Smith Avatar answered Sep 27 '22 19:09

Iain Smith