【问题标题】:Is there a way to use valueForKey with NSPredicate in UITest?有没有办法在 UITest 中使用 valueForKey 和 NSPredicate?
【发布时间】:2016-08-30 16:41:36
【问题描述】:

我的 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 一起使用?还有另一种优雅的方法吗?

【问题讨论】:

    标签: ios xcode-ui-testing uitest


    【解决方案1】:

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

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

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

    【讨论】:

    • 太好了,行得通!奇怪的是,我在调试器中尝试了它,但它不起作用:error: &lt;EXPR&gt;:2:1: error: value of type 'XCUIElement' has no member 'hasKeyboardFocus'。而其他具有该语法的谓词,例如:NSPredicate(format: "hittable == true") DO 在调试器中工作。但hasKeyboardFocus 仅适用于调试器中的valueForKey。很奇怪。
    【解决方案2】:

    您可以这样做,将valueForKey("") 的语句作为闭包传递给方法:

    func waitForElementToHaveKeyboardFocus(statement statement: () -> Bool, timeoutSeconds: Int)
    {
        var second = 0
        while statement() != true {
            if second >= timeoutSeconds {
                XCTFail("statement reached timeout of \(timeoutSeconds) seconds")
            }
    
            sleep(1)
            second = second + 1
        }
    }
    

    然后在测试中使用:

    waitForElementToHaveKeyboardFocus(statement: { usernameTextField.valueForKey("hasKeyboardFocus") as? Bool == true }, timeoutSeconds: 10)
    

    您可以将此方法重命名为更通用,它将验证您传递给它的任何闭包。希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-13
      • 2021-12-21
      相关资源
      最近更新 更多