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

时间:2016-06-24 09:52:11

标签: ios xcode ios-simulator xcode-ui-testing

我有测试登录功能的UI测试(并用它来测试其他东西),但有时当焦点从一个字段更改为另一个字段时 - 键盘隐藏,虽然光标在字段中闪烁,但我收到错误在 //define field lengths FixedWidthFields fields = new FixedWidthFields(); accountFields.addField("ID", 10); accountFields.addField("Bank", 8); accountFields.addField("AccountNumber", 15); accountFields.addField("Swift", 12); //configure the parser FixedWidthParserSettings settings = new FixedWidthParserSettings(fields); //many options here, check the tutorial settings.getFormat().setLineSeparator("\n"); //We can now parse all rows FixedWidthParser parser = new FixedWidthParser(settings); List<String[]> rows = parser.parseAll(new File("/path/to/file.txt")); - field.typeText

不知怎的,我意识到,单击no focused fields to fill会使键盘在屏幕上保持不变,因此测试效果很好。但是我需要让它在任何开发人员机器上的任何测试设备上工作,所以我想以编程方式设置这个选项,而不要讨厌“如果测试失败,请在项目的自述文件中设置...并手动设置。”

有可能吗?

7 个答案:

答案 0 :(得分:11)

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

Xcode 9回答:

Import-CSV listofusers.csv | Foreach-Object -Process {
    $GivenName = $_.Firstname
    $Surname = $_.Lastname
    $User = Get-ADUser -Filter {
        (GivenName -eq $GivenName) -and (Surname -eq $Surname)
    }
    Add-ADGroupmember -Identity $_.group -Members $User
}

或者您可以使用此其他代码来影响所有模拟器:

#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

答案 1 :(得分:10)

在Xcode 9之前,您可以通过禁用Simulator.app中的硬件键盘来解决此问题,这将导致软件键盘始终存在。例如:

defaults write com.apple.iphonesimulator ConnectHardwareKeyboard -bool NO

答案 2 :(得分:9)

已在Xcode 10.3和Xcode 11中进行了测试。下面的代码段必须位于应用程序目标(不是测试包)中-例如,在 AppDelegate.swift 中。通过将任何UIKeyboardInputMode的{​​{1}}属性设置为automaticHardwareLayout,将禁止任何硬件键盘自动连接。

  

?不依赖于模拟器的设置。

     

?注意:此问题已在Simulator 11.0(Xcode 11)中修复。

nil

或Objective-C:

#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

答案 3 :(得分:4)

按照布鲁克斯的回答,这适用于所有模拟器:

0:{Year: "2007", Exports: "314,448.7"}
1:{Year: "2008", Exports: "320,805.2"}
2:{Year: "2007", Imports: "314,448.7"}
3:{Year: "2008", Imports: "320,805.2"}

答案 4 :(得分:1)

使用Xcode 10.1测试 我尝试了不同的方法,但是没有一个方法解决如何获得模拟器的UDID

$numbers = [];
for ($a=1;$a<=10000;$a++){
  $numbers[] = '('.$a.')';
}

$b = implode(",", $numbers);
$ins = "INSERT into (number) values $b";
$stmt = $db->query($ins);
echo $ins."\n";

您还可以验证这些更改。

找到模拟器的UDID :(更多信息:https://nshipster.com/simctl/

#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查找更改:

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

答案 5 :(得分:1)

对于xcode 10.2,这些解决方案都不适合我,plist正确更改,但键盘一直隐藏。如果未显示键盘,则必须使用oascript在Simulator上按Ctrl + Shift +K。这不是美丽,但这是唯一可行的解​​决方法。

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

答案 6 :(得分:1)

受上述real_kappa_guy的answer的启发,我通过检查是否已启用键盘,然后运行Apple脚本来发送键盘快捷键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