我有我的iOS用戶界面下面的測試輔助功能檢查:有沒有辦法在UITest中使用valueForKey和NSPredicate?
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
上的UI測試與NSPredicate
一個XCUIElement
工作?有沒有另一種優雅的方式來做到這一點?
太棒了,那有效!奇怪的是,我在調試器中試過,它不起作用:錯誤::2:1:錯誤:類型'XCUIElement'的值沒有成員'hasKeyboardFocus''。而具有該語法的其他謂詞,例如:'NSPredicate(format:「hittable == true」)'DO可以在調試器中工作。但'hasKeyboardFocus'只能在調試器中使用'valueForKey'。奇怪的。 –
Mitochondrion