disable iOS simulator 'connect hardware keyboard' programmatically
Asked Answered
P

2

13

I'm writing XCUITests for view has 2 UITextField and by defualt the simulator has hardware keyboard connected, hence the software one is not being presented.

Is it possible to set some settings (maybe in schema?) to force-disable connecting hardware keyboard by the simulator before test runs.

I went through few links but none seems to be helpful. iOS simulator - disable connect hardware keyboard programmatically

Disable Hardware Keyboard for iOS Simulator using UIAutomation

When i try this on xcode local it works but, XCUItest runs on CI and i configured fastlane file for this called enable keyboard script before Test execution starts. But it throws error

Set: Entry, ":DevicePreferences::ConnectHardwareKeyboard ", Does Not Exist

Any help would be really appreciated

Photocomposition answered 25/4, 2019 at 8:53 Comment(0)
D
11

Went through the same problem. Found a simple solution to add a prescript in the UI tests scheme.

First, this is image of the plist file opened with Xcode. This is located at ~/Library/Preferences/com.apple.iphonesimulator.plist

enter image description here

Now here is the prescript I added to my ui tests scheme. Read the comments in script for further explanation.

enter image description here

killall Simulator
defaults write com.apple.iphonesimulator ConnectHardwareKeyboard -bool false

How it works First the simulator is killed. Next the "defaults" command writes the key/value into the simulators plist. Now when the tests start, they launch the simulator which takes into account the value we wrote in plist.

Hope it works for you too. :)

Dorcasdorcea answered 30/7, 2019 at 9:35 Comment(5)
It works if the test is run from XCode but fails if you run from fastlaneCustomhouse
Agreed. Fails from fastlaneArmorial
I have no experience with fastlane. I use XcodeServer for running CI tests only.Dorcasdorcea
Fastlane is supposed to disable the connected hardware keyboard itself. There is an open ticket for this: github.com/fastlane/fastlane/issues/16083.Freudian
This doesn't seem to work anymore. Its killing the simulator but not affecting the keyboard. Has the key changed? Do you have an updated script?Lubbi
L
8

To disable iOS Simulators Connect Hardware Keyboard setting (Simulator -> Hardware -> Connect Hardware Keyboard), add this sniped in AppDelegate. ref link

#if targetEnvironment(simulator)
// Disable hardware keyboards.
let setHardwareLayout = NSSelectorFromString("setHardwareLayout:")
UITextInputMode.activeInputModes
    // Filter `UIKeyboardInputMode`s.
    .filter({ $0.responds(to: setHardwareLayout) })
    .forEach { $0.perform(setHardwareLayout, with: nil) }
#endif
Lingonberry answered 21/4, 2020 at 8:29 Comment(5)
This worked for me up until Xcode 14 - looking into alternatives now.Jeanne
@Jeanne found anything better? just ran into the same issueLubbi
@SimonMcLoughlin the only thing we did further was to disable secure text entry during UI tests and it worked pretty well. Then when we upgraded to Ventura and Xcode 14.3.1 we added forceTap from https://mcmap.net/q/203211/-xcode-ui-test-ui-testing-failure-failed-to-scroll-to-visible-by-ax-action-when-tap-on-search-field-quot-cancel-39-buttonJeanne
@Jeanne I don't understand your response, it seems unrelated to the above. What does secure text entry and force tapping have to do with disconnecting the hardware keyboard?Lubbi
@SimonMcLoughlin - sorry, I understand that fixes a completely different problem. The other things we did to fix this particular issue was to run this every day on our CI: xcrun simctl shutdown all && xcrun simctl erase all Bear in mind we host our own CI, so we ensure their state beforehand. We haven't found a good way of ensuring a dev machine is in the correct state.Jeanne

© 2022 - 2024 — McMap. All rights reserved.