xcode 是否可以通过UI测试中的代码“切换软件键盘”?

suzh9iv8  于 2023-01-18  发布在  其他
关注(0)|答案(7)|浏览(208)

我有测试登录功能的UI测试(并使用它来测试其他东西),但有时当焦点从一个字段切换到另一个字段时-键盘隐藏,尽管光标在字段中 Flink ,但我在field.typeText-no focused fields to fill上得到错误。
不知何故我意识到,点击Hardware -> Keyboard -> toggle software keyboard会使键盘停留在屏幕上,所以test is工作得很好,但我需要使它在任何测试设备上,在任何开发人员的机器上工作,所以我想通过编程设置这个选项,而不需要在项目的自述文件中烦人的"如果测试失败,转到...并手动设置..."。
有可能吗?

7y4bm7vi

7y4bm7vi1#

在Xcode 10.3和Xcode 11中测试。以下代码片段需要位于应用目标中(不是测试包)-例如,位于 AppDelegate.swift 中。它将通过将任何UIKeyboardInputModeautomaticHardwareLayout属性设置为nil来禁用任何硬件键盘的自动连接。
🔥 不取决于模拟器的设置。

#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

或者Objective-C:

#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 dark magic.
        ((void (*)(id, SEL, id))[inputMode methodForSelector:setHardwareLayout])(inputMode, setHardwareLayout, NULL);
    }
}
#endif
utugiqy6

utugiqy62#

模拟器的.plist文件已更改,以添加对多个模拟器的支持。ConnectHardwareKeyboard布尔值现在嵌套在设备的UDID之下。幸运的是,此UDID也存储在plist文件中。您可以在UITest目标的构建阶段下使用“运行脚本”添加此代码。
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

或者,您可以使用以下代码来影响所有模拟器:

/usr/libexec/PlistBuddy -c "Print :DevicePreferences" ~/Library/Preferences/com.apple.iphonesimulator.plist | perl -lne 'print $1 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
lvmkulzt

lvmkulzt3#

在Xcode 9之前,您可以通过在www.example.com中禁用硬件键盘来解决此Simulator.app,这将导致软件键盘始终存在。

defaults write com.apple.iphonesimulator ConnectHardwareKeyboard -bool NO
7kjnsjlb

7kjnsjlb4#

根据布鲁克斯的回答,这将适用于所有模拟器:

/usr/libexec/PlistBuddy -c "Print :DevicePreferences" ~/Library/Preferences/com.apple.iphonesimulator.plist | perl -lne 'print $1 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
93ze6v8z

93ze6v8z5#

我用Xcode 10.1进行了测试,尝试了不同的方法,但都没有解决如何获得模拟器UDID的问题

#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

您也可以验证这些更改。
查找您的模拟器UDID:(更多信息请访问:https://nshipster.com/simctl/

xcrun simctl list devices | grep "(Booted)" | grep -E -o -i "([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})"

运行以下命令之一,并根据上面的UDID查找更改:

defaults read com.apple.iphonesimulator
#OR
open ~/Library/Preferences/com.apple.iphonesimulator.plist
xlpyo6sf

xlpyo6sf6#

受真实的_kappa_guy的answer启发,我检查键盘是否已启用,然后运行苹果脚本发送键盘快捷键Cmd-shift-k(需要添加到预测试操作中),从而在不重启模拟器的情况下成功完成此操作:

if test `/usr/libexec/PlistBuddy -c "Print DevicePreferences:${TARGET_DEVICE_IDENTIFIER}:ConnectHardwareKeyboard" ~/Library/Preferences/com.apple.iphonesimulator.plist` == true; then 
osascript <<EOD
  tell application "Simulator" to activate
  tell application "System Events"
      keystroke "K" using {command down, shift down}
  end tell
EOD
fi
nxagd54h

nxagd54h7#

对于Xcode 10.2,这些解决方案对我来说都不起作用,plist被正确地修改了,但是键盘仍然隐藏着。如果键盘没有显示,我必须使用osascript在模拟器上按Ctrl + Shift + K。这不是很漂亮,但这是唯一有效的解决方案。

tell application "Simulator" to activate
tell application "System Events"
    keystroke "K" using {command down, shift down}
end tell

相关问题