ios - 有没有办法在 UITest 中将 valueForKey 与 NSPredicate 一起使用?

标签 ios xcode-ui-testing uitest

我的 iOS UI 测试有以下测试辅助函数:

func waitForElementToHaveKeyboardFocus(element: XCUIElement) {
    self.expectationForPredicate(NSPredicate(format:"valueForKey(\"hasKeyboardFocus\") == true"), evaluatedWithObject:element, handler: nil)
    self.waitForExpectationsWithTimeout(5, handler: nil)
}

在我的测试中,我有:

let usernameTextField = app.textFields["Username"]
let passwordTextField = app.secureTextFields["Password"]
waitForElementToHaveKeyboardFocus(usernameTextField)

测试失败并出现以下错误:

error: -[ExampleAppUITests.ExampleAppUITests testExampleApp] : failed: caught "NSUnknownKeyException", "[<_NSPredicateUtilities 0x10e554ee8> valueForUndefinedKey:]: this class is not key value coding-compliant for the key hasKeyboardFocus."

如果我在失败时在测试中设置断点,并在聚焦和未聚焦的字段上手动调用 valueForKey("hasKeyboardFocus") 我似乎得到了正确的行为:

(lldb) po usernameTextField.valueForKey("hasKeyboardFocus")
    t =    51.99s     Find the "Username" TextField
    t =    51.99s         Use cached accessibility hierarchy for ExampleApp
    t =    52.00s         Find: Descendants matching type TextField
    t =    52.01s         Find: Elements matching predicate '"Username" IN identifiers'
▿ Optional<AnyObject>
  - Some : 1

(lldb) po passwordTextField.valueForKey("hasKeyboardFocus")
    t =   569.99s     Find the "Password" SecureTextField
    t =   569.99s         Use cached accessibility hierarchy for ExampleApp
    t =   570.01s         Find: Descendants matching type SecureTextField
    t =   570.01s         Find: Elements matching predicate '"Password" IN identifiers'
▿ Optional<AnyObject>
  - Some : 0

是否可以在 UI 测试中使 XCUIElement 上的 valueForKeyNSPredicate 配合使用?还有另一种优雅的方式来做到这一点吗?

最佳答案

看起来你的谓词有点不对劲。尝试将其更改为以下内容:

NSPredicate(format: "hasKeyboardFocus == true"), evaluatedWithObject:element, handler: nil)

创建谓词时不需要传入valueForKey部分。

关于ios - 有没有办法在 UITest 中将 valueForKey 与 NSPredicate 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39232896/

相关文章:

ios - 在 iOS 中获取 wifi 路由器类型

ios - TableView 扩展 indexPathsFor `Un` SelectedRows

ios - 如何检查我是否在 UI 测试中将应用留在测试中?

ios - Xcode 8.1 dyld : Library not loaded: @rpath/XCTest. 框架/XCTest 错误

ios - swift in UI test(测试UITableView中cell的个数)

ios - 在 iOS UI 测试中,如何识别 MKUserLocation 注释?

ios - 即使设置了 contentSize,UIScrollView 也不会滚动

objective-c - 将 UITextField 限制为特定字符范围

ios - 并行运行测试时 UI 测试的输出

ios - Xcode 8.1 UI 测试 twoFingerTap() 在谷歌地图 View 上失败