ios - 有没有办法在 UITest 中使用 valueForKey 和 NSPredicate?
<p><p>我的 iOS UI 测试有以下测试助手函数:</p>
<pre><code>func waitForElementToHaveKeyboardFocus(element: XCUIElement) {
self.expectationForPredicate(NSPredicate(format:"valueForKey(\"hasKeyboardFocus\") == true"), evaluatedWithObject:element, handler: nil)
self.waitForExpectationsWithTimeout(5, handler: nil)
}
</code></pre>
<p>在我的测试中我有:</p>
<pre><code>let usernameTextField = app.textFields["Username"]
let passwordTextField = app.secureTextFields["Password"]
waitForElementToHaveKeyboardFocus(usernameTextField)
</code></pre>
<p>测试失败并出现以下错误:</p>
<pre><code>error: - : failed: caught "NSUnknownKeyException", "[<_NSPredicateUtilities 0x10e554ee8> valueForUndefinedKey:]: this class is not key value coding-compliant for the key hasKeyboardFocus."
</code></pre>
<p>如果我在失败时在测试中设置断点并在焦点和非焦点字段上手动调用 <code>valueForKey("hasKeyboardFocus")</code> 我似乎得到了正确的行为:</p>
<pre><code>(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
</code></pre>
<p>是否可以使 <code>XCUIElement</code> 上的 <code>valueForKey</code> 在 UI 测试中与 <code>NSPredicate</code> 一起使用?还有另一种优雅的方法吗?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>看起来你的谓词有点不对劲。尝试将其更改为以下内容:</p>
<pre><code>NSPredicate(format: "hasKeyboardFocus == true"), evaluatedWithObject:element, handler: nil)
</code></pre>
<p>创建谓词时不需要传入<code>valueForKey</code>部分。</p></p>
<p style="font-size: 20px;">关于ios - 有没有办法在 UITest 中使用 valueForKey 和 NSPredicate?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/39232896/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/39232896/
</a>
</p>
页:
[1]