我的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
是否有可能做valueForKey
一个XCUIElement
工作,NSPredicate
在UI测试?还有另一种优雅的方法可以做到这一点吗?
看来您的谓词略有偏离。尝试将其更改为以下内容:
NSPredicate(format: "hasKeyboardFocus == true"), evaluatedWithObject:element, handler: nil)
valueForKey
创建谓词时,您无需传递零件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句