xcode 是否可以通过 UI 测试中的代码“切换软键盘”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38010494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to "toggle software keyboard" via the code in UI test?
提问by extempl
I have UI tests which testing login functionality (and uses it to test other stuff), but sometimes when focus is changed from one field to another - the keyboard hides, and although the cursor is blinking in the field, I getting error on field.typeText
- no focused fields to fill
.
我有用于测试登录功能的 UI 测试(并使用它来测试其他内容),但有时当焦点从一个字段更改为另一个字段时 - 键盘隐藏,尽管光标在该字段中闪烁,但我在field.typeText
- 上出现错误no focused fields to fill
。
Somehow I realized, that clicking on a Hardware -> Keyboard -> toggle software keyboard
makes keyboard to persist on the screen, so test is works well. But I need to make it working on any testing device, on any developer machine, so I want to set this option programmatically without annoying "if test fails, go to … and set … by hand" in readme of the project.
不知何故,我意识到,点击 aHardware -> Keyboard -> toggle software keyboard
会使键盘持续显示在屏幕上,所以测试效果很好。但是我需要让它在任何测试设备上,在任何开发者机器上工作,所以我想以编程方式设置这个选项,而不是烦人的“如果测试失败,去……手动设置……”在项目的自述文件中。
Is it possible?
是否可以?
采纳答案by Brooks DuBois
The simulator's .plist file changed to add support for multiple simulators. The ConnectHardwareKeyboard boolean is now nested underneath the device's UDID. Luckily this UDID is also stored in the plist file. You can add this code using 'run script' under your UITest target's build phases.
模拟器的 .plist 文件已更改以添加对多个模拟器的支持。ConnectHardwareKeyboard 布尔值现在嵌套在设备的 UDID 下。幸运的是,这个 UDID 也存储在 plist 文件中。您可以在 UITest 目标的构建阶段下使用“运行脚本”添加此代码。
Xcode 9 answer:
Xcode 9 答案:
#grab the UDID from the plist
UDID=$(defaults read com.apple.iphonesimulator CurrentDeviceUDID)
#overwrite the existing value with false
#OR if the plist doesn't have that value add it in
/usr/libexec/PlistBuddy -c "Set :DevicePreferences:$UDID:ConnectHardwareKeyboard
false" ~/Library/Preferences/com.apple.iphonesimulator.plist
||
/usr/libexec/PlistBuddy -c "Add :DevicePreferences:$UDID:ConnectHardwareKeyboard
bool false" ~/Library/Preferences/com.apple.iphonesimulator.plist
Or you can use this other code to affect all simulators:
或者您可以使用其他代码来影响所有模拟器:
/usr/libexec/PlistBuddy -c "Print :DevicePreferences" ~/Library/Preferences/com.apple.iphonesimulator.plist | perl -lne 'print if /^ (\S*) =/' | while read -r a; do /usr/libexec/PlistBuddy -c "Set :DevicePreferences:$a:ConnectHardwareKeyboard
false" ~/Library/Preferences/com.apple.iphonesimulator.plist || /usr/libexec/PlistBuddy -c "Add :DevicePreferences:$a:ConnectHardwareKeyboard
bool false" ~/Library/Preferences/com.apple.iphonesimulator.plist; done
回答by Chris Zielinski
Tested in Xcode 10.3 & Xcode 11. The snippet below needs to be located in the app target (not the test bundle) — for instance, in AppDelegate.swift. It will disable any hardware keyboards from automatically connecting by setting any UIKeyboardInputMode
's automaticHardwareLayout
properties to nil
.
在 Xcode 10.3 和 Xcode 11 中测试。下面的代码片段需要位于应用程序目标(而不是测试包)中——例如,在AppDelegate.swift 中。它将通过将 anyUIKeyboardInputMode
的 automaticHardwareLayout
属性设置为 来禁用任何硬件键盘的自动连接nil
。
Does not depend on the settings of the Simulator.
Note:This appears fixed in Simulator 11.0 (Xcode 11).
不依赖于模拟器的设置。
注意:这在 Simulator 11.0 (Xcode 11) 中已修复。
#if targetEnvironment(simulator)
// Disable hardware keyboards.
let setHardwareLayout = NSSelectorFromString("setHardwareLayout:")
UITextInputMode.activeInputModes
// Filter `UIKeyboardInputMode`s.
.filter({ #if TARGET_IPHONE_SIMULATOR
SEL setHardwareLayout = NSSelectorFromString(@"setHardwareLayout:");
for (UITextInputMode *inputMode in [UITextInputMode activeInputModes]) {
if ([inputMode respondsToSelector:setHardwareLayout]) {
// Note: `performSelector:withObject:` will complain, so we have to use some black magic.
((void (*)(id, SEL, id))[inputMode methodForSelector:setHardwareLayout])(inputMode, setHardwareLayout, NULL);
}
}
#endif
.responds(to: setHardwareLayout) })
.forEach { defaults write com.apple.iphonesimulator ConnectHardwareKeyboard -bool NO
.perform(setHardwareLayout, with: nil) }
#endif
Or Objective-C:
或目标-C:
/usr/libexec/PlistBuddy -c "Print :DevicePreferences" ~/Library/Preferences/com.apple.iphonesimulator.plist | perl -lne 'print if /^ (\S*) =/' | while read -r a; do /usr/libexec/PlistBuddy -c "Set :DevicePreferences:$a:ConnectHardwareKeyboard
false" ~/Library/Preferences/com.apple.iphonesimulator.plist || /usr/libexec/PlistBuddy -c "Add :DevicePreferences:$a:ConnectHardwareKeyboard
bool false" ~/Library/Preferences/com.apple.iphonesimulator.plist; done
回答by Jeremy Huddleston Sequoia
Prior to Xcode 9, you can work around this by disabling the hardware keyboard in Simulator.app which will cause the software keyboard to always be present. Eg:
在 Xcode 9 之前,您可以通过禁用 Simulator.app 中的硬件键盘来解决此问题,这将导致软件键盘始终存在。例如:
#Find the UDID of the booted simulator
#And use PlistBuddy to change the flag to true or false
#Set the variable useHardwareKeyboard with the desired result
#Relaunch the simulator
useHardwareKeyboard=false
export UDID=$(xcrun simctl list devices | grep "(Booted)" | grep -E -o -i "([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})")
/usr/libexec/PlistBuddy -c "Set :DevicePreferences:$UDID:ConnectHardwareKeyboard ${useHardwareKeyboard}" ~/Library/Preferences/com.apple.iphonesimulator.plist
xcrun simctl shutdown $UDID
xcrun simctl boot $UDID
回答by Everton Cunha
Following Brooks great answer, this will work for all simulators:
遵循布鲁克斯的好答案,这将适用于所有模拟器:
xcrun simctl list devices | grep "(Booted)" | grep -E -o -i "([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})"
回答by Jorge Quezada
Tested with Xcode 10.1 I tried different approaches but none of them solve how to get the simulator UDID
用 Xcode 10.1 测试我尝试了不同的方法,但没有一个解决如何获取模拟器 UDID
defaults read com.apple.iphonesimulator
#OR
open ~/Library/Preferences/com.apple.iphonesimulator.plist
You can also verify these changes.
您还可以验证这些更改。
Find your simulator UDID: (More here: https://nshipster.com/simctl/)
找到您的模拟器 UDID:(更多信息请访问:https: //nshipster.com/simctl/)
tell application "Simulator" to activate
tell application "System Events"
keystroke "K" using {command down, shift down}
end tell
Run one of these commands and find your changes based on the UDID from above:
运行以下命令之一并根据上面的 UDID 查找更改:
##代码##回答by real_kappa_guy
For Xcode 10.2, none of these solutions work for me, the plist
is changed correctly but keyboard is till hidden. I have to use oascript to press Ctrl + Shift + K on Simulator if the keyboard is not displayed. It's not beauty but it's the only workaround that works.
对于 Xcode 10.2,这些解决方案都不适合我,plist
正确更改但键盘直到隐藏。如果未显示键盘,我必须使用 oascript 在模拟器上按 Ctrl + Shift + K。这不是美,但它是唯一有效的解决方法。