UItesting失败 – 无论是元素还是任何后代都没有关注secureTextField的键盘

这是我的情况:

let passwordSecureTextField = app.secureTextFields["password"] passwordSecureTextField.tap() passwordSecureTextField.typeText("wrong_password") //here is an error 

用户界面testing失败 – 任何元素或任何后代都没有键盘焦点。 元件:

哪里不对? 这对正常的textFields secureTextFields用,但问题只出现在secureTextFields 。 任何解决方法?

这个问题引起了我一个痛苦的世界,但我设法找出一个适当的解决scheme。 在模拟器中,确保“硬件 – >键盘 – >连接硬件键盘”已closures。

最近我们发现一个黑客可以从被接受的答案中解决问题。 要从命令行中禁用模拟器设置:“硬件 – >键盘 – >连接硬件键盘”,应写入:

 defaults write com.apple.iphonesimulator ConnectHardwareKeyboard 0 

它不会影响正在运行的模拟器 – 您需要重新启动模拟器或启动一个新的模拟器来使该设置起作用。

我写了一个很适合我的小扩展(Swift)。 这里是代码:

 extension XCTestCase { func tapElementAndWaitForKeyboardToAppear(element: XCUIElement) { let keyboard = XCUIApplication().keyboards.element while (true) { element.tap() if keyboard.exists { break; } NSRunLoop.currentRunLoop().runUntilDate(NSDate(timeIntervalSinceNow: 0.5)) } } } 

主要想法是在键盘出现之前继续敲击一个元素(文本字段)。

斯坦尼斯拉夫有正确的想法。

在团队环境中,你需要一些能够自动工作的东西。 我在博客上提出了一个修复scheme。

基本上你只需粘贴:

 UIPasteboard.generalPasteboard().string = "Their password" let passwordSecureTextField = app.secureTextFields["password"] passwordSecureTextField.pressForDuration(1.1) app.menuItems["Paste"].tap() 

它多次发生在我身上。 您必须在模拟器中禁用键盘硬件和相同布局作为OSX

硬件/键盘(全部禁用)

之后,键盘软件不会解雇,您的testing可以键入文本

禁用硬件

 func pasteTextFieldText(app:XCUIApplication, element:XCUIElement, value:String, clearText:Bool) { // Get the password into the pasteboard buffer UIPasteboard.generalPasteboard().string = value // Bring up the popup menu on the password field element.tap() if clearText { element.buttons["Clear text"].tap() } element.doubleTap() // Tap the Paste button to input the password app.menuItems["Paste"].tap() } 

无论你想要logging的情况下,我的意思是与键盘或没有键盘连接。 玩游戏时请做以下的事情。

在这里输入图像说明

玩这个选项(连接硬件键盘)应该在游戏testing时不勾选。

在启动应用程序和在文本框中input数据之间使用睡眠,如下所示:

 sleep(2) 

在我的情况下,我每次都得到这个错误,只有这个解决scheme帮助了我。

这可能有所帮助:我只是在错误之前添加一个“tap”动作; 就这样 :)

 [app.textFields[@"theTitle"] tap]; [app.textFields[@"theTitle"] typeText:@"kk"]; 

[重新发布BartłomiejSemańczyk的评论作为答案,因为它解决了我的问题]

我需要在模拟器菜单栏中执行模拟器>重置内容和设置,以使其开始为我工作。

你的第一行只是一个查询定义 ,并不意味着passwordSecureTextField实际上是存在的。

您的第二行将dynamic执行查询并尝试(重新)将查询绑定到UI元素。 您应该在其上放置一个断点,并validation是否find一个元素。 或者只是使用一个断言:

 XCTAssertFalse(passwordSecureTextField.exists); 

否则,它看起来不错, tap应该强制键盘可见,然后typeText应该只是工作。 错误日志应该告诉你更多的信息。

不要搞砸了,有问题的原因是你logging你的testing时间,你的应用程序将连接硬件键盘,而你的自动testing时间模拟器只需要软件键盘。 所以如何解决这个问题。 只需在您的录音时间使用软件键盘。 你可以看到魔法。

对我来说问题和泰德一样。 实际上,如果在login字段和硬件KB处于打开状态之后敲击密码字段,则软件键盘将在第二个字段的抽头中自行解散,而不是特定于UItesting。

经过一段时间搞乱AppleScript,这是我想出了(改进是欢迎):

tell application "Simulator" activate tell application "System Events" try tell process "Simulator" tell menu bar 1 tell menu bar item "Hardware" tell menu "Hardware" tell menu item "Keyboard" tell menu "Keyboard" set menuItem to menu item "Connect Hardware Keyboard" tell menu item "Connect Hardware Keyboard" set checkboxStatus to value of attribute "AXMenuItemMarkChar" of menuItem if checkboxStatus is equal to "✓" then click end if end tell end tell end tell end tell end tell end tell end tell on error tell application "System Preferences" activate set securityPane to pane id "com.apple.preference.security" tell securityPane to reveal anchor "Privacy_Accessibility" display dialog "Xcode needs Universal access to disable hardware keyboard during tests(otherwise tests may fail because of focus issues)" end tell end try end tell end tell

用上面的代码创build一个脚本文件,并将其添加到必要的目标(可能只是UItesting的目标,您可能希望添加类似的脚本到您的开发目标,以重新启用开发过程中的HW键盘)。 您应该在构build阶段添加Run Script阶段,并像这样使用它: osascript Path/To/Script/script_name.applescript

当为包含UIControl子视图的自定义视图( UIStackView子类)设置accessibilityIdentifier值时,我们遇到了同样的错误。 在这种情况下,XCTest无法获得后代元素的键盘焦点。

我们的解决scheme是简单地从我们的父视图中删除accessibilityIdentifier ,并通过专用属性设置子视图的accessibilityIdentifier

与Securetextfields有相同的问题。 我的模拟器中的连接硬件选项是,但仍然遇到问题。 最后,这对我有用(Swift 3):

  let enterPasswordSecureTextField = app.secureTextFields["Enter Password"] enterPasswordSecureTextField.tap() enterPasswordSecureTextField.typeText("12345678")